PHP if statement in html-table outputs error - php

I am new to HTML5 and PHP, I am trying to output a specific value in table data, If the database-retrieved-value is per condition.
My code:
<table class="scroll">
<thead style="background-color: #99E1D9; color: #705D56;">
<tr>
<th>ID</th>
<th>Name Client</th>
<th>Last Update</th>
<th style="padding-left: 30%;">Status</th>
</tr>
</thead>
<tbody id="hoverTable">
<?php
$connection = mysql_connect('localhost', 'root', '');
mysql_select_db('patientdb');
$query = "SELECT id, name, date FROM clients";
$result = mysql_query($query);
//status waarden uit
$status = "SELECT status FROM clients";
$status_ = mysql_query($status);
while($row = mysql_fetch_array($result)){ //Loop through results
echo "<tr>
<td>" . $row['id'] . "</td>
<td>" . $row['name'] . "</td>
<td>" . $row['date'] . "</td>
<td style='padding-left: 30%;'>" .
if ($status_ > 60){echo "red";
} elseif ($status_ > 50){echo "yellow";
} else{echo "green";}
. "</td>
</tr>";
}
mysql_close();
?>
</tbody>
</table>
Error output
Parse error: syntax error, unexpected T_IF in
/test/composition/login/portal/portal.php
on line 204
What is the right way to solve this?
EDIT
my current code:
<table class="scroll">
<thead style="background-color: #99E1D9; color: #705D56;">
<tr>
<th>Naam Client</th>
<th>Laatste Update</th>
<th style="margin-left: 40%; padding-left: 0%;">Status</th>
</tr>
</thead>
<tbody id="hoverTable" style="font-size: 11pt;">
<?php
$connection = mysql_connect('localhost', 'root', '');
mysql_select_db('patientdb');
$query = "SELECT id, naam, datum FROM clients";
$result = mysql_query($query);
$query2 = "SELECT status FROM clients";
$result2 = mysql_query($query2);
if (!empty ($result2)) {
while ($row2 = mysql_fetch_assoc($result2)) {
echo $row2['status'] . "<br />";
}
}
while($row = mysql_fetch_array($result)){ //Loop through results
echo "<tr>
<td>" . $row['id'] . "</td>
<td>" . $row['naam'] . "</td>
<td>" . $row['datum'] . "</td>
<td style='padding-left: 30%;'>";
if ($results2 > 60 && $results2 < 70) {
echo "red";
} elseif ($results2 > 50 && $results2 < 60) {
echo "yellow";
} else {
echo "green";
}
echo "</td>
</tr>";
}
mysql_close();
?>
</tbody>
</table>
Output the right data. but partly outside and partly inside the table.

You will have to remove the if statement out of the echo to get rid of the error Try this:
<table class="scroll">
<thead style="background-color: #99E1D9; color: #705D56;">
<tr>
<th>ID</th>
<th>Name Client</th>
<th>Last Update</th>
<th style="padding-left: 30%;">Status</th>
</tr>
</thead>
<tbody id="hoverTable">
<?php
$connection = mysql_connect('localhost', 'root', '');
mysql_select_db('patientdb');
$query = "SELECT id, name, date FROM clients";
$result = mysql_query($query);
//status waarden uit
$status = "SELECT status FROM clients";
$status_ = mysql_query($status);
while($row = mysql_fetch_array($result)){ //Loop through results
echo "<tr>
<td>" . $row['id'] . "</td>
<td>" . $row['name'] . "</td>
<td>" . $row['date'] . "</td>
<td style='padding-left: 30%;'>";
if ($status_ > 60) {
echo "red";
} elseif ($status_ > 50) {
echo "yellow";
} else {
echo "green";
}
echo "</td>
</tr>";
}
mysql_close();
?>
</tbody>
</table>

You can't have an if statement (or any other statement, for that matter) in the middle of another statement like echo. If you want to concatenate different strings depending on a variable, you can use the conditional (AKA "ternary") operator.
echo "<tr>
<td>" . $row['id'] . "</td>
<td>" . $row['name'] . "</td>
<td>" . $row['date'] . "</td>
<td style='padding-left: 30%;'>" .
$status_ > 60 ? "red" : ($status_ > 50 ? "yellow" : "green" )
. "</td>
</tr>";

