Dealing with duplicate arrays - php

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.

Related

Not display table SQLite and php

$table = "<p><table width=\"770px\">
<thead>
<tr>
<th><b>FECHA</b></th>
<th><b>PRODUCTO</b></th>
<th><b>CANTIDAD</b></th>
<th><b>PRECIO</b></th>
</tr>
</thead>
<tbody> " . while($res = $result->fetchArray(SQLITE3_ASSOC))
{
echo
"<tr>
<td>" . $res['fecha'] . "</td>
<td>" . $res['nombre'] . "</td>
<td>" . $res['cantidad'] . "</td>
<td>" . $res['precio_hospital'] . "<br></td>
</tr>";
} . "
</tbody>
</table>";
Hello friends, I have a php page that does not allow me to show the table correctly I do not know how to place the while, please can you help me.
works friend, but I want to save the result of this table in a php variable to print it in
$ pdf-> writeHTML ($table, true, false, true, false, '');
You can't use echo when you are already inside and echo. Try:
$output = '<p><table width="770px">
<thead>
<tr>
<th><b>FECHA</b></th>
<th><b>PRODUCTO</b></th>
<th><b>CANTIDAD</b></th>
<th><b>PRECIO</b></th>
</tr>
</thead>
<tbody>';
while($res = $result->fetchArray(SQLITE3_ASSOC)) {
$output .= '<tr><td>' . $res['fecha'] . '</td>' .
'<td>' . $res['nombre'] . '</td>' .
'<td>' . $res['cantidad'] . '</td>' .
'<td>' . $res['precio_hospital'] .
'<br></td></tr>';
}
$output .= '</tbody></table>';
Like this:
$myVar = "<p><table width=\"770px\">
<thead>
<tr>
<th><b>FECHA</b></th>
<th><b>PRODUCTO</b></th>
<th><b>CANTIDAD</b></th>
<th><b>PRECIO</b></th>
</tr>
</thead>
<tbody> ";
while($res = $result->fetchArray(SQLITE3_ASSOC)) {
$myVar .=
"<tr>
<td>" . $res['fecha'] . "</td>
<td>" . $res['nombre'] . "</td>
<td>" . $res['cantidad'] . "</td>
<td>" . $res['precio_hospital'] . "<br></td>
</tr>";
}
$myVar .= "</tbody></table></p>";
//echo $myVar; //if you want to see the result
//$pdf->writeHTML ($myVar, true, false, true, false, ''); //to pdf
You can't concatenate a control structure to a string. PHP will throw a syntax error.
Edited to use a variable

PHP if statement in html-table outputs error

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

For loop for the first row, the button cant work

This is the table i created , so when i press the Edit button it should bring me to another page to edit. For the first edit button on the first row it does not work however for the rest of the edit button on the second row onward it works. Please help. Thank you !
<table border='1'>
<tr>
<th width='120'>Production ID</th>
<th width='120'>Description</th>
<th width='120'>BOM</th>
</tr>
<?php
for ($i = 0; $i < $num; $i++) {
$fetch = mysqli_fetch_row($select);
echo "<tr>
<td>" . $fetch[0] . "</td>
<td>" . $fetch[1] . "</td>
<td>" . $fetch[2] . "</td>
<td><form action='AddProductMainEdit.php?prodid' method='GET'><input type='hidden' value='".$fetch[0]."' name='prodid'><input type='submit' id='edit' value='Edit'></form></td>
</tr>";
}
?>
</table>
Try this:
<table border='1'>
<tr>
<th width='120'>Production ID</th>
<th width='120'>Description</th>
<th width='120'>BOM</th>
</tr>
<?php
while ($fetch=mysqli_fetch_row($select)) {
echo "<tr>
<td>" . $fetch[0] . "</td>
<td>" . $fetch[1] . "</td>
<td>" . $fetch[2] . "</td>
<td><form action='AddProductMainEdit.php' method='GET'><input type='hidden' value='".$fetch[0]."' name='prodid'><input type='submit' id='edit' value='Edit'></form></td>
</tr>";
}
?>
</table>
You don't need to add the variable prodid to the url, the get method add all the variables to the url.

making hyperlink of a value stored in table column

