Better way to do it without using IDs?
Posted: Fri Nov 04, 2005 5:59 pm
Right now I'm using ids to calucate the offsets (yes they works fine.. but a bit buggy... read on to see why), but I don't want to use ids for one reason... whenever I delete a file from the database, the IDs aren't right so therefore it get messed up. So I don't want to use IDs (autoin.), so I'd like to ask you to see if you have a better way to do that? How do you select a selected row and get row number from it so I can calucate LIMIT offset for prev, next and of course first and last pages.
Simple script sample(s) would be helpful too. Thanks.
Simple script sample(s) would be helpful too. Thanks.
Code: Select all
// Calucate offsets for prev and next links
$total_results = mysql_result(mysql_query("SELECT COUNT(*) FROM $table"),0);
$prev = $row['id']-1;
$next = $row['id']+1;
$sqloffsetp = "SELECT * FROM $table WHERE id='$prev'";
$sqloffsetn = "SELECT * FROM $table WHERE id='$next'";
$sqloffsetf = "SELECT * FROM $table ORDER BY date LIMIT 1";
$sqloffsetl = "SELECT * FROM $table ORDER BY date DESC LIMIT 1";
$resoffsetp = mysql_query($sqloffsetp);
$resoffsetn = mysql_query($sqloffsetn);
$resoffsetf = mysql_query($sqloffsetf);
$resoffsetl = mysql_query($sqloffsetl);
$testp = mysql_fetch_assoc($resoffsetp); //prev
$testn = mysql_fetch_assoc($resoffsetn); //next
$testf = mysql_fetch_assoc($resoffsetf); //first
$testl = mysql_fetch_assoc($resoffsetl); //last
//MYSQL ENDS
nextprev();Code: Select all
function nextprev() // <<PREV 10 out of 20 images NEXT>>
{
global $row, $table, $testp, $testf, $testn, $testl, $total_results;
echo '<div align="center">';
if ($row['id'] != 1) {
echo "<a href='" . $_SERVER['PHP_SELF'] ."?pix=". $testf['name'] ."'><FIRST </a> ";
}
if (isset($testp['id'])) {
echo "<a href='" . $_SERVER['PHP_SELF'] ."?pix=". $testp['name'] ."'><<PREV</a> ";
}
echo $row['id'] .' out of '. $total_results .' images ';
if (isset($testn['id'])) {
echo "<a href='". $_SERVER['PHP_SELF'] ."?pix=". $testn['name'] ."'>NEXT>></a>";
}
if ($row['id'] != $total_results) {
echo "<a href='". $_SERVER['PHP_SELF'] ."?pix=". $testl['name'] ."'> LAST></a>";
}
echo '</div><br>';
}