Limiting items per page
Moderator: General Moderators
- evilmonkey
- Forum Regular
- Posts: 823
- Joined: Sun Oct 06, 2002 1:24 pm
- Location: Toronto, Canada
Limiting items per page
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.
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
you could try
and make multiple pages or use a switch.
or something like this.
Code: Select all
$result = mysql_query($sql, $mysql) or die(mysql_error());
while ($row = mysql_fetch_array($result)){
if ($rowї"id"] > 0 && $rowї"id"] < 11){
print "$rowї"id"]";
}
}or something like this.
Code: Select all
$temp_sql = "SELECT * FROM tabel WHERE id > 0 && id < 11";
while ($row = mysql_fetch_array($result)){
print "$rowї"id"]";
}search in the Databases forum for the word "LIMIT" and you will find e.g.
http://forums.devnetwork.net/viewtopic.php?t=3864
http://forums.devnetwork.net/viewtopic.php?t=3318
http://forums.devnetwork.net/viewtopic.php?t=2298
http://forums.devnetwork.net/viewtopic.php?t=3864
http://forums.devnetwork.net/viewtopic.php?t=3318
http://forums.devnetwork.net/viewtopic.php?t=2298
- evilmonkey
- Forum Regular
- Posts: 823
- Joined: Sun Oct 06, 2002 1:24 pm
- Location: Toronto, Canada
Ok, I got something going here:
This gives me an error on line where 'while' begins. Can someone tell me why, and suggest a way to fix it? Thanks.
Code: Select all
<script type="text/javascript">
function popup_all(id)
{
window.open("page2.php?id=" + id, "window_name", "0","0","0","0","yes","yes","200","300");
}
</script>
<?php
$db = mysql_connect("localhost","","");
mysql_select_db("",$db);;
$callsign = $HTTP_POST_VARSї'id'];
$records = 10; // Number of records per page
$start = $_GETї"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("<td><a href="javascript:popup_all($row->id)">$row->title</a></td>");
}
// Add number of records per page to starting value to get records for next page
$next = $start + $records;
print("<form method="GET" action="?$next">");
print("<input type="submit" value="Next">");
print("</form>");
?>not very helpfulThis gives me an error on line where 'while' begins.
try
Code: Select all
$result=mysql_query ($sql, $db) or die($sql.' :'.mysql_error());- evilmonkey
- Forum Regular
- Posts: 823
- Joined: Sun Oct 06, 2002 1:24 pm
- Location: Toronto, Canada
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.
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.
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
<HTML>
<body>
<script type="text/javascript">
function popup_all(id)
{
window.open("somewhere.php?id=" + id, "window_name", "0","0","0","0","yes","yes","200","300");
}
</script>
<?php
$db = mysql_connect("localhost","","");
mysql_select_db("",$db);;
$callsign = $HTTP_POST_VARSї'id'];
$records = 10; // Number of records per page
$start = $_GETї"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("<td><a href="javascript:popup_all($row->id)">$row->title</a></td>");
}
// Add number of records per page to starting value to get records for next page
$next = $start + $records;
print("<form method="GET" action="?$next">");
print("<input type="submit" value="Next">");
print("</form>");
?>
</body>
</HTML>withyou can get the amount of recordsets. With a simple for-loop you can print those links.
e.g. (without <a href...>)(completely untested, conditions maybe false)
Code: Select all
SELECT count(*) from tablename WHERE conditione.g. (without <a href...>)
Code: Select all
for ($i=0; $i<$countRecords; $i+=$recordsPerPage)
echo $i+1, '-', ($i+$recordsPerPage < $countRecords) ? $i+$recordsPerPage+1 : $countRecords, '&nbsp;';- evilmonkey
- Forum Regular
- Posts: 823
- Joined: Sun Oct 06, 2002 1:24 pm
- Location: Toronto, Canada
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:
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.
Code: Select all
print("<form method="GET" action="?$next">");Thanks.
try$_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 '<form method="GET" action="', $_SERVERї'PHP_SELF'], '?id=', $theIdVariableForNextPage, '">';You may also add a hidden field to the form.
Code: Select all
...
echo '<form method="GET" action="', $_SERVERї'PHP_SELF'], '">';
echo '<input type="hidden" name="id" value="', $theIdVariableForNextPage, '" />';
....- evilmonkey
- Forum Regular
- Posts: 823
- Joined: Sun Oct 06, 2002 1:24 pm
- Location: Toronto, Canada