Color table row by status - php

I wanna do if in by my status field.
if my status is "blocked" so the font background color will be in red and if the status is "authorized" so in green.
how should i do that?
foreach($data as $row)
{
$output .= '
<tr>
<td>'.$row->client_name.'</td>
<td>'.$row->adrress.'</td>
<td>'.$row->occupation.'</td>
<td>'.$row->payeee.'</td>
<td>'.$row->status.'</td>
</tr>
';
}

Try this
foreach($data as $row)
{
$output .= '
<tr>
<td>'.$row->client_name.'</td>
<td>'.$row->adrress.'</td>
<td>'.$row->occupation.'</td>
<td>'.$row->payeee.'</td>
#if($row->status == "blocked")
<td> <button type="button" class="btn btn-danger">blocked</button> </td>
#elseif($row->status == "authorized")
<td> <button type="button" class="btn btn-success">authorized</button> </td>
#endif
</tr>
OR
foreach($data as $row){
$output .= '
<tr>
<td>'.$row->client_name.'</td>
<td>'.$row->adrress.'</td>
<td>'.$row->occupation.'</td>
<td>'.$row->payeee.'</td>';
if($row->status == "bad"){
$output .= '<td style="background-color:red;">'.$row->status.'</td>';
} else if($row->status == "good") {
$output .= '<td style="background-color:green;">'.$row->status.'</td>';
}
$output .= '</tr>';
}

You shouldn't really be dealing with view logic in your controller.
But to do what you want in the controller
foreach($data as $row){
$output .= '
<tr>
<td>'.$row->client_name.'</td>
<td>'.$row->adrress.'</td>
<td>'.$row->occupation.'</td>
<td>'.$row->payeee.'</td>';
if($row->status == "bad"){
$output .= '<td style="background-color:red;">'.$row->status.'</td>';
} else if($row->status == "good") {
$output .= '<td style="background-color:green;">'.$row->status.'</td>';
}
$output .= '</tr>';
}
However what you should do is handle it in your blade file. Also you probably want to create styles in your style sheet and apply classes rather than inline the css.

this is the code that worked
foreach($data as $row){
$output .= '
<tr>
<td>'.$row->client_name.'</td>
<td>'.$row->adrress.'</td>
<td>'.$row->occupation.'</td>
<td>'.$row->payeee.'</td>';
if($row->status == "bad"){
$output .= '<td style="background-color:red;">'.$row->status.'</td>';
} else if($row->status == "good") {
$output .= '<td style="background-color:green;">'.$row->status.'</td>';
}
$output .= '</tr>';
}

Related

Display image depending of result in MySQL query, using PHP

I need to change a value depending a result.
In this case, if my row "status" is "Active" I want to put something like that in a table:
<td>'.$row['register_status'].'</td>
<td>'if($row['status']=='Active'){print "<span class="glyphicon glyphicon-time" aria-hidden="true" style="color:orange"></span>";}'</td>
<td>'.$row['register_status'].'</td>
What is wrong?
Edit: Sorry. I did not put all code. Here starting my array and I put with quotes your code but can't run
if(mysql_num_rows($result) > 0)
{
$number = 1;
while($row = mysql_fetch_assoc($result))
{
$data .= '<tr>
<td>'.$row['register_status'].'</td>
<td>'
if($row['status']=='Active')
{
echo '<span class="glyphicon glyphicon-time" aria-hidden="true" style="color:orange"></span>';
}
'</td>
</tr>';
$number++;
}
}
else
{
// records now found
$data .= '<tr><td colspan="6">Records not found!</td></tr>';
}
$data .= '</table>';
echo $data;

Select all from table and display result mysql

<?php
$query_announcement = mysql_query("SELECT * FROM announcement");
$get_announcement = mysql_fetch_assoc($query_announcement);
?>
<?php
if(mysql_num_rows($query_announcement) > 0){
$row = mysql_num_rows($query_announcement);
for($x = 1; $x<= $row; $x++){
$html = '<tr>';
$html .= '<td>'.$get_announcement['id'].'</td>';
$html .= '<td>'.$get_announcement['announce_name'].'</td>';
$html .= '<td>'.$get_announcement['announce_description'].'</td>';
$html .= '<td>'.$get_announcement['announce_location'].'</td>';
$html .= '<td>'.date('M d,Y', strtotime($get_announcement['date_start'])).'</td>';
$html .= '<td>'.date('M d,Y', strtotime($get_announcement['date_end'])).'</td>';
$html .= '<td>'.date('M d,Y', strtotime($get_announcement['date_added'])).'</td>';
$html .= '<td width="15%">
<button type="button" class="btn btn-default" title="Edit"><span class="fa fa-pencil"></span></button>
<button type="button" class="btn btn-default" title="Delete" ><span class="fa fa-trash-o"></span></button>
</td>';
$html .= '</tr>';
echo $html;
}
}
else{
echo '<tr><td colspan="8" align="center"><h3>No Announcement</h3></td></tr>';
}
?>
/this is my code but when i run it i only get the announcement with the same id, not all announcement that i created. please help thanks
You are looping the announcement but are not then pulling the next announcement.
for($x = 1; $x<= $row; $x++){
//Pull next record
$row = mysql_num_rows($query_announcement);
If you change it to the code above the for loop will start. The data will be pulled, displayed and then it will loop back around again.
your r fetching data only one time
for($x = 1; $x<= $row; $x++){
$get_announcement= mysql_fetch_assoc($query_announcement);
Try this: You need to loop through the rows to and use the each row to display them.
<?php
$query_announcement = mysql_query("SELECT * FROM announcement");
$get_announcement = mysql_fetch_assoc($query_announcement);
?>
<?php
if($get_announcement){
while($row = mysql_fetch_array($get_announcement)) {
$html = '<tr>';
$html .= '<td>'.$row['id'].'</td>';
$html .= '<td>'.$row['announce_name'].'</td>';
$html .= '<td>'.$row['announce_description'].'</td>';
$html .= '<td>'.$row['announce_location'].'</td>';
$html .= '<td>'.date('M d,Y', strtotime($row['date_start'])).'</td>';
$html .= '<td>'.date('M d,Y', strtotime($row['date_end'])).'</td>';
$html .= '<td>'.date('M d,Y', strtotime($row['date_added'])).'</td>';
$html .= '<td width="15%">
<button type="button" class="btn btn-default" title="Edit"><span class="fa fa-pencil"></span></button>
<button type="button" class="btn btn-default" title="Delete" ><span class="fa fa-trash-o"></span></button>
</td>';
$html .= '</tr>';
echo $html;
}
}
else{
echo '<tr><td colspan="8" align="center"><h3>No Announcement</h3></td></tr>';
}

php / MySQL - How to join 'cart' table together

How can I join this table together, showing the header only once? I have tried to take th out of the while loop but I had no luck, maybe I made a mistake?
Here is my code so far:
<?php
function cart() {
foreach($_SESSION as $name => $value) {
if ($value>0){
if (substr($name, 0, 5)=='cart_') {
$id = substr($name, 5, (strlen($name)-5));
$get = mysql_query('SELECT id, name, price FROM products WHERE id='.mysql_real_escape_string((int)$id));?>
<center>
<table class='menufinal' border=0 width=75%>
<th>Remove Item</th>
<th>Item Name</th>
<th>Item Price</th>
<th>Quantity</th>
<th>Line Total</th>
<?php while ($get_row = mysql_fetch_assoc($get)) {
$sub = $get_row['price']*$value;?>
<tr>
<td><?echo '<img src="x.png"><br>'?></td>
<td><?echo $get_row['name']?></td>
<td><?echo '&pound' . number_format($get_row['price'], 2);?></td>
<td><?echo '- ' .$value. ' +' ?> </td>
<td> <?echo '&pound ' . number_format($sub, 2);?> </td>
</tr>
<?
}
}
if (empty($total)) {
if (empty($sub)) {
//do nothing
} else {
$total = $sub;
}
} else {
$total += $sub;
}
}
}
if (!empty($total)){
echo '<br>Total: &pound' . number_format($total, 2) . '<br>';
echo '<div id="dorc"><p><img src="dishes.png" width="240" height="152"> <img src="spacer.png" width="200"> <img src="checkout.png" width="240" height="152">';
} else {
header ('Location: index.php');
}
}
?>
Currently this code displays:
Change the script as follow. Hope it will work
$value) {
if ($value>0){
if (substr($name, 0, 5)=='cart_') {
$id = substr($name, 5, (strlen($name)-5));
$get = mysql_query('SELECT id, name, price FROM products WHERE id='.mysql_real_escape_string((int)$id));?>
if($i==0)
{
print("
<center>
<table class='menufinal' border=0 width=75%>
<th>Remove Item</th>
<th>Item Name</th>
<th>Item Price</th>
<th>Quantity</th>
<th>Line Total</th>
</tr>
");
}
$i++;
while ($get_row = mysql_fetch_assoc($get)) {
$sub = $get_row['price']*$value;?>
<tr>
<td><?echo '<img src="x.png"><br>'?></td>
<td><?echo $get_row['name']?></td>
<td><?echo '&pound' . number_format($get_row['price'], 2);?></td>
<td><?echo '- ' .$value. ' +' ?> </td>
<td> <?echo '&pound ' . number_format($sub, 2);?> </td>
</tr>
<?
}
}
if (empty($total)) {
if (empty($sub)) {
//do nothing
} else {
$total = $sub;
}
} else {
$total += $sub;
}
}
}
if (!empty($total)){
echo '<br>Total: &pound' . number_format($total, 2) . '<br>';
echo '<div id="dorc"><p><img src="dishes.png" width="240" height="152"> <img src="spacer.png" width="200"> <img src="checkout.png" width="240" height="152">';
} else {
header ('Location: index.php');
}
}
?>
Below is the corrected code, the creation of the table is moved outside of the loop.
Also the table header was added into a tr to create valid html, and also collected data into an $output variable to prevent a bug with your location redirect since data would already be written to the browser (you may vary well still have a problem with this since there is likely other output prior to this cart() function being called.
<?php
function cart() {
$output = '';
$output .= '<center>';
$output .= '<table class='menufinal' border=0 width=75%>';
$output .= '<tr>';
$output .= '<th>Remove Item</th>';
$output .= '<th>Item Name</th>';
$output .= '<th>Item Price</th>';
$output .= '<th>Quantity</th>';
$output .= '<th>Line Total</th>';
$output .= '</tr>';
foreach($_SESSION as $name => $value) {
if ($value>0){
if (substr($name, 0, 5)=='cart_') {
$id = substr($name, 5, (strlen($name)-5));
$get = mysql_query('SELECT id, name, price FROM products WHERE id='.mysql_real_escape_string((int)$id));
while ($get_row = mysql_fetch_assoc($get)) {
$sub = $get_row['price']*$value;
$output .= '<tr>';
$output .= '<td><img src="x.png"><br></td>';
$output .= '<td>' . $get_row['name'] . '</td>';
$output .= '<td>&pound ' . number_format($get_row['price'], 2) . '</td>';
$output .= '<td>- ' .$value. ' +</td>';
$output .= '<td>&pound ' . number_format($sub, 2) . '</td>';
$output .= '</tr>';
}
}
if (empty($total)) {
if (empty($sub)) {
//do nothing
} else {
$total = $sub;
}
} else {
$total += $sub;
}
}
}
$output .= '</table>';
if (empty($total)){
header ('Location: index.php');
exit;
}
$output .= '<br>Total: &pound' . number_format($total, 2) . '<br>';
$output .= '<div id="dorc"><p><img src="dishes.png" width="240" height="152"> <img src="spacer.png" width="200"> <img src="checkout.png" width="240" height="152">';
echo $output;
}
?>

How do I concatenate html and php?

I currently have this:
$output .= '
<tr>
<th style="text-align: left">'.(__("Item", "WSPSC")).'</th><th>'.(__("Quantity", "WSPSC")).'</th><th>'.(__("Price", "WSPSC")).'</th><th></th>
</tr>';
But I need to replace Item with a piece of php like:
$output .= '
<tr>
<th style="text-align: left">'.(__("
<?php if(ICL_LANGUAGE_CODE=='en'); ?>
Item
<?php elseif(ICL_LANGUAGE_CODE=='it'); ?>
Products
<?php endif; ?>
", "WSPSC")).'</th><th>'.(__("Quantity", "WSPSC")).'</th><th>'.(__("Price", "WSPSC")).'</th><th></th>
</tr>';
The issue I am having is that this is obviously wrong but I can't get my head around the correct concatenation of html and php
If I right understand you need something like this:
$output .= '
<tr>
<th style="text-align: left">'.
(
(ICL_LANGUAGE_CODE=='en')?
'Item':
( (ICL_LANGUAGE_CODE=='it')? 'Products': '' ),
"WSPSC"
)
.'</th><th>'.(__("Quantity", "WSPSC")).'</th><th>'.(__("Price", "WSPSC")).'</th><th></th>
</tr>';
You should not use it in that way. Look at this pseudo code:
$output .= '
<tr>
<th style="text-align: left">';
if (something...) {
$output.= 'sss';
}
elseif (something...) {
$output.= 'ddd';
}
That's the way you should do it.
Your replies helped me thanks, this worked:
$output .= '
<tr>
<th style="text-align: left">';
if (ICL_LANGUAGE_CODE=='en') {
$output .= (__("Item", "WSPSC"));
} elseif (ICL_LANGUAGE_CODE=='it') {
$output .= (__("PRODOTTO", "WSPSC"));
}
$output .= '</th><th>'.(__("Quantity", "WSPSC")).'</th><th>'.(__("Price", "WSPSC")).'</th><th></th>
</tr>';

Table max 3 <td> loop in other tr

How to make it like only 3 offers in row NOW I have one row and all offer is in one line I just want to to show max 3 td in tr and loop.
if(count($offer_list['item']) > 0)
{
$main_content .= '<table cellspacing="2" cellpadding="2" width=30>';
foreach($offer_list['item'] as $item)
{
$main_content .= '<td class="shop">
<a class="shop" style="display: block;" href="/shop/item/id/'.$item['id'].'">
<center><img src="item_images/'.$item['item_id'].'.gif"><br><font color="white"><b>'.$item['name'].'</font></b><font color="red"> <b> '.$item['points'].' points</font></b>';
if(!$logged)
{
$main_content .= '<p><b>[Login to buy]</b>';
}
else
{
$main_content .= '<br><b><font size=1 color="white">Your points balance is: '.$user_premium_points;
$main_content .= '<br><font color="white"> <fieldset class="shop"><legend>DESCRIPTION</legend>'.$item['description'].'</fieldset></font><br><font color="white"><b> [Click to buy] </b></font></form> </a></center></td>';
}
$main_content .= '';
}
$main_content .= '</tr></table>';
}
$row_count = 1;
$main_content .= '<table cellspacing="2" cellpadding="2" width=30>';
foreach($offer_list['item'] as $item) {
// Check, if we are at position "1". If so, start new row
if ($row_count === 1) { $main_content .= '<tr>'; }
$main_content .= '<td class="shop"><a class="shop" style="display: block;" href="/shop/item/id/'.$item['id'].'">
<center><img src="item_images/'.$item['item_id'].'.gif"><br><font color="white"><b>'.$item['name'].'</font></b><font color="red"> <b> '.$item['points'].' points</font></b>';
if(!$logged) {
$main_content .= '<p><b>[Login to buy]</b>';
}
else {
$main_content .= '<br><b><font size=1 color="white">Your points balance is: '.$user_premium_points;
$main_content .= '<br><font color="white"> <fieldset class="shop"><legend>DESCRIPTION</legend>'.$item['description'].'</fieldset></font><br><font color="white"><b> [Click to buy] </b></font></form> </a></center></td>';
}
// Check, if we we are at position "3". If so, end row. (set position back to 1)
// Otherwise increase position with one level.
if ($row_count === 3) {
$row_count = 1;
$main_content .= '</tr>';
}
else {
$row_count++;
}
} // end foreach
// This is code for filling the "blank" table cells, if you don't have exactly some item count / 3 = 0
if ($row_count !== 3) {
for ($i=0; $i<=(3-$row_count); $i++) {
$main_content .= '<td> </td>';
}
$main_content .= '</tr>';
}
$main_content .= '</table>';
PS: <center> is deprecated or more to say not valid HTML anymore.

Categories