searls03 Posted November 18, 2012 Share Posted November 18, 2012 how would it be possible to use a while loop, but on the first time through, it will have different content than on the rest of the loops through. for example: <?php $sql = mysql_query("SELECT * FROM categ where pares='0' order by ids ASC"); while($row = mysql_fetch_array($sql)){ $category1 = $row["categorys"]; $id =$row["ids"]; ?> first loop will say this:12345 all other loops say this: this is the second loop <?php } ?> how could I do this? Link to comment https://forums.phpfreaks.com/topic/270873-something-different-with-each-loop/ Share on other sites More sharing options...
Barand Posted November 18, 2012 Share Posted November 18, 2012 <?php $sql = mysql_query("SELECT * FROM categ where pares='0' order by ids ASC"); $first = true; while($row = mysql_fetch_array($sql)){ if ($first) echo "First time"; else echo "Something else"; $first = false; } ?> Link to comment https://forums.phpfreaks.com/topic/270873-something-different-with-each-loop/#findComment-1393418 Share on other sites More sharing options...
Andy123 Posted November 18, 2012 Share Posted November 18, 2012 <?php $sql = mysql_query("SELECT * FROM categ where pares='0' order by ids ASC"); $first = true; while($row = mysql_fetch_array($sql)){ if ($first) echo "First time"; else echo "Something else"; $first = false; } ?> As a minor optimization, I would put the assignment of false to $first within the code block that is executed on the first iteration. It is not necessary to do this assignment on every iteration. Just a minor detail. Link to comment https://forums.phpfreaks.com/topic/270873-something-different-with-each-loop/#findComment-1393420 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.