Reputation: 305
I had a really long description written for my problem, but I've progressed and have managed to get it partially working... I basically want every other row in a table to be a different color - and to highlight certain rows if they meet a condition that I set.
foreach ($data as $row) {
$style = null;
while (($values = fgetcsv($handle, 0, '|')) !== false) {
$comment_lines = $values[6];
$priority = $values[7];
$time_worked = $values[11];
$var_X = strpos($priority, '1');
$var_C1 = strpos($comment_lines, 'CCB');
$var_C2 = strpos($comment_lines, 'CEB');
$var_U = empty($time_worked);
}
if (empty($values[0]) && count($values) === 1) {
continue;
}
if (strlen($var_X)) {
echo '<tr class="gradeX">';
foreach ($values AS $index => $value) {
echo '<td>' . $value . '</td>' ;
}
} else if ($var_C1 !== false) {
echo '<tr class="gradeC">';
foreach ($values AS $index => $value) {
echo '<td>' . $value . '</td>' ;
}
} else if ($var_C2 !== false) {
echo '<tr class="gradeC">';
foreach ($values AS $index => $value) {
echo '<td>' . $value . '</td>' ;
}
} else if ($var_U !== false) {
echo '<tr class="gradeU">';
foreach ($values AS $index => $value) {
echo '<td>' . $value . '</td>' ;
}
} else if ($odd) {
$odd = !$odd;
echo '<tr class="even gradeA">';
foreach ($values AS $index => $value) {
echo '<td>' . $value . '</td>' ;
}
} else {
$odd = !$odd;
echo '<tr class="odd gradeA">';
foreach ($values AS $index => $value) {
echo '<td>' . $value . '</td>' ;
}
}
echo '</tr>';
}
Right now, only the 'every other' is working. I can't get the gradeX, gradeC and gradeU to show anything...
EDIT:
This is the working code, cheers.
while (($values = fgetcsv($handle, 0, '|')) !== false) {
$style = null;
$comment_lines = $values[6];
$priority = $values[7];
$time_worked = $values[11];
$var_X = strpos($priority, '1');
$var_C1 = strpos($comment_lines, 'CCB');
$var_C2 = strpos($comment_lines, 'CEB');
$var_U = empty($time_worked);
if (empty($values[0]) && count($values) === 1) {
continue;
}
if ($var_X !==false) {
echo '<tr class="gradeX">';
} elseif ($var_C1 !== false) {
echo '<tr class="gradeC">';
} elseif ($var_C2 !== false) {
echo '<tr class="gradeC">';
} elseif ($var_U !== false) {
echo '<tr class="gradeU">';
} else {
//nothing
}
foreach ($values as $index => $value) {
echo '<td>' . $value . '</td>';
}
echo '</tr>';
}
Upvotes: 0
Views: 1186
Reputation: 5868
If you can target IE later than IE8 and forget about IE8, you can use CSS3:
tr:nth-child(odd) {
background-color: #EEEEEE;
}
tr:nth-child(even) {
background-color: #FFFFFF;
}
Upvotes: 0
Reputation: 4674
In your loop you need to add a counter to identify whether you're currently on an 'even' or an 'odd' iteration, and then add either class 'odd' or 'even' into the as you output it.
There's a related question already on here that covers that here: php: how to add odd/even loop in array
The other parts should be fairly easy by using if statements to check if the relevant data exists or not.
For example, strlen will tell you whether a variable has length or not (to put out 'gradeX'), and strpos will allow you to check if a varibale contains a string (for 'gradeC').
Upvotes: 1