php query - Background color changes - php

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
}

Related

How Do I Change the Background of a Cell based on Value

<?php
try
{
$db = new PDO("sqlite:/var/www/html/mobile_app.db");
print "<table border=1>";
print "<tr><td>Game ID</td><td>Game Name</td><td>Approved Version</td><td>Current Version</td><td>Status</td><td>Date Checked</td><td>Date Changed</td></tr>";
$result = $db->query('SELECT * FROM mobile_version');
foreach($result as $row)
{
print "<tr><td>".$row['gameid']."</td>";
print "<td>".$row['game_name']."</td>";
print "<td>".$row['approved_version']."</td>";
print "<td>".$row['current_version']."</td>";
print "<td>".$row['status']."</td>";
print "<td>".$row['date_checked']."</td>";
print "<td>".$row['date_changed']."</td>";
}
print "</table>";
$db = NULL;
}
catch(PDOException $e) {
echo $e->getMessage();
}
?>
My aim is to change just the cell with the data in column 'status'. Red if it equals UNAPPROVED and green if it equals APPROVED. Any help would be awesome! Data is in a single table in an sqlite3 database.
You can do it by adding the class to td and add style to that class.
In PHP
print "<td class=".$row['status'].">".$row['status']."</td>";
In CSS
td.APPROVED {
background-color: green;
color: white;
}
td.UNAPPROVED {
background-color: red;
color: white;
}

PHP script printing div

Is there a possible way to multiply code and print it out using PHP?
I am trying to make a script that checks users and draws a line depending on count.
CSS Code
#outer { // gray backround - always on.
background-color: #401800;
width: 150px;
height: 6px;
}
.inner { // used if there's less than 100 ppl online
width: 1px;
height: 6px;
display:block;
float:left;
}
.bigger { // used if 100+ people are online
width: 10px;
height: 6px;
display:block;
float:left;
}
}
.bigger.trys { // color for small px
background-color: green;
}
.inner.vienas { //color for big px
background-color: green;
}
PHP code ($rows = counted users):
if {$rows <= "0"
echo"<span class="inner vienas"></span>"; //draws small 1px width bar
}
elseif{$rows >= "10"
echo"<span class="inner vienas"></span><span class="inner vienas"> </span>"; // count =10 or greater - draws x2 bars.
}
elseif{$rows >= "100" // if users are 100 OR more counts new variable
$padala = $rows/10 //variable is users/10
echo 10*"<span class="bigger trys"></span>") //i want to print this out as 1px each 10 users
}
else {
echo "Script error";
}
Is there a way to do that?
Basicly you have lot of syntax error.
First, if statement need a condition:
Wrong:
if {
$rows <= "0"
echo"<span class="inner vienas"></span>"; //draws small 1px width bar
}
Correct (i don't know if $row have an integer or string, but if you use numbers, the correct is to storage an integer):
if ($rows <= 0) {
echo '<span class="inner vienas"></span>';
}
Second, echo:
You have an error with double quote marks when inserting HTML code, you don't have to cut the echo
Wrong:
echo"<span class="inner vienas"></span>";
Correct (using simple quotes):
echo '<span class="inner vienas"></span>';
Another error is that echo*10, you need to use a for loop.
for($i=0; $i<=9; $i++) {
echo '<span class="bigger trys"></span>';
}
Btw, I don't understand what you want, but the correct maybe is this:
if ($rows <= 0) {
echo '<span class="inner vienas"></span>';
} else if ($rows >= 10) {
echo '<span class="inner vienas"></span><span class="inner vienas"> </span>';
} else if ($rows >= 100) {
$padala = $rows / 10
for($i=0; $i<=9; $i++) {
echo '<span class="bigger trys"></span>';
}
} else {
echo "Script error";
}
Here you can read more about if statements and for:
PHP Manual - If Statements
PHP W3Schools - For loops
Without knowing more about your data, I'm not sure how I would go about doing what you want to accomplish. However, Here is your above code with the syntax fixes.
The conditions in the if statements should be wrapped in Parentheses, and the double quotes in the strings after the the echo functions should be escaped
if ($rows <= "0") {
echo"<span class=\"inner vienas\"></span>"; //draws small 1px width bar
} elseif($rows >= "10") {
echo"<span class=\"inner vienas\"></span><span class=\"inner vienas\"> </span>"; // count =10 or greater - draws x2 bars.
} elseif($rows >= "100") { // if users are 100 OR more counts new variable
$padala = $rows/10 //variable is users/10
echo 10*"<span class=\"bigger trys\"></span>") //i want to print this out as 1px each 10 users
} else {
echo "Script error";
}

