Solved

php mysql script returning 10 results per page,  next page feature not displaying results from db query

Posted on 2007-11-20
5
391 Views
Last Modified: 2013-12-13
I have a script that is querying db for results.  I have results being displayed with a max of 10 results per page.  When there are more than 10 results from that query the user has the ability to click the 'Next' which will display the remaining results in the next page.  However, when you click the 'Next' page the results are not being displayed.  I know that the query is working  as I have increased the limit to 20 and it shows all 20 items.  Therefore my code is not passing the db query to the next page and i have no idea what I am doing wrong.  I have included the code for this below.  thanks for your help as I am frustrated.

If you follow this link and click on the 'searc' button in th Advanced Search section you will see what is happening.  http://www.milehighlistings.com/buy.php
<? session_start(); ?>
<div id="loading"> Performing Search... </div>
<?
 
	### DEBUG
	$debugP = 0;
	### END DEBUG
 
 	#Fetch database connection
	require_once('Connections/myconn.php');
	mysql_select_db($database_myconn,$myconn);
	require_once('myfunctions.php');
	$mysql = new mysql();
	$mysql->conn = $myconn;
	
	#Fetch Header
	include('header.php');
         $currentPage = $_SERVER["PHP_SELF"];
	
	#### BUILD SEARCH SQL BASED ON SEARCH TYPE ####
	
	#defauts
	
	$maxRows_p = 10;
	$pageNum_p = 0;
	if (isset($_GET['pageNum_p'])) {
	  $pageNum_p = $_GET['pageNum_p'];
	}
	$startRow_p = $pageNum_p * $maxRows_p;
	
	
            //implode to search string on ' and ';
          $searchStr = @implode(' and ',$search);
           $str = "";
          if(!empty($_REQUEST['keyword'])) {
            $str = ' and MATCH(description) AGAINST ("'.$_REQUEST['keyword'].'" in BOOLEAN MODE)';
}
          $searchStr .= $str;
          $sql = 'select * FROM items WHERE (expires > NOW()) and active = "Yes" and sold = "No" and ';
          $sql .= $searchStr .' order by featured desc'.$sort;
 
  	        //Add column sorting
		if($_REQUEST['sort'] != '')
			$sort = ' order by price asc ';
		else
			$sort = $_REQUEST['sort'];
	
		### DEBUG
		if($debugP) echo 'Advanced Search Sql<hr>'.$sql;
		
		$error['Results'] = 'No results found, please search again';
		}	
	### Finished Building search sql and execting #####
	$sql .= $sort . $limit;
        	
	//Perform search
	$searchResults = $mysql->exSql($sql);
 
	### BUILD OUTPUT ####
	if (isset($_GET['totalRows_p'])) {
	  $totalRows_p = $_GET['totalRows_p'];
	} else {
	  $all_p = mysql_query($sql.$sort);
	  $totalRows_p = @mysql_num_rows($all_p);
	}
	$totalPages_p = ceil($totalRows_p/$maxRows_p)-1;
 
$queryString_p = "";
if (!empty($_SERVER['QUERY_STRING'])) {
  $params = explode("&", $_SERVER['QUERY_STRING']);
  $newParams = array();
  foreach ($params as $param) {
    if (stristr($param, "pageNum_p") == false && 
        stristr($param, "totalRows_p") == false) {
      array_push($newParams, $param);
    }
  }
  if (count($newParams) != 0) {
    $queryString_p = "&" . htmlentities(implode("&", $newParams));
  }
}
$queryString_p = sprintf("&totalRows_p=%d%s", $totalRows_p, $queryString_p);
	
?>
<div align="left" class="locText"><a href="index.php" class="locLink">Home</a> <span class="locArrow">&nbsp;>&nbsp;</span> <a href="buy.php" class="locLink">Search Properties</a> <span class="locArrow">&nbsp;>&nbsp;</span>Homes For Sale</div>
<hr size="1" color="#666666">
<table border="0" align="center">
  <tr>
    <td align="center">
      <?php if ($pageNum_p > 0) { // Show if not first page ?>
      <a href="<?php printf("%s?pageNum_p=%d%s", $currentPage, 0, $queryString_p); ?>" class="pageLink">First</a> | 
      <?php } // Show if not first page ?>
      <?php if ($pageNum_p > 0) { // Show if not first page ?>
      <a href="<?php printf("%s?pageNum_p=%d%s", $currentPage, max(0, $pageNum_p - 1), $queryString_p); ?>" class="pageLink">Previous</a> | 
      <?php } // Show if not first page ?>
      <?php if ($pageNum_p < $totalPages_p) { // Show if not last page ?>
      <a href="<?php printf("%s?pageNum_p=%d%s", $currentPage, min($totalPages_p, $pageNum_p + 1), $queryString_p); ?>" class="pageLink">Next</a> | 
      <?php } // Show if not last page ?>
      <?php if ($pageNum_p < $totalPages_p) { // Show if not last page ?>
      <a href="<?php printf("%s?pageNum_p=%d%s", $currentPage, $totalPages_p, $queryString_p); ?>" class="pageLink">Last</a>
      <?php } // Show if not last page ?>
    </td>
  </tr>
