Solved

How would I get php to highlight a table cell

Posted on 2010-09-15
6
284 Views
Last Modified: 2013-12-12
I have two php mssql queries that echo the results back to the same page in two different tables. Each have one column in common "ord.tractor". Can anyone tell me how I could get php to check if the column fields data are the same and highlight them ? Sort of like if table 1 has a row that has 125 in the ord.tractor column, and table 2 has a row with ord.tractor column as 125 than both would turn red. I
0
Comment
Question by:GD_GRAY
6 Comments
 
LVL 13

Expert Comment

by:darren-w-
ID: 33686046
define 2 arrays, ie $array1 $array2
read id's from both columns into each individual arrays
pass both array's to the array dif function :  http://php.net/manual/en/function.array-diff.php

$difference = array_diff(array1, array2)

populate the columns, put an if statement on the column that is being displayed and use the in_array function:

if (in_array($currrent_item, $difference){
print "<div class=\"yellow_background\">$current_item</div>";
}

This should do the job
0
 
LVL 7

Expert Comment

by:marklogan
ID: 33689287
If you need to highlight matching rows in both tables you could also loop through both results sets first and store all the ord.tractor values in two arrays.

Then loop them again to produce the output table but in each iteration check to see if the ord.tractor value is in the other array. If so, highlight red.

0
 
LVL 7

Expert Comment

by:marklogan
ID: 33689298
Code didn't attach.
$result_01 = mysql_query("SELECT .... ");

$resultArray_01 = array();

while ($row = mysql_fetch_assoc($result_01)) {
 array_push($resultArray_01, $row['ord.tractor']);
}

$result_02 = mysql_query("SELECT .... ");

$resultArray_02 = array();

while ($row = mysql_fetch_assoc($result_02)) {
 array_push($resultArray_02, $row['ord.tractor']);
}

while ($row = mysql_fetch_assoc($result_01)) {
 if(in_array($row['ord.tractor'], $resultArray_02)
 {
   // output with red row here
 }
 else
 {
   // output normally here
 }
}

while ($row = mysql_fetch_assoc($result_02)) {
 if(in_array($row['ord.tractor'], $resultArray_01)
 {
   // output with red row here
 }
 else
 {
   // output normally here
 }
}

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 110

Expert Comment

by:Ray Paseur
ID: 33691332
The "turn red" part would be something you would do with CSS.  Finding the "same" fields might depend on a handful of factors - the order of the results set from the query, whether any fields were missing, etc.  If you knew for sure that you had exactly the same number and order of rows it would be simpler.
<?php // RAY_temp_redhtml.php
error_reporting(E_ALL);

// TEST DATA ARRAYS
$a1 = array("acme store",  "acme gas station", "acme dentist");
$a2 = array("acme barber", "acme gas station", "acme doctor");

// FINDING THE ELEMENTS THAT ARE THE SAME
$same = array();
foreach ($a1 as $key => $a1_value)
{
    // MAN PAGE http://us2.php.net/manual/en/function.in-array.php
    if (in_array($a1_value, $a2)) $same[] = $a1_value;
}

// CREATING TABULAR INFORMATION
echo "<table>" . PHP_EOL;

// USING AN ITERATOR TO WALK THE TWO ARRAYS
foreach ($a1 as $key => $a1_value)
{
    $a2_value = $a2[$key];
    $my_color = 'green';
    if (in_array($a1_value, $same)) $my_color = 'red';

    echo "<tr><td><span style='color:$my_color'>$a1_value</td><td><span style='color:$my_color'>$a2_value</td></tr>" . PHP_EOL;
}
echo "</table>" . PHP_EOL;

Open in new window

0
 

Author Comment

by:GD_GRAY
ID: 33692574
Ray:

The number of Row's will very from one table to the other but the field I need to tag is always the 2nd column in every row and never null. Wow this is much deeper than I had planned Im sorry about that. I thought their might be somthing that would just work somthing like: if  td t1=td t2  highlight  t1/t2. Im not getting the array pulled for some reason.
0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 33693783
To tag only the second column change line 26 in the code snippet above like this.  Sorry I forgot about the close span in the prior post.  Please post the code that pulls the arrays.
echo "<tr><td>$a1_value</td><td><span style='color:$my_color'>$a2_value</span></td></tr>" . PHP_EOL;

Open in new window

0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question