Eiffel-Mtl Posted October 16, 2022 Share Posted October 16, 2022 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 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 Quote Link to comment https://forums.phpfreaks.com/topic/315427-different-class-if-first-row/ Share on other sites More sharing options...
mac_gyver Posted October 16, 2022 Share Posted October 16, 2022 before the start of the loop, set a variable to the unique/one-time output you want. echo that variable at the point where you want the output, then set that variable to an empty string after the point where you echo it. every pass through the loop after that point will echo an empty string, i.e. nothing will be output. Quote Link to comment https://forums.phpfreaks.com/topic/315427-different-class-if-first-row/#findComment-1601683 Share on other sites More sharing options...
Eiffel-Mtl Posted October 16, 2022 Author Share Posted October 16, 2022 Is it more easy to get the ID of the first row? If it's possible, how to? Quote Link to comment https://forums.phpfreaks.com/topic/315427-different-class-if-first-row/#findComment-1601684 Share on other sites More sharing options...
mac_gyver Posted October 16, 2022 Share Posted October 16, 2022 no. it's not more easy. Quote Link to comment https://forums.phpfreaks.com/topic/315427-different-class-if-first-row/#findComment-1601685 Share on other sites More sharing options...
Eiffel-Mtl Posted October 16, 2022 Author Share Posted October 16, 2022 (edited) So I don't understand how to do your solution. For now I found a solution, not the pretty one : Make two query, one with offset 1 and the other one with limit 1. Edited October 16, 2022 by Eiffel-Mtl Quote Link to comment https://forums.phpfreaks.com/topic/315427-different-class-if-first-row/#findComment-1601686 Share on other sites More sharing options...
Solution mac_gyver Posted October 16, 2022 Solution Share Posted October 16, 2022 // before the start of the loop, set a variable to the unique/one-time output you want. $first = 'class="active"'; while($row = mysqli_fetch_array($result)) { // echo that variable at the point where you want the output ?> <li><a id="<?php echo $row["urlName"] ?>Tab" <?=$first?> href="#<?php echo $row["urlName"] ?>Content"><?php echo $row["title"] ?></a></li> <?php // then set that variable to an empty string after the point where you echo it. every pass through the loop after that point will echo an empty string, i.e. nothing will be output. $first = ''; } Quote Link to comment https://forums.phpfreaks.com/topic/315427-different-class-if-first-row/#findComment-1601689 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.