Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

BACK/ NEXT buttons, pls help... (newbie!) 1

Status
Not open for further replies.

Circuit

Programmer
May 19, 2002
3
BE
I have cobbled together a script for displaying Next/ Back links in search results, but it's not doing exactly what I want it to do. This is really really hard to explain but I'll try.

I want to display 4 "reviews" per page. Each review is made up of 4 database results - title, category, ID number and body. My script is currently displaying four reviews (4 database results each) per page.

However, it's also showing a Next button when there are no more entries. Presumably because this script is designed to assume that 1 "result" = 1 database hit, not 1 "result" = 4 database hits.

Am I making sense?

Here's my script - can anyone suggest a fix? Please be descriptive (i.e. "type this, delete this") as I don't have the necessary knowledge to modify code myself.

TIA!


Code:
<?php 

// Default value for $p 
if (!isset($p)) { 
$p = 0; 
} 

// Connect to database 
$db = mysql_connect(&quot;localhost&quot;, &quot;XXXXXXXX&quot;, &quot;XXXXXXXX&quot;); 
mysql_select_db(&quot;dbname&quot;,$db); 

// Configuration 
$max = 4; 
$current = $p * $max; 

// Select rows 
$sql=&quot;SELECT * FROM nucleus_item WHERE icat='2' ORDER BY ititle ASC LIMIT $current,$max&quot;; 
$result = @mysql_query($sql,$db); 
$num = @mysql_num_rows($result); 

// Display results 

for ($cur=0; $num > $cur; $cur++) { 

$row = mysql_fetch_array($result); 

$ititle = $row[&quot;ititle&quot;]; 
$icat = $row[&quot;icat&quot;]; 
$inumber = $row[&quot;inumber&quot;]; 
$ibody = $row[&quot;ibody&quot;]; 

echo &quot;<div class=\&quot;title\&quot;>$ititle</div><div class=\&quot;body\&quot;>$ibody</div><div class=\&quot;more\&quot;><a href=\&quot;index.php?itemid=$inumber&catid=$icat\&quot;>[read this...]</a></div><br>&quot;; 

} 
?> 
<hr> 
<?php 


// Show BACK button 
if ($p != 0) { 
$back = $p-1; 
echo &quot;<a href=\&quot;reviews-archive.php?p=$back\&quot;><img src=\&quot;back.gif\&quot; border=0></a>&quot;; 
} 

// Show FORWARD button 
$current = $current + $max; 
$sql = &quot;SELECT * FROM nucleus_item&quot;; 
$result = @mysql_query($sql,$db); 
$num = @mysql_num_rows($result); 

if ($current < $num) { 
$forward = $p+1; 
echo &quot;<a href=\&quot;reviews-archive.php?p=$forward\&quot;><img src=\&quot;next.gif\&quot; border=0></a>&quot;; 
} 

?>
 
This lot seems very longwinded.

// Show FORWARD button
$current = $current + $max;
$sql = &quot;SELECT * FROM nucleus_item&quot;;
$result = @mysql_query($sql,$db);
$num = @mysql_num_rows($result);

if ($current < $num) {
$forward = $p+1;
echo &quot;<a href=\&quot;reviews-archive.php?p=$forward\&quot;><img src=\&quot;next.gif\&quot; border=0></a>&quot;;
}


You have already set $num at the initial data select so you dont need to do it again.

