0

I've got a mysql query running and it checks to see if an iterator is equal to 1, then display this div title...

if ($this->dliterator == 1) {echo "<div class='clientsection' id='downloads'>Downloads</div><br/>";};

The problem is, is that the dl iterator may not necessarily start at 1. (it is directly related to a downloadid from the database).

How can I get this to display only for the first time through the loop ONLY?

while ($row = mysql_fetch_assoc($result)) { 

   if ($row['download'] != null){
    if ($this->dliterator == 1) {echo "<div class='clientsection' id='downloads'>Downloads</div><br/>";};


    if ($editDownload == 1) {
     echo "<div class='clientlink' style='margin-top: 15px;'>";
     echo "<input name='downloads[$this->dliterator][name]' type='text' id='download$this->dliterator' value='" . $row['download'] . "'/>";
     echo "<input name='downloads[$this->dliterator][title]' type='text' id='downloadtitle$this->dliterator' value='" . $row['downloadtitle'] . "'/>";
     echo "<img class='removelink' src='/images/deletelink.png' width='15' />";
     echo "<input id='downloadid' name='downloads[$this->dliterator][id]' type='hidden' value='".$row['downloadid']."' style='display: none'/>";
     echo "<br/><img id='uploaddownload$uploaditerator' class='uploaddownload' src='../images/upload.png' width='80'/>"; 
     echo "</div>";


    };

   };

   $this->dliterator++;

   $uploaditerator++;
};

Thanks for all the answers! Here's my working solution thanks to Zuul:

if ($row['download'] != null){
if (($this->dliterator != null ) && ($check ==0)) {echo "<div class='clientsection' id='downloads'>Downloads</div><br/>";
$check++;};
0

4 Answers 4

10

Try this:

$check=0;

if (($this->dliterator == 1) && ($check==0)) {
  echo "<div class='clientsection' id='downloads'>Downloads</div><br/>";
  $check++;
};

It will enter the if statement if your dliterator==1 and if $check hasn't change... After inside, it will change the $check thus preventing the if statement to run again!

Hope it helps!

Sign up to request clarification or add additional context in comments.

1 Comment

I think making $check a boolean would make the example more readable, but otherwise +1.
2

Put a flag out of the loop at false $firstLineFetched = false;, then if it is false you set it true and do your first line processing

Comments

0
$row = mysql_fetch_assoc($result);

if ($row && $row['download'] != null) {

    echo "<div class='clientsection' id='downloads'>Downloads</div><br/>";
}

while ($row) {

    // ...

    $row = mysql_fetch_assoc($result);
}

2 Comments

This looks weird - first, $row should also be checked before the while, second you would skip displaying the data of the last fetch into $row.
There is a lack of clarity in the question. My understanding was simply "do an extra step at the beginning of the loop on the first iteration only". This can be achieved by unrolling the loop as I have done. If this was not the goal, I did not understand the question.
0

You could just set a counter outside the loop to 0 and do something like:

if($i == 0)
  printf("<div class=........>\n");

Then be sure to increment $i ($i++) at the end of the while() and next time through the loop it will pass right over the printf() statement.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.