DIfferent class if first row

Codes here !

Moderators: gesf, Michalio

Post Reply
Eiffelmtl
New php-forum User
New php-forum User
Posts: 1
Joined: Sat Oct 15, 2022 4:21 pm

Hi,

I got a code who give class name different if rowid = 1, but if row('id') 1 is deleted the class is not appear.

I want the class use only on first row.

Here is my code

Code: Select all

while($row = mysqli_fetch_array($result))
{ 
if ($row['id'] == '1'){
?>
<li><a id="<?php echo $row["urlName"] ?>Tab" class="active" href="#<?php echo $row["urlName"] ?>Content"><?php echo $row["title"] ?></a></li>
<?php } else {	?>
<li><a id="<?php echo $row["urlName"] ?>Tab" href="#<?php echo $row["urlName"] ?>Content"><?php echo $row["title"] ?></a></li>
} 
}
Any help will be useful.

Thanks
User avatar
Michalio
Moderator
Moderator
Posts: 337
Joined: Sun Jul 18, 2021 1:33 pm
Location: Poland

You need to count the cycles in the loop:

Code: Select all

$i = 0;
while($row = mysqli_fetch_array($result))
{ 
    $className = $i === 0 ? 'first' : 'other';
    ?>
    <li>
        <a id="<?php echo $row["urlName"] ?>Tab" class="<?php echo $className; ?>" href="#<?php echo $row["urlName"] ?>Content">
            <?php echo $row["title"] ?>
        </a>
    </li>
    <?php
    $i++;
}
Free coding lessons: https://php-forum.com/phpforum/viewtopic.php?t=29852
Post Reply