Limiting items per page

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Limiting items per page

Post by evilmonkey »

Hello.

I have a db (mysql of course) with id, title and other (unimportant) fields. I want to create a query that will only show for example titles with id 1-10. Then when we get number 11 to create a new page that will say 11-20. I want this to look like any search engine, with the possibility to skip ahead, or click next. How would I go about doing that? Thanks.
Silver_Eclipse
Forum Commoner
Posts: 61
Joined: Sun Aug 18, 2002 7:26 pm

Post by Silver_Eclipse »

you could try

Code: Select all

$result = mysql_query($sql, $mysql) or die(mysql_error());
while ($row = mysql_fetch_array($result)){
if ($row&#1111;"id"] > 0 && $row&#1111;"id"] < 11)&#123;
print "$row&#1111;"id"]";
&#125;
&#125;
and make multiple pages or use a switch.
or something like this.

Code: Select all

$temp_sql = "SELECT * FROM tabel WHERE id > 0 && id < 11";
while ($row = mysql_fetch_array($result))&#123;
print "$row&#1111;"id"]";
&#125;
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

Ok, I got something going here:

Code: Select all

&lt;script type="text/javascript"&gt; 
function popup_all(id) 
{ 
window.open("page2.php?id=" + id, "window_name", "0","0","0","0","yes","yes","200","300"); 
} 
&lt;/script&gt;
&lt;?php 
$db = mysql_connect("localhost","","");
mysql_select_db("",$db);;
$callsign = $HTTP_POST_VARS&#1111;'id']; 
$records = 10; // Number of records per page 
$start = $_GET&#1111;"id"]; // Get "id" value from address line  
if ($start == NULL) // No address line variable 
{ 
$start = 0; // Default value 
} 
$sql = "SELECT id, title FROM table WHERE category=something LIMIT " .$start. ", " .$records; 
$result=mysql_query ($sql, $db);
while ($row = mysql_fetch_object ($result)) 
{
 print("&lt;td&gt;&lt;a href="javascript:popup_all($row-&gt;id)"&gt;$row-&gt;title&lt;/a&gt;&lt;/td&gt;"); 
} 

  // Add number of records per page to starting value to get records for next page 
  $next = $start + $records; 

  print("&lt;form method="GET" action="?$next"&gt;"); 
  print("&lt;input type="submit" value="Next"&gt;"); 
  print("&lt;/form&gt;"); 
?&gt;
This gives me an error on line where 'while' begins. Can someone tell me why, and suggest a way to fix it? Thanks.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

This gives me an error on line where 'while' begins.
not very helpful ;)
try

Code: Select all

$result=mysql_query ($sql, $db) or die($sql.' :'.mysql_error());
and take a look into the (web-)server(-software)'s error.log
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

Thanks Volka. Tht shined the light on my problem. Quite a stupid mistake actually, I made a mistake with the quotes. Thanks.

I have another question though. All this script does is it gives me the 'next' button. Is there a way that I can make links to the beginning number? For example, if my database id filed goes up to 30, can I have three links: 1-10, 11-20, and 21-30? If so, then how?

Thanks. This is my new code, feel free to suggest any alterations.

Code: Select all

&lt;HTML&gt;
&lt;body&gt;
&lt;script type="text/javascript"&gt; 
function popup_all(id) 
{ 
window.open("somewhere.php?id=" + id, "window_name", "0","0","0","0","yes","yes","200","300"); 
} 
&lt;/script&gt;

&lt;?php 
$db = mysql_connect("localhost","","");
mysql_select_db("",$db);;
$callsign = $HTTP_POST_VARS&#1111;'id']; 
$records = 10; // Number of records per page 
$start = $_GET&#1111;"id"]; // Get "id" value from address line  
if ($start == NULL) // No address line variable 
{ 
$start = 0; // Default value 
} 
$sql = "SELECT id, title FROM userjokes WHERE category='animal' LIMIT " .$start. ", " .$records; 
$result=mysql_query ($sql, $db) or die($sql.' :'.mysql_error());
while ($row = mysql_fetch_object ($result)) 
{
 print("&lt;td&gt;&lt;a href="javascript:popup_all($row-&gt;id)"&gt;$row-&gt;title&lt;/a&gt;&lt;/td&gt;"); 
} 

  // Add number of records per page to starting value to get records for next page 
  $next = $start + $records; 

  print("&lt;form method="GET" action="?$next"&gt;"); 
  print("&lt;input type="submit" value="Next"&gt;"); 
  print("&lt;/form&gt;"); 
?&gt;
&lt;/body&gt;
&lt;/HTML&gt;
Another problem, ?$next doesn't work. In my browser, once I press the button, all it does is it changes the address by adding a question mark (?) at the end. Why? Thanks.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

with

Code: Select all

SELECT count(*) from tablename WHERE condition
you can get the amount of recordsets. With a simple for-loop you can print those links.
e.g. (without <a href...>)

Code: Select all

for ($i=0; $i&lt;$countRecords; $i+=$recordsPerPage)
	echo $i+1, '-', ($i+$recordsPerPage &lt; $countRecords) ? $i+$recordsPerPage+1 : $countRecords, '&amp;nbsp;';
(completely untested, conditions maybe false)
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

Hello Volka. I don't really get the above, but I know where my problem with the button is. Something is wrong with $next. Is:

Code: Select all

print("&lt;form method="GET" action="?$next"&gt;");
the proper syntax? Everything else seems to be fine. (If I put it in the address bar manually) When I press the button, all I see on the address bar is it adds "?" at the end of the address. I need it to add "?id=whatever". ID is an integer. Can someone tell me how to fix it?

Thanks.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

try

Code: Select all

echo '&lt;form method="GET" action="', $_SERVER&#1111;'PHP_SELF'], '?id=', $theIdVariableForNextPage, '"&gt;';
$_SERVER['PHP_SELF'] contains the uri (without protocol or server) to the current script.
You may also add a hidden field to the form.

Code: Select all

...
echo '&lt;form method="GET" action="', $_SERVER&#1111;'PHP_SELF'], '"&gt;';
echo '&lt;input type="hidden" name="id" value="', $theIdVariableForNextPage, '" /&gt;';
....
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

Thanks Volka!

The hidden script worked.

Cheers!
Post Reply