note 76536 deleted from function.mysql-data-seek by nlopess
| From: | nlopess@php.net | Date: | Thu, 19 Jul 2007 18:25:45 +0000 |
| Subject: | note 76536 deleted from function.mysql-data-seek by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-128736@lists.php.net to get a copy of this message | ||
Note Submitter: fili at fili dot nl
----
I needed to get the next and previous row using a specified row as an offset. This sounds easier
then it seems.
Finally i came up with the below, some-else might know of an easier way which i overlooked.
<?php
// retreive current pointer
$result = mysql_query("SELECT * FROM
database.table WHERE
something = '1' ORDER BY timestamp DESC");
for ($pointer=0; $pointer <= mysql_num_rows($result); $pointer++) {
$row = mysql_fetch_array($result);
if ($row['id'] == $id) break;
}
// calculate next pointer
$next_pointer = ($pointer == mysql_num_rows($result)-1 ? 0 : $pointer+1);
mysql_data_seek($result, $next_pointer);
$next_row = mysql_fetch_array($result);
// calculate previous pointer
$prev_pointer = ($pointer == 0 ? mysql_num_rows($result)-1 : $pointer-1);
mysql_data_seek($result, $prev_pointer);
$prev_row = mysql_fetch_array($result);
?>
First it seeks out the current result pointer of the row which will be used as an offset.
For this you need at least one certainty to identify the row in question (as an example i used $id).
Next it adds or subtracts one from the pointer.
If the new pointer is out of bounds it respectively returns the first/last row instead (which is
what i personally needed).