Compare values of cells in different rows in table using PHP


Compare values of cells in different rows in table using PHP



My question is similar to the one found here:



compare values of cells in different rows in table using jquery



Using PHP, what is the best way to group together rows and cells - if the value in the first column and the value of the 3rd columns are the the same, while maintaining the zebra-stripe pattern?



This is what I'm trying to do: http://jsfiddle.net/zmcjwqk9/


<table border="1">
<thead>
<tr>
<td>TITLE A</td>
<td>TITLE B</td>
<td>TITLE C</td>
</tr>
</thead>
<tbody>
<tr style="background-color: yellow">
<td>A</td>
<td>B</td>
<td>C</td>
</tr>
<tr style="background-color: red">
<td>AA</td>
<td>BB</td>
<td>CC</td>
</tr>
<tr style="background-color: yellow">
<td>AAA</td>
<td>BBB</td>
<td>CCC</td>
</tr>
<tr style="background-color: yellow">
<td>AAA</td>
<td>BBBB</td>
<td>CCC</td>
</tr>
<tr style="background-color: red">
<td>AA</td>
<td>BBB</td>
<td>CC</td>
</tr>
<tr style="background-color: yellow">
<td>A</td>
<td>B</td>
<td>C</td>
</tr>
<tr style="background-color: red">
<td>AA</td>
<td>BB</td>
<td>CC</td>
</tr>
<tr style="background-color: red">
<td>AA</td>
<td>BBB</td>
<td>CC</td>
</tr>
<tr style="background-color: yellow">
<td>AAAA</td>
<td>BBBB</td>
<td>CCCC</td>
</tr>
<tr style="background-color: yellow">
<td>AAAA</td>
<td>BBB</td>
<td>CCCC</td>
</tr>
<tr style="background-color: yellow">
<td>AAAA</td>
<td>BB</td>
<td>CCCC</td>
</tr>
<tr style="background-color: red">
<td>AAA</td>
<td>BBB</td>
<td>CC</td>
</tr>
</tbody>





PHP


echo '<table class="tableListingTable">
<thead style="font-size: 2.5em; height: 40px; line-height: 40px; width: 100%;">
<tr>
<td>To</td>
<td>Time</td>
<td>Airline</td>
</tr>
</thead>
<tbody>';

foreach ($productPages as $productArray => $v) {
$class = ($c = !$c) ? 'odd' : 'even';
echo '<tr class="future ' . $class . '">';
echo '<td width="16%">' . $v['destinationCity'] . '</td>n';
echo '<td width="16%">' . $v['currentTime'] . '</td>n';
echo '<td width="16%">' . $v["airlineName"] .'</td>';}
echo '</tr></tbody></table>';





Are you talking about doing this in the PHP script that creates the table, or is the PHP script downloading the HTML and parsing it with a DOM parser?
– Barmar
Aug 8 '14 at 22:34





I have the PHP creating the table rows and cells already. The part I'm having issue with is comparing the rows > values in column A and column B. If the values match, give it a certain color.
– Gdsmk
Aug 8 '14 at 22:36






That doesn't answer my question. Are you trying to do this grouping in the script that creates the table, or in another script that reads the table (similar to the way jQuery processes the result)?
– Barmar
Aug 8 '14 at 22:37





Oh sorry - in the script that creates the table
– Gdsmk
Aug 8 '14 at 22:39





Then please show the code that creates the table. You should do the grouping there. Or you could do it in the database query, so it doesn't return duplicate rows.
– Barmar
Aug 8 '14 at 22:41




1 Answer
1



The code that creates the table should be something like this:


$color = 'red';
$prevA = $prevC = null;
foreach ($data as $row) {
if ($row['A'] != $prevA || $row['C'] != $prevC) {
$color = $color == 'red' : 'yellow';
$prevA = $row['A'];
$prevC = $row['C'];
}
echo "<tr class='$color'><td>{$row['A']}</td><td>{$row['B']}</td><td>{$row['C']}</td></tr>";
}





Thank you Barmar!
– Gdsmk
Aug 8 '14 at 23:12






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Opening a url is failing in Swift

Export result set on Dbeaver to CSV