Jump to content

Pagination and search results


ragrim

Recommended Posts

I have a page setup that paginates 20 results per page and works fine when i do SELECT * or what ever, but when i do a search query it displays fine on page 1, but when i go to page 2 or any other page it displays all results. Im assuming to use pagination i have to load my query into an array or something instead or using a while loop. Im not great with php so any help here would be appreciated.

 

heres my code, i have a search page thats posts to my search results page.

 

 

<?php

include('include/dbconnect.php');

$fname = $_POST['fname'];
$lname = $_POST['lname'];
$address = $_POST['address'];
$suburb = $_POST['suburb'];

// find out how many rows are in the table 
$sql = "SELECT subscribernumbers.SubNumber,
userinfo.FirstName,
userinfo.LastName,
userinfo.Suburb,
userinfo.Phone,
userinfo.Mobile,
userinfo.Email,
userinfo.BusinessName
FROM
userinfo
LEFT JOIN subscribernumbers ON userinfo.User_ID = subscribernumbers.FKUserID WHERE FirstName LIKE '%$fname%' AND LastName LIKE '%$lname%' AND Suburb LIKE '%$suburb%'";
$result = mysql_query($sql) or trigger_error("SQL", E_USER_ERROR);
$r = mysql_fetch_row($result);
$numrows = mysql_num_rows($result);

//echo $sql;

echo "<p class='messagetext'>Query returned " . $numrows . " results</p>";

// number of rows to show per page
$rowsperpage = 20;
// 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;


$result = mysql_query("SELECT 
subscribernumbers.SubNumber,
userinfo.User_ID,
userinfo.FirstName,
userinfo.LastName,
userinfo.Phone,
userinfo.Mobile,
userinfo.Email,
userinfo.BusinessName,
userinfo.Address,
userinfo.Suburb
FROM
userinfo
LEFT JOIN subscribernumbers ON userinfo.User_ID = subscribernumbers.FKUserID WHERE FirstName LIKE '%$fname%' AND LastName LIKE '%$lname%' AND Suburb LIKE '%$suburb%' LIMIT $offset, $rowsperpage");

echo "<table id='maintable' align='center' cellspacing='0'>";
echo "<thead class='thead'>";
echo "<th style='width:50px'>Sub Number</th>";
echo "<th style='width:200px'>Name</th>";
echo "<th style='width:200px'>Business Name</th>";
echo "<th style='width:150px'>Phone</th>";
echo "<th style='width:150px'>Mobile</th>";
echo "<th style='width:200px'>Email</th>";
if($address=="")
{
}
else
{
echo "<th style='width:200px'>Address</th>";
}
if($suburb=="")
{
}
else
{
echo "<th>Suburb</th>";

}
echo "</thead>";

while($row = mysql_fetch_array($result))
  {
  ?>
    <tr 
    onmouseover="ChangeColor(this, true); tooltip.show('<?php echo "<strong>Description: </strong>" . $row['wo_text']; ?>');"  
              onmouseout="ChangeColor(this, false); tooltip.hide();" 
              onclick="DoNav('index.php?page=subscriber_view.php&user=<?php echo $row['User_ID']; ?>');">
<td><?php echo $row['SubNumber'];?></td><td><?php echo $row['FirstName'] . " " . $row['LastName'];?></td><td class="td"><?php echo $row['BusinessName'];?></td><td class="td"><?php echo $row['Phone'];?></td><td class="td"><?php echo $row['Mobile'];?></td><td><?php echo $row['Email']; ?></td>
<?
if($address=="")
{
}
else
{
echo "<td>" . $row['Address'] . "</td>";
}
if($suburb=="")
{
}
else
{
	echo "<td>" . $row['Suburb'] . "</td>";

}
?>

  <?php
  //echo "<td class='td'>" . $row['wo_id'] . "</td><td>" . $row['wo_description'] . "</td><td class='td'>" . $row['priority_description'] . "</td><td class='td' onmouseover='tooltip.show('$test')';' onmouseout='tooltip.hide();'>" . $row['status_description'] . "</td><td class='td'>" . $row['wo_creation_date'] . "</a></td>";
  echo "</tr>";
  }
echo "</table>";
echo "<div class='pagination'>";
/******  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
echo "</div>";
/****** end build pagination links ******/

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.