Jump to content

alternate row colors, by blocks


emehrkay

Recommended Posts

this was pissing me off so i moved on a while ago, but now im back to revisit it. - it may be because i am holding on to this one way of trying to accomplsih it, anyway...

i am outputting a table and one of the columns is 'Location' (its semi-grouped by this field). i need to alternate colors between different locations.

so i am trying

if($i > 0){
if($data[$i]->site_name != $data[$i-1]->site_name){
//switch here
}
}

but i can not figure out the logic to save my life.

any ideas? thanks
Link to comment
https://forums.phpfreaks.com/topic/26858-alternate-row-colors-by-blocks/
Share on other sites

I would use $thisSite and $lastSite variables. Your conditional on if statement would be if($thisSite!=$lastSite) and you'd have to add a $lastSite='NONSENSICALDATATOREDUCECHANCESOFBEINGTHESAMEASMYFIRSTROW'; before the $i loop. Then, the last line of the loop should read $lastSite=$thisSite.

Personally, I always have trouble getting array indices working inside of conditionals and/or any other function. Especially with -> operator. So, I assign the values I want inside that array to temporary variables before I actually use them in any fundtion.
ralph, i have a table and when it is printed out, the rows are generally grouped by location. i want one group to be white, then when the location changes i need for that group to be blue. then back to white for the next and so on.

blear, i believe that i tried your approach before. the best i could do was get the first row of the new loaction to be different while the rest were the same as the first block.

-im stuck
i figured it out, its a little long but it gets ti done. thanks for the help

[code=php:0]
$rowColor = 'x'; //row color basic definition
for($i = 0; $i < $count; $i++){
$name = $data[$i]->last_name .", ".$data[$i]->first_name;
$class = $data[$i]->course_type_name;
$location = $data[$i]->site_name;
$dates = LmsEnroll::format_date($data[$i]->class_start_date) ." - ". LmsEnroll::format_date($data[$i]->class_end_date);
$checkBox = $data[$i]->class_id.'|'.$data[$i]->person_id;


/**
* Row Colors
**/
if($i > 0){
if($data[$i]->site_name != $data[$i-1]->site_name){
$rowColor = ($rowColor == 'x') ? 'y' : 'x';
}elseif($data[$i]->site_name == $data[$i-1]->site_name){
$rowColor = ($rowColor == 'x') ? 'x' : 'y';
}
}
[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.