Jump to content

Recommended Posts

Hi There,

 

I apologize for this as I'm not a "Expert" in PHP/MYSQL i have a general basic understanding and can do certain things however. I am new to "paging" systems and am looking for help with the following code.

 

The Paging system all works good and well however when I click "Page 2" it shows no results, so I'm assuming it's lost the Database somewhere along the lines.

 

Basically, at this stage someone will "Search" for something it will go to domain.com?page=search&number=1 which will display the first 5 entries of the search they should be able to click next which go to domain.com?page=search&number=2 which will display the next 5 however as soon as you click "next page" it will show an empty page... any idea as to why? I will provide the code below.

 

 

 




$page = $_GET['page'];
// GET PAGE SEARCH OR CATEGORY or ALL
if ($page == "search") {

$pages=$_GET['number']; //Get the current page

$limit=5;

$qresult = mysql_query("SELECT * FROM products");  // Let's get the query

$nrResults=mysql_num_rows($qresult); // Count the results

if (($nrResults%$limit)<>0) {

$pmax=floor($nrResults/$limit)+1;  // Divide to total result by the number of query you want

// to display per page($limit) and create a Max page
} else {

$pmax=floor($nrResults/$limit);
}

echo "Detected Search continuing";

// If SEARCH Then 
$var = $_POST['q'] ; // get the query for the search engine (if applicable)
$trimmed = trim($var); //trim whitespace from the stored variablE
$wsearch = $_POST['type'];


if ($wsearch == "Books") {


// DISPLAY NORMAL BOOKS BULLSHIT
if ($trimmed == "") {
echo "<p>Please enter something to search</p>";
exit;
}

echo $trimmed;
$result = mysql_query("SELECT * FROM `products` WHERE `name` LIKE '%$trimmed%' ORDER BY `id` LIMIT ".(($_GET["number"]-1)*$limit).", $limit") or die(mysql_error());
$count = mysql_numrows($result);
$numrows=mysql_num_rows($result);

if ($numrows == 0 ) { echo "<p style='font-weight: bold'>Sorry, no results were found for your search term $trimmed</p>"; exit; }
// get results
if($numrows > 1){ $return = "results";}
else{ $return = "result"; }

// display what the person searched for
echo "<p>Your search for " . $var . "&quot returned $numrows $return.</p>";
// begin to show results set
$count = 1 + $s ;

while($row = mysql_fetch_array($result)) {
$name = $row['name'];
$desc = $row['description'];
$bookcover = $row['bookcover'];
$price = $row['price'];
$idx = $row['id'];
$insidebook = $row['insidebook'];
$category = $row['category'];
$count++ ;

?>

<!-- PRODUCT LIST>

// Just the table where I display the information.....


<?	 } // END LOOP 
?>
<div class="navigation">
<?
if($pages > 1) {
$prevp="<a href='http://mysite.com.au/books.php?page=search&number=".($pages-1)."' title='Previous Page'>Previous Page</a>"; } else {echo "";}   
echo $prevp;
$pid=1;
while ($pid<=$pmax) {
$paging= "<a href='http://mysite.com.au/books.php?page=search&number=$pid' title='Page $pid of $pmax'> $pid</a>";

$newpaging=str_replace("<a href='http://mysite.com.au/books.php?page=search&number=$pages' title='Page $pages of $pmax'> $pages</a>", "<span>$pages</span>", $paging);
echo $newpaging;
$pid++;  // create pages until reach the result
}
if($pages < $pmax) {
$nextp="<a href='http://mysite.com.au/books.php?page=search&number=".($pages+1)."' title='Next Page'>Next Page</a>"; } else {echo "";}   
echo $nextp;
echo  "<a href='http://mysite.com.au/books.php?page=search&number=$pmax' title='Last Page'>Last Page</a>";
}
?>



Link to comment
https://forums.phpfreaks.com/topic/264971-paging-issue-with-mysql/
Share on other sites

Web servers are stateless. Other than an entry in the access log file, they don't know or care what happened before the current http request and they don't know or care what will happen after the current http request. Every page that is requested is completely separate from every other page request. All values, except session variables, that exist in the code on any page are destroyed when the code on that page finishes running.

 

The $_POST data (what was searched for) that was submitted to your page and the data from the database query no longer exist after the code on the page finishes running.

 

To pass the search term and type with the pagination links, you will need to build the pagination links with keys/values for the 'q' and 'type' in them, then use $_GET['q'] and $_GET['type'] to access the values.

 

 

 

 

 

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.