Solved

How would I get php to highlight a table cell

Posted on 2010-09-15
6
280 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 109

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 109

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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.
In this tutorial viewers will learn how to customize the background color and font color of highlighted text using the ::selection element in CSS Begin by defining the selected text as an element in CSS by typing "::selection": Style the ::selection…
In this tutorial viewers will learn how to style rounded corners for elements in CSS using the border-radius property Begin with a normal styled element such as a div: To style all four corners of the div to be the same degree of roundness, use the …

813 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

16 Experts available now in Live!

Get 1:1 Help Now