print invoice data blank php foreach loop error - php

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.

Related

problem getting the right outcome with foreach

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>

show multiple rows for each result value

I have two tables one for products and another for items.
I want to show all items details for each product in one table like this:
I have tried to make a nested while loops but the result not as I want.
<table border="1">
<thead>
<th>Product No.</th>
<th>Product Name</th>
<th>T.Qty</th>
<th>Item No.</th>
<th>Item Name</th>
<th>Qty </th>
</thead>
<tbody>
<tr>
<td><?php echo $Product_no ?></td>
<td><?php echo $Product_name?></td>
<td><?php echo $TQty ?></td>
<?php
// my problem is here
$Items= $connect->prepare("Query Statment?");
$Items->execute();
$res = $Items->get_result();
while($GetItems = $res->fetch_assoc()){
?>
<td><?php echo GetItems['Item_no'];?></td>
<td><?php echo GetItems['Item_name']; ?></td>
<td><?php echo GetItems['Qty']; ?></td>
<?php } ?>
</tr>
</tbody>
</table>
but the items displayed beside each other not below.
You have a problem in your html table code. You should close the tag for each row you have, and, in case it is not the first line for that element, insert 3 cells with no data:
<tbody>
<tr>
<td><?php echo $Product_no ?></td>
<td><?php echo $Product_name?></td>
<td><?php echo $TQty ?></td>
<?php
// my problem is here
$Items= $connect->prepare("Query Statment?");
$Items->execute();
$res = $Items->get_result();
$i=0;
while($GetItems = $res->fetch_assoc()){
if ($i!=0){
echo "<td></td><td></td><td></td>";
}
?>
<td><?php echo GetItems['Item_no'];?></td>
<td><?php echo GetItems['Item_name']; ?></td>
<td><?php echo GetItems['Qty']; ?></td>
<?php
echo "</tr>";
$i++;
} ?>
</tbody>

PHP while code error

<?php
$host="localhost";
$user="root";
$pwd="";
$db="assigment";
$conn=mysqli_connect($host,$user,$pwd,$db);
$query="SELECT * FROM 'tdata'";
$result=mysqli_query($conn,$query);
while ($row=mysqli_fetch($result)) {
?>
<table class="table">
<thead>
<tr>
<th>Full Name</th>
<th>Email</th>
<th>Birthday</th>
<th>Gender</th>
<th>Intrests</th>
<th>Address</th>
<th></th>
</thead>
</tr>
<tbody>
<?php echo "<tr><td>".$row[full_name]."</td></tr>";
}
?>
</tbody>
</table>
Error# Fatal error: Uncaught Error: Call to undefined function mysqli_fetch() in C:\xampp\htdocs\gsoft\assigment\tabledb.php:9 Stack trace: #0 {main} thrown in C:\xampp\htdocs\gsoft\assigment\tabledb.php on line 9
if you want fetch without index use mysqli_fetch_assoc().
Or
if you want to fetch with index use mysqli_fetch_array()
And i think you are doing wrong here because your table tag is inside while loop and your <thead> too.
i think it should be outside of while loop. and you have syntax error in your query and you have one extra <th>
your full code.
<?php
$host="localhost";
$user="root";
$pwd="";
$db="assigment";
$conn=mysqli_connect($host,$user,$pwd,$db);
$query="SELECT * FROM tdata ";
$result=mysqli_query($conn,$query); ?>
<table class="table">
<thead>
<tr>
<th>Full Name</th>
<th>Email</th>
<th>Birthday</th>
<th>Gender</th>
<th>Intrests</th>
<th>Address</th>
</tr>
</thead>
<tbody>
<?php while ($row=mysqli_fetch_assoc($result)) { ?>
<tr>
<td><?php echo $row['full_name']; ?></td>
<td><?php echo $row['your table field name']; ?></td>
<td><?php echo $row['your table field name']; ?></td>
<td><?php echo $row['your table field name']; ?></td>
<td><?php echo $row['your table field name']; ?></td>
<td><?php echo $row['your table field name']; ?></td>
</tr>
<?php }
?>
</tbody>
</table>
use mysqli_fetch_array function instead of mysqli_fetch
Use mysqli_fetch_array instead of mysqli_fetch
Also your table is inside the loop

How to stop php 'for loop' from creating multiple table headers

I am creating a table that fetches data from an SQL database using a PDO method. The data loads fine but the issue I'm having is that the 'for loop' I'm using is multiplying a (table header) after every (table row).
I am wondering what a possible solution the the issue could be.
Any help is appreciated, thanks!
Here is the code:
<?php
for($i=0; $row = $result->fetch(); $i++){
?>
<table id="eventstable">
<tr>
<th>Event ID</th>
<th>Event Name</th>
<th>Location</th>
<th>Date</th>
</tr>
<tr>
<td><?php echo $row['event_id']; ?></td>
<td><?php echo $row['event_name']; ?></td>
<td><?php echo $row['event_location']; ?></td>
<td><?php echo $row['event_date']; ?></td>
</tr>
</table>
<?php }
?>
Up at the very top is the connection file that creates a connection to my local database and a statement that brings in the information I want to display from the database like so:
$result = $conn->prepare("SELECT * FROM events");
$result->execute();
Few possible solutions are
i) Put the header part and the table opening and closing tag outside the for loop. This will give you an empty table with headers if there is no data.
ii) Put an if condition and print headers only when i = 0, and put table tags outside the loop. This will give you an empty table with nothing if there is no data.
Edit: Method II (since you are learning)
<table id="eventstable">
<?php
for($i=0; $row = $result->fetch(); $i++){
if($i == 0){
?>
<tr>
<th>Event ID</th>
<th>Event Name</th>
<th>Location</th>
<th>Date</th>
</tr>
<?php }//if statment ends here ?>
<tr>
<td><?php echo $row['event_id']; ?></td>
<td><?php echo $row['event_name']; ?></td>
<td><?php echo $row['event_location']; ?></td>
<td><?php echo $row['event_date']; ?></td>
</tr>
<?php }
?>
I would also suggest since you are learning, use better ways than for loop. Look at the php PDO manuals and see the use of while or foreach. It will help more.
Put in loop only, what should be looped, everything else should be outside of loop.
For example, your code could look like this
<table id="eventstable">
<tr>
<th>Event ID</th>
<th>Event Name</th>
<th>Location</th>
<th>Date</th>
</tr>
<?php
for($i=0; $row = $result->fetch(); $i++){
?>
<tr>
<td><?php echo $row['event_id']; ?></td>
<td><?php echo $row['event_name']; ?></td>
<td><?php echo $row['event_location']; ?></td>
<td><?php echo $row['event_date']; ?></td>
</tr>
<?php
}
?>
</table>
try this :
<table id="eventstable">
<tr>
<th>Event ID</th>
<th>Event Name</th>
<th>Location</th>
<th>Date</th>
</tr>
<?php
foreach($result->fetch() as $row){
?>
<tr>
<td><?php echo $row['event_id']; ?></td>
<td><?php echo $row['event_name']; ?></td>
<td><?php echo $row['event_location']; ?></td>
<td><?php echo $row['event_date']; ?></td>
</tr>
<?php }
?>
</table>

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.

Categories