Change background color of TD - php

I want to change my background color for one of the rows if one value is bigger than the other.
I tried using jQuery to change the color but it changes the color for every row.
Here is my code:
while ($row = mysqli_fetch_assoc($result)) {
echo'<tr>';
echo'<td>'.$row['id'].'</td>';
echo'<td style="width:25%">'.$row['date'].'</td>';
echo'<td class="starttime">'.$row['start'].'</td>';
echo'<td class="finishtime">'.$row['finish'].'</td>';
echo'<td>'.$row['total'].'</td>';
echo'<td>'.$row['cash'].'</td>';
echo'</tr>';
$start =$row['start'];
$finish = $row['finish'];
if ( $start<$finish ) {
//CHANGE BACK GROUND COLOR FOR finish time to red
}else{
//Keep the color as it was
}
}

Test the value before rendering the TD, then add a specific class to the TD, then style the TD with the class
<style type="text/css">
.redCell {
background-color: red;
}
</style>
<?php
// Loop Through DB Rows
while ($row = mysqli_fetch_assoc($result)) {
echo'<tr>';
echo'<td>'.$row['id'].'</td>';
echo'<td style="width:25%">'.$row['date'].'</td>';
$start =$row['start'];
$finish = $row['finish'];
// Test the value
if ( $start<$finish ) {
$class = "redCell"; // Add a new class to make it red
}else{
$class = ""; // Keep the color as it was
}
echo'<td class="starttime">'.$row['start'].'</td>';
// Add the style to the TD
echo'<td class="finishtime '.$class.'">'.$row['finish'].'</td>';
echo'<td>'.$row['total'].'</td>';
echo'<td>'.$row['cash'].'</td>';
echo'</tr>';
}
?>

Here is yet another way with jQuery - http://jsfiddle.net/7jm3R/
I created a class for the red background called redback;
$('tr').each(function() {
var start = parseInt($(this).find('.start').html());
var finish = parseInt($(this).find('.finish').html());
if(start < finish) {
$(this).addClass('redback');
}
});

What about something like:
while ($row = mysqli_fetch_assoc($result)) {
$style = ''
$start =$row['start'];
$finish = $row['finish'];
if ( $start<$finish ) {
$style = 'style="background-color: red"';
}
echo'<tr>';
echo'<td>'.$row['id'].'</td>';
echo'<td style="width:25%">'.$row['date'].'</td>';
echo'<td class="starttime">'.$row['start'].'</td>';
echo'<td ' . $style . ' class="finishtime">'.$row['finish'].'</td>';
echo'<td>'.$row['total'].'</td>';
echo'<td>'.$row['cash'].'</td>';
echo'</tr>';
}
you do not need to do anything with jQuery, especially NOT within the table

I have not tested, but this should work:
$('tr').each(function() {
var start = $(this).find('.starttime').html(); //assuming this td contains number
var finish = $(this).find('.finishtime').html(); //assuming this td contains number
if(start < finish) $(this).css('background', '#F00');
});

Place your if statement above your echos then use it to create a class in the tr I couldn't tell if you wanted the whole row (tr) red or the specific cell (td) since you mentioned both, but this will style the entire row.
while ($row = mysqli_fetch_assoc($result)) {
$start =$row['start'];
$finish = $row['finish'];
if ( $start<$finish ) {
$bgRed = 'class="bgRed"';
}else{
$bgRed = '';
}
echo'<tr'.$bgRed.'>';
echo'<td>'.$row['id'].'</td>';
echo'<td style="width:25%">'.$row['date'].'</td>';
echo'<td class="starttime">'.$row['start'].'</td>';
echo'<td class="finishtime">'.$row['finish'].'</td>';
echo'<td>'.$row['total'].'</td>';
echo'<td>'.$row['cash'].'</td>';
echo'</tr>';
}
Then style that class appropriately:
.bgRed td {
background-color: red;
}

Related

How to design the data that you gathered from the table

