json and php If, else statement - php

This php code generates a table with some information like: temperature, water health, ph level and other things ...
if (count($auxGateway->getMaquina()) > 0)
{
foreach($auxGateway->getMaquina() as $maq)
{
echo '<div class="cont-maq-uni">';//Cria a div para cada maquina
echo ' <table class="maq">';//Cria uma tabela
echo ' <tr>';
echo ' <td colspan="2">';
echo ' <h2 class="maq-head">'.$maq->getNome().'</h2>';
echo ' </td>';
echo ' </tr>';
foreach($maq->getVariavel() as $var)
{
echo ' <tr class="maq">';
echo ' <td class="maq">';
echo ' <p class="n_font maq">'.$var->getNome().'</p>';
echo ' </td>';
echo ' <td class="maq">';
echo ' <p class="n_font maq">'.(floatval($var->getValor()) * floatval($var->getMultiplicador()) + floatval($var->getSomador())).' '.$var->getUnidadeValor().'</p>';
echo ' </td>';
echo ' </tr>';
}
echo ' </table>';
echo '</div>';
}
}
so I need to create a code to show some images if that information is bigger or less. For example: if 'temperature' <30 then it will show the image (notgood.jpeg). The problem is: how do I name these values since they are inside the MYSQL database? How will I name each row in the table?
Thank you guys!

You can use if..else.. or ternary operators
echo '<img src="' . ($var->getTemperature() < 30 ? 'notgood.jpeg' : 'good.jpeg') . '">' # using ternary operators;
This will give, if $var->getTemperature() < 30 <img src="notgood.jpeg">
else <img src="good.jpeg">

Related

PHP leaderboard not showing the correct way

I am making a leaderboard with persons out of my database. My website is build in PHP (PDO) the problem is that I cant get the users out of the database to show the correct way under each other.
The function I use to get info out of the database is:
public function get_klanten(){
$getKlant = $this->database->query("SELECT * FROM klanten ORDER BY id ASC LIMIT 1");
$klanten = $this->database->resultset();
return $klanten;
}
How it is now
The problem I get is that it puts every records after each other and not on a new line that why I added LIMIT 1 but I want that the second user in this case Amet also be out of the database.
The HTML with this is a different file and is the code down below.
<thead>
<tr>
<th>#</th>
<th>Lid</th>
<th><span class="glyphicon glyphicon-sort-by-attributes" aria-hidden="true"></span></th>
<th><span class="glyphicon glyphicon-ok" aria-hidden="true"></span></th>
</tr>
</thead>
<tbody>
<tr>
<?php
$klantten = $app->get_klanten();
foreach ($klantten as $klant) {
echo '<td>' . $klant['id'] . ' </td>';
echo '<td>' . $klant['voornaam'] . ' ' . $klant['achternaam'] . ' </td>';
echo '<td>' . $klant['punten'] . ' </td>';
echo '<td>' . $klant['punten'] . ' </td>';
}
?>
</tr>
<tr>
<td>2</td>
<td>amet</td>
<td>456</td>
<td>52</td>
</tr>
BONUS
<div class="col-md-6">
<div class="well dash-box">
<h2><span class="glyphicon glyphicon-list-alt" aria-hidden="true"></span> Stel jezelf voor</h2>
<h5> Laat wetn wie jij en je business zijn</h5>
</div>
</div>
<div class="col-md-6">
<div class="well dash-box">
<h2><span class="glyphicon glyphicon-list-alt" aria-hidden="true"></span> 12</h2>
<?php
$toppics = $app->get_topics();
$i = 0;
foreach ($toppics as $topic) {
echo '' . $topic['onderwerp'] . '';
}
?>
</div>
</div>
Function:
public function get_topics(){
$getTopic = $this->database->query("SELECT * FROM topics ORDER BY id DESC");
$topics = $this->database->resultset();
return $topics;
}
I want this also to be each record in a new block. So each topic is a new block. You can see now 2 blocks I want each block to get a different record out of the database
1: https://i.stack.imgur.com/lH8oz.png
Remove limit 1 from query, then move your TR tag so it's inside your foreach loop.
<?php
$klantten = $app->get_klanten();
foreach ($klantten as $klant) {
echo '<tr>';
echo '<td>' . $klant['id'] . ' </td>';
echo '<td>' . $klant['voornaam'] . ' ' . $klant['achternaam'] . ' </td>';
echo '<td>' . $klant['punten'] . ' </td>';
echo '<td>' . $klant['punten'] . ' </td>';
echo '</tr>';
}
?>

