0

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

2
  • 3
    Increment a counter on every loop iteration, and if it is equal to the items count -1, it's the last ^^. Commented Aug 2, 2013 at 14:22
  • 1
    Why the heck HEREDOC? Just do echo $rd_div;, no? Commented Aug 2, 2013 at 14:24

1 Answer 1

3
$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>' : ('');
Sign up to request clarification or add additional context in comments.

1 Comment

please a better explanation if you have one. this makes for better answers. thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.