당신은 카운터 변수를 사용할 수 있습니다. 그렇지 않으면 이상합니다.
$counter = 1;
while($fetch = $stm->fetchAll()) {
echo '<tr class="', (!($counter % 2) ? 'even' : 'odd'), '">';
echo '<td>' . $fetch['name'] . '</td>';
echo '</tr>';
$counter++;
}
또한 단지 nth-child
CSS 선택기를 시도하고 모든 PHP에서이 일을 피할 수 있습니다. 그러나 크로스 브라우저가 아닙니다. IE < 8을 지원해야하는 경우 위의 작업을 수행해야합니다.
예 :
while($fetch = $stm->fetchAll()) {
if(!isset($row_num)) $row_num = 1; // at first $row_num does not exist so create it and make uneven (start with 1)
$row_class = (++$row_num % 2) ? 'even' : 'odd'; // then check whether $row_num is odd or even and assign it the corresponding class name
echo '<tr class="'.$row_class.'">';
echo '<td>' . $fetch['name'] . '</td>';
echo '</tr>';
}
: 여기
#your-table tr:nth-child(odd) {
// style odd rows differently
}