Forum Moderators: coopster

Message Too Old, No Replies

1 Record per page

         

bodycount

11:59 am on Nov 1, 2005 (gmt 0)

10+ Year Member



I want to display one record per page ( which I can do ) but what i want to do next is. is to view another record on a new page by clicking a next button. How do i do this? I am using ID so do I have to do somthing like $ID+1 to goto the next record, and $ID-1 to go back?

Twisted Mind

12:59 pm on Nov 1, 2005 (gmt 0)

10+ Year Member



exactly :)

if u make id a variable with post u can make something like $back = $id-1
and with some scripting you can let it send to the one before...

bodycount

4:16 pm on Nov 1, 2005 (gmt 0)

10+ Year Member



Thanks for the quick reply. I am a noobie at that this PHP lark. Is there any example code I can look at?

kamakaze

4:32 pm on Nov 1, 2005 (gmt 0)

10+ Year Member



Something like this should work for you.

index.php?id=1&move=-1 to go back one record

index.php?id=1&move=+1 to go forward one record

Example code:

<?php
mysql_connect("localhost", "username", "password");
mysql_select_db("database");
$query = "SELECT * FROM table WHERE id = $id " . $move;
$result = mysql_query($query);
$num=mysql_numrows($result);
$i=0;
while ($i < $num) {

$record=mysql_result($result,$i,"name");

echo $name;

$i++;
}

?>

bodycount

9:31 am on Nov 2, 2005 (gmt 0)

10+ Year Member



I tried that code and I get the following error.

Warning: mysql_numrows(): supplied argument is not a valid MySQL result resource in "" on line 15

<?php
include ("connection.php");

$query = "SELECT * FROM `ADDRESSES` WHERE ID = $ID " . $move;
$result = mysql_query($query);
$num=mysql_numrows($result);
$i=0;
while ($i < $num) {

$record=mysql_result($result,$i,"FULLNAME");

echo $FULLNAME;

$i++;
}
echo "<a href=nextrecord.php?id=1&move=-1>Go Back</a>";
echo "<a href=nextrecord.php?id=1&move=+1>Next record</a>";

?>

mcibor

12:43 pm on Nov 2, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I prefer other way of doing the pagination. However here I won't post the whole code. I can send it to you via sticky.

<?php
mysql_connect("localhost", "username", "password");
mysql_select_db("database");
if(isset($_GET['id']) && (int)$_GET['id']) $id = $_GET['id'];
else $id = 1;

$query = "SELECT * FROM table WHERE id = '$id' LIMIT 1";
$result = mysql_query($query);
if($num=mysql_numrows($result))
{
$record = mysql_fetch_assoc($result);
$name = $record["name"];

echo $name;
}

$next = $id + 1;
$prev = $id - 1;
?>
<a href="index.php?id=<? echo $prev;?>">Prev</a><br>
<a href="index.php?id=<? echo $next;?>">Next</a><br>

Hope this helps!
Michal Cibor

Kamakaze, you are using the register_globals on, never do that, because it is very risky and unsecure!

bodycount

1:20 pm on Nov 2, 2005 (gmt 0)

10+ Year Member



Thanks Michal Cibor that works great.

kamakaze

2:28 pm on Nov 2, 2005 (gmt 0)

10+ Year Member



bodycount,

You are getting that error because in your Go Back and Next Record links you are refering to lowercase id and in your sql query you are refering to uppercase id.

mysql_numrows is working on the basis of your query and when you click your current links $ID is null and there are no results returned.

kamakaze

2:32 pm on Nov 2, 2005 (gmt 0)

10+ Year Member



Kamakaze, you are using the register_globals on, never do that, because it is very risky and unsecure!

========================

I am aware of what I am doing. I was just giving him an example on how to accomplish what he is asking. How he takes that and applies it is up to him. Thanks for the comments though.