I've printed all the data from the database, but my main problem is how to design my data.
I have a table named post_tbl and columns(post_id,post_message,post_date)
this is my query:
$query = "SELECT `post_id`,`post_message` FROM `post_tbl` ORDER BY `post_date`;
This is how I print in php:
if($query_run = mysql_query($query))
{
while($query_row = mysql_fetch_assoc($query_run))
{
$ex_post_id = $query_row['post_id'];
$ex_post_message = $query_row['user_name'];
$ex_post_date= $query_row['post_date'];
echo $ex_post_message;
}
}
how do I make my ex_post_message have a unfirom border and width using html and css? pls help. thanks
if($query_run = mysql_query($query))
{
while($query_row = mysql_fetch_assoc($query_run))
{
$arrMaster[] = $ex_post_message;
}
}
foreach ($arrMaster as $key => $value)
{
if($i==0)
{
$table1.="<tr>";
foreach ($value as $keyc => $valuec)
{
$table1.="<th>".$keyc."</th>";
}
$table1.="</tr>";
$i=1;
}
$table1.="<tr>";
foreach ($value as $keyc => $valuec)
{
$table1.="<td>".$valuec."</td>";
}
$table1.="</tr>";
}
$table1 .= "</table>";
echo $table1;
at this way you can add any style to your table or any class
Yes you can echo HTML element like:
echo "<div class='classname1'>" . $ex_post_message . "</div>";
Then the class classname1 should handle the design. Like the following:
<style>
.classname1{
width: 100px;
height: 30px;
border: 1px solid blackl
}
</style>
There are many ways to achieve this. The one that I've provided is just an example.

php query - Background color changes

What I want is to change the color of a cell in a table based on the value that is returned by the query.
what I have done is this --
In the style
.priority_1, priority_-1, priority_0{
background-color: green;
color:green;
}
.priority_4, .priority_5, .priority_6, .priority_7, .priority_-4, .priority_-5, .priority_-6, .priority_-7{
background-color: red;
color:red;
}
and in the body - cell
<?php
$result = mysqli_query($con,"SELECT SHOP, FORMAT(VARMP,0) AS value FROM recordstable WHERE SHOP='1' AND Month='1' AND Type='TCheck'");
while($row = mysqli_fetch_array($result)) {
$priority = $row['value'];
echo "<td class=\"priority_{$priority}\"><center>";
echo $priority . "";
}
?>
</td>
this gives me what i want however, what if the value falls outside the range - if i get a value of 43 I want it red. but the .priority will only chnage to red for 7 to -7. how can i do a range easy easy style. without doing priority 1 - 100 plus an minus.
No need to create that many classes. If I've understood correctly, you probably need something like:
CSS:
.priority_green{
background-color: green;
color:green;
}
.priority_red{
background-color: red;
color:red;
}
PHP:
while($row = mysqli_fetch_array($result)) {
$priority = $row['value'];
$class = 'green';
if($priority >= -7 and $priority <= 7){ $class = 'red'; } //from 7 to -7 only
echo "<td class=\"priority_$class\"><center>";
echo $priority . "";
//don't forget to close <center> and <td> and you also don't have rows - just a note
}

applying stylesheet class for dynamic rows

