Reply
Help with LIMIT...
Old 06-29-2008, 08:35 PM Help with LIMIT...
Experienced Talker

Posts: 48
I wonder if somebody could help me please. I'm displaying results from a database search, but I only want two results per page. I have managed to get it to display on two results, but when I click next or previous the same two results are displayed. To limit the results I've got:
PHP Code:
$sql_order " ORDER BY date DESC";
$start = (int) $_GET['start'];
$sql_limit " LIMIT $start,2"
and to navigate through the results I've added:
PHP Code:
<a href='?start=<? $start ?>'>previous</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href='?start=<? $start ?>'>next</a>
to the html of the page. When it goes to the next page the url is the same ,but has ?start= at the end of it
thehappyappy is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
     
Old 06-30-2008, 08:34 AM Re: Help with LIMIT...
stoot98's Avatar
Ultra Talker

Posts: 340
Name: Stuart
Location: Glasgow, Scotland
The $start variable is never changing. You will need to manipulate it to show the next results. Off the top of my head for the "next" link you need to have $start+2 and include a second variable for the end limit "$end+2". This is assuming that the very frst page of results $start=0 and $end=2.

You might want to google pagination as there are lots of resources out there.

Hope this helps some
Stoot
stoot98 is offline
Reply With Quote
View Public Profile
 
Old 06-30-2008, 08:50 AM Re: Help with LIMIT...
Experienced Talker

Posts: 48
I changed the code to
PHP Code:
$result mysql_query ($sql_select $sql_from $sql_where $sql_order $sql_limit);
$results implode(mysql_fetch_assoc(mysql_query ("SELECT COUNT(*) " $sql_from $sql_where $sql_order))); 
$start = isset($_GET['start']) ? (int) $_GET['start'] : 0;
$pages floor($results 5);
echo 
"Page ";
for (
$i 0$i $pages$i++ ) {
    if (
$i != $start) echo "<a href=\?start= . (5 * $i) . \>";
    echo  
$i 1;
    if (
$i != $start"</a> ";
}
echo 
"<br />";
if (
$start != 0) echo "<a href=\?start= . ($start - 5) . \>Previous</a> ";
if (
$start != $pages 5) echo "<a href=\?start= . ($start + 5) . \>Next</a>";

if (!
$result ) { 
    echo(
"<p>Doh! Error performing query: " mysql_error($result) . "</p>"); 
    exit(); 

 
if (
$row mysql_fetch_array($result)) { 

do { 
print 
"<div style='width: 659px; padding: 5px 20px; height: 241px; background-image: url(images/search_results_box.jpg); background-repeat: no-repeat;'>";
print (
"<p>"); 
but when I went to the next page the last part of the address was
Quote:
%5C?start=
thehappyappy is offline
Reply With Quote
View Public Profile
 
Old 06-30-2008, 03:18 PM Re: Help with LIMIT...
stoot98's Avatar
Ultra Talker

Posts: 340
Name: Stuart
Location: Glasgow, Scotland
Try changing the relevant bits with those below:

PHP Code:

$start 
= isset($_GET['start']) ? (int) $_GET['start'] : 0;
$pages floor($results 5);

echo 
"Page ";
for (
$i 0$i $pages$i++ ) {
    if (
$i != $start) echo "<a href=\"?start=" . ($i) . "\">";
    echo  
$i 1;
    if (
$i != $start"</a> ";
}
echo 
"<br />";
if (
$start != 0) echo "<a href=\"?start=" . ($start 5) . "\">Previous</a> ";
if (
$start != $pages 5) echo "<a href=\"?start=" . ($start 5) . "\">Next</a>"

Last edited by stoot98 : 06-30-2008 at 03:19 PM. Reason: Code block
stoot98 is offline
Reply With Quote
View Public Profile
 
Old 06-30-2008, 03:44 PM Re: Help with LIMIT...
Experienced Talker

Posts: 48
Great thanks. That works.
thehappyappy is offline
Reply With Quote
View Public Profile
 
Old 06-30-2008, 04:10 PM Re: Help with LIMIT...
stoot98's Avatar
Ultra Talker

Posts: 340
Name: Stuart
Location: Glasgow, Scotland
glad to help
stoot98 is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Help with LIMIT...
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.13740 seconds with 13 queries