How to make the values inserted in table column($row['survey_name']) a hyperlink. this is my code which I have tried in php:
while($row=mysql_fetch_array($result1))
{
$content .= "
<tr id=\"special\" style=\"background:;\">
<td>". $row['survey_name'] . "</td>
<td>" . $row['date'] . "</td>
</tr>
";
?>
<script
$('#special').onclick(function(){window="http://urllinkto.com/x/y/z";})
</script>
<?php }//end of while
if ($content) {
// See note on the "quote switch" here
echo '
<table border= "5" cellpadding="2" cellspacing="2" width="100%">
<tr>
<td><strong>Survey Name</strong></td>
<td><strong>Date Created</strong></td>
/tr>' .
$content . '
</table> ';
Chage
<td>". $row['survey_name'] . "</td>
to:
<td><a href='URL HERE'>". $row['survey_name'] . "</a></td>
Try to declare variables first.
while($row=mysql_fetch_array($result1)){
$surveyname = $row['survey_name']
$date = $row['date']
$content .= "
<tr id=\'special\' style=\"background:;\">
<td><a href='#'>echo $surveyname</a> </td>
<td><a href='#'>echo $date</a></td>
</tr>
";
}

echoing summation result

please i am trying to echo the sumation of a column in the next row
, but it is displaying with the prev row.
this is the code
$body = "<html><body><table border='1'>
<tr>
<th>Shop Name</th>
<th>Product Name</th>
<th>Size</th>
<th>Color Name</th>
<th>Quantity</th>
<th>Price</th>
<th>Cost</th>
</tr>";
$totalPrice = 0;
$pplresult = mysql_query("SELECT * FROM repplac WHERE Uname = '{$_SESSION['username']}'");
while($row = mysql_fetch_assoc($pplresult)){
$body .= "<tr>
<td>" . $row['Sname'] ."</td>
<td>" . $row['Pname'] ."</td>
<td>" . $row['Psize'] ."</td>
<td>" . $row['Pcolour'] ."</td>
<td>" . $row['Pquantity'] ."</td>
<td>" . $row['Price'] ."</td>
<td>" . $row['Tprice'] ."</td>
</tr>";
$totalPrice += $row['Tprice'];
}
$body .= "<tr>
<td>" . $totalprice ."</td>
</tr>";
$body .="</table></body></html>";
You want to display the total under the last column right?
for that you will need to add some td' s in the later tr i.e.,
$body .="<tr>
<td colspan=6></td>
<td>" . $totalprice ."</td>
</tr>";
Even your code is not appropriate. Why would you iterate through that while loop twice for the same mysql query result set,
$pplresult = mysql_query("SELECT * FROM repplac WHERE Uname = '{$_SESSION['username']}'");
while($row = mysql_fetch_assoc($pplresult)){
$body .= "<tr>
<td>" . $row['Sname'] ."</td>
<td>" . $row['Pname'] ."</td>
<td>" . $row['Psize'] ."</td>
<td>" . $row['Pcolour'] ."</td>
<td>" . $row['Pquantity'] ."</td>
<td>" . $row['Price'] ."</td>
<td>" . $row['Tprice'] ."</td>
</tr>";
$totalprice += $row['TPrice'];
}
$body .="<tr>
<td colspan=6>Total :</td>
<td>" . $totalprice ."</td>
</tr>";
$body .="</table></body></html>";
First off, why are you running the same query twice? You can do the summation in the same iteration as when you're writing out the rows:
$body = "<html><head><title></title></head><body><table>";
$totalPrice = 0;
$pplresult = mysql_query("SELECT * FROM repplac WHERE Uname = '{$_SESSION['username']}'");
while($row = mysql_fetch_assoc($pplresult)){
$body .= "<tr>
<td>" . $row['Sname'] ."</td>
<td>" . $row['Pname'] ."</td>
<td>" . $row['Psize'] ."</td>
<td>" . $row['Pcolour'] ."</td>
<td>" . $row['Pquantity'] ."</td>
<td>" . $row['Price'] ."</td>
<td>" . $row['Tprice'] ."</td>
</tr>";
$totalPrice += $row['Tprice'];
}
// echo "$totalprice";
// Add a column with colspan 6 to push the totalprice column under the Tprice column.
$body .= '<tr>
<td colspan="6">
<td>' . $totalprice ."</td>
</tr>";
$body .="</table></body></html>";
Give the td a colspan of 7, so it's 100% wide:
$body .="<tr>
<td colspan="7">" . $totalprice ."</td>
</tr>";
Also you can improve efficiency by summing the prices in your query:
SELECT *, SUM(Tprice) AS total_price FROM repplac WHERE Uname = '{$_SESSION['username']}'
or
SELECT Sname, Pname, Psize, Pcolour, Pquantity, Price, Tprice, SUM(Tprice) AS total_price FROM repplac WHERE Uname = '{$_SESSION['username']}'
then in your PHP
$body .= ... $row['total_price'] ...;
Try to add colspan like <td colspan=7>" . $totalprice ."</td>

Categories