• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 415
  • Last Modified:

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

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
mnoel76
Asked:
mnoel76
  • 2
  • 2
1 Solution
 
nizsmoDeveloperCommented:
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
 
mnoel76Author Commented:
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
 
nizsmoDeveloperCommented:
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
 
mnoel76Author Commented:
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
 
steelseth12Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now