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!
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("localhost", "XXXXXXXX", "XXXXXXXX");
mysql_select_db("dbname",$db);
// Configuration
$max = 4;
$current = $p * $max;
// Select rows
$sql="SELECT * FROM nucleus_item WHERE icat='2' ORDER BY ititle ASC LIMIT $current,$max";
$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["ititle"];
$icat = $row["icat"];
$inumber = $row["inumber"];
$ibody = $row["ibody"];
echo "<div class=\"title\">$ititle</div><div class=\"body\">$ibody</div><div class=\"more\"><a href=\"index.php?itemid=$inumber&catid=$icat\">[read this...]</a></div><br>";
}
?>
<hr>
<?php
// Show BACK button
if ($p != 0) {
$back = $p-1;
echo "<a href=\"reviews-archive.php?p=$back\"><img src=\"back.gif\" border=0></a>";
}
// Show FORWARD button
$current = $current + $max;
$sql = "SELECT * FROM nucleus_item";
$result = @mysql_query($sql,$db);
$num = @mysql_num_rows($result);
if ($current < $num) {
$forward = $p+1;
echo "<a href=\"reviews-archive.php?p=$forward\"><img src=\"next.gif\" border=0></a>";
}
?>