Menu
Hello again,
I want to make a next and a previous link with my data.
here is my query:
[code=php]
<?php $sql = “SELECT DISTINCT pdp.PackageId, p.PackageType
FROM productpackaging pdp, packaging p
WHERE pdp.ProductId =2
AND pdp.PackageId = p.PackageId”;
$result = mysql_query($sql);
How do I write the code to get next or get previous??
Do I need to put the records in an array?
[code=php]function get_packages()
{
// assuming connection exists
$sql = mysql_query("SELECT packageID, packageName FROM packages ORDER BY packageID");
while($row = mysql_fetch_assoc($sql))
{
$rows[] = $row;
}
return $rows;
}
$currentPackage = $_GET['packageID'];
// don't forget to validate the packageID
$packages = get_packages();
$prevLink = false;
$nextLink = false;
for($i = 0; $i < count($packages); $i++)
{
if($packages[$i]['packageID'] == $currentPackage)
{
// check for previous link
if($i != 0)
{
$prevLink = ($i - 1);
}
// check for next link
if($i != (count($packages) - 1))
{
$nextLink = ($i + 1);
}
// done, stop searching
break;
}
}
// previous link
if($prevLink)
{
echo "<a href="/store/?packageID={$packages[$prevLink]['packageID']}">{$packages[$prevLink]['packageName']}</a>";
}
// nextlink
if($nextLink)
{
echo "<a href="/store/?packageID={$packages[$nextLink]['packageID']}">{$packages[$nextLink]['packageName']}</a>";
}
[/code]
[code=php]switch($_GET['type'])
{
case 'Previous':
$sql = "IFNULL(MAX(IF($priKey<'$id',$priKey,null)),MIN($priKey))";
break;
case 'Next':
$sql = "IFNULL(MIN(IF($priKey>'$id',$priKey,null)),MAX($priKey))";
break;
case 'Last':
$sql = "MAX($priKey)";
break;
default:
$sql = "MIN($priKey)";
}
$result = mysql_query("
SELECT *
FROM $tablename
WHERE $priKey = (SELECT $sql FROM $tablename)
LIMIT 1
");[/code]
This works with DATETIME columns also. Here's [code=php]<?php
function get_packages($pid)
{
$ProductId = $pid;
$sql = mysql_query("SELECT DISTINCT pdp.PackageId, PackageType FROM productpackaging pdp, packaging p WHERE pdp.ProductId = $ProductId AND pdp.PackageId = p.PackageId");
while($row = mysql_fetch_assoc($sql))
{
$rows[] = $row;
}
return $rows;
}
$currentPackage = $_GET['PackageID'];
// don't forget to validate the packageID
$packages = get_packages($ProductId);
$prevLink = false;
$clink = false;
$nextLink = false;
for($i = 0; $i < count($packages); $i++)
{
if($packages[$i]['PackageID'] == $currentPackage)
{
// check for previous link
if($i != 0)
{
$prevLink = ($i - 1);
$clink = ($i);
}
// check for next link
if($i != (count($packages) - 1))
{
$nextLink = ($i + 1);
$clink = ($i);
}
// done, stop searching
break;
}
}
mysql_free_result($result);
?>
<td><?php // previous link
if($prevLink)
{
echo '<button onclick="" ><<</button>';
}
// nextlink
if($nextLink)
{
echo '<button onclick="" >>></button>';
} ?></td>
</tr><?php
?>
<tr>
<td><div align="center" style="font-size:smaller;"><?php echo $packages[$clink]['PackageType']; ?></div></td>
[/code]
Where would you set the $id in this example?[/QUOTE]It would be based on the current ID.
[code=php]<?php switch($_GET['type']) [/code]
does 'type' refer to previous, next etc or to the package type? I assume the latter.does 'type' refer to previous, next etc or to the package type? I assume the latter.[/QUOTE]No, the former. Here's the example script so you can see how it all works in context.
[code=php]<?php
mysql_connect(/*******/);
mysql_select_db(/*******/);
$tablename = 'images';
$priKey = /*******/;
$ImagePath = /*******/;
$id = preg_replace('/D/', '', @$_GET['referer']);
switch(@$_GET['type'])
{
case 'Previous':
$sql = "IFNULL(MAX(IF($priKey<'$id',$priKey,null)),MIN($priKey))";
break;
case 'Next':
$sql = "IFNULL(MIN(IF($priKey>'$id',$priKey,null)),MAX($priKey))";
break;
case 'Last':
$sql = "MAX($priKey)";
break;
default:
$sql = "MIN($priKey)";
}
$result = mysql_query("
SELECT *
FROM $tablename
WHERE $priKey = (SELECT $sql FROM $tablename)
LIMIT 1
");
if(mysql_num_rows($result))
{
$row = mysql_fetch_assoc($result);
$id = urlencode($row[$priKey]);
$content = "<p>n <img src='$ImagePath{$row['src']}' width='{$row['width']}' height='{$row['height']}' alt='Image: {$row['id']}'>n </p>n";
}
else
{
$content = "<p class='advise'>Sorry, the image table is empty!</p>n";
}
$links = ' <p class="pagination-links">' ."n ".
links('First' )."n |n ".
links('Previous',$id)."n |n ".
links('Next', $id)."n |n ".
links('Last' )."n </p>n ";
header('Content-Type: text/html; charset=ISO-8859-1');
ob_start('ob_gzhandler');
?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<style type="text/css">
p.pagination-links{clear:both;font-weight:bold;}
p.pagination-links a{color:#369;}
p.pagination-links a:hover{color:#f63;}
p.advise{color:#DD002B;font-weight:bold;}
p{margin:1em auto;text-align:center;}
</style>
<title>First, Previous, Next, Last</title>
</head>
<body>
<?php echo $links.$content ?>
</body>
</html><?php
function links($get, $referer = null)
{
$referer and $referer='&referer='.$referer;
return '<a href="'.htmlentities($_SERVER['PHP_SELF']).'?type='.$get.$referer.'">'.$get.'</a>';
}
?>[/code]
0.1.9 — BETA 5.27