Jump to content

Something Different With Each Loop


searls03

Recommended Posts

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

<?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;
}
?>

<?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. :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.