display next and previous records in a form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jawahar
Forum Newbie
Posts: 2
Joined: Fri Jun 15, 2012 2:27 am

display next and previous records in a form

Post by jawahar »

I have a simple form to display the records of students.

The 'student.php' page lists all the students and 'details.php' show the details of the selected student from the 'student.php' page in a form.

Now I want to move to NEXT/PREVIOUS record of a student when I click the buttons, without going back to 'student.php' page.

Below is the code for the above said pages......

This is 'student.php'

Code: Select all

<?php
$con	=	mysql_connect("localhost", "root", "");
$db		=	mysql_select_db("sample", $con);

$sql	=	mysql_query("select * from student order by 'id'");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<table width="500" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td><strong>ID</strong></td>
    <td><strong>Name</strong></td>
    <td><strong>Class</strong></td>
    <td><strong>Marks</strong></td>
  </tr>
<?php
while($row = mysql_fetch_array($sql)) {
 $id = $row['id'];
 $name = $row['name'];
 $class = $row['class'];
 $mark = $row['mark'];
	echo "<tr>
			<td>$id</td>
			<td><a href='details.php?id=$id'>$name</a></td>
			<td>$class</td>
			<td>$mark</td>
		  </tr>";
}
?>

</table>
</body>
</html>
The code for 'details.php'

Code: Select all

<?php
$id = $_GET['id'];

$con	=	mysql_connect("localhost", "root", "");
$db		=	mysql_select_db("sample", $con);

$sql	=	mysql_query("select * from student where id='$id'");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<form method="post">
<table width="500" border="0" align="center" cellpadding="0" cellspacing="0">
  
<?php
while($row = mysql_fetch_array($sql)) {
// $id = $row['id'];
 $name = $row['name'];
 $class = $row['class'];
 $mark = $row['mark'];
	echo "<tr>
			<td>Name</td>
			<td><input type='text' name='name' value='$name'></td>
		  </tr>";
		  
	echo "<tr>
			<td>Class</td>
			<td><input type='text' name='class' value='$class'></td>
		  </tr>";
		  
	echo "<tr>
			<td>Mark</td>
			<td><input type='text' name='mark' value='$mark'></td>
		  </tr>";
}
?>
	<tr>
    	<td colspan="2"><input type="submit" name="first" value="First" />
        <input type="submit" name="previous" value="Previous" />
        <input type="submit" name="next" value="Next" />
        <input type="submit" name="last" value="Last" /></td>
    </tr>
</table>
</form>
</body>
</html>

Thanks in advance for your help
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: display next and previous records in a form

Post by Celauran »

Code: Select all

<a href="details.php?id=<?php echo $id - 1; ?>">Previous</a>
Post Reply