Php change color one cell text from stored proc

I use sql server stored proc where one column send me information about color, but i need to apply it one an other column but more exactly on one cell and not the complete row. For now, what i can do it's on the row only and i need to do it exclusively on the red one. Here's my code for now :
while ($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC))
{
//print_r( $row ); // debug code
$couleur='black';
$font= 'normal';
$font2= '#transparent';
if( strstr($row['HTML_CODE'], "BOLD()")){
$font= 'bold';
}
if( strstr($row['HTML_CODE'], "BG()")){
$font2= '#D8D8D8';
}
if( strstr($row['HTML_CODE'], "RED()")){
$couleur='red';
}
?>
<tbody>
<tr>
<?php echo "<tr style=\"font-weight:$font; color:$couleur; background-color:$font2;\">"; ?>
<td><?php echo ($row['TextLine']);?></td>
<td style="text-align: right"><?php echo is_null($row['TotalQty']) ? ' ' : number_format($row['TotalQty'],0,'',' ');?></td>
<td style="text-align: right"><?php echo is_null($row['TotalAmountDrawer']) ? ' ' : number_format($row['TotalAmountDrawer'],2, ".", " ") . " $";?></td>
<td style="text-align: right"><?php echo is_null($row['TotalAmountPickup']) ? ' ' : number_format($row['TotalAmountPickup'],2, ".", " ") . " $";?></td>
<td style="text-align: right"><?php echo is_null($row['TotalAmountOverShort']) ? ' ' : number_format($row['TotalAmountOverShort'],2, ".", " ") . " $";?></td>
</tr>
</tbody>
<?php
}
?>
I need to apply it on the column name : TotalAmountOverShort only!
Thank you for your help, it's really appreciate!
Change the CSS color rule from TR to TD.
If the background color also applies to only one cell, do the same with background-color:
<tr>
<?php echo "<tr style=\"font-weight:$font; background-color:$font2;\">"; ?>
<td><?php echo ($row['TextLine']);?></td>
<td style="text-align: right"><?php echo is_null($row['TotalQty']) ? ' ' : number_format($row['TotalQty'],0,'',' ');?></td>
<td style="text-align: right"><?php echo is_null($row['TotalAmountDrawer']) ? ' ' : number_format($row['TotalAmountDrawer'],2, ".", " ") . " $";?></td>
<td style="text-align: right"><?php echo is_null($row['TotalAmountPickup']) ? ' ' : number_format($row['TotalAmountPickup'],2, ".", " ") . " $";?></td>
<td style="text-align: right; color: <?php echo $couleur; ?>"><?php echo is_null($row['TotalAmountOverShort']) ? ' ' : number_format($row['TotalAmountOverShort'],2, ".", " ") . " $";?></td>
</tr>

List data on page2 from a link on page1

page1, the code, below, works fine and lists recipe names i.e. BBQ Pork etc
<form action="recipe_show.php" method="post">
<?php
$result = mysql_query("SELECT recipe_name FROM recipes ORDER BY recipe_name ASC"); // Run the query
if ($result) { // If it ran OK, display the records
// Table header
echo '<table>
<td align="left"><b>Recipe Name</b></td>
</tr>';
// Fetch and print all the records
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo '<tr>
<td>'. $row['recipe_name'] .'</td>
</tr>';
}
echo '</table>'; // Close the table
mysql_free_result ($result); // Free up the resources
} else { // If it did not run OK
// Message
echo '<p class="error">The current recipe_name could not be retrieved. We apologize for any inconvenience.</p>';
// Debugging message
echo '<p>' . mysql_error($dbcon) . '<br><br />Query: ' . $q . '</p>';
} // End of if ($result)
mysql_close($dbcon); // Close the database connection.
?></p>
</form>
</div>
When I click the link, from page1, it opens page2. I get the headings but not the data for the BBQ Pork link. Below is the code on page2.
<form action="" method="post">
<?php
$recipe = mysql_real_escape_string($_GET['recipe_name']); //if using mysql
$myresult = "SELECT * FROM recipes WHERE recipe_name = '".$recipe. "'";
$num = mysql_num_rows($myresult);
for ($i = 0; $i < $num; $i++){
$row = mysql_fetch_array($myresult, MYSQL_ASSOC);
$row = (($i % 2) == 0) ? "table_odd_row" : "table_even_row";
echo "<tr row=".$row.">";
}
if ($myresult) { // If it ran OK, display the records
echo '<table>
<td width="250" align="center"><b>Recipe Name</b></td>
<td width="250" align="left"><b>Instructions</b></td>
<td width="250" align="left"><b>Directions</b></td>
<td width="250" align="left"><b>Notes</b></td>
</tr>';
while ($row = mysql_fetch_array($myresult, MYSQL_ASSOC)) {
echo '<tr>
<td align="left">' . $row['recipe_name'] . '</td>
<td align="left">' . $row['ingredients'] . '</td>
<td align="left">' . $row['directions'] . '</td>
<td align="left">' . $row['notes'] . '</td>
</tr>';
}
echo '</table>'; // Close the table
mysql_free_result ($myresult); // Free up the resources
} else {
echo '<p row="error">The current Recipe could not be retrieved. We apologize for any inconvenience.</p>';
echo '<p>' . mysql_error($dbcon) . '<br><br />Query: ' . $q . '</p>';
} ($myresult)
mysql_close($dbcon); // Close the database connection.
?>
Hope I have correctly set this out? Many thanks, in advance, for help and guidence

