Saturday, August 11, 2012

Move to Next and Previous records PHP MySQL



Move to Next and Previous records PHP MySQL

Viewing single record and move from one record to another could be done in MySQL by using PHP.Bellow table I have taken from person table in my_database, which you have created in previous tutorial.anyway creating the table also,I have included codes bellow.

CREATE TABLE IF NOT EXISTS `person` (
  `id` int(45) NOT NULL AUTO_INCREMENT,
  `FirstName` varchar(15) DEFAULT NULL,
  `LastName` varchar(15) DEFAULT NULL,
  `birth_day` date DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;

--
-- Dumping data for table `person`
--

INSERT INTO `person` (`id`, `FirstName`, `LastName`, `birth_day`) VALUES
(1, 'Scarl', 'Johan', '1949-06-04'),
(2, 'Clooneer', 'Henderson', '1974-06-04'),
(3, 'Katena ', 'Daviddson', '1950-06-04'),
(4, 'Mark', 'Jones', '1955-06-05'),
(5, 'Silvi', 'James', '1970-06-05'),
(6, 'Jerry', 'Harfer', '1987-04-08'),
(7, 'Mark', 'Header', '1955-03-08'),
(8, 'Kim', 'Darrel', '1990-06-05'),
(9, 'Tiny', 'Dare', '1951-06-05');

Displaying data file looks like as in bellow

Person details

Clooneer
Henderson
1974-06-04
Following codes move records as previous and next

<h2><center>Person details</center></h2>
<?php
mysql_connect("localhost", "root", "");
mysql_select_db("my_database");
if(isset($_GET['id']) && (int)$_GET['id']) $id = $_GET['id'];
else $id = 1;

$query = "SELECT*FROM person WHERE id = '$id' LIMIT 1";
$result = mysql_query($query);
if($num=mysql_numrows($result))
{
$record = mysql_fetch_assoc($result);
$fname = $record["FirstName"];
$lname = $record["LastName"];
$birth_day =$record["birth_day"];

echo "<table bgcolor=#7c98d5 align='center' width=200><tr bgcolor=#e2e6ef><td>".$fname."</td></tr>";
echo "<tr bgcolor=#e2e6ef><td>".$lname."</td></tr>";
echo "<tr bgcolor=#e2e6ef><td>".$birth_day."</td></tr></table>";
echo $max;
}

$next = $id + 1;
$prev = $id - 1;
?>
<table align="center"><tr><td><a href="nrecord.php?id=<?php echo $prev;?>"><input type="button" value="<<"></a></td>
<td><a href="nrecord.php?id=<?php echo $next;?>"><input type="button" value=">>"></a></td>
</tr></table>

No comments:

Post a Comment