Solved

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

Posted on 2007-11-20
5
379 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
  • 2
  • 2
5 Comments
 
LVL 21

Expert Comment

by:nizsmo
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
PHP and Soap 3 26
Multilanguage Database Design in MySQL 5 34
Link SQL table to Webpage 9 35
Page showing diff display 4 19
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Creating and Managing Databases with phpMyAdmin in cPanel.
The viewer will learn how to dynamically set the form action using jQuery.
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.

771 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now