What's wrong with this php-generated table?

I am trying to make a table via PHP, but when I load this, it displays it like this..
The code:
<table border="1" cellpadding="5">
<?php
while($test= mysql_fetch_assoc($countquery)){
echo '<tr><td>';
echo $test["count"];
echo 'x</td>';
};
while($row=mysql_fetch_array($topresult)) {
echo '<td width="150">';
echo $row["productnaam"];
echo '</td><td width="100" style="text-align:center;">€ ';
echo $row["prijs"];
echo '</td><td width="50" style="text-align:center;">';
echo '<a style="text-decoration:none;color:red;" href="#"><img width="25" src="trash.png"></a>';
echo '</td></tr>';
};
?>
</table>
My goal is to display a table of 4 columns by 3 rows..
EDIT:
Found it already, it makes a new <tr> tag everytime the first while is performed.
The block :
while($test= mysql_fetch_assoc($countquery)){
echo '<tr><td>';
echo $test["count"];
echo 'x</td>';
};
will create 3 cells 1x, 1x, 2x first,
next the block:
while($row=mysql_fetch_array($topresult)) {
echo '<td width="150">';
echo $row["productnaam"];
echo '</td><td width="100" style="text-align:center;">€ ';
echo $row["prijs"];
echo '</td><td width="50" style="text-align:center;">';
echo '<a style="text-decoration:none;color:red;" href="#"><img width="25" src="trash.png"></a>';
echo '</td></tr>';
};
will create cells from Monitor, so the result become like this.
To fix it, you should save the result from the first while loop to an array and go through at the 2nd while loop
You are echoing nested rows.
while($test= mysql_fetch_assoc($countquery)){
echo '<tr><td>';// Here you open a row
echo $test["count"];
echo 'x</td>';//No closing of row, you close the td and open another tr on the next iteration
};

Order list/ while loop php issue

