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'>$ ".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);
}
Why don’t you just use css?
Add the value to the
classattribute on thetdthen set the rules in css.Add the class like
class='CONTENTS':And then replace your jQuery code with a stylesheet:
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)