Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.