OK so I have already created many pages with my paging all working correctly however I have just created a new paging page that allows the user to use a search criteria. With the paging and search when you click on page 2 it dosen't load and dosen't display any information what is wrong with my code:

<form action="<?php echo $self  ?>" method="post" class="registration_form2" >
  <fieldset>
    <legend>Posts </legend>
    <div class="elements">
      <label for="hi">Type Bird Species :</label>
      <input type="text" name="hi" size="25" value = "<?php echo $hi ?>"  />
    </div>
    <div class="submit">
     <input type="hidden" name="formsubmitted" value="TRUE" />
      <input type="submit" value="Search" />
    </div>

    <?php
        include('connect-db.php');
        $per_page =1;
        if (isset($_POST['formsubmitted'])) {
    $hi = $_POST['hi'];
        $result = mysql_query("SELECT * FROM birds where species='$hi' ") 
                ;  
                }
        $total_results = mysql_num_rows($result);
        $total_pages = ceil($total_results / $per_page);
 if (isset($_GET['page']) && is_numeric($_GET['page']))
        {
                $show_page = $_GET['page'];
                if ($show_page > 0 && $show_page <= $total_pages)
                {
                        $start = ($show_page -1) * $per_page;
                        $end = $start + $per_page; 
                }
                else
                {
                        // error - show first set of results
                        $start = 1;
                        $end = $per_page; 
                }               
        }
        else
        {
                // if page isn't set, show first set of results
                $start = 0;
                $end = $per_page; 
        }
         echo "<p><b>View Page:</b> ";
        for ($i = 1; $i <= $total_pages; $i++)
        {
                echo "<a href='birdsearch.php?page=$i'>$i</a> ";
        }
        echo "</p>";
        echo "<table border='1' cellpadding='10'>";
        echo "<tr> <th>Species</th> <th>Age</th> <th>Sex</th> <th>Date</th> <th>Time</th> <th>Location</th> <th>Latitude</th> <th>Longitude</th> <th>information</th> <th>Pic</th>          </tr>";
for ($i = $start; $i < $end; $i++)
        {
 if ($i == $total_results) { break; }

                echo "<tr>";
                echo '<td>' . mysql_result($result, $i, 'species') . '</td>';
                echo '<td>' . mysql_result($result, $i, 'age') . '</td>';
                echo '<td>' . mysql_result($result, $i, 'sex') . '</td>';
                echo '<td>' . mysql_result($result, $i, 'date') . '</td>';
                echo '<td>' . mysql_result($result, $i, 'time') . '</td>';
                echo '<td>' . mysql_result($result, $i, 'location') . '</td>';
                echo '<td>' . mysql_result($result, $i, 'Latitude') . '</td>';
                echo '<td>' . mysql_result($result, $i, 'Longitude') . '</td>';
                echo '<td>' . mysql_result($result, $i, 'information') . '</td>';
                    echo '<td><img src=UPLOADS/'.mysql_result($result,$i,'pic').'></td>';
                echo "</tr>"; 

        }
        echo "</table>";        
?>
  </fieldset>
</form>

Recommended Answers

The problem is right now you only run the search query if the form data has been submitted, see lines 16-20:

  if (isset($_POST['formsubmitted'])) {
  17.    $hi = $_POST['hi'];
  18.        $result = mysql_query("SELECT * FROM birds where species='$hi' ") 
  19.                ;  
  20.                }

And when the user clicks your …

Jump to Post

All 2 Replies

The problem is right now you only run the search query if the form data has been submitted, see lines 16-20:

  if (isset($_POST['formsubmitted'])) {
  17.    $hi = $_POST['hi'];
  18.        $result = mysql_query("SELECT * FROM birds where species='$hi' ") 
  19.                ;  
  20.                }

And when the user clicks your link, all the form data is lost. You may want to consider using a GET form and then appending the data onto your pagination link so that it is passed through when they click it.

Member Avatar

Agreed, search pages (and subsequent SELECT from DB) should be using GET. Many reasons for this, including SEO.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.