if ($current < $num ok here it goes wrong, say you have 200 results from the database, and this is 4 per page 200/4 = 50
At page 50 you say if ($current(50) < $num(200) and what you really need to do is:

if ($current < ($num / 4)){

// button etc

***************************************
Party on, dudes!
[cannon]
 
KarveR, thanks for your helpful response. I managed to work out it was something to do with division of replies but couldn't figure out where I should do the dividing!

The code is now modified as follows. It brings up 8 full results (i.e. 8 groups of 4 in reality) on a page.

Unfortunately it also seems to bring up a 'Next' button if there are exactly 8 groups of 4 returned (so clicking 'Next' brings up a blank page with a Back button). I haven't tested it properly yet, so I've yet to see if the problem recurs if there are 14, 15 or 16 entries.

It's definitely better than before, and I can live with it, but I'm sure there's something else I could do to stop this happening.



Code:
// Configuration
$max = 8;
$current = $p * $max;

// Select rows
$sql=&quot;SELECT * FROM nucleus_item WHERE icat='1' ORDER BY ititle ASC LIMIT $current,$max&quot;;
$result = @mysql_query($sql,$db);
$num    = @mysql_num_rows($result);

// Display results

for ($cur=0; $num > $cur; $cur++) {

    $row = mysql_fetch_array($result);

$ititle = $row[&quot;ititle&quot;];
$icat = $row[&quot;icat&quot;];
$inumber = $row[&quot;inumber&quot;];
$ibody = $row[&quot;ibody&quot;];

echo &quot;<div class=\&quot;title\&quot;>$ititle</div><div class=\&quot;body\&quot;>$ibody</div><div class=\&quot;more\&quot;><a href=\&quot;index.php?itemid=$inumber&catid=$icat\&quot;>[read this...]</a></div><br>&quot;;

}
?>
<hr>
<?php


// Show BACK button
if ($p != 0) {
    $back = $p-1;
    echo &quot;<a href=\&quot;reviews-archive.php?p=$back\&quot;><img src=\&quot;back.gif\&quot; border=0></a>&quot;;
}

// Show FORWARD button

if ($current < ($num /4)) {
    $forward = $p+1;
    echo &quot;<a href=\&quot;reviews-archive.php?p=$forward\&quot;><img src=\&quot;next.gif\&quot; border=0></a>&quot;;
}

?>
 
Yes it won't always be perfect e.g if you have 201 records /4 = 50.25 and if you are on page 50 then 50 is less than 50.25.
You'll need to work out how to round the number up so that when you get to page 51 there is no way forward.

I'm not sure on how to do it , search the function manual at php.net. ***************************************
Party on, dudes!
[cannon]
 
*grins* in swoops the newb :)
easy..

$no_pages = $num / 4;
$no_pages = settype($no_pages, double);
$no_pages = explode(&quot;.&quot;, $nopages)
if($no_pages[1] > 0)
{
$no_pages[0]++;
}

if ($current < $no_pages[0]) {
//snip
}
ok, so we devide num by 4, giving us the number oof pages we need. then we make sure this is a double (so it has a decimal point for us to explode) then we split it, giving us the number after the decimal on its own. we test that to see if its more than 0, if it is (1-3 results on final page) we add one to the number of pages, if not, then we carry on with the whole number we had anyway.
so if we had say 6 results, 6/4 = 1.5
split at the . the second half is 5, which is greater than 0, so add one to the number of pages we need (currently 1) 1 + 1 = 2, giving us 2 pages. when we goto page two, current is 2, and so is no_pages[0] meaning its not less than 2, and we get no next button.
Maybe long winded.. maybe slightlyu extreamist, but it works :)
Cheers.



/Sib
programmer in the making
icq: 44167565
e-mail: siberdude@ntlworld.com
 
<a href=&quot;javascript: history.go(1)&quot;>Forward</a> <a href=&quot;javascript: history.go(-1)&quot;>Back</a> --BB
 
what kind of solution are you giving?

go back and forward ising browser? for that you have the toolbar buttons.

ok.

i will help you out giving tyou the code you need.

$query=&quot;SELECT count(*) from table&quot;;
list($total)=mysql_fetch_row(mysql_query($query));
$rowspage=50;
$currentpage=$HTTP_GET_VARS[page]?$HTTP_GET_VARS[page]:1;
$min=$rowspage*($currentpage-1);
$max=$min+$rowspage-1;
$totpages=($total % $rowspage == 0) ? ($total / $rowspage) : (floor($total / $rowspage)+1);
$query=&quot;SELECT * from table LIMIT $min,$max&quot;;


this way you filter by page instead of records.
page 1 -> [0,49]
page 2 -> [50,99]
and so on.

Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top