</table>

Open in new window

0
Comment
Question by:mnoel76
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 21

Expert Comment

by:nizsmo
ID: 20323620
you had an extra bracket which was unneeded, maybe that was why it wasn't working?
<? session_start(); ?>
<div id="loading"> Performing Search... </div>
<?
 
        ### DEBUG
        $debugP = 0;
        ### END DEBUG
 
        #Fetch database connection
        require_once('Connections/myconn.php');
        mysql_select_db($database_myconn,$myconn);
        require_once('myfunctions.php');
        $mysql = new mysql();
        $mysql->conn = $myconn;
        
        #Fetch Header
        include('header.php');
         $currentPage = $_SERVER["PHP_SELF"];
        
        #### BUILD SEARCH SQL BASED ON SEARCH TYPE ####
        
        #defauts
        
        $maxRows_p = 10;
        $pageNum_p = 0;
        if (isset($_GET['pageNum_p'])) {
          $pageNum_p = $_GET['pageNum_p'];
        }
        $startRow_p = $pageNum_p * $maxRows_p;
        
        
            //implode to search string on ' and ';
          $searchStr = @implode(' and ',$search);
           $str = "";
          if(!empty($_REQUEST['keyword'])) {
            $str = ' and MATCH(description) AGAINST ("'.$_REQUEST['keyword'].'" in BOOLEAN MODE)';
}
          $searchStr .= $str;
          $sql = 'select * FROM items WHERE (expires > NOW()) and active = "Yes" and sold = "No" and ';
          $sql .= $searchStr .' order by featured desc'.$sort;
 
                //Add column sorting
                if($_REQUEST['sort'] != '')
                        $sort = ' order by price asc ';
                else
                        $sort = $_REQUEST['sort'];
        
                ### DEBUG
                if($debugP) echo 'Advanced Search Sql<hr>'.$sql;
                
                $error['Results'] = 'No results found, please search again';
                       
        ### Finished Building search sql and execting #####
        $sql .= $sort . $limit;
                
        //Perform search
        $searchResults = $mysql->exSql($sql);
 
        ### BUILD OUTPUT ####
        if (isset($_GET['totalRows_p'])) {
          $totalRows_p = $_GET['totalRows_p'];
        } else {
          $all_p = mysql_query($sql.$sort);
          $totalRows_p = @mysql_num_rows($all_p);
        }
        $totalPages_p = ceil($totalRows_p/$maxRows_p)-1;
 
$queryString_p = "";
if (!empty($_SERVER['QUERY_STRING'])) {
  $params = explode("&", $_SERVER['QUERY_STRING']);
  $newParams = array();
  foreach ($params as $param) {
    if (stristr($param, "pageNum_p") == false && 
        stristr($param, "totalRows_p") == false) {
      array_push($newParams, $param);
    }
  }
  if (count($newParams) != 0) {
    $queryString_p = "&" . htmlentities(implode("&", $newParams));
  }
}
$queryString_p = sprintf("&totalRows_p=%d%s", $totalRows_p, $queryString_p);
        
