Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6166581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:21:47+00:00 2026-05-23T22:21:47+00:00

I have a database table that contains: Industry risk ————– A B C And

  • 0

I have a database table that contains:

Industry risk
--------------
      A
      B
      C

And so on. I use query.php to generate a dynamic table to index.php and refresh it automatically using AJAX. The data is pulled with:

$sql="SELECT * FROM scoreboard";

And the table generated with:

// Construct the table

echo "<table id='querytable'>\n";

The result is processed by query.js and displayed in index.php with: <div id="querydiv"></div>. So far, so good. Now for the tricky part. I want to conditionally format the background of the <td> based on the content, so that if it contains “A” then background-color:red;. The <td> are generated like so:

// Construct the array

while($row = mysql_fetch_assoc($result)){
    echo '<tr>'."\n";
    echo "<td align='center'>{$row['codcliente']}</td>\n" . "<td align='center'>{$row['nombre']}</td>\n" . "<td align='center'>{$row['ejecutivo']}</td>\n" . "<td align='center'>{$row['banca_as400']}</td>\n" . "<td align='center'>{$row['banca_real']}</td>\n" . "<td align='right'>$&nbsp;".number_format($row['ingresos'], 2)."</td>\n" . "<td align='center'>{$row['ciiu']}</td>\n" . "<td align='center'>{$row['division']}</td>\n" . "<td align='center'>{$row['actividad']}</td>\n" . "<td align='center'>{$row['riesgo_industria']}</td>\n" . "<td align='center'>{$row['riesgo_cliente']}</td>\n" . "<td align='center'>{$row['fecha']}</td>\n" . "<td align='center'>{$row['analista']}</td>\n";
    echo '</tr>'."\n";
}
echo "</table>\n";

I made a JavaScript file called highlight.js which contains:

$(function() {
            $("#querytable td:contains('A')").css('background-color','#C0504D'),
            $("#querytable td:contains('B')").css('background-color','#FDE480'),
            $("#querytable td:contains('C')").css('background-color','#9BBB59');
            });

And I try to call it from query.php using:

// Load higlighting syntax

echo "<script type='text/javascript' src='highlight.js'></script>";

but nothing happens. I’ve tried giving the <div> tag and id and calling the JavaScript from index.php (since it has HTML and a <head> tag), but that didn’t work either.

Is there anyway to do this? What am I doing wrong? Thanks!

Additional info. Contents of query.js:

// Customize these settings

var seconds = 2;
var divid = "querydiv";
var url = "query.php";

// Refreshing the DIV

function refreshdiv(){

// The XMLHttpRequest object

var xmlHttp;
try{
xmlHttp=new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari
}
catch (e){
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); // Internet Explorer
}
catch (e){
try{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e){
alert("Your browser does not support AJAX.");
return false;
}
}
}

// Timestamp for preventing IE caching the GET request

fetch_unix_timestamp = function()
{
return parseInt(new Date().getTime().toString().substring(0, 10))
}

var timestamp = fetch_unix_timestamp();
var nocacheurl = url+"?t="+timestamp;

// The code...

xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState==4){
document.getElementById(divid).innerHTML=xmlHttp.responseText;
setTimeout('refreshdiv()',seconds*1000);
}
}
xmlHttp.open("GET",nocacheurl,true);
xmlHttp.send(null);
}

// Start the refreshing process

var seconds;
window.onload = function startrefresh(){
setTimeout('refreshdiv()',seconds*1000);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T22:21:47+00:00Added an answer on May 23, 2026 at 10:21 pm

    Why don’t you just use css?

    Add the value to the class attribute on the td then set the rules in css.

    Add the class like class='CONTENTS':

    echo "<td align='center' class='{$row['codcliente']}'> ...
    

    And then replace your jQuery code with a stylesheet:

    #querytable td.A {background-color : #C0504D; }
    #querytable td.B {background-color : #FDE480; }
    #querytable td.C {background-color : #9BBB59; }
    

    In your example the content was simple and valid css, if the contents had spaces or started with numbers you would need to transform them to valid css class names.

    (edit, I copied the js code into my css without fixing the syntax)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a database table that essentially contains different types of things. I'll use
I have a database table that contains Swedish/Norwegian strings. When I query some data,
I have a database table TravelRequest that contains, amongst other things, the fields SignOffName
I have a database that contains a table that looks a bit like this:
I have two simple tables in my database. A card table that contains Id,
I'm trying to normalize a mysql database.... I currently have a table that contains
I have a table in a sql server 2008 database that contains bunch of
I have a database structure that has a Person table which contains fields such
I have a database table that contains entries for start time and end time
I have a MySQL database table that contains an Article ID ( primary key

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.