jusko
jusko

Reputation: 31

MySQL Fetch Array doubt

I´m fetching my database for images to create a gallery. Every row appear inside a <li>. My question is, is it possible, that the first <li> have a class (for example, "visible"), and all the other <li> have a class named "hidden". So the first $row would have a different class than the following... Hope I made myself clear! Thanks

Upvotes: 1

Views: 233

Answers (3)

karim79
karim79

Reputation: 342635

It can be done more shortlier like this:

$i = 1;
while ($row = mysql_fetch_assoc($result)) {
    echo '<li class="' . (($i == 1) ? 'visible' : 'hidden') . '">';
    $i++;
}

Upvotes: 2

kander
kander

Reputation: 4286

How about something like

$visible = true;

while(...) {

     if($visible) {
         echo "<li class='visible'>";
     else {
         echo "<li class='hidden'>";
     }
     $visible = false; // Every loop sets it to false, which after the first one will make no difference.
}

Upvotes: 0

Shubham
Shubham

Reputation: 22307

Well thats easy! Just track the row number, if it is the first row then echo out class="visible" else class='hidden"

Upvotes: 2

Related Questions