...

View Full Version : Resolved Pagination Problem!



greens85
10-26-2009, 12:10 PM
Hi All,

I have a page pagination script, which will brake up the returned results (for the searched terms) but once I click through to the second page, instead of returning the 2nd page of results I searched for.... It starts to return all of the results..

So say I searched for apple, it would return 5 instances of that and split then when i clicked page 2 it would return everything, e.g. orange, grape, pear etc etc...

The code is:



<?php
$name = mysql_real_escape_string($_POST['name']);
$lea = mysql_real_escape_string($_POST['lea']);
$type = mysql_real_escape_string($_POST['type']);
$postcode = mysql_real_escape_string($_POST['postcode']);

if (isset($_GET['pageno'])) {
$pageno = $_GET['pageno'];
} else {
$pageno = 1;
} // if

$query = "SELECT count(*) FROM schools WHERE Name LIKE '%$name%' AND Leaname LIKE '%$lea%' AND SchoolType LIKE '%$type%' AND PostcodeSubArea LIKE '%$postcode' ORDER BY Name ASC";
$result = mysql_query($query) or die (mysql_error());
$query_data = mysql_fetch_row($result);
$numrows = $query_data[0];

$rows_per_page = 5;
$lastpage = ceil($numrows/$rows_per_page);

$pageno = (int)$pageno;
if ($pageno > $lastpage) {
$pageno = $lastpage;
} // if
if ($pageno < 1) {
$pageno = 1;
} // if

$limit = 'LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page;

$query = "SELECT * FROM schools WHERE Name LIKE '%$name%' AND Leaname LIKE '%$lea%' AND SchoolType LIKE '%$type%' AND PostcodeSubArea LIKE '%$postcode' $limit";
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array ($result)) {
?>
<a href="schooldetails.php?RID=<?php echo $row['RID'];?>"><?php echo $row['Name'];?></a>
<?php
echo '<br/>';
echo '<br/>';
echo 'LEA:'.' '.'<span class="normalfont">'.$row['Leaname'].'</span>';
echo '<br/>';
echo 'Address:'.' '.'<span class="normalfont">'.$row['Address1'].'</span>';
echo '<br/>';
echo '<span class="normalfont">'.$row['Address2'].'</span>';
echo '<br/>';
echo '<span class="normalfont">'.$row['Address3'].'</span>';
echo '<br/>';
echo 'Postcode:'.' '.'<span class="normalfont">'.$row['Postcode'].'</span>';
echo '<br/>';
echo 'Telephone:'.' '.'<span class="normalfont">0'.$row['Phone'].'</span>';
echo '<br/>';
echo 'Fax:'.' '.'<span class="normalfont">0'.$row['Fax'].'</span>';
echo '<br/>';?>
Website: <span class="normalfont"><a href="<?php echo $row['url']?>"><?php echo $row['url'];?></a></span>
<?php
echo '<br/>';
echo '<br/>';
}
if ($pageno == 1) {
echo " FIRST PREV ";
} else {
echo " <a href='{$_SERVER['PHP_SELF']}?pageno=1'>FIRST</a> ";
$prevpage = $pageno-1;
echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$prevpage'>PREV</a> ";
} // if

echo " ( Page $pageno of $lastpage ) ";

if ($pageno == $lastpage) {
echo " NEXT LAST ";
} else {
$nextpage = $pageno+1;
echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$nextpage'>NEXT</a> ";
echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$lastpage'>LAST</a> ";
} // if

Can anyone see what I'm missing here?

I followed a tutorial on the net, but it doesn't seem to be working!

Thanks,

Greens85

tomws
10-26-2009, 01:58 PM
You must have missed something in the tutorial because the POST variables won't be passed to the "next" page by clicking a link. Without those, all of your WHERE..LIKE clauses are empty, which means you're going to get all results.

greens85
10-26-2009, 02:20 PM
You must have missed something in the tutorial because the POST variables won't be passed to the "next" page by clicking a link. Without those, all of your WHERE..LIKE clauses are empty, which means you're going to get all results.

Hey Tomws,

Thanks for the reply... I don't see how i could of missed anything as its broken up into different pieces of code, and I copied them one by one (I could have even copied and pasted but I thought it might help me learn if I copied the code my self)...

Just for the reference what I used was:

http://www.tonymarston.net/php-mysql/pagination.html#a4

I see your point about no variables being passed, but if you click that link you'll see it is the same there :confused:

tomws
10-26-2009, 02:29 PM
You're right: you didn't miss anything. But you did add to it. (Just the POST vars.) :)

You'll need to pass the initial POST variables to subsequent pages. I'm not sure what the usual way of doing this is, but you could look at passing keywords in the GET string or using sessions. Maybe other ways. I would probably go for sessions, but the GET option would allow for bookmarking of results pages, if that's desired.

