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

Recommended Answers

All 2 Replies

<?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!

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>"; }
commented: This is much simpler than what I thought! :) +10
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.