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:

4------ Anthony

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?



Recommended Answers

All 2 Replies

$con = mysql_connect("localhost","root");
$q = "select * from table1";
$result = mysql_query($q);
while($row = mysql_fetch_array($result)) {
	$result_set[$i] = $row;
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 "Now the array points to:<br><br>";
	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, learning, and sharing knowledge.