greens85
10-26-2009, 02:42 PM
You're right: you didn't miss anything. But you did add to it. (Just the POST vars.) :)

You'll need to pass the initial POST variables to subsequent pages. I'm not sure what the usual way of doing this is, but you could look at passing keywords in the GET string or using sessions. Maybe other ways. I would probably go for sessions, but the GET option would allow for bookmarking of results pages, if that's desired.

Sorry I didnt really get what you meant by that :(

When you say I added to it, im guessing you mean these lines, where i am just posting what the user entered?


$name = mysql_real_escape_string($_POST['name']);
$lea = mysql_real_escape_string($_POST['lea']);
$type = mysql_real_escape_string($_POST['type']);
$postcode = mysql_real_escape_string($_POST['postcode']);

When you say passing keywords in the get string, do you mean when they click next, if so im not sure how to do this :confused:

tomws
10-26-2009, 02:51 PM
Yes, those are the added lines I'm talking about. They're important because those are your search terms. Subsequent pages need that information because you're querying based upon those terms. POST values are only available to the page to which the form action points.

Passing the keywords (or "search terms", or "magic monkeys", or whatever you want to call them) in the GET string is easy. Do it just like you're passing the page number. Here's one line modified, for example:

echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$nextpage&name=$name&lea=$lea&type=$type&postcode=$postcode'>NEXT</a> ";

Doing that, you'd need to modify the top of the code to test whether the variables are coming in GET or POST. Perhaps something like this:

// Am I coming from the form page?
if (isset($_POST['submit'])) // the name of the submit button, whatever you named it
{
// use the POST vars
}
else // Not coming from the form page
{
// use the GET vars
}

greens85
10-26-2009, 03:17 PM
With the if statement am i correct in thinking:


if (isset($_POST['submit'])) {
$name = mysql_real_escape_string($_POST['name']);
$lea = mysql_real_escape_string($_POST['lea']);
$type = mysql_real_escape_string($_POST['type']);
$postcode = mysql_real_escape_string($_POST['postcode']);
} else {
// Not sure how I display the GET vars here???
}

tomws
10-26-2009, 03:50 PM
if (isset($_POST['submit'])) {
$name = mysql_real_escape_string($_POST['name']);
$lea = mysql_real_escape_string($_POST['lea']);
$type = mysql_real_escape_string($_POST['type']);
$postcode = mysql_real_escape_string($_POST['postcode']);
} else {
$name = mysql_real_escape_string($_GET['name']);
$lea = mysql_real_escape_string($_GET['lea']);
$type = mysql_real_escape_string($_GET['type']);
$postcode = mysql_real_escape_string($_GET['postcode']);
}

greens85
10-26-2009, 03:57 PM
if (isset($_POST['submit'])) {
$name = mysql_real_escape_string($_POST['name']);
$lea = mysql_real_escape_string($_POST['lea']);
$type = mysql_real_escape_string($_POST['type']);
$postcode = mysql_real_escape_string($_POST['postcode']);
} else {
$name = mysql_real_escape_string($_GET['name']);
$lea = mysql_real_escape_string($_GET['lea']);
$type = mysql_real_escape_string($_GET['type']);
$postcode = mysql_real_escape_string($_GET['postcode']);
}

Hey Tomws,

This is the code I have now:


<?php
if (isset($_POST['submit'])) {
$name = mysql_real_escape_string($_POST['name']);
$lea = mysql_real_escape_string($_POST['lea']);
$type = mysql_real_escape_string($_POST['type']);
$postcode = mysql_real_escape_string($_POST['postcode']);
} else {
$name = mysql_real_escape_string($_GET['name']);
$lea = mysql_real_escape_string($_GET['lea']);
$type = mysql_real_escape_string($_GET['type']);
$postcode = mysql_real_escape_string($_GET['postcode']);
}

echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$nextpage&amp;Name=$name&amp;Leaname=$lea&amp;SchoolType=$type&amp;PostcodeSubArea=$postcode'>NEXT</a> ";
?>

But the same problems are occuring :confused:

Have I missed something?

tomws
10-26-2009, 04:15 PM
Why is that the only code you have? Re-read the previous posts. I never said to destroy everything and use only those lines.

greens85
10-26-2009, 04:33 PM
Why is that the only code you have? Re-read the previous posts. I never said to destroy everything and use only those lines.

sorry didnt make myself clear...

I only posted those lines as they are the only ones that have been altered (upon your knowledge)... the full code is:


<?php

if (isset($_POST['submit'])) {
$name = mysql_real_escape_string($_POST['name']);
$lea = mysql_real_escape_string($_POST['lea']);
$type = mysql_real_escape_string($_POST['type']);
$postcode = mysql_real_escape_string($_POST['postcode']);

} else {

$name = mysql_real_escape_string($_GET['name']);
$lea = mysql_real_escape_string($_GET['lea']);
$type = mysql_real_escape_string($_GET['type']);
$postcode = mysql_real_escape_string($_GET['postcode']);
}

if (isset($_GET['pageno'])) {
$pageno = $_GET['pageno'];
} else {
$pageno = 1;
} // if

$query = "SELECT count(*) FROM schools WHERE Name LIKE '%$name%' AND Leaname LIKE '%$lea%' AND SchoolType LIKE '%$type%' AND PostcodeSubArea LIKE '%$postcode' ORDER BY Name ASC";
$result = mysql_query($query) or die (mysql_error());
$query_data = mysql_fetch_row($result);
$numrows = $query_data[0];

$rows_per_page = 5;
$lastpage = ceil($numrows/$rows_per_page);

$pageno = (int)$pageno;
if ($pageno > $lastpage) {
$pageno = $lastpage;
} // if
if ($pageno < 1) {
$pageno = 1;
} // if

$limit = 'LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page;

$query = "SELECT * FROM schools WHERE Name LIKE '%$name%' AND Leaname LIKE '%$lea%' AND SchoolType LIKE '%$type%' AND PostcodeSubArea LIKE '%$postcode' $limit";
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array ($result)) {
?>
<a href="schooldetails.php?Name=<?php echo $row['Name'];?>"><?php echo $row['Name'];?></a>
<?php
echo '<br/>';
echo '<br/>';
echo 'LEA:'.' '.'<span class="normalfont">'.$row['Leaname'].'</span>';
echo '<br/>';
echo 'Address:'.' '.'<span class="normalfont">'.$row['Address1'].'</span>';
echo '<br/>';
echo '<span class="normalfont">'.$row['Address2'].'</span>';
echo '<br/>';
echo '<span class="normalfont">'.$row['Address3'].'</span>';
echo '<br/>';
echo 'Postcode:'.' '.'<span class="normalfont">'.$row['Postcode'].'</span>';
echo '<br/>';
echo 'Telephone:'.' '.'<span class="normalfont">0'.$row['Phone'].'</span>';
echo '<br/>';
echo 'Fax:'.' '.'<span class="normalfont">0'.$row['Fax'].'</span>';
echo '<br/>';?>
Website: <span class="normalfont"><a href="<?php echo $row['url']?>"><?php echo $row['url'];?></a></span>
<?php
echo '<br/>';
echo '<br/>';
}
if ($pageno == 1) {
echo " FIRST PREV ";
} else {
echo " <a href='{$_SERVER['PHP_SELF']}?pageno=1'>FIRST</a> ";
$prevpage = $pageno-1;
echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$prevpage'>PREV</a> ";
} // if

