Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How would I get php to highlight a table cell

Posted on 2010-09-15
6
Medium Priority
?
298 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 111

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 111

Accepted Solution

by:
Ray Paseur earned 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

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…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn how to count occurrences of each item in an array.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

670 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