Try:
$status = "green";
if ($status > 50)
{
$status="yellow";
}
elseif($status>60)
{
$status="red";
}
echo "<tr>
<td>" . $row['id'] . "</td>
<td>" . $row['name'] . "</td>
<td>" . $row['date'] . "</td>
<td style='padding-left: 30%;'>" .$status. "</td>
</tr>";
You can't append to a string a conditional statement, assign to a variable first for example (like I posted)

This part isn't at the right place:
if ($status_ > 60){echo "red";
} elseif ($status_ > 50){echo "yellow";
} else{echo "green";}
should be:
echo "<tr>
<td>" . $row['id'] . "</td>
<td>" . $row['name'] . "</td>
<td>" . $row['date'] . "</td>
<td style='padding-left: 30%;'>";
if ($status_ > 60){
echo "red";
} elseif ($status_ > 50){
echo "yellow";
} else{
echo "green";
}
echo "</td></tr>";

Surely status_ would not come back with a number, but an array.
$status_ = mysql_query($status);
Without knowing what data is coming back, it is difficult to help.
mysql_query

Related

Formatting an SQL PHP script

I'm in a class, and I've been asked to retrieve information from an SQL table and display it as an invoice. I've got the basics down, however I've now been asked to format the PHP page to look something like this.
I'm a little confused as to how to do this or even where to begin for that matter.
PHP:
<?php
require("connect.php");
$inNo = $_POST["inNo"];
$sql = "SELECT invoice.invoice_no, invoice.date, invoice.cust_id, invoice.emp_id, invoice_line.prod_id, invoice_line.qty, product.cost_price, (product.cost_price * invoice_line.qty) FROM invoice INNER JOIN invoice_line ON invoice.invoice_no = invoice_line.invoice_no INNER JOIN product ON invoice_line.prod_id = product.id WHERE cust_id = '" . $inNo . "'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
//open table
echo '<table class="table table-striped" id="outTable">';
echo "<tr><th>Invoice no.</th><th>Date</th><th>Customer ID</th><th>Employee ID</th><th>Product ID</th><th>Qty</th><th>Price</th><th>Total cost</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "
<tr>
<td>" . $row["invoice_no"]. "</td>
<td>" . $row["date"]. "</td><td>" . $row["cust_id"]. "</td>
<td>" . $row["emp_id"]. "</td><td>" . $row["prod_id"]. "</td>
<td>" . $row["qty"]. "</td><td>" . $row["cost_price"]. "</td>
<td>" . $row["(product.cost_price * invoice_line.qty)"]. "</td>
</tr>";
}
} else {
echo "0 results";
}
$conn->close();
?>
Try this:
<?php
require("connect.php");
$inNo = $_POST["inNo"];
$sql = "SELECT invoice.invoice_no, invoice.date, invoice.cust_id, invoice.emp_id, invoice_line.prod_id, invoice_line.qty, product.cost_price, (product.cost_price * invoice_line.qty) FROM invoice INNER JOIN invoice_line ON invoice.invoice_no = invoice_line.invoice_no INNER JOIN product ON invoice_line.prod_id = product.id WHERE cust_id = '" . $inNo . "'";
$result = $conn->query($sql);
echo"<div style='width:50em'>";
if ($result->num_rows > 0) {
$count =0;
while($row = $result->fetch_assoc()) {
if ($count==0){
echo '<h2 style="text-align: center">Invoice no. " . $row["invoice_no"]. "</h2>';
echo '<table width=100%>';
echo "<tr style='text-align: left'><th>Date</th><th>Customer ID</th><th>Employee ID</th></tr>";
echo '<td>" . $row["date"]. "</td><td>" . $row["cust_id"]. "</td>
<td>" . $row["emp_id"]. "</td></table>';
echo '<table class="table table-striped" id="outTable" width=100% style="text-align: left">';
echo '<th>Product ID</th><th>Qty</th><th>Price</th></tr>';
}
echo "
<tr>
<td>" . $row["prod_id"]. "</td>
<td>" . $row["qty"]. "</td><td>" . $row["cost_price"]. "</td>
</tr>";
if ($count==$result->num_rows-1){
echo '<div style="text-align: right">Total cost: " . $row["(product.cost_price * invoice_line.qty)"]. "</div>';
}
$count++;
}
echo "</table></div>";
} else {
echo "0 results";
}
$conn->close();
?>

