Solved

How would I get php to highlight a table cell

Posted on 2010-09-15
6
277 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
This is a how to build your own CSS3 slideshow and when I say CSS3 I mean just CSS with no javascript in sight! There a few examples online of how to do this but most just show you an example without any explanation, others make it more complicat…
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

758 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