I have put together a basic order list for admin users in php for checking order contents placed by logged in users.
The aim of this script is to retrieve the order details (item, quantity, price) as well as the user’s first name and surname (where ‘Order for:’ is).
The script below does everything ok in that it retrieves the order (and orders if there are more than one) and it’s/their item, quantity and price.
However, it doesn’t display the user’s name and surname.
I know the problem is that where I am trying to display the name is outside the while loop but Im a little stuck in where it should sit. Any suggestions? Code is below:
<?php
$page_title = 'View Individual Order';
include ('includes/header.html');
// Check for a valid user ID, through GET or POST.
if ( (isset($_GET['id'])) && (is_numeric($_GET['id'])) )
{ // Accessed through view_users.php
$id = $_GET['id'];
} elseif ( (isset($_POST['id'])) && (is_numeric($_POST['id'])) )
{ // Form has been submitted.
$id = $_POST['id'];
} else { // No valid ID, kill the script.
echo '<h1 id="mainhead">Page Error</h1>
<p class="error">This page has been accessed in error.</p><p><br /><br /></p>';
include ('./includes/header.html');
exit();
}
?>
<h1>Order Details</h1>
<?php
require_once ('database.php'); // Connect to the db.
// Retrieve the user's, order and product information.
$query = "SELECT us.users_id, us.users_sales_id, us.users_first_name, us.users_surname, us.users_dealer_name,
ord.order_id, ord.users_id, ord.total, ord.order_date,
oc.oc_id, oc.order_id, oc.products_id, oc.quantity, oc.price,
prd.products_id, prd.products_name, prd.price
FROM users AS us, orders AS ord, order_contents AS oc, products AS prd
WHERE ord.order_id=$id
AND us.users_id = ord.users_id
AND ord.order_id = oc.order_id
AND oc.products_id = prd.products_id
";
$result = mysql_query ($query) or die(mysql_error());
if (mysql_num_rows($result)) { // Valid user ID, show the form.
echo '<p>Order for:<strong>' . $row[2] . ' ' . $row[3] . ' </strong> </p>
<table border="0" style="font-size:11px;" cellspacing="1" cellpadding="5">
<tr class="top">
<td align="left"><b>Product</b></td>
<td align="center"><b>Price</b></td>
<td align="center"><b>Qty</b></td>
</tr>';
$bg = '#dddddd'; // Set the background color.
while($row = mysql_fetch_array($result, MYSQL_NUM)) { // WHILE loop start
$bg = ($bg=='#eaeced' ? '#dddddd' : '#eaeced');
echo '<tr bgcolor="' . $bg . '">';
echo '<td align="left">' . $row[15] . '</td>
<td align="center">' . $row[13] . ' pts</td>
<td align="center">' . $row[12] . '</td>
</tr>';
echo '';
}// end of WHILE loop
echo '</table>
<p> Here:</p>
<br><br>
<p> << Back to Orders</p>
<p> </p>
<p> </p>
<p> </p>
';
} else { // Not a valid user ID.
echo '<h1 id="mainhead">Page Error</h1>
<p class="error">This page has been accessed in error.</p><p><br /><br /></p>';
}
mysql_close(); // Close the database connection.
?>
<p>Footer here</p>
<?php
include ('./includes/footer_admin_user.html'); // Include the HTML footer.
?>
One way you could do it is grab the row first, and then use a do/while loop instead of just a basic while loop. Like this:
if (mysql_num_rows($result)) { // Valid user ID, show the form.
/*********** I added this line ***********/
$row = mysql_fetch_array($result, MYSQL_NUM);
echo '<p>Order for:<strong>' . $row[2] . ' ' . $row[3] . ' </strong> </p>
<table border="0" style="font-size:11px;" cellspacing="1" cellpadding="5">
<tr class="top">
<td align="left"><b>Product</b></td>
<td align="center"><b>Price</b></td>
<td align="center"><b>Qty</b></td>
</tr>';
$bg = '#dddddd'; // Set the background color.
/*********** I changed this from a while loop to a do-while loop ***********/
do { // WHILE loop start
$bg = ($bg=='#eaeced' ? '#dddddd' : '#eaeced');
echo '<tr bgcolor="' . $bg . '">';
echo '<td align="left">' . $row[15] . '</td>
<td align="center">' . $row[13] . ' pts</td>
<td align="center">' . $row[12] . '</td>
</tr>';
echo '';
} while($row = mysql_fetch_array($result, MYSQL_NUM)); // end of WHILE loop
It looks like the problem is when you're trying to display the user's name:
echo '<p>Order for:<strong>'.$row[2].' '.$row[3]
The $row variable doesn't exist yet. Not seeing your database or the result from your database query, my guess is that the user's name is repeated next to every single item in their order, so it might be as simple as just starting the WHILE loop, and checking to see if you've printed their name yet:
$lastUser = NULL;
while($row = mysql_fetch_array($result, MYSQL_NUM)) {
if ($row[0] !== $lastUser) {
if (isset($lastUser)) {
// finish up the report for the previous user
}
// echo the stuff for the current user's name
$lastUser = $row[0];
}
// go on echo-ing their order information
}
// after the while loop is over,
// close up the last user's report
But like I said, this is just a guess, and might be totally off.
The problem is that you tried to access $row[2] and $row[3] before mysql_fetch_array(). Since you are already echo'ing HTML tags, why don't you "buffer" your output first like this?:
while($row = mysql_fetch_array($result, MYSQL_NUM)) {
$bg = ($bg=='#eaeced' ? '#dddddd' : '#eaeced');
$order = '<tr bgcolor="' . $bg . '">
<td align="left">' . $row[15] . '</td>
<td align="center">' . $row[13] . ' pts</td>
<td align="center">' . $row[12] . '</td>
</tr>';
$orders[$row[2] . " " . $row[3]][] .= $order;
}
Then do a second foreach loop for the $orders
foreach($orders as $name => $orderList)
{
echo "Order for: $name";
echo "<table ...>";
foreach($orderList as $order)
{
echo $order;
}
echo "</table>";
}

Categories