div and class not showing up

I'm testing a barcode on my website but for some reason the div with the class="b128" which stands for the barcodes is not showing up. If I test the same source code on another website it does show up but with different styling.
Does anyone know how I can make this source code show up and keep the original styling at the same time?
<style>
div.b128{
border-left: 1px black solid;
height: 60px;
}
</style>
<?php
global $char128asc,$char128charWidth;
$char128asc=' !"#$%&\'()*+,-./0123456789:;<=>?#ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~';
$char128wid = array(
'212222','222122','222221','121223','121322','131222','122213','122312','132212','221213', // 0-9
'221312','231212','112232','122132','122231','113222','123122','123221','223211','221132', // 10-19
'221231','213212','223112','312131','311222','321122','321221','312212','322112','322211', // 20-29
'212123','212321','232121','111323','131123','131321','112313','132113','132311','211313', // 30-39
'231113','231311','112133','112331','132131','113123','113321','133121','313121','211331', // 40-49
'231131','213113','213311','213131','311123','311321','331121','312113','312311','332111', // 50-59
'314111','221411','431111','111224','111422','121124','121421','141122','141221','112214', // 60-69
'112412','122114','122411','142112','142211','241211','221114','413111','241112','134111', // 70-79
'111242','121142','121241','114212','124112','124211','411212','421112','421211','212141', // 80-89
'214121','412121','111143','111341','131141','114113','114311','411113','411311','113141', // 90-99
'114131','311141','411131','211412','211214','211232','23311120' ); // 100-106
////Define Function
function bar128($text) { // Part 1, make list of widths
global $char128asc,$char128wid;
$w = $char128wid[$sum = 104]; // START symbol
$onChar=1;
for($x=0;$x<strlen($text);$x++) // GO THRU TEXT GET LETTERS
if (!( ($pos = strpos($char128asc,$text[$x])) === false )){ // SKIP NOT FOUND CHARS
$w.= $char128wid[$pos];
$sum += $onChar++ * $pos;
}
$w.= $char128wid[ $sum % 103 ].$char128wid[106]; //Check Code, then END
//Part 2, Write rows
$html="<table cellpadding=0 cellspacing=0><tr>";
for($x=0;$x<strlen($w);$x+=2) // code 128 widths: black border, then white space
$html .= "<td><div class=\"b128\" style=\"border-left-width:{$w[$x]};width:{$w[$x+1]}\"></div>";
return "$html<tr><td colspan=".strlen($w)." align=center><font family=arial size=2><b>$text</table>";
}
?>
<?php
$test = "12345";
echo '<div style="border:3px double #ababab; padding:5px;margin:5px auto;width:135px;">';
echo bar128(stripslashes($test));
echo '</div>';
?>
You aren't closing most of your tags. Try this:
$html = "<table cellpadding=0 cellspacing=0><tr>";
for ($x = 0; $x < strlen($w); $x += 2) // code 128 widths: black border, then white space
$html .= "<td><div class=\"b128\" style=\"border-left-width:{$w[$x]};width:{$w[$x+1]}\"></div></td>";
return "$html</tr><tr><td colspan=" . strlen($w) . " align=center><font family=arial size=2><b>$text</b></font></td></tr></table>";

Change background color of TD

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;
}

PHP user account type highlighting in CSS

I have some problem getting the user account type from db.
I create a column on db called acc_type, it checks if user acc_type is 1 or greater highlight the user name.
I wrote this code but it highlights all the username as red admin.
colors meaning:
Blue = Regular member = 0
Red = admin = 1
Green = Mod = 2
Orange = Super User = 3
Code here
<?php
if($dn5['acc_type'] =='0')
{
echo '<style>
.acc{
color:blue;
}
</style>';
}else if($dn5['acc_type']=='1'){
echo '<style>
.acc{
color:red;
}
</style>';
}else if($dn5['acc_type']=='2'){
echo '<style>
.acc{
color:green;
}
</style>';
}
?>
<span class="acc"><?php echo $username;?></span>
Have you checked what values you're passing into $dn5['acc_type']?
As well, your code is rather repetitive, you could simplify it greatly with something like
switch($dn5['acc_type']) {
case 1: $color = 'red'; break;
case 2: $color = 'green'; break;
default: $color = 'blue';
}
echo "<style>.acc { color: $color; }</style>";

Categories