Can anyone help?
I query a database and return a result on the column "reference".
There might be 7 listings. Each row is displayed in a table, with
links through to a detail page. I am working on having a "previous"
record and a "next" record link on the detail page.
This code below works, for the "next" record, by searching the values
in the array $myarray for the variable $ref. It then returns the key
value and the key value, as a number, is deducted by one and plus'd by
one to give me the previous row and the next row.
Returning the value of the next row works a treat, but for some reason
the previous row, deduct 1, while it echoes a value to screen, doesn't
work.
I'd appreciate any feedback, any help. It's been a challenge for a
newbie to sort this out.
This is the code:
(query database and then:)
//$ref is a variable carried into the page
$num_rows = mysql_num_rows($result2);
$i=0;
while ($i < $num_rows) {
$next=mysql_result($result2,$i,"reference");
++$i;
$myarray = array("$i"=>"$next");
foreach($myarray as $key=>$value) {
if ($value == "$ref"){
$b=($key);
$c=($key+1);
$a=($key-1);
}}
if ($a == "0") {
echo "No Preceding Record";
} else {
if ($key == "$a") echo "<a
href=../detail/index.php?name=$value>Previous Record</a>";
}
if ($key == "$b") echo "This is the current Record";
if ($num_rows < $c) {
echo "Records End";
} else {
if ($key == "$c") echo "<a href=../detail/index.php?name=$value>Next
Record</a>";
}
}
} else {
echo "Sorry, no records were found";
}
?>