problem getting the right outcome with foreach - php

I have the following code to generate a HTML table with the placed orders.
// Select data FROM ORDER & ORDERLINE
$id = $value->ID;
$datenow = date("Y-m-d");
$temp = 0;
$stmt2 = $mysql->prepare("SELECT salesorder.order_id AS soid, salesorder.delivery_date AS sdd, orderline.qty AS olq, food.food_type AS fft
FROM orderline
INNER JOIN salesorder ON salesorder.order_id = orderline.order_id
INNER JOIN food ON food.food_id = orderline.food_id
WHERE salesorder.client_id=? AND orderline.qty!=?
ORDER BY sdd");
$stmt2->bind_param('ss', $id, $temp);
$stmt2->execute();
$result2 = $stmt2->get_result();
// determen if there are orders to display
if (mysqli_num_rows($result2) > 0) {
while ($value2 = $result2->fetch_object()) {
$results[] = $value2;
}
?>
<table class="center table">
<thead>
<tr>
<th>Order Number</th>
<th>Delivery Date</th>
<th>QTY</th>
<th>Food Type</th>
</tr>
</thead>
<tbody>
<b>Delivered orders :</b> (and orders delivered today...)
<?php foreach ( $results as $result ) : ?>
<?php if (($result->sdd)<=$datenow) { ?>
<td><?php echo ($result->soid); ?></td>
<td><?php echo ($result->sdd); ?></td>
<td><?php echo ($result->olq); ?></td>
<td><?php echo ($result->fft); ?></td>
</tr>
<?php } ?>
<?php endforeach;?>
</tbody>
</table>
The problem is that the 'delivery date' and 'order number' ALSO are displayed as many times as there are QTY and food types. i know this has to do with indexes, but however i try, i cant find the right syntax or way to do it. Any ideas?
I have added the data tables as images. Please let me know...

Inside of foreach u need to open <tr> Tag
You Can Loop the array directly mysqli_fetch_assoc returning array, then why you store into $results[] array,

Based on your comments on the other answer by Eibs please could you check if the following gives you what you need. First when you are looping the results we create an associative array grouped by the order number and date and add each item to the items array. Then when creating the table we loop the results array and then the items array. I have used separate rows only because you have a headings row, however, using this same approach you can design your table output a number of different ways.
// determen if there are orders to display
if(mysqli_num_rows($result2) > 0)
{
while($value2 = $result2->fetch_object())
{
$key = $value2->soid.'-'.$value2->sdd;
if(!isset($results[$key]))
{
$results[$key] = array(
'soid' => $value2->soid,
'sdd' => $value2->sdd,
'items' => array(),
);
}
$results[$key]['items'][] = $value2;
}
}
?>
<table class="center table">
<thead>
<tr>
<th>Order Number</th>
<th>Delivery Date</th>
<th>QTY</th>
<th>Food Type</th>
</tr>
</thead>
<tbody>
<tr>
<td colspan="4"><b>Delivered orders :</b> (and orders delivered today...)</td>
</tr>
<?php foreach($results as $result) : ?>
<?php if($result['sdd'] <= $datenow): ?>
<tr>
<td><?php echo($result['soid']); ?></td>
<td><?php echo($result['sdd']); ?></td>
<td></td>
<td></td>
</tr>
<?php foreach($result['items'] as $item): ?>
<tr>
<td></td>
<td></td>
<td><?php echo($item->olq); ?></td>
<td><?php echo($item->fft); ?></td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
<?php endforeach; ?>
</tbody>
</table>

Related

remove duplication of records in foreach loop in php and cakephp

I need help with how to remove the duplication within list of countries based on country_id.
I have a table which includes many projects for a country and counting the projects number which is working fine but need to print without duplication, only one country_id with the count of its project.
The code has been added - if anyone can help it is appreciated
<table>
<tr>
<th>Country ID</th>
<th>Country Name</th>
<th>Number of Place</th>
</tr>
<?php
$country_counts = [];
foreach ($projects as $project) {
$country_id = $project['Project']['country_id'];
if (isset($country_counts[$country_id])) {
$country_counts[$country_id]++;
?>
<tr>
<td style="width: 30%"><?php echo $project['Project']['country_id']; ?></td>
<td style="width: 30%"><?php echo 'Country Name'; ?></td>
<td style="width: 30%"><?php echo $country_counts[$project['Project']['country_id']]; ?></td>
</tr>
<?php
} else {
$country_counts[$country_id] = 1;
}
}
?>
</table>
the result which i got is
shows the result
Do something like this
$checkout_array = array_unique($checkout_array, SORT_REGULAR);
I did this for my own you can do it for your array
I am unable to test this code but the idea of what I suggested as the second options goes as follows:
<table>
<tr>
<th>Country ID</th>
<th>Country Name</th>
<th>Number of Place</th>
</tr>
<?php
$country_counts=[];
$ids=array();// Store unique country_id values here
foreach( $projects as $project ) {
$country_id = $project['Project']['country_id'];
# Check if the country_id is NOT in the array and display if OK.
if( isset( $country_counts[ $country_id ] ) && !in_array( $country_id, $ids ) ) {
$country_counts[$country_id]++;
?>
<tr>
<td style="width: 30%"><?php echo $project['Project']['country_id']; ?></td>
<td style="width: 30%"><?php echo 'Country Name'; ?></td>
<td style="width: 30%"><?php echo $country_counts[$project['Project']['country_id']]; ?></td>
</tr>
<?php
// add the country_ID to the array so that
// on the next iteration of the loop the $ids
// array will be checked again and no output if
// it is already in the array.
$ids[]=$country_id;
} else {
$country_counts[$country_id] = 1;
}
}
?>
</table>

print invoice data blank php foreach loop error

I want to print invoice data. but some error only one image print. how to print all data in an invoice I am using PHP 7.2 and loop foreach please help me...
error->Warning: Invalid argument supplied for foreach() in D:\xammp\htdocs\tam\admin\invoice.php on line 133
PHP GET id wise invoice data script
<?php
require 'setting/config.php';
$id=$_GET['id'];
$query="select * from orders where id='$id'";
$galrun=mysqli_query($conn, $query);
$result=mysqli_fetch_assoc($galrun);
$total=$result['total'];
$array[0]=$result['image'];
$array[1]=$result['productName'];
$array[2]=$result['product_qty'];
$array[3]=$result['salseprice'];
$resultdata= implode(",", $array);
?>
HTML invoice
<table class="table">
<thead>
<tr>
<th>Sr. No.</th>
<th>image</th>
<th>Description</th>
<th>Qty</th>
<th>Amount</th>
<th>Total</th>
</tr>
</thead>
<tbody>
<?php
foreach ($resultdata as $item){
?>
<tr>
<td> ? </td>
<td><img src="image/product/<?php echo $item[0] ; ?>" style="width:100px;height:50px;"></td>
<td><?php echo $item[1] ; ?></td>
<td><?php echo $item[2] ; ?></td>
<td><?php echo $item[3] ; ?></td>
<td><?php echo $total ; ?</td>
</tr>
<?php }?>
</tbody>
</table>
You try to loop through $resultdata, but it is a string, because of this part:
$resultdata= implode(",", $array);
PHP implode() returns a string.

How to use php/mysql for select and display data in multiple categories

I tried to create a table similar to the sporting tables. There are 8 group in this table. (A-H)
There are 4 teams for each group. I tried to select all the teams with the SQL code below, And sort them by group name
SELECT *
FROM teams
ORDER BY groupName
I want the results to be displayed in a panel that contains a html table.
below code:
<?php
while($row = $result->fetch_assoc()){
?>
<div class="flip">
<h3>Group <?php echo($row['groupName']);?></h3>
</div>
<div class="panel">
<table>
<thead>
<tr>
<th>#</th>
<th>Team</th>
</tr>
</thead>
<tbody>
<tr>
<td><?php echo($row['team_pos']); ?></td>
<td><?php echo($row['teamName_Pr']); ?></td>
</tr>
</tbody>
</table>
</div>
<?php
}
?>
But with every repetition of the while loop, the group name repeats for each team.
How can I display all the groups teams in a specific table?
Also, when I use GROUP BY instead of ORDER BY, only one team is displayed per group.
My connection method is mysqli.
You need to accumulate the rows by the groupName. Don't use GROUP BY as it will not return all rows.
<?php
//$result = ...
$groups = [];
while($row = $result->fetch_assoc()){
#$groups[$row['groupName']][] = $row;
}
foreach ($groups as $groupName => $rows) {
?>
<div class="flip">
<h3>Group <?php echo($groupName);?></h3>
</div>
<div class="panel">
<table>
<thead>
<tr>
<th>#</th>
<th>Team</th>
</tr>
</thead>
<tbody>
<?php
foreach ($rows as $row) {
?>
<tr>
<td><?php echo($row['team_pos']); ?></td>
<td><?php echo($row['teamName_Pr']); ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
<?php
}
Btw, please consider using a template engine.

PHP HTML table retrieve multiple data in one <td> tag

I'm trying to generate a list from database in a HTML table just like image below;
https://i.stack.imgur.com/61XLl.png
And here's what i did;
https://i.stack.imgur.com/lLsvF.png
And the code;
<table cellpadding="3" border="1" style="width:100%;margin-top:30px; margin-bottom:50px; font-size:12px">
<thead>
<tr>
<th>KURSUS</th>
<th rowspan="2">NAMA PENSYARAH</th>
<th rowspan="2">NO. SIRI</th>
</tr>
<tr>
<th>NAMA</th>
</tr>
</thead>
<tbody align="center">
<?php
if($numrow>0)
{
while($row = $select->fetch_assoc()){
$code=explode("/",$row['po_code']);
$list=$connect->query("SELECT * FROM polist WHERE polist_poid='".$row['po_id']."' ORDER BY polist_bil ASC");
?>
<tr>
<td><?php echo $row['po_name']; ?></td>
<?php while($rowlist = $list->fetch_assoc()){
$name=$connect->query("SELECT * FROM user WHERE user_id='".$rowlist['polist_userid']."'");
$rowname=$name->fetch_array();?>
<td><?php echo $rowname['user_name']; ?></td>
<td><?php echo $code[0]."/PO/".$code[1]." - ".$rowlist['polist_bil']; ?></td>
<?php } ?>
</tr>
<?php
}
}
?>
</tbody>
</table>
Help me. Thank you in advance :)
Use this code. Concat user names and code with "br" tags in the second while loop and display them in "tds" after while loop.
<tbody align="center">
<?php
if($numrow>0)
{
while($row = $select->fetch_assoc()){
$code=explode("/",$row['po_code']);
$list=$connect->query("SELECT * FROM polist WHERE polist_poid='".$row['po_id']."' ORDER BY polist_bil ASC");
?>
<tr>
<td><?php echo $row['po_name']; ?></td>
<?php
$user_names = $codes = ''; // define empty variables
while($rowlist = $list->fetch_assoc()){
$name=$connect->query("SELECT * FROM user WHERE user_id='".$rowlist['polist_userid']."'");
$rowname=$name->fetch_array();
$user_names .= $rowname['user_name']."<br/>"; //concat to a single string
$codes .= $code[0]."/PO/".$code[1]." - ".$rowlist['polist_bil']."<br/>"; //concat to a single string
}?>
<td><?php echo $user_names;?></td>
<td><?php echo $codes;?></td>
</tr>
<?php
}
}
?>
</tbody>
Put the <td> outside the <?php while($rowlist = $list->fetch_assoc()){
Or get all your data before you start display html and store it in a multi-dimensional array. Then simply loop through the data array. That way you won't have as much php mixed with html also.

Trying to show products from database

I am creating a estore for my website and I'm starting out by just trying to display the product information from my database in a html table but I can't get it to display. It shows the table but no information in it. Here's what I have:
<?php
require 'connectto.php';
//get all product data
$query = 'SELECT * FROM Products';
$products = $db->query($query);
$products = $products->fetch();
?>
<table width="500" border="1">
<tr>
<th>Name</th>
<th >Description</th>
<th >Price</th>
<th>Quantity</th>
</tr>
<?php foreach ($products as $product) { ?>
<tr>
<td><?php echo $product['Name']; ?></td>
<td><?php echo $product['Description']; ?></td>
<td><?php echo $product['Price']; ?></td>
<td><?php echo $product['Quantity']; ?></td>
<td><input type = "submit" value = "Delete" align = "right" ></td>
</tr>
<?php } ?>
</table>
</body>
</html>
Change here: $products = $products->fetch();
To:
$products = $products->fetch_all();
or
$products = $products->fetch_object();
Replace fetch ( ) with fetch_all ( ) .
The error was in fetch (), because it return one row from product table.

Categories