note 76536 added to function.mysql-data-seek

From: Date: Thu, 19 Jul 2007 15:40:44 +0000
Subject: note 76536 added to function.mysql-data-seek
Groups: php.notes php.notes 
Request: Send a blank email to php-notes+get-128732@lists.php.net to get a copy of this message
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). ---- Server IP: 194.109.193.76 Probable Submitter: 86.94.171.185 ---- Manual Page -- http://www.php.net/manual/en/function.mysql-data-seek.php Edit -- https://master.php.net/note/edit/76536 Del: integrated -- https://master.php.net/note/delete/76536/integrated Del: useless -- https://master.php.net/note/delete/76536/useless Del: bad code -- https://master.php.net/note/delete/76536/bad+code Del: spam -- https://master.php.net/note/delete/76536/spam Del: non-english -- https://master.php.net/note/delete/76536/non-english Del: in docs -- https://master.php.net/note/delete/76536/in+docs Del: other reasons-- https://master.php.net/note/delete/76536 Reject -- https://master.php.net/note/reject/76536 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#128732) next »