?>
<div align="left" class="locText"><a href="index.php" class="locLink">Home</a> <span class="locArrow">&nbsp;>&nbsp;</span> <a href="buy.php" class="locLink">Search Properties</a> <span class="locArrow">&nbsp;>&nbsp;</span>Homes For Sale</div>
<hr size="1" color="#666666">
<table border="0" align="center">
  <tr>
    <td align="center">
      <?php if ($pageNum_p > 0) { // Show if not first page ?>
      <a href="<?php printf("%s?pageNum_p=%d%s", $currentPage, 0, $queryString_p); ?>" class="pageLink">First</a> | 
      <?php } // Show if not first page ?>
      <?php if ($pageNum_p > 0) { // Show if not first page ?>
      <a href="<?php printf("%s?pageNum_p=%d%s", $currentPage, max(0, $pageNum_p - 1), $queryString_p); ?>" class="pageLink">Previous</a> | 
      <?php } // Show if not first page ?>
      <?php if ($pageNum_p < $totalPages_p) { // Show if not last page ?>
      <a href="<?php printf("%s?pageNum_p=%d%s", $currentPage, min($totalPages_p, $pageNum_p + 1), $queryString_p); ?>" class="pageLink">Next</a> | 
      <?php } // Show if not last page ?>
      <?php if ($pageNum_p < $totalPages_p) { // Show if not last page ?>
      <a href="<?php printf("%s?pageNum_p=%d%s", $currentPage, $totalPages_p, $queryString_p); ?>" class="pageLink">Last</a>
      <?php } // Show if not last page ?>
    </td>
  </tr>
</table>

Open in new window

0
 

Author Comment

by:mnoel76
ID: 20323693
nizsmo,

thanks for the quick reply.  Unfortunetely that is not the solution as it not is returning no resutls by removing that bracket.  thanks though
0
 
LVL 21

Expert Comment

by:nizsmo
ID: 20323706
what is it outputting at the moment? can't test your code as i don't have the database, but it would be useful to know what it is doing so far to try and diagnose and find the error.
0
 

Author Comment

by:mnoel76
ID: 20323714
Nizsmo,

I should have noted that I had removed some of the code so they wasn't some much to wade through. so that may be why there was a extra bracket.  This is the entire code below.  sorry for that
<? session_start(); ?>
<div id="loading"> Performing Search... </div>
<?
 
	### DEBUG
	$debugP = 0;
	### END DEBUG
 
 	#Fetch database connection
	require_once('Connections/myconn.php');
	mysql_select_db($database_myconn,$myconn);
	require_once('myfunctions.php');
	$mysql = new mysql();
	$mysql->conn = $myconn;
	
	#Fetch Header
	include('header.php');
         $currentPage = $_SERVER["PHP_SELF"];
	
	#### BUILD SEARCH SQL BASED ON SEARCH TYPE ####
	
	#defauts
	
	$maxRows_p = 10;
	$pageNum_p = 0;
	if (isset($_GET['pageNum_p'])) {
	  $pageNum_p = $_GET['pageNum_p'];
	}
	$startRow_p = $pageNum_p * $maxRows_p;
	
	
         //Add state search
         if(!empty($_REQUEST['state'])) {
               $search[] = 'state = "'.$_REQUEST['state'].'"';
         //Add zipcode code search
        if(!empty($_REQUEST['zipcode']))
            $search[] = 'zip = '.$_REQUEST['zipcode'];
         //Add Price From
         if(!empty($_REQUEST['pricefrom']) && ($_REQUEST['pricefrom'] != '-1'))
             $search[] = 'price >= '.$_REQUEST['pricefrom'];
         //Add Max Price
         if(!empty($_REQUEST['priceto']) && ($_REQUEST['priceto'] != '-1'))
               $search[] = 'price <= '.$_REQUEST['priceto'];          
         //Add Property ID
           if(!empty($_REQUEST['idnum']))
               $search[] = 'id = '.$_REQUEST['idnum'];               
          //Add Property Type
           if(!empty($_REQUEST['cid']))
               $search[] = 'cid = '.$_REQUEST['cid'];
          //Add Property Style Type
           if(!empty($_REQUEST['style']))
              $search[] = 'styleid = '.$_REQUEST['style'];
          //Add Min Bed
           if(!empty($_REQUEST['beds']))
               $search[] = 'bed >= "'.$_REQUEST['beds'].'"';
          //Add Min Bath
           if(!empty($_REQUEST['baths']))
                $search[] = 'bath >= "'.$_REQUEST['baths'].'"';
          //Add city
           if(!empty($_REQUEST['city']))
               $search[] = 'city = "'.$_REQUEST['city'].'"';
         //Add min square feet
           if(!empty($_REQUEST['sqft']))
               $search[] = 'sqft >= '.$_REQUEST['sqft'];
          //Add Garage
           if(!empty($_REQUEST['garage']))
               $search[] = 'garage = "'.$_REQUEST['garage'].'"';
          //Add lot size
           if(!empty($_REQUEST['lot_size']))
               $search[] = 'lot_size >= '.$_REQUEST['lot_size'];
          //Add county ID
           if(!empty($_REQUEST['countyID']))
               $search[] = 'countyID = '.$_REQUEST['countyID'].' ';
           if(!empty($_REQUEST['subdiv']))
               $search[] = 'subdiv = "'.$_REQUEST['subdiv'].'"';
 
         //implode to search string on ' and ';
          $searchStr = @implode(' and ',$search);
           $str = "";
          if(!empty($_REQUEST['keyword'])) {
            $str = ' and MATCH(description) AGAINST ("'.$_REQUEST['keyword'].'" in BOOLEAN MODE)';
}
          $searchStr .= $str;
          $sql = 'select * FROM items WHERE (expires > NOW()) and active = "Yes" and sold = "No" and ';
          $sql .= $searchStr .' order by featured desc'.$sort;
 
  	        //Add column sorting
		if($_REQUEST['sort'] != '')
			$sort = ' order by price asc ';
		else
			$sort = $_REQUEST['sort'];
	
		### DEBUG
		if($debugP) echo 'Advanced Search Sql<hr>'.$sql;
		
		$error['Results'] = 'No results found, please search again';
		
}	
 
	### Finished Building search sql and execting #####
	$sql .= $sort . $limit;
        	
	//Perform search
	$searchResults = $mysql->exSql($sql);
 
 
	### BUILD OUTPUT ####
	
	if (isset($_GET['totalRows_p'])) {
	  $totalRows_p = $_GET['totalRows_p'];
	} else {
	  $all_p = mysql_query($sql.$sort);
	  $totalRows_p = @mysql_num_rows($all_p);
	}
	$totalPages_p = ceil($totalRows_p/$maxRows_p)-1;
 
