i search long time on forum , can't find solution problem.
i need create foreach
loop 12 elements 3 of them have different class.
this have done far:
$result = $db->query("select `views` `views` `views` = `id` , date_sub(curdate(),interval 7 day) <= date(`views`.`time`) order `id` desc limit 12"); while($row = $result->fetch_assoc()) { $users[] = $row; } $output .= '<ul>'; foreach($rows $row) { $output .= '<li class="grid__item"></li>'; } $output .= '<ul>'; return $output;
how can add different class li
objects @ 2, 8 , 12 position?
to output this:
<ul> <li class="grid__item"></li> <li class="grid__item grid__item--deco grid__item--deco-1"></li> <li class="grid__item"></li> <li class="grid__item"></li> <li class="grid__item"></li> <li class="grid__item"></li> <li class="grid__item"></li> <li class="grid__item grid__item--deco grid__item--deco-2"></li> <li class="grid__item"></li> <li class="grid__item"></li> <li class="grid__item"></li> <li class="grid__item grid__item--deco grid__item--deco-3"></li> </ul>
just use key
of foreach
, set additional class inside loop block. add if
condition:
$i = 1; $class_keys = array(2, 8, 12); $output = '<ul>'; foreach($rows $key => $row) { // ^ use $default_class = 'grid__item'; if(in_array($key + 1, $class_keys)) { $default_class .= " grid__item--deco grid__item--deco-{$i}"; $i++; } $output .= "<li class=\"$default_class\"></li>"; } $output .= '<ul>';
Comments
Post a Comment