I am trying to apply css class for dynamic rows in php. But its not happening. Here is my code
please someone suggest me where i am going wrong. I have declared a counter for rows but not getting where to increment its count.
<table width='100%' border='0' cellspacing='0' cellpadding='2'>
<tr>
<td class='tddash'>10 latest unpaid customer invoices</td>
</tr>
<tr>
<td valign='top'>";
$SQL = "SELECT salesorders.orderno,
debtorsmaster.name,
custbranch.brname,
salesorders.customerref,
salesorders.orddate,
salesorders.deliverydate,
salesorders.deliverto,
salesorders.printedpackingslip,
salesorders.poplaced,
SUM(salesorderdetails.unitprice*salesorderdetails.quantity*(1-salesorderdetails.discountpercent)/currencies.rate) AS ordervalue
FROM salesorders INNER JOIN salesorderdetails
ON salesorders.orderno = salesorderdetails.orderno
INNER JOIN debtorsmaster
ON salesorders.debtorno = debtorsmaster.debtorno
INNER JOIN custbranch
ON debtorsmaster.debtorno = custbranch.debtorno
AND salesorders.branchcode = custbranch.branchcode
INNER JOIN currencies
ON debtorsmaster.currcode = currencies.currabrev
WHERE salesorderdetails.completed=0
GROUP BY salesorders.orderno,
debtorsmaster.name,
custbranch.brname,
salesorders.customerref,
salesorders.orddate,
salesorders.deliverydate,
salesorders.deliverto,
salesorders.printedpackingslip,
salesorders.poplaced
ORDER BY salesorders.orderno";
$SalesOrdersResult1 = DB_query($SQL,$db);
echo "<table width='100%' celpadding='2' class='selection'><tbody>";
$TableHeader = "<tr><th> Customer </th><th>Order Date</th><th>Delivery Date</th><th>Delivery To</th><th>Order Total</th></tr> ";
$k = 0;
while ($row = DB_fetch_array($SalesOrdersResult1))
{
if ($k == 1){
echo '<tr class="EvenTableRows">';
$k = 0;
} else {
echo '<tr class="OddTableRows">';
$k = 1;
}
$FormatedOrderValue1 = locale_number_format($row['ordervalue'],$row['currdecimalplaces']);
//$TotalOrderValue = $array_sum($FormatedOrderValue1);
//$FormatedOrderValue1 = locale_number_format($myrow['ordervalue'],$_SESSION['CompanyRecord']['decimalplaces']);
$FormatedOrderDate = ConvertSQLDate($row['orddate']);
$FormatedDelDate = ConvertSQLDate($row['deliverydate']);
echo " <td> " . $row['name'] . " </td>";
echo " <td>$FormatedOrderDate</td><td>$FormatedDelDate</td><td> " . $row['deliverto'] . " </td><td>$FormatedOrderValue1</td> ";
}
//echo "<tr><td colspan='3'>Total</td><td colspan='2'>$TotalOrderValue</td></tr></tbody>";
//echo $array_sum($FormatedOrderValue1);
echo "</table>";
echo"</td>
</tr>
</table>
Try to put this inside your while loop.
if ($k == 1){
echo '<tr class="EvenTableRows">';
$k = 0;
} else {
echo '<tr class="OddTableRows">';
$k = 1;
}
If you want decorate your table, and you can use css use it. Don't use server side language
tr:nth-child(even) {background: #CCC}
tr:nth-child(odd) {background: #FFF}
Three issues with your code (four if you count the fact that you have a massive amount of HTML that should be moved outside of being echo'ed within php)
1) You have no $k++; to increment your $k value
2) You have a "tr" within the $row['name'] line that needs to be removed.
3) You need to move the entire if($k) block INSIDE of your while() loop.

PHP - "if" in "while" problems to alternate html table rows colour

