Вы можете использовать область $ GLOBAL для хранения текущего выбранного состояния класса, см. Ниже функцию table_row_toggle (). Да, я знаю, что неправильно злоупотреблять областью действия $ GLOBAL, но мы здесь, чтобы исправить проблемы, не так ли? :)
Вызов функции переключения строк таблицы в HTML:
<tr <? table_row_toggle(); ?>>
Функция в PHP:
/* function to toggle row colors in tables */
function table_row_toggle() {
/* check if $trclass is defined in caller */
if(array_key_exists('trclass', $GLOBALS)) {
$trclass = $GLOBALS['trclass'];
}
/* toggle between row1 and row2 */
if(!isset($trclass) || $trclass == 'row2') {
$trclass = 'row1';
} else {
$trclass = 'row2';
}
/* set $trclass in caller */
$GLOBALS['trclass'] = $trclass;
/* write the desired class to the caller */
echo ' class="' . $trclass . '"';
}