0

Hi guys, this is a brainteaser for me and I'm wondering if you could help. Lets say I'm looping through these results from MySQL, and the result set is as follows:

ID------Name
4------ Anthony
76------John
31------Andrew
98------Sonia

If I was on the 2nd row (with an id of 76, name John), and I wanted for some to include a sentence saying "The next ID will be: {ID}", how would I go about doing that?

So the question really is, how to you jump a row, get the id, then go back to the row you were on?

Thanks!


Anthony

3
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by pritaeas
0
<?php
$con = mysql_connect("localhost","root");
mysql_select_db("test");
$q = "select * from table1";
$result = mysql_query($q);
$i=0;
while($row = mysql_fetch_array($result)) {
	$result_set[$i] = $row;
	$i++;	
}
for($i=0;$i<count($result_set);$i++) {
	print "<pre>";
	if($next_element < count($result_set)-1) {
		$next_element = $i+1;
		print "Next element in the array is: <br><br>";
		print_r($result_set[$next_element]);
	}
	print "Now the array points to:<br><br>";
	print_r($result_set[$i]);
	print "</pre>"; 
}
?>

On last element of the array, it doesn't print Next element in the array is:

Maybe this isn't the best solution!

1

How about this:

print "<pre>";
$row = mysql_fetch_array($result);
while ($row) {
	print 'This ID: ' . $row[0] . ' :: ';
	$row = mysql_fetch_array($result);
	if ($row)
		print 'Next ID: ' . $row[0] . '<br/>';
}
print "</pre>"; }
Votes + Comments
This is much simpler than what I thought! :)
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.