$queryString_p = "";
if (!empty($_SERVER['QUERY_STRING'])) {
  $params = explode("&", $_SERVER['QUERY_STRING']);
  $newParams = array();
  foreach ($params as $param) {
    if (stristr($param, "pageNum_p") == false && 
        stristr($param, "totalRows_p") == false) {
      array_push($newParams, $param);
    }
  }
  if (count($newParams) != 0) {
    $queryString_p = "&" . htmlentities(implode("&", $newParams));
  }
}
$queryString_p = sprintf("&totalRows_p=%d%s", $totalRows_p, $queryString_p);
	
?>
<div align="left" class="locText"><a href="index.php" class="locLink">Home</a> <span class="locArrow">&nbsp;>&nbsp;</span> <a href="buy.php" class="locLink">Search Properties</a> <span class="locArrow">&nbsp;>&nbsp;</span>Homes For Sale</div>
<hr size="1" color="#666666">
<table border="0" align="center">
  <tr>
    <td align="center">
      <?php if ($pageNum_p > 0) { // Show if not first page ?>
      <a href="<?php printf("%s?pageNum_p=%d%s", $currentPage, 0, $queryString_p); ?>" class="pageLink">First</a> | 
      <?php } // Show if not first page ?>
      <?php if ($pageNum_p > 0) { // Show if not first page ?>
      <a href="<?php printf("%s?pageNum_p=%d%s", $currentPage, max(0, $pageNum_p - 1), $queryString_p); ?>" class="pageLink">Previous</a> | 
      <?php } // Show if not first page ?>
      <?php if ($pageNum_p < $totalPages_p) { // Show if not last page ?>
      <a href="<?php printf("%s?pageNum_p=%d%s", $currentPage, min($totalPages_p, $pageNum_p + 1), $queryString_p); ?>" class="pageLink">Next</a> | 
      <?php } // Show if not last page ?>
      <?php if ($pageNum_p < $totalPages_p) { // Show if not last page ?>
      <a href="<?php printf("%s?pageNum_p=%d%s", $currentPage, $totalPages_p, $queryString_p); ?>" class="pageLink">Last</a>
      <?php } // Show if not last page ?>
    </td>
  </tr>
</table>

Open in new window

0
 
LVL 20

Accepted Solution

by:
steelseth12 earned 500 total points
ID: 20326676
Again as in the previous question ... if the form is send by post then $_SERVER['QUERY_STRING'] is empty and $queryString_p is left empty. Change your form to get or do

foreach($_REQUEST as $name => $value){
            if($name != "pageNum_p")
                  $queryString_p .= "&$name=$value";
      }

0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question