By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,349 Members | 1,353 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,349 IT Pros & Developers. It's quick & easy.

Php Paging not showing all records

P: 41
I have a problem that I've spent countless hours on and I'm more than certain this is a obviuos issue to an expert but I am still learning. I have a paging script that I have modified to display a certian amount of records per page. But when I click on the next link , no new result are displayed it keep displaying the same result. Here is a sample of the script
[PHP]
<?php include('hdb.html'); ?>
<?php
// Get the search variable from URL
$var = @$_GET['category'] ;
$trimmed = trim($var); //trim whitespace from the stored variable

// rows to return
$limit=2;

// check for an empty string and display a message.
if ($trimmed == "")
{
echo "<p class=T1>Please enter a search...</p>";
exit;
}

// check for a search parameter
if (!isset($var))
{
echo "<p class=T1>We dont seem to have a search parameter!</p>";
exit;
}

//connect to your database ** EDIT REQUIRED HERE **
mysql_connect("xxxxx","xxxxxxx","xxxxxx"); //(host, username, password)

//specify database ** EDIT REQUIRED HERE **
mysql_select_db("xxxxxxxxx") or die("Unable to select database"); //select which database we're using

// Build SQL Query
$query = "select * from pix where category like \"%$trimmed%\"
order by category, pid, albumname, "; // EDIT HERE and specify your table and field names for the SQL query

$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults);

// If we have no results, offer a google search as an alternative

if ($numrows == 0)
{
echo "<h4>Results</h4>";
echo "<p class=T1>Sorry, your search: &quot;" . $trimmed . "&quot; returned zero results</p>";

// google
echo "<p class=T1><a href=\"http://www.google.com/search?q="
. $trimmed . "\" target=\"_blank\" title=\"Look up
" . $trimmed . " on Google\">Click here</a> to try the
search on google</p>";
}

// next determine if s has been passed to script, if not use 0
if (empty($s)) {
$s=0;
}

// get results
$query .= " limit $s,$limit";
$result = mysql_query($query) or die("Couldn't execute query");

// display what the person searched for
echo '<center><table width="550" border="0" cellspacing="1" cellpadding="2" bgcolor="#Ffffff"><tr>';
echo '<td width=230 class=T1>';
echo "<p class=T1>Your searched for: &quot;" .$var . "&quot;</p>";
echo '</td>
<td align=right width=335 padding="3" bgcolor=#eoeccc class=T1>
//html goes here
</td>
</tr></table></center>';

// begin to show results set
echo "";
$count = 1 + $s ;

// now you can display the results returned
while ($row= mysql_fetch_array($result)) {
$state = stripslashes($row["state"]);
$pid= stripslashes($row['pid']);
$albumname= stripslashes($row['albumname']);

echo '<center><table width="550" border="0" cellspacing="1" cellpadding="2" bgcolor="#F7F7F7"><tr>
<td onmouseover="this.className=\'bgcl\';" onmouseout="this.className=\'gb2\';" width=330 align=top bgcolor=#Ffffff class=T1>
//html from output goes here
</td>
</tr>';
}
echo "</table></center>";
$count++ ;

$currPage = (($s/$limit) + 1);

//break before paging
echo "<br />";

// next we need to do the links to other results
if ($s>=1) { // bypass PREV link if s is 0
$prevs=($s-$limit);
print "&nbsp;<a href=\"".$_SERVER['PHP_SELF']."??s=$prevs&category=$var\">&lt;&lt;
Prev 10</a>&nbsp&nbsp;";
}

// calculate number of pages needing links
$pages=intval($numrows/$limit);

// $pages now contains int of pages needed unless there is a remainder from division

if ($numrows%$limit) {
// has remainder so add one page
$pages++;
}

// check to see if last page
if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {

// not last page so give NEXT link
$news=$s+$limit;

echo "&nbsp;<a href=\"".$_SERVER['PHP_SELF']."?s=$news&category=$var\">Next 10 &gt;&gt;</a>";
}

$a = $s + ($limit) ;
if ($a > $numrows) { $a = $numrows ; }
$b = $s + 1 ;
echo "<center><p class=T1>Showing results $b to $a of $numrows</p></center>";

?>
<?php include('footer.html'); ?>
[/PHP]
I've already modified the php self function to work properly but the info being pass back to page when next link is clicked is not envoking the script to display the next set of records
May 8 '07 #1
Share this Question
Share on Google+
7 Replies


code green
Expert 100+
P: 1,726
You are conducting this test, [PHP]if (empty($s)) {
$s=0;[/PHP]but have not read the value of $s through your GET array so $s will always be zero. You seem to pass it OK [PHP]echo "&nbsp;<a href=\"".$_SERVER['PHP_SELF']."
?s=$news&category=$var\">Next 10 &gt;&gt;</a>";[/PHP] but only read category [PHP]$var = @$_GET['category'] ;[/PHP]By the way DO NOT USE '@' except when deploying the final script live. This is a terrible habit.
May 8 '07 #2

P: 41
I think I grasp what your saying. I will try it again and post back my findings latter.
;-)
May 8 '07 #3

pbmods
Expert 5K+
P: 5,821
By the way DO NOT USE '@' except when deploying the final script live. This is a terrible habit.
I might add to that by saying that you should really never need to use '@' ever. If you want to hide your errors from the end User, just set:

Expand|Select|Wrap|Line Numbers
  1. display_errors = Off
  2. log_errors = On
  3.  
And optionally:

Expand|Select|Wrap|Line Numbers
  1. error_log /path/to/error_log
  2.  
in your php.ini file (usually located at /usr/local/lib/php.ini or somewhere else on Windows [somebody who uses a 'doze box on a regular basis will probably be a lot more helpful than I can in that respect]).

Not only will your User never see error messages, but you don't have to go back into your code and remove a bunch of '@' characters if your script messes up.

[EDIT: And most web hosting providers allow you to edit a php.ini file at the root level of your site.]
May 8 '07 #4

P: 41
Hi ,I just got in and used your advice the script worked perfectly. I added the Variable s to the get function! No more paging problem

Thanks for the help
May 9 '07 #5

code green
Expert 100+
P: 1,726
Well done Underground. I agree with pbmods. Why do people feel compelled to use '@'. in their scripts, especially when developing? I use the following code within a config file on all my scripts. Then I can use DEBUG and print 'Any message I want' all over the script and simply set DEBUG to zero to turn everything off.[PHP]
if(!defined('DEBUG'))
define('DEBUG',1);

if(DEBUG)
error_reporting(E_ALL | E_STRICT);
else
ini_set('display_errors',0);[/PHP]
May 10 '07 #6

P: 3
Hey hey hey hey hey hey
Jul 2 '07 #7

pbmods
Expert 5K+
P: 5,821
Heya, Mesut.

Hey hey hey hey hey hey
Yes?
Jul 2 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.