Pagination Help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
shurrock
Forum Newbie
Posts: 1
Joined: Tue Sep 15, 2009 11:43 am

Pagination Help

Post by shurrock »

Hey guys, i have the following code 'Almost' working, and its driving me nuts i just cant figure it out!

Code: Select all

 
<?php require_once('connections/admin.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
 
  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
 
  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}
 
 
$currentPage = $_SERVER["PHP_SELF"];
 
$maxRows_hotels = 8;
$pageNum_hotels = 0;
if (isset($_GET['pageNum_hotels'])) {
  $pageNum_hotels = $_GET['pageNum_hotels'];
}
$startRow_hotels = $pageNum_hotels * $maxRows_hotels;
 
$colname_hotels = "-1";
if (isset($_POST['keyword'])) {
  $colname_hotels = $_POST['keyword'];
}
mysql_select_db($database_admin, $admin);
$query_hotels = sprintf("SELECT * FROM hotels WHERE `Company_Name` LIKE %s", GetSQLValueString("%" . $colname_hotels . "%", "text"));
$query_limit_hotels = sprintf("%s LIMIT %d, %d", $query_hotels, $startRow_hotels, $maxRows_hotels);
$hotels = mysql_query($query_limit_hotels, $admin) or die(mysql_error());
$row_hotels = mysql_fetch_assoc($hotels);
 
if (isset($_GET['totalRows_hotels'])) {
  $totalRows_hotels = $_GET['totalRows_hotels'];
} else {
  $all_hotels = mysql_query($query_hotels);
  $totalRows_hotels = mysql_num_rows($all_hotels);
}
$totalPages_hotels = ceil($totalRows_hotels/$maxRows_hotels)-1;
 
$queryString_hotels = "";
if (!empty($_SERVER['QUERY_STRING'])) {
  $params = explode("&", $_SERVER['QUERY_STRING']);
  $newParams = array();
  foreach ($params as $param) {
    if (stristr($param, "pageNum_hotels") == false && 
        stristr($param, "totalRows_hotels") == false) {
      array_push($newParams, $param);
    }
  }
  if (count($newParams) != 0) {
    $queryString_hotels = "&" . htmlentities(implode("&", $newParams));
  }
}
$queryString_hotels = sprintf("&totalRows_hotels=%d%s", $totalRows_hotels, $queryString_hotels);
?>
 
 
 
    <?php if ($totalRows_hotels == 0) { // Show if recordset empty ?>
      <p>There were no companies which match that name, please try again.</p>
      <?php } // Show if recordset empty ?>
    <?php if ($totalRows_hotels > 0) { // Show if recordset not empty ?>
    <center>
    Search Results <?php echo ($startRow_hotels + 1) ?> to <?php echo min($startRow_hotels + $maxRows_hotels, $totalRows_hotels) ?> of <?php echo $totalRows_hotels ?> </p>
    </center>
  <table border="0">
      <?php do { ?>
      
        <tr>
          <td>
           <br/>
          <br/>
         
        
        
    
          <div id="images"><img src="images/Companies/<?php echo $row_hotels['image']; ?>">
          
    
            
              </br>
              </br>
              </div>
          </td>
          
          
          <td>
           <br/>
          <br/>
         
          <div id="content">
          
          <a href="details.php?id=<?php echo $row_hotels['ID']; ?>"><strong><font size="2"><?php echo $row_hotels['Company_Name']; ?></font></strong></a><br/>
          
          
          <?php 
          $position=130; // Define how many character you want to display.
          $message=$row_hotels['info'];
          $post = substr($message, 0, $position);
 
            echo $post;
            echo "...";
          ?>
          <br/><br/>
          
          Telephone Number&nbsp;:&nbsp;<strong><?php echo $row_hotels['telephone_number']; ?></strong>
          
         
          
          </div>
          
          
          </td>
          
           </div>
         
           </tr>
           
       
        <?php } while ($row_hotels = mysql_fetch_assoc($hotels)); ?>
    </table>
    <center>
        
    <table border="0">
      <tr>
        <td><?php if ($pageNum_hotels > 0) { // Show if not first page ?>
              <a href="<?php printf("%s?pageNum_hotels=%d%s", $currentPage, 0, $queryString_hotels); ?>"><<First</a>
              <?php } 
                else {?>
                <<first
                <?php } ?>
        </td>
        <td><?php if ($pageNum_hotels > 0) { // Show if not first page ?>
              <a href="<?php printf("%s?pageNum_hotels=%d%s", $currentPage, max(0, $pageNum_hotels - 1), $queryString_hotels); ?>"><Previous</a>
               <?php } 
                else {?>
                <Previous
                <?php } ?>
        </td>
        <td>
        |
        </td>
        <td><?php if ($pageNum_hotels < $totalPages_hotels) { // Show if not last page ?>
              <a href="<?php printf("%s?pageNum_hotels=%d%s", $currentPage, min($totalPages_hotels, $pageNum_hotels + 1), $queryString_hotels); ?>">Next></a>
               <?php } 
                else {?>
                Next>
                <?php } ?>
        </td>
        <td><?php if ($pageNum_hotels < $totalPages_hotels) { // Show if not last page ?>
              <a href="<?php printf("%s?pageNum_hotels=%d%s", $currentPage, $totalPages_hotels, $queryString_hotels); ?>">Last>></a>
               <?php } 
                else {?>
                Last>>
                <?php } ?>
                
                
        </td>
      </tr>
    </table>
    
    </center>
<?php } ?>
<?php
mysql_free_result($hotels);
?>

the problem is, when i select next it says results 9-14 out of 100 etc, but no results are displayed.

The first page of results are displayed correctly though.

Any help would be appreciated! Thanks!!

-Shurrock
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Pagination Help

Post by Eric! »

Echo out your $query_limit_hotels and compare the working query with the failing query. If the query looks good and the returned data is there, then there is probably something wrong with how your are showing the data.

Also in line 53 why don't you just remove the LIMIT from the previous query instead of running the query again (on line 56)?
Post Reply