echo " ( Page $pageno of $lastpage ) ";

if ($pageno == $lastpage) {
echo " NEXT LAST ";
} else {
$nextpage = $pageno+1;
echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$nextpage&amp;Name=$name&amp;Leaname=$lea&amp;SchoolType=$type&amp;PostcodeSubArea=$postcode'>NEXT</a> ";
echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$lastpage'>LAST</a> ";
} // if
?>

But like I said, im having the same problem as I was before :confused:

tomws
10-26-2009, 04:49 PM
See post #6 (http://www.codingforums.com/showpost.php?p=880801&postcount=6) again. You're not using the link like I provided. Compare these lines:

echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$nextpage&name=$name&lea=$lea&type=$type&postcode=$postcode'>NEXT</a> ";
echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$nextpage&amp;Name=$name&amp;Leaname=$lea&amp;SchoolType=$type&amp;PostcodeSubArea=$postcode'>NEXT</a> ";

Note also that I said that's one line modified and that you need to pass the values in each link if you're going to use the GET string.

greens85
10-26-2009, 04:52 PM
See post #6 (http://www.codingforums.com/showpost.php?p=880801&postcount=6) again. You're not using the link like I provided. Compare these lines:

echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$nextpage&name=$name&lea=$lea&type=$type&postcode=$postcode'>NEXT</a> ";
echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$nextpage&amp;Name=$name&amp;Leaname=$lea&amp;SchoolType=$type&amp;PostcodeSubArea=$postcode'>NEXT</a> ";

Note also that I said that's one line modified and that you need to pass the values in each link if you're going to use the GET string.

Ahhh, sorry about that I thought that the text before the equals should have represented the name of the field in the database, but obviously not!

I realised you had only done one line, I was just doing one also while I tested it!

Anyway it's working now, so many thanks for your help which is greatly appreciated.

tomws
10-26-2009, 04:55 PM
Ahhh, sorry about that I thought that the text before the equals should have represented the name of the field in the database, but obviously not!
Ah! I didn't know where you got those from and thought you were crazy. :D

You can make them whatever you want, but they must match the names used in the assigments at the top. I only used those to match the POST names - thought it might be less confusing.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum