AlexB
AlexB

Reputation: 2174

Add class "last" to the last element of an array in while loop

I have script which I use to create top navigation on my site.

My problem, I do not know how to add class last to the class that is already used to the last element of an array to make it look class="tab selected last" when selected or class="tab last" when not selected. Class last will get rid of the divider line on the right of the nave menu element.

here is my script

while ($info = $res -> fetch()){
    $link_lbl = $info['link_lbl'];
    $link_dir = $info['link_dir'];
    $link_url = $info['link_url'];
    $link = ($link_dir == NULL) ? SITE_DOMAIN.DST.$link_url : SITE_DOMAIN.DST.$link_dir.DS.$link_url;
    $link_title = $info['link_title'];
    $selected = ($info['ID'] == $number) ? 'tab selected' :('tab');
    $rd_div = ($info['link_show'] == 1) ? '<div><a class="'.$selected.'" href="'.$link.'" title="'.$link_title.'">'.$link_lbl.'</a></div>' : ('');

print<<<END
$rd_div

END;
}

Please help.

I have tried to use end($rd_div) to find the last element, but do not know how to change it.

Thanks in advance

Upvotes: 0

Views: 848

Answers (1)

klkvsk
klkvsk

Reputation: 670

$rowNum = 0;
while ($info = $res -> fetch()){
    $rowNum++;
    // ...
    $last = ($rowNum == $res->rowCount()) ? ' last' : '';
    $rd_div = ($info['link_show'] == 1) ? '<div><a class="'. $selected . $last . '" href="'.$link.'" title="'.$link_title.'">'.$link_lbl.'</a></div>' : ('');

Upvotes: 3

Related Questions