$banana=0;
$view = mysql_query('SELECT ......') or die ('Encountered an error.') ;
while($rows3=mysql_fetch_array($view))
{
$total_price2=$rows3['qty']*$rows3['number'];
$banana = $banana + 1;
if ($total_price2!=0)
{
if ($banana %2 ==0)
{
echo "<tr class=\"alt\">";
}
else
{
echo "<tr>";
}
echo "<td>".$rows3['member']."</td>";
echo "<td>".$rows3['payment']."</td>";
echo "<td>$".number_format($total_price2,2)."</td>";
}
echo "</tr>";
}
Problems:
The "banana" alternates the colour of the table row (class="alt") by using modulus (%) to check if banana is a odd number.
Not Working, I see the browser is self-closing the opening <tr> tag.
eg:
<tr><td>person</td><td>data</td><td>$10.00</td></tr>
<tr class="alt"></tr> (Repeats in this fashion)
UPDATE
I have discovered that the reiterating banana always returns ODD NUMBERS: 1 , 3, 5, etc
MySQL is not running correctly
SELECT table1.member, table1.paid, table1.payment,table2.qty,table3.number FROM table1,table2,table3 WHERE table1.member = table2.member AND table1.payment="fruit"
It is giving me wrong data like so:
person1 $10.00
person1 $0.00
person2 $10.00
person2 $0.00
etc
If all else fails, you could just use CSS:
tr {background-color: blue;}
tr:nth-of-type(2n) {background-color: red;}
Try doing this for a debug first:
echo "<tr class=\"alt\"> ";
My guess is that you have no data contained in your <tr> and is being squished to 0 px tall by your browser.
EDIT: I'm not entirely sure giving a class to your <tr> will filter down into the <td> based on certain browser DOM parsing. Whenever I do a zebra row, I'll assign the class to the <td>. I'm no designer by any standard, though. :)
Humor me and try this please:
$banana=0;
$view = mysql_query('SELECT ......') or die ('Encountered an error.') ;
while($rows3=mysql_fetch_array($view))
{
$total_price2=$rows3['qty']*$rows3['number'];
$banana++;
if ($banana % 2 == 0) {
$td_class = "alt";
} else {
$td_class = "";
}
echo "<tr>";
if ($total_price2!=0) {
echo "<td class='{$td_class}'>".$rows3['member']."</td>";
echo "<td class='{$td_class}'>".$rows3['payment']."</td>";
echo "<td class='{$td_class}'>$".number_format($total_price2,2)."</td>";
}
echo "</tr>";
}
That's not going to work. You're only opening the table row if $total_price2!=0. It seems you should only output the closing </tr> tag inside that IF block.
Try this instead:
<?php
$banana=0;
$view = mysql_query('SELECT ......') or die ('Encountered an error.') ;
while($rows3=mysql_fetch_array($view))
{
$total_price2=$rows3['qty']*$rows3['number'];
if ( !$total_price2)
continue;
$banana = $banana + 1;
if ($banana %2 ==0)
{
echo "<tr class=\"alt\">";
}
else
{
echo "<tr>";
}
echo "<td>".$rows3['member']."</td>";
echo "<td>".$rows3['payment']."</td>";
echo "<td>$".number_format($total_price2,2)."</td>";
echo "</tr>";
}

Table tr background color change

I'm trying to make in sort that odd <tr> and even <tr> have different color for easier reading.
Here's my code:
$show_res = mysql_query($show_query);
while ($show_row = mysql_fetch_assoc($show_res)){
$rc++;
if (($rc > 1)){
$tr = '#cccccc';
} else {
$tr = '#ffffff';
}?>
<tr style="background-color:<?php echo $tr ;?>">
It doesn't work, am I missing something?
You want to use modulo %
Some examples:
5 % 2 = remainder is 1
4 % 2 = remainder is 0
6 % 2 = remainder is 0
9 % 2 = remainder is 1
So based on whether the remainder is 1 or 0 you change the color. And you want the <tr> element to be a part of your loop as that's constantly being changed from one color or the other, back and forth.
$show_res = mysql_query($show_query);
while ($show_row = mysql_fetch_assoc($show_res))
{
$rc++;
if ($rc % 2 == 1)
{
$tr = '#cccccc';
}
else
{
$tr = '#ffffff';
?>
<tr style="background-color:<?php echo $tr ;?>">
<?
}
}
?>
$show_res = mysql_query($show_query);
while ($show_row = mysql_fetch_assoc($show_res)){
$rc++;
if (($rc > 1)){
$tr = 'odd';
} else {
$tr = 'even';
}?>
<tr class="<?php echo $tr ;?>">
CSS
.odd td { background-color: #FFF; }
.even td { backgorund-color: #F6F6F6; }
Use CSS and set the color background to the TD not the TR element.
As this is strictly Display, I would recommend doing is using Javascript, not PHP.
Look how simple it can be:
http://paragasu.wordpress.com/2009/01/05/alternate-table-row-color-the-easy-way/
Did you define $rc=0; anywhere?
The issue you're having appears to be that you're not resetting $rc++ after you've incremented it. Try setting it to 0 then reset it after you've incremented it to 1.
$show_res = mysql_query($show_query);
while ($show_row = mysql_fetch_assoc($show_res))
{
if ( isset($k) and $k==1)
{
echo '<tr class="EvenTableRows">';
$k=0;
} else {
echo '<tr class="OddTableRows">';
$k=1;
}
..... more statement
}

Categories