Reputation: 185
Say I have 3 records of same users with IDs 2,4,7,9 and I want to be able to go from one to another by navigation via next/previous links. The problem is, I don't know how to fetch 1 record per page with link redirecting to same page.
So when I have record with ID 4, If I click previous it must redirect me to same page with record of having ID 2 and If I click next it should take me to 7.Please help with redirection and displaying one record per page.
So far I have done:
<?php
$query1 = "SELECT * FROM table2 WHERE id='{$user}' order by srno desc " ;
$result1=mysql_query($query1,$con);
if (!($result1) )
{
die('Error: ' . mysql_error($con));
}
else
{
$values1= mysql_fetch_array($result1);
}
mysql_close($con);
?>
Do I need to change query to :
$query1 = "SELECT * FROM table2 WHERE id='{$user}' order by srno desc LIMIT 1 OFFSET 1" ;
Upvotes: 0
Views: 172
Reputation: 868
I'd use something like this for prev/next
links...
html
<a href="?action=prev&id=<?php echo $values['id'] ?>">prev</a>
<a href="?action=next&id=<?php echo $values['id'] ?>">next</a>
php
<?php
if(!is_numeric($_GET['id'] || $_GET['id'] < 1)
throw new \Exception('Illegal id value');
if(!isset($_GET['action']))
$_GET['action'] = 'view';
switch($_GET['action']){
case 'prev':
$q = "SELECT * FROM table2 WHERE id<'{$_GET['id']}' ORDER BY id DESC LIMIT 1" ;
break;
case 'next':
$q = "SELECT * FROM table2 WHERE id>'{$_GET['id']}' ORDER BY id ASC LIMIT 1" ;
break;
case 'view':
$q = "SELECT * FROM table2 WHERE id='{$_GET['id']}'";
break;
default:
throw new \Exception('Illegal action value');
}
$result1=mysql_query($query1,$con);
if (!($result1) )
{
die('Error: ' . mysql_error($con));
}
else
{
$values1= mysql_fetch_array($result1);
}
mysql_close($con);
I didn't understand what srno
is, and how did you get your user id
in $user
var, so I left them out. Code should be functional nontheless, once you understand it, you can use it to rewrite your own.
Upvotes: 1
Reputation: 672
if you displaying one record per page you may simply use pagination. You can get a pagination example from http://www.tutorialspoint.com/php/mysql_paging_php.htm
Upvotes: 0