Jump to content

DIfferent class if first row


Go to solution Solved by mac_gyver,

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/315427-different-class-if-first-row/
Share on other sites

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.

  • Solution
// 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 = '';
}

 

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.