How can I check if all checkboxes have been checked in PHP?

I am implementing a list of items and each has a checkbox. I currently can see which checkboxes have been checked but what I want to do is check if all of them have been checked. How can I implement that?
Here is my code:
<form action="" method="post">
<?php
echo "<table>
<tr>
<th>Customer ID</th>
<th>Report ID</th>
<th>Report message</th>
<th>Device</th>
<th>Device no.</th>
<th>Barcode</th>
<th>IMEI</th>
<th>Sale-date</th>
</tr>";
while ($row2 = $clientUsername->fetch_assoc()) {
$_SESSION['cl_username'] = $row2["username"];
while ($row = $message->fetch_assoc()) {
$_SESSION['accept'] = $row["acceptance"];
$_SESSION['client_comment'] = $row["message"];
$_SESSION['name'] = $row["name"];
$_SESSION['sales_date'] = $row["sales_date"];
$_SESSION['date_sent'] = $row["date_sent"];
$_SESSION['countable_array'] = $row;
?>
<?php if ($row['acceptance'] == 3) {
echo "<tr> <td>
" . '<input type=checkbox name=devices[] value=' . $row['dev_id'] . '>' . "
</td> <td>" . $cus_id . " </td> <td>" . $rep_id . "</td> <td>" . $_SESSION['client_comment'] . "</td> <td>" . $_SESSION['name'] . "</td> <td>" . $row["device_no"] . "</td> <td>" . $row["barcode"] . "</td> <td>" . $row["serial_imei"] . "</td> <td>" . $row["serial_no"] . "</td> <td>" . $row["sales_date"] . "</td></tr>";
echo "</table>";
}
}
}
</form>
if ($_SERVER['REQUEST_METHOD'] == "POST" && isset($_POST['rejected'])) {
if (count($count_devices) == 1) {
...
}
}
<?php
while ($row2 = $clientUsername->fetch_assoc()) {
$_SESSION['cl_username'] = $row2["username"];
$i = 0; // initiate the variable here
$total_number_of_rows = $message->num_rows(); // Get total number of rows from your object
while ($row = $message->fetch_assoc()) {
$_SESSION['accept'] = $row["acceptance"];
$_SESSION['client_comment'] = $row["message"];
$_SESSION['name'] = $row["name"];
$_SESSION['sales_date'] = $row["sales_date"];
$_SESSION['date_sent'] = $row["date_sent"];
$_SESSION['countable_array'] = $row;
if ($row['acceptance'] == 3) {
$i++; // When conditions trues, increment the variable.
echo "<tr> <td>
" . '<input type=checkbox name=devices[] value=' . $row['dev_id'] . '>' . "
</td> <td>" . $cus_id . " </td> <td>" . $rep_id . "</td> <td>" . $_SESSION['client_comment'] . "</td> <td>" . $_SESSION['name'] . "</td> <td>" . $row["device_no"] . "</td> <td>" . $row["barcode"] . "</td> <td>" . $row["serial_imei"] . "</td> <td>" . $row["serial_no"] . "</td> <td>" . $row["sales_date"] . "</td></tr>";
echo "</table>";
}
}
if($i == $total_number_of_rows){ // Here implement this condition, If both equal then all inputs have checked.
echo "Check box checked all inputs";
}
}
?>
I think you expecting the same as above. We need to check the Total number of rows with Incremental variable value.
Please review my comment inside the code part, so that you can understand terms.

How to return average data through the loop in mysqli and php

