Solved

How would I get php to highlight a table cell

Posted on 2010-09-15
6
278 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 108

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 108

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

CSS is a visual language used to classify objects and define rules about how they should be displayed. CSS skills aren’t restricted to developers anymore, there is a big benefit to having a basic understanding of the language, regardless of your occ…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
In this tutorial viewers will learn how to style different bullet points for unordered lists in CSS. Begin with a normal unordered list; the default bullet point is a solid circle: In the CSS, create a defined class of unordered list by typing "ul.S…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

863 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now