Ok, So I found a pagination class fro ma tutorial and decided to try and impliment it into my mysql search query I use here at work to search past Parts Ordered records. I got it working to the point that it only shows 10 results on a page, and it shows the next page links at the bottom of the page. However, when I click on page "2" or any of the page links at the bottom, it takes me to a page showing 0 results. I am still learning PHP, and just need a fresh eye to see if they know what may be the issue. My code is below.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">

            <head>
                <title>Search For A PO</title>
<link href="style.css" rel="stylesheet" type="text/css" />
            </head>

<div id="static">
<div class="static">
<a href="http://lmtl-linux.lmtl.local/podat">Home</a> </div>
</div>

<div id="static2">
<div class="static2">
<a href="http://lmtl-linux.lmtl.local/podat/insert.php">Create A New PO</a> </div>
</div>

            <body>
<div id="content">
<div class="contentbox">
        <h1>Search For A PO</h1>
            <form method="post" action="">
<table class="search">
            <tr><td> Location: <input type="radio" name="loc" value="IA" <?php if(isset($_REQUEST['loc']) && $_REQUEST['loc']=='IA'){print "checked";}?>>Iowa</td>
            <td><input type="radio" name="loc" value="GA" checked="checked" <?php if(isset($_REQUEST['loc']) && $_REQUEST['loc']=='GA'){print "checked";}?>>Georgia</td>
            </tr>


            <tr><td> Search: <select name="type">
            <option value="po_num"<?php if(isset($_REQUEST['type']) && $_REQUEST['type']=='po_num'){print "selected";}?>>PO Number</option>
            <option value="date" <?php if(isset($_REQUEST['type']) && $_REQUEST['type']=='date'){print "selected";}?>>Date</option>
            <option value="vin_num"<?php if(isset($_REQUEST['type']) && $_REQUEST['type']=='vin_num'){print "selected";}?>>VIN Number</option>
                     </select></td>
<td>for: <input type="text" name="term" /><br /></td></tr>
           <tr><td> <input type="submit" name="submit" value="Search" /></td>
<td class="date">* date format: yyyymmdd</td></tr>
        </table>
        </form>
</div>
<hr width='100%'></hr>
    <?php

        if($_SERVER['REQUEST_METHOD'] === "POST")  {
// database connection info
$conn = mysql_connect('localhost','user','pass') or trigger_error("SQL", E_USER_ERROR);
$db = mysql_select_db('partsorder',$conn) or trigger_error("SQL", E_USER_ERROR);

// find out how many rows are in the table
$sql = "SELECT COUNT(*) FROM podat";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
$r = mysql_fetch_row($result);
$numrows = $r[0];

// number of rows to show per page
$rowsperpage = 10;
// find out total pages
$totalpages = ceil($numrows / $rowsperpage);

// get the current page or set a default
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
   // cast var as int
   $currentpage = (int) $_GET['currentpage'];
} else {
   // default page num
   $currentpage = 1;
} // end if

// if current page is greater than total pages...
if ($currentpage > $totalpages) {
   // set current page to last page
   $currentpage = $totalpages;
} // end if
// if current page is less than first page...
if ($currentpage < 1) {
   // set current page to first page
   $currentpage = 1;
} // end if

// the offset of the list, based on current page
$offset = ($currentpage - 1) * $rowsperpage;


        $loc = mysql_real_escape_string($_POST['loc']);
        $type = mysql_real_escape_string($_POST['type']);
        $term = trim(mysql_real_escape_string($_POST['term']));
        $sql = "select * from podat where $type like '%$term%' AND location='$loc' AND date<>'0' LIMIT $offset, $rowsperpage";
        $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);

        while ($row = mysql_fetch_assoc($result)){
                //var_dump($row);

echo "<br/><table class='results'>
<tr><td class='short'><B>PO Number:</B> </td><td>".$row['po_num']."</td></tr>
<tr><td><B>Vendor:</B> </td><td>".$row['vendor']."</td></tr>
<tr><td><B>Date:</B> </td><td>".$row['date']."</td></tr>
<tr><td><B>VIN Number:</B> </td><td>".$row['vin_num']."</td></tr>
<tr><td><B>Description:</B> </td><td>".$row['descr']."</td></tr>
<tr><td><B>Invoice Number:</B> </td><td>".$row['inv_num']."</td></tr>
<tr><td><B>Purchase Agent:</B> </td><td>".$row['agt']."</td></tr>
<tr><td colspan='2'><a href=\"edit.php/?id=".$row['po_num']."\">Edit</a></td></tr></table>";

                }
/******  build the pagination links ******/
// range of num links to show
$range = 3;

// if not on page 1, don't show back links
if ($currentpage > 1) {
   // show << link to go back to page 1
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";
   // get previous page num
   $prevpage = $currentpage - 1;
   // show < link to go back to 1 page
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> ";
} // end if

// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
   // if it's a valid page number...
   if (($x > 0) && ($x <= $totalpages)) {
      // if we're on current page...
      if ($x == $currentpage) {
         // 'highlight' it but don't make a link
         echo " [<b>$x</b>] ";
      // if not current page...
      } else {
         // make it a link
         echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";
      } // end else
   } // end if
} // end for

// if not on last page, show forward and last page links
if ($currentpage != $totalpages) {
   // get next page
   $nextpage = $currentpage + 1;
    // echo forward link for next page
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> ";
   // echo forward link for lastpage
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> ";
} // end if
/****** end build pagination links ******/
                }
        ?>
<br/>
<hr width='100%'></hr>
</div>
</body>
</html>

Recommended Answers

All 2 Replies

Update: I just found out that if I search for a term, it shows the first 10 results. I then click on page 2, and it shows 0 results as I explained earlier. But here's the catch: If i search the same term from page 2 (that is showing 0 results) it shows the results that were SUPPOSED to be there in the first place. Why do i have re-search the term to get it to show up? I should just be able to click page 2 and the results should show automatically. Thanks for your time!

Be a part of the DaniWeb community

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