I am a beginner programmer, and i want to fetch AVG() data in every rows created by the while loop,
this is the code,
<?php
$sql = "SELECT sites_id, sites_nama, sites_alamat, sites_kota_kabupaten, perpanjangan_pagu, sites_tanggal_start, sites_tanggal_finish, perpanjangan_invoice, AVG(perpanjangan_pagu) FROM site";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table>
<tr>
<th>Site ID</th>
<th>Site Name</th>
<th>Alamat</th>
<th>Kab.Kota</th>
<th>Pagu</th>
<th>Harga Rata Rata</th>
<th>Awal Kontrak</th>
<th>Akhir Kontrak</th>
<th>Invoice</th>
</tr>";
// output data of each row
$rows = array();
while ($row = $result->fetch_assoc()) {
$rows = $row["AVG(perpanjangan_pagu)"];
echo "
<tr>
<td>" . $row["sites_id"] . "</td>
<td>" . $row["sites_nama"] . "</td>
<td>" . $row["sites_alamat"] . "</td>
<td>" . $row["sites_kota_kabupaten"] . "</td>
<td>" . $row["perpanjangan_pagu"] . "</td>
<td>" . $rows . "</td>
<td>" . $row["sites_tanggal_start"] . "</td>
<td>" . $row["sites_tanggal_finish"] . "</td>
<td>" . $row["perpanjangan_invoice"] . "</td>
</tr>";
}
echo "</table>";
}
else {
//echo "0 results";
}
$conn->close();
?>
and this is the image, you can see that it only return one row.
and this is the image when I deleted the avg function.
please help me to fetch the average data in every row in the table, not just only one table,
Thanks.
can you try this I have modified user code & added the AVG() column.
<?php
$sql = "SELECT sites_id, sites_nama, sites_alamat, sites_kota_kabupaten, perpanjangan_pagu, sites_tanggal_start, sites_tanggal_finish, perpanjangan_invoice, (SELECT AVG(perpanjangan_pagu) FROM site) AS 'AVG_pagu' FROM site";
$result = $conn->query($sql);
// AVG_pagu has the AVG value of all columns of `perpanjangan_pagu` in table `site`
if ($result->num_rows > 0) {
echo "<table>
<tr>
<th>Site ID</th>
<th>Site Name</th>
<th>Alamat</th>
<th>Kab.Kota</th>
<th>Pagu</th>
<th>Harga Rata Rata</th>
<th>Awal Kontrak</th>
<th>Akhir Kontrak</th>
<th>Invoice</th>
</tr>";
// output data of each row
// $rows = array(); // This is not actually required
while ($row = $result->fetch_assoc()) {
//$rows[] = $row["AVG_pagu"]; // This is not actually required
echo "
<tr>
<td>" . $row["sites_id"] . "</td>
<td>" . $row["sites_nama"] . "</td>
<td>" . $row["sites_alamat"] . "</td>
<td>" . $row["sites_kota_kabupaten"] . "</td>
<td>" . $row["perpanjangan_pagu"] . "</td>
<td>" . $row["AVG_pagu"] . "</td>
<td>" . $row["sites_tanggal_start"] . "</td>
<td>" . $row["sites_tanggal_finish"] . "</td>
<td>" . $row["perpanjangan_invoice"] . "</td>
</tr>";
}
echo "</table>";
}
else {
echo "No records found!";
}
$conn->close();
?>

HTML table error in php

