I’m trying to add the CSS class “selected” to the first accordionButton div created. Why doesn’t this work?
// Create nav from database
$chapter_array = get_chapter_names();
for ($i=0; $i < sizeof($chapter_array); $i++) {
if($i == 0) {
$selected = 'selected';
}
echo '<div class="accordionButton '.$selected.'">'.$chapter_array[$i].'</div>';
$page_id_array = get_page_ids($i);
echo '<div class="accordionContent">';
for ($j=0; $j < sizeof($page_id_array); $j++) {
$page_name = get_page_name($page_id_array[$j]);
echo '<a href="?page_id='.$page_id_array[$j].'">'.$page_name.'</a><br />';
}
echo '</div>';
}
You never set
$selectedback after the first div so this adds that class to every div, because$selectedalways contains the string'selected'.You can set it to the empty string before your if statement:
If you like this better you can also write that as a nice ternary expression: