473,387 Members | 1,583 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Paging Problems

When running the script below I am able to get all related table results and display them the way I want. When it came to my paging script I entered and limited the rows per page to 2 (for a 3 record result) I get the first 2 records on page fine, when click through to next I get notices about undefined variables. I really dont understand.

The code is,
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3.  
  4. </head>
  5. <body>
  6. <?php 
  7.  
  8. //include connection.php
  9.  
  10. //This checks to see if there is a page number. If not, it will set it to page 1
  11. if (!(isset($pagenum)))
  12. {
  13. $pagenum = 1;
  14.  
  15. $min_price = $_POST['min_price'];
  16. $max_price = $_POST['max_price'];
  17. $property_type = $_POST['type'];
  18. $location = $_POST['location'];
  19. $min_bedrooms = $_POST['min_beds'];
  20. $keywords = $_POST['keywords'];
  21.  
  22. if($keywords != " ")
  23. {
  24. $result = mysql_query("SELECT * FROM search WHERE price BETWEEN '$min_price' AND '$max_price' AND bedrooms >= '$min_bedrooms' AND location = '$location' AND type = '$property_type' AND keywords LIKE '%$keywords%' "); 
  25. }
  26. else
  27. {
  28. $result = mysql_query("SELECT * FROM search WHERE price BETWEEN '$min_price' AND '$max_price' AND bedrooms >= '$min_bedrooms' AND location = '$location' AND type = '$property_type' "); 
  29. }
  30. if(!$result) die(mysql_error()); 
  31. $rows = mysql_num_rows($result);
  32. if($rows== 0) die("<p>No matches met your criteria."); 
  33.  
  34. //This is the number of results displayed per page
  35. $page_rows = 2;
  36.  
  37. //This tells us the page number of our last page
  38. $last = ceil($rows/$page_rows);
  39.  
  40. //this makes sure the page number isn't below one, or more than our maximum pages
  41. if ($pagenum < 1)
  42. {
  43. $pagenum = 1;
  44. }
  45. elseif ($pagenum > $last)
  46. {
  47. $pagenum = $last;
  48. }
  49.  
  50. //This sets the range to display in our query
  51. $max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;
  52. //This is your query again, the same one... the only difference is we add $max into it
  53. //$data_p = mysql_query("SELECT * FROM search WHERE price BETWEEN '$min_price' AND '$max_price' AND bedrooms >= '$min_bedrooms' AND location = '$location' AND type = '$property_type' AND keywords LIKE '%$keywords%' $max") or die(mysql_error());
  54. if($keywords != " ")
  55. {
  56. $data_p = mysql_query("SELECT * FROM search WHERE price BETWEEN '$min_price' AND '$max_price' AND bedrooms >= '$min_bedrooms' AND location = '$location' AND type = '$property_type' AND keywords LIKE '%$keywords%' $max"); 
  57. }
  58. else
  59. {
  60. $data_p = mysql_query("SELECT * FROM search WHERE price BETWEEN '$min_price' AND '$max_price' AND bedrooms >= '$min_bedrooms' AND location = '$location' AND type = '$property_type' $max"); 
  61. }
  62. if(!$data_p) die(mysql_error()); 
  63.  
  64. while($info=mysql_fetch_array($data_p)){ 
  65.  
  66. $formatted = number_format($info['price'],2);
  67.  
  68. echo "<div id=\"right\"><img src=\"".$info['image']."\"><a href=\"detail.php\">".$info['title']."</a><h4>Offers over £".$formatted."</h4><p>".$info['type'].", ".$info['bedrooms']." bedrooms</p><br /></div>\n";
  69.  
  70.  
  71. echo "<p>";
  72.  
  73. // This shows the user what page they are on, and the total number of pages
  74. echo " --Page $pagenum of $last-- <p>";
  75.  
  76. // First we check if we are on page one. If we are then we don't need a link to the previous page or the first page so we do nothing. If we aren't then we generate links to the first page, and to the previous page.
  77. if ($pagenum == 1)
  78. {
  79. }
  80. else
  81. {
  82. echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
  83. echo " ";
  84. $previous = $pagenum-1;
  85. echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> ";
  86. }
  87.  
  88. //just a spacer
  89. echo " ---- ";
  90.  
  91. //This does the same as above, only checking if we are on the last page, and then generating the Next and Last links
  92. if ($pagenum == $last)
  93. {
  94. }
  95. else {
  96. $next = $pagenum+1;
  97. echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";
  98. echo " ";
  99. echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";
  100. }
  101.  
  102. ?>
  103. </body>
  104. </html>
  105.  
It may be because I'm tired but I can't see what would cause this. Please help if you can.
Oct 3 '07 #1
18 2018
code green
1,726 Expert 1GB
When you pass the URL variable pagenum
Expand|Select|Wrap|Line Numbers
  1. <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";
You need to extract this on the new page with $_GET['pagenum'].
The following means pagenum will always be 1
[PHP]if (!(isset($pagenum)))
{
$pagenum = 1;
} [/PHP] You need
[PHP]if (!(isset($_GET['pagenum'])))
{
$pagenum = 1;
} [/PHP]
Oct 3 '07 #2
When you pass the URL variable pagenum
Expand|Select|Wrap|Line Numbers
  1. <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";
You need to extract this on the new page with $_GET['pagenum'].
The following means pagenum will always be 1
[PHP]if (!(isset($pagenum)))
{
$pagenum = 1;
} [/PHP] You need
[PHP]if (!(isset($_GET['pagenum'])))
{
$pagenum = 1;
} [/PHP]
Thanks for the reply but it made no difference just the exact same output!
Oct 3 '07 #3
code green
1,726 Expert 1GB
Thanks for the reply but it made no difference just the exact same output!
Well you have to make some personal effort. I was only showing where you have gone wrong, not rewriting the code for you.
You still need to extract the pagenum variable from the URL and pass this into $pagenum.[PHP]$pagenum = $_GET['pagenum'];[/PHP]
Oct 3 '07 #4
Well you have to make some personal effort. I was only showing where you have gone wrong, not rewriting the code for you.
You still need to extract the pagenum variable from the URL and pass this into $pagenum.[PHP]$pagenum = $_GET['pagenum'];[/PHP]
sorry if I offended you with the last comment but all I am saying is that, to me, the code you advised to put in has the exact same effect as the code I already had. It's just written a different way.
Oct 3 '07 #5
ak1dnar
1,584 Expert 1GB
Hi,
This forum software provides CODE tags to format your source codes. when the text editor appears, See the REPLY GUIDELINES on the right side of the page, to find out how to use them.

[code=php]
Oct 3 '07 #6
Still having problems with this. Anyone else evr had problems with an "unidentified index" whilst trying to insert pagination?
Oct 3 '07 #7
code green
1,726 Expert 1GB
Still having problems with this.
That is because you have made no attempt. I have shown you what you are doing wrong.
You are not extracting the URL variables when paging through to the next page.
the code you advised to put in has the exact same effect as the code I already had. It's just written a different way.
Of course it is written a different way. It is written the correct way.
Anyone else evr had problems with an "unidentified index" whilst trying to insert pagination
Please do not talk over me.
It seems like you need taking by the hand and walking through this.
What does this piece of code look like now? [PHP]if (!(isset($pagenum)))
{
$pagenum = 1;
} [/PHP]
Oct 3 '07 #8
I have put in ...
[PHP]<?php
$pagenum = $_GET['pagenum'];[/PHP]

at the top of my file andfollowed on with...
[PHP]if (!(isset($pagenum)))
{
$pagenum = 1;
}[/PHP]

And I also tested it further done the code with an echo statement to extract the page number. It appeared on first page as expected - yet proceeding page just had an unidentified index message and also printed my "No Matches" message.

I sincerley appologise if I have upset you in anyway I did not intend to insult you in any manner. I hole heartidly appreciate your advice. Thank you.
Oct 3 '07 #9
code green
1,726 Expert 1GB
OK no problem. It's been a long day for me also.
Well at least now it looks like we are extracting the URL variable pagenum.
After this line [PHP]$pagenum = $_GET['pagenum'];[/PHP]Write [PHP]echo $pagenum;[/PHP]
The following test is now redundant because $pagenum is set as soon as you declare it.
[PHP]if (!(isset($pagenum)))[/PHP]It is better if now [PHP]if (!empty($pagenum))[/PHP]This will tell you if anything was actually passed.
Lets have a look at the hyperlinks. They appear more than once which is not ideal
[PHP]echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";
echo " ";
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";[/PHP]The quotes may be causing a problem, single quotes are literal strings and you have not escaped any.
I have a rule. Single quotes for php. Double for HTML.
The following is faster but requires more care when writing. But it is correct.
[PHP]echo '<a href="'.$_SERVER['PHP_SELF'].'?pagenum='.$next.'">Next -></a>';[/PHP]And echo out all pagenum, next and previous variables so we can see what is there. Good luck.
Oct 3 '07 #10
I still haven't conquered this problem, I spent most of the day yesterday and all of last night trying to sort this out but I am totally stumped. I think I'm just going to have to go with just having 1 results page and maybe just links on it to keep it small. Not ideal but at least it will work, unlike the myth that is pagination.
Oct 5 '07 #11
When I put in this part of test code to see what the variable contained...
[PHP]$pagenum = $_GET['pagenum'];
echo"Statement after declare of pagenum";
echo $pagenum;[/PHP]

it produced the text line but no value what so ever for $pagenum. Any suggestions anybody?
Oct 5 '07 #12
code green
1,726 Expert 1GB
Now we can start to narrow down your problem.
You have implemented one suggestion of mine but you have obviously not done this[PHP] if (!empty($pagenum))[/PHP] Like I said, this will show if pagenum contains a value. Whilst isset() will not.
And if you check your code, everything depends upon pagenum having a value.
But pagenum does not have a value when the script first runs.
That is why nothing happens.
You need something like [PHP] if (empty($pagenum))
$pagenum = 1;[/PHP] Did you follow my suggestion to re-write the hyperlinks by the way?
Oct 5 '07 #13
I implemented all of your suggestions yesterday but still couldn't crack the conundrun, my code as is ....
[PHP]<html>
<head>
<html>
<head>
</head>
<body>
<?php
$pagenum = $_GET['pagenum'];
echo"Statement after declare of pagenum";
echo $pagenum;

// Connects to your Database
// have my connection details here

//This checks to see if there is a page number. If not, it will set it to page 1
if (!empty($pagenum))
{
$pagenum = 1;
}
echo $pagenum;

$min_price = $_POST['min_price'];
$max_price = $_POST['max_price'];
$property_type = $_POST['type'];
$location = $_POST['location'];
$min_bedrooms = $_POST['min_beds'];
$keywords = $_POST['keywords'];

if($keywords != " ")
{
$result = mysql_query("SELECT * FROM search WHERE price BETWEEN '$min_price' AND '$max_price' AND bedrooms >= '$min_bedrooms' AND location = '$location' AND type = '$property_type' AND keywords LIKE '%$keywords%' ");
}
else
{
$result = mysql_query("SELECT * FROM search WHERE price BETWEEN '$min_price' AND '$max_price' AND bedrooms >= '$min_bedrooms' AND location = '$location' AND type = '$property_type' ");
}
if(!$result) die(mysql_error());
$rows = mysql_num_rows($result);
if($rows== 0) die("<p>No matches met your criteria.");

//This is the number of results displayed per page
$page_rows = 2;

//This tells us the page number of our last page
$last = ceil($rows/$page_rows);

//this makes sure the page number isn't below one, or more than our maximum pages
if ($pagenum < 1)
{
$pagenum = 1;
}
elseif ($pagenum > $last)
{
$pagenum = $last;
}

//This sets the range to display in our query
$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;
//This is your query again, the same one... the only difference is we add $max into it
//$data_p = mysql_query("SELECT * FROM search WHERE price BETWEEN '$min_price' AND '$max_price' AND bedrooms >= '$min_bedrooms' AND location = '$location' AND type = '$property_type' AND keywords LIKE '%$keywords%' $max") or die(mysql_error());
//echo $pagenum;
if($keywords != " ")
{
$data_p = mysql_query("SELECT * FROM search WHERE price BETWEEN '$min_price' AND '$max_price' AND bedrooms >= '$min_bedrooms' AND location = '$location' AND type = '$property_type' AND keywords LIKE '%$keywords%' $max");
}
else
{
$data_p = mysql_query("SELECT * FROM search WHERE price BETWEEN '$min_price' AND '$max_price' AND bedrooms >= '$min_bedrooms' AND location = '$location' AND type = '$property_type' $max");
}
if(!$data_p) die(mysql_error());

while($info=mysql_fetch_array($data_p))
{
$formatted = number_format($info['price'],2);

echo "<div id=\"right\"><img src=\"".$info['image']."\"><a href=\"detail.php?id=". $info['id']. "\">".$info['title']."</a><h4>Offers over £".$formatted."</h4><p>".$info['type'].", ".$info['bedrooms']." bedrooms</p><br /></div>\n";

}

echo "<p>";

// This shows the user what page they are on, and the total number of pages
echo " --Page $pagenum of $last-- <p>";

// First we check if we are on page one. If we are then we don't need a link to the previous page or the first page so we do nothing. If we aren't then we generate links to the first page, and to the previous page.
if ($pagenum == 1)
{
}
else
{
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
echo " ";
$previous = $pagenum-1;

echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> ";
}

//just a spacer
echo " ---- ";

//This does the same as above, only checking if we are on the last page, and then generating the Next and Last links

if ($pagenum == $last)
{
}
else {
$next = $pagenum+1;
echo '<a href="'.$_SERVER['PHP_SELF'].'?pagenum='.$next.'">Next -></a>';
echo '<a href="'.$_SERVER['PHP_SELF'].'?pagenum='.$last.'">Last-></a>';
}

?>
</body>
</html>[/PHP]

I'm not sure why there is no value what so ever given for pagenum when I echo it at the top.
Oct 5 '07 #14
code green
1,726 Expert 1GB
Your not quite there with this.[PHP]if (!empty($pagenum))
{
$pagenum = 1;
}
echo $pagenum;[/PHP] empty returns true if the variable is empty and NOT will flip this. Try to be a little more diligent [PHP]if (empty($pagenum))
{
$pagenum = 1;
}
echo $pagenum;[/PHP]
Oct 5 '07 #15
Your not quite there with this.[PHP]if (!empty($pagenum))
{
$pagenum = 1;
}
echo $pagenum;[/PHP] empty returns true if the variable is empty and NOT will flip this. Try to be a little more diligent [PHP]if (empty($pagenum))
{
$pagenum = 1;
}
echo $pagenum;[/PHP]
Sorry, I don't understand what you mean with the last post. Could you explain please.
Oct 5 '07 #16
code green
1,726 Expert 1GB
Your test is asking
Expand|Select|Wrap|Line Numbers
  1. If pagenum is NOT empty, set pagenum to 1
I have re-written to say
Expand|Select|Wrap|Line Numbers
  1. If pagenum IS empty, set pagenum to 1
The only difference is the '!' meaning NOT
Oct 5 '07 #17
Your test is asking
Expand|Select|Wrap|Line Numbers
  1. If pagenum is NOT empty, set pagenum to 1
I have re-written to say
Expand|Select|Wrap|Line Numbers
  1. If pagenum IS empty, set pagenum to 1
The only difference is the '!' meaning NOT
Oops, sorry I didn't see the adjustment. Just put that in and that has given pagenum an initial value now, when I clicked onto the 2nd page pagenum value was 22!
Oct 5 '07 #18
code green
1,726 Expert 1GB
22? I assume these are the offending lines. [PHP]$next = $pagenum+1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";[/PHP] Then pagenum must be 21.
You haven't changed the hyperlink string to how I suggested.
It's probably OK it is just not the way I write them.
When using mathematical operators always ensure all variables are initialised.
If not they may contain spurious data.
This may not be the problem.
I cannot spot the problem so I suggest echoing out the variables throughout to see where their values are changing
Oct 5 '07 #19

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

Similar topics

4
by: Coleen | last post by:
Sorry about the multi-posting, but it does not seem as though the other newsgroups are frequented enough to get any advice on this problem...If anyone can possibly help, I would greatly appreciate...
2
by: Maziar Aflatoun | last post by:
Hi, I'm having problems with Custom Paging. Most of the articles out there show that the custom paging is done as the following article. ...
1
by: Danny Ni | last post by:
Hi, We use ASP.Net datagrid paging a lot and we have been experiencing "Index out of bound" error messages when we click on rows of datagrid on differnet pages every now and then. The...
2
by: asad | last post by:
Hello friends, i am designing a ASP.NET page where i want to use custom paging bcoz data is too heavy so pls tell me how can i use custom paging in ASP.NET Thanks
3
by: GD | last post by:
Got a simple ASPX page (.NET v1.1) with a DataGrid displaying a list of widgets. If the page has a QueryString called WidgetId specific, I need to automatically mark the specific DataGridItem as...
2
by: farhad13841384 | last post by:
Hi , I Hope You fine. I have some problem with this code for paging in asp.net this bottom code work correctly without any error but when I try to place separate code in .VB file then error is...
0
by: anonieko | last post by:
This approach I found very efficient and FAST when compared to the rowcount, or Subquery Approaches. This is before the advent of a ranking function from DB such as ROW_NUMBER() in SQL Server...
0
by: Mrozik | last post by:
Hi! I'm looking for a solution to show data in a table. I think about DataView control - but I see one problem. My data source is a WebService returning paged pieces of data (I can't change...
2
by: rn5a | last post by:
In a shopping cart app, a ASPX page retrieves the order details & personal details of a user from a MS-Access database table depending upon the username of the user. The order details of a...
5
by: Donald Adams | last post by:
Hi, I will have both web and win clients and would like to page my data. I could not find out how the datagrid control does it's paging though I did find some sample code that says they do it...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.