i am new to php and i try to solve this many times but i couldn't.
this is my php code
Can someone help me with this it should be easy for a php expert.
<?php
require_once 'connector.php';
$result = mysql_query('SELECT * FROM highscores ORDER BY score DESC');
$username = mysql_query('SELECT username FROM users WHERE id in(SELECT user_id FROM highscores)');
echo"<html>
<head>
<title>Highscores</title>
</head>
<body>
<table border='1'>
<tr>
<th>user</th>
<th>score</th>
<th>Date</th>
</tr>
";
while ($name = mysql_fetch_array($username) )
{
echo "<tr>
<td>" . $name ['username'] . "</td>";
}
while( $row = mysql_fetch_array($result))
{
echo"
<td>" . $row ['score'] . "</td>
<td>" . $row ['date'] . "</td>
</tr>";
}
echo"
</table>
</body>
</html>
";
the table i want to take
mysql_* is deprecated! use mysqli_*
<?php
require_once 'connector.php';
$SQL = "SELECT u.username, h.score, h.date
FROM users AS u
JOIN highscores AS h ON (h.user_id = u.users_id)";
$result = mysql_query($SQL) or die( mysql_error() );
echo "<html>
<head>
<title>Highscores</title>
</head>
<body>";
if( mysql_num_rows($result) > 0 )
{
echo "<table border='1'>
<tr>
<th>user</th>
<th>score</th>
<th>Date</th>
</tr>";
while ( $row = mysql_fetch_array($result) )
{
echo "<tr>";
printf("<td>%s</td>", $row['username']);
printf("<td>%s</td>", $row['score']);
printf("<td>%s</td>", $row['date']);
echo "</tr>";
}
echo "</table>
</body>
</html>";
}
mysql_free_result($result);
Can you try this,
$result = mysql_query('SELECT hs.score, hs.date, u.username FROM highscores as hs, users as u where hs.user_id=u.id ORDER BY score DESC');
echo "<html>
<head>
<title>Highscores</title>
</head>
<body>
<table border='1'>
<tr>
<th>user</th>
<th>score</th>
<th>Date</th>
</tr>
";
while( $row = mysql_fetch_array($result))
{
echo"<tr>
<td>" . $row ['username'] . "</td>
<td>" . $row ['score'] . "</td>
<td>" . $row ['date'] . "</td>
</tr>";
}
Instead of putting everything in a double quote, you can use string concatenation with (.) operator. For example, Instead of writing like this
echo"<html>
<head>
<title>Highscores</title>
</head>
<body>
<table border='1'>
<tr>
<th>user</th>
<th>score</th>
<th>Date</th>
</tr>
";
You can write like this:
echo "<html>" .
"<head>" .
"<title>Highscores</title>"
"</head>" .
"<body>" .
"<table border='1'>" .
"<tr>" .
"<th>user</th>" .
"<th>score</th>" .
"<th>Date</th>" .
"</tr>" ;
The following code block
echo "<tr>
<td>" . $name ['username'] . "</td>";
Should be written as
echo "<tr>" .
"<td>" . $name ['username'] . "</td>";
In this way the code looks more readable as well.
The second loop needs to be inside the first loop, and the closing </tr> shouldn't be inside the second loop.
<?
while ($name = mysql_fetch_array($username)) {
echo "<tr>";
echo "<td>" . $name["username"] . "</td>";
while ($row = mysql_fetch_array($result)) {
echo "<td>" . $row["score"] . "</td>";
echo "<td>" . $row["date"] . "</td>";
}
echo "</tr>";
}
?>

Dealing with duplicate arrays

I have a scanner project I'm developing where a class reads in a txt file created by scanner and queries the database to display "bundles" scanner by a scanner per room. However now I have some rooms which duplicate the results. The results from the query are stop in arrays per day and manipulate. What i want to do is to display unique bundles and remove duplicates leaving the earliest entry. Below is my index page. Any tips advise would be much appreciated.
<?php
// First of all initialise the user and check for permissions
require_once "/var/www/users/user.php";
$user = new CHUser(13);
// Initialise the template
require_once "/var/www/template/template.php";
$template = new CHTemplate();
// Initialise the scanner class
require_once "/var/www/Scanners/scanners.php";
$scanner = new CHScanners();
$_GET['date'] = date("Y-m-d", $scanner->GetPreviousMonday(strtotime($_GET['date'])));
$weeklyTotal = 0;
$content = "<h1>" . $scanner->GetRoomName($_GET['room']) . " Information</h1>
<form action='room.php' method='get'>
Enter date: <input type='text' name='date' /><input type='submit' /><input type='hidden' name='room' value='" . $_GET['room'] ."' />
</form>
<table width='100%'>
<tr>
<td style='vertical-align: top;'>
<table width='100%'>
<tr>
<th colspan='2'>Monday</th>
</tr>
<tr>
<th>Bundle #</th>
<th>Qty</th>
</tr>";
$result = $scanner-
>ListRoomTotals($_GET['room'],$_GET['date']);
$total = 0;
foreach($result as $x) {
$content .= "<tr>
<td>" . $x[0] . "</td>
<td>" . $x[1] . "</td>
</tr>";
$total += $x[1];
}
$weeklyTotal += $total;
$content .= "<tr><td>Total Pairage:</td><td>".$total."
</td></tr>
<tr><td>Total Dozens:</td><td>".number_format($total/12,1)."</td></tr></table>
</td>
<td style='vertical-align: top;'>
<table width='100%'>
<tr>
<th colspan='2'>Tuesday</th>
</tr>
<tr>
<th>Bundle #</th>
<th>Qty</th>
</tr>";
$date = date("Y-m-d",(strtotime($_GET['date']) + 86400));
$result = $scanner->ListRoomTotals($_GET['room'], $date);
$total = 0;
foreach($result as $x) {
$content .= "<tr>
<td>" . $x[0] . "</td>
<td>" . $x[1] . "</td>
</tr>";
$total += $x[1];
}
$weeklyTotal += $total;
$content .= "<tr><td>Total Pairage:</td><td>" . $total . "
</td></tr>
<tr><td>Total Dozens:</td><td>" .
number_format($total/12,1) . "</td></tr></table>
</td>
<td style='vertical-align: top;'>
<table width='100%'>
<tr>
<th colspan='2'>Wednesday</th>
</tr>
<tr>
<th>Bundle #</th>
<th>Qty</th>
</tr>";
$date = date("Y-m-d",(strtotime($_GET['date']) +
(86400*2)));
$result = $scanner->ListRoomTotals($_GET['room'], $date);
$total = 0;
foreach($result as $x) {
$content .= "<tr>
<td>" . $x[0] . "</td>
<td>" . $x[1] . "</td>
</tr>";
$total += $x[1];
}
$weeklyTotal += $total;
$content .= "<tr><td>Total Pairage:</td><td>" . $total . "
</td></tr>
<tr><td>Total Dozens:</td><td>" .
number_format($total/12,1) . "</td></tr></table>
</td>
<td style='vertical-align: top;'>
<table width='100%'>
<tr>
<th colspan='2'>Thursday</th>
</tr>
<tr>
<th>Bundle #</th>
<th>Qty</th>
</tr>";
$date = date("Y-m-d",(strtotime($_GET['date']) +
(86400*3)));
$result = $scanner->ListRoomTotals($_GET['room'], $date);
$total = 0;
foreach($result as $x) {
$content .= "<tr>
<td>" . $x[0] . "</td>
<td>" . $x[1] . "</td>
</tr>";
$total += $x[1];
}
$weeklyTotal += $total;
$content .= "<tr><td>Total Pairage:</td><td>" . $total . "
</td></tr>
<tr><td>Total Dozens:</td><td>" .
number_format($total/12,1) . "</td></tr></table>
</td>
<td style='vertical-align: top;'>
<table width='100%'>
<tr>
<th colspan='2'>Friday</th>
</tr>
<tr>
<th>Bundle #</th>
<th>Qty</th>
</tr>";
$date = date("Y-m-d",(strtotime($_GET['date']) +
(86400*4)));
$result = $scanner->ListRoomTotals($_GET['room'], $date);
$total = 0;
foreach($result as $x) {
if($x[0] != "" and isset($x[0])) {
$content .= "<tr>
<td>" . $x[0] . "</td>
<td>" . $x[1] . "</td>
</tr>";
$total += $x[1];
}
}
$weeklyTotal += $total;
$content .= "<tr><td>Total Pairage:</td><td>" . $total . "
</td></tr>
<tr><td>Total Dozens:</td><td>" .
number_format($total/12,1) . "</td></tr></table>
</td>
</tr>
</table>";
$options .= "Weekly Pairs: " . $weeklyTotal . "<br>
Weekly Dozens: " . $weeklyTotal/12;
$template->SetTag("options", $options);
$template->SetTag("content", $content);
echo $template->Display();
?>
Before appending do a in_array() test.
It may be better to implement the constraint in your query than to do it in PHP. Try using group by and using min on your date to get the room with the earliest entry. Here is a relevant example on this site.
If you wish to do it inside PHP, you would have to some filter. Set up a temp array to store the final results, and loop through all your results. If the room is not in the result array, add it. If it is, compare the date and use the earliest date.

Categories