How can i manipulate array with json encoded data? - php

I have the array of data like:
Array
(
[0] => Array
(
[lot_no] => ["A001","A001","B002"]
[qty] => ["4","5","6"]
[weight] => ["4","5","6"]
[particular] => ["100% cashmere","100% cashmere","20% silk 80% cashmere"]
[remarks] => ["4","5","6"]
)
)
i want to throw this data in table shown in pic table no 1 like of the second pic. How can i do that?

You can iterate the loop of array in view
<?php if (count($detailListDatas ) > 0): ?>
<table>
<thead>
<tr>
<th>Sno</th>
<th>Particular</th>
<th>Lot no</th>
<th>Total Qty</th>
<th>Total Weight</th>
<th>Remark</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php $i=0;
foreach ($detailListDatas as $row) ?>
<tr>
<td><?php echo $i++?></td>
<td><?php echo $row['particular']; ?></td>
... similarly all the element
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>

I tried your answer which didn't gave me the output i needed. Eventually i did it by myself like this
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>S.No</th>
<th>Particulars</th>
<th>Lot no</th>
<th>Total Qty</th>
<th>Total Weight</th>
<th>Remarks</th>
</tr>
</thead>
<tbody>
<?php $sn =1;
foreach ($detailListDatas as $key ) {
$json_array['lot_no'] = json_decode($key['lot_no'], true);
$lot_no = $json_array['lot_no'];
$i = count($lot_no);
$json_array['qty'] = json_decode($key['qty'],true);
$qty = $json_array['qty'];
$json_array['weight'] = json_decode($key['weight'],true);
$weight = $json_array['weight'];
$json_array['particular'] = json_decode($key['particular'],true);
$particular = $json_array['particular'];
$json_array['remarks'] = json_decode($key['remarks'],true);
$remarks = $json_array['remarks'];
for ($j=0; $j < $i ; $j++) { ?>
<tr>
<td><?php echo $sn?></td>
<td><?php echo $particular[$j];?></td>
<td><?php echo $lot_no[$j];?></td>
<td><?php echo $qty[$j];?></td>
<td><?php echo $weight[$j];?></td>
<td><?php echo $remarks[$j];?></td>
</tr>
<?php $sn++; } ?>
<?php } ?>
</tbody>
</table>

Related

How to Create Table on Html with PHP data Looping

I just want to create a table on HTML with a PHP loop. So, I try to do this:
<table id="tdesign">
<thead>
<tr>
<th>No</th>
<th>Nama</th>
<th>Kelas</th>
</tr>
</thead>
<tbody>
<?php $no = 1; ?>
<?php $kls = 10;?>
<?php for ($i=1; $i <= 10 ; $i++) :?>
<tr>
<td><?php echo $no++; ?></td>
<td>Name <?php echo $i; ?></td>
<?php endfor; ?>
<?php for ($j=10; $j >= 1 ; $j--) : ?>
<td><?php echo "Class ". $j . "\n" ;?></td>
<?php endfor; ?>
</tr>
</tbody>
</table>
But, why the output becomes this?
Assuming you have an array like this:
[0] => Array
(
[stud_id] => 1234
[name] => John Doe
[class] => Class 1
)
[1] => Array
(
[stud_id] => 2345
[name] => Jane Doe
[class] => Class 2
)
My table loop will be look like this:
<table class="table table-striped table-hover">
<thead>
<tr>
<th>Student ID</th>
<th>Student Name</th>
<th>Class</th>
</tr>
</thead>
<tbody>
<?php
foreach($array as $data) {
?>
<tr>
<td><?=$data['stud_id']?></td>
<td><?=$data['name']?></td>
<td><?=$data['class']?></td>
</tr>
<?php } ?>
</tbody>
</table>
You must put the tbody contents (tr, td) inside the loop
It's because you've got a loop inside a loop.
Try this instead:
<table id="tdesign">
<thead>
<tr>
<th>No</th>
<th>Nama</th>
<th>Kelas</th>
</tr>
</thead>
<tbody>
<?php $no = 1; ?>
<?php $kls = 10;?>
<?php for ($i=1; $i <= 10 ; $i++) :?>
<tr>
<td><?php echo $no++; ?></td>
<td>Name <?php echo $i; ?></td>
<td><?php echo "Class ". 10-$i . "\n" ;?></td>
</tr>
<?php endfor; ?>
</tbody>
</table>
Assuming that you really need two nested loops.
You need to move the endfor to the end of the loop, otherwise there will be <tr> before the 2nd loop
So
<table id="tdesign">
<thead>
<tr>
<th>No</th>
<th>Nama</th>
<th>Kelas</th>
</tr>
</thead>
<tbody>
<?php $no = 1; ?>
<?php $kls = 10;?>
<?php for ($i=1; $i <= 10 ; $i++) :?>
<tr>
<td><?php echo $no++; ?></td>
<td>Name <?php echo $i; ?></td>
<?php for ($j=10; $j >= 1 ; $j--) : ?>
<td><?php echo "Class ". $j . "\n" ;?></td>
<?php endfor; ?>
<?php endfor; ?>
</tr>
</tbody>
</table>

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>

DataTables - PHP while loop issue can't find the last result and add new TR

I am using DataTables and I want to add new TR at the end of while loop.
I know we can add <tfoot></tfoot>, but I don't want to add '' because I am filtering data with custom Ajax.
I have tried below code but it's not working:
<?php
$Itesres = mysqli_query($con_db,"SELECT * FROM tbl_area ORDER BY `tbl_area`.`name` ASC");
while($ItemResult = mysqli_fetch_array($Itesres)){
?>
<table id="printData" class="table table-bordered table-hover ">
<thead>
<tr>
<th>Group</th>
<th>Party Name</th>
<th>Balance</th>
</tr>
</thead>
<tbody id="getGroups">
<?php
$i = 1;
while($row = mysqli_fetch_array($sdetails)){
$totalAmount += $row['total_debtors'];
$i++;
?>
<tr>
<td><?php echo getAreaName($row['area_id']); ?></td>
<td><?php echo GrabAccountIDName($row['client_id']); ?></td>
<td><?php echo abs($row['total_debtors']); ?></td>
</tr>
<?php if( $i == ( $numRows - 1 ) ) { ?>
<tr>
<td> </td>
<td style="text-align:right">Total:</td>
<td><?php echo abs($totalAmount); ?></td>
</tr>
<?php } } ?>
</tbody>
</table>
Also, when I use <tfoot></tfoot> it's not printable.
Probably your problem is in $numRows which is not defined.
So you can try this:
<?php
$Itesres = mysqli_query($con_db,"SELECT * FROM tbl_area ORDER BY `tbl_area`.`name` ASC");
$numRows = mysqli_num_rows($Itesres);
while($ItemResult = mysqli_fetch_array($Itesres)){
?>
<table id="printData" class="table table-bordered table-hover ">
<thead>
<tr>
<th>Group</th>
<th>Party Name</th>
<th>Balance</th>
</tr>
</thead>
<tbody id="getGroups">
<?php
$i = 1;
while($row = mysqli_fetch_array($sdetails)){
$totalAmount += $row['total_debtors'];
$i++;
?>
<tr>
<td><?php echo getAreaName($row['area_id']); ?></td>
<td><?php echo GrabAccountIDName($row['client_id']); ?></td>
<td><?php echo abs($row['total_debtors']); ?></td>
</tr>
<?php if( $i == ( $numRows - 1 ) ) { ?>
<tr>
<td> </td>
<td style="text-align:right">Total:</td>
<td><?php echo abs($totalAmount); ?></td>
</tr>
<?php } } ?>
</tbody>
</table>

Loop into a multidimensional array and built a table with rowspan

I have this array:
Array
(
[France] => Array
(
[0] => Array
(
[city] => Paris
)
)
[Canada] => Array
(
[0] => Array
(
[city] => Montreal
)
[1] => Array
(
[city] => Ottawa
)
)
)
Sometimes, like you can see a country can have one city (case for France) but sometimes the country can have more than one city (case for Canada).
I'm looking to have this final output:
<table>
<thead>
<tr>
<th>Country</th>
<th>Cities</th>
</tr>
</thead>
<tbody>
<tr>
<td rowspan="1">France</td>
<td>Paris</td>
</tr>
<tr>
<td rowspan="2">Canada</td>
<td>Montreal</td>
</tr>
<tr>
<td>Toronto</td>
</tr>
</tbody>
</table>
Here's what I have actually:
foreach($countries as $country => $city) {
$count = count($country) ;
if($count == 1) {
echo '
<tr>
<td rowspan="1">'.$country.'</td>
<td>'.$city.'</td>
</tr>
'
}
else {
echo '
<tr>
<td rowspan="'.$count.'">'.$country.'</td>
<td>'.$city.'</td>
</tr>
<tr>
<td>'.$city.'</td>
</tr>
'
}
}
My problem is the loop and how to print the cities.
Thanks for any help.
Because you are creating a row for each city, you must loop over each city. Determining the rowspan value and whether to show the first city on the same row is simply based upon whether its the first iteration of the country's cities.
So the following code will produce your desired result:
<?php
$array = [
'France' => [
[
'city' => 'Paris'
]
],
'Canada' => [
[
'city' => 'Montreal'
], [
'city' => 'Ottawa'
],
],
];
?>
<table>
<thead>
<tr>
<th>Country</th>
<th>Cities</th>
</tr>
</thead>
<tbody>
<?php foreach ($array as $country => $city): ?>
<?php foreach (array_values($city) as $i => $value): ?>
<tr>
<?php if ($i === 0): ?>
<td rowspan="<?= count($city) ?>"><?= $country ?></td>
<?php endif ?>
<td><?= $value['city'] ?></td>
</tr>
<?php endforeach ?>
<?php endforeach ?>
</tbody>
</table>
https://3v4l.org/vp2bl
Result:
<table>
<thead>
<tr>
<th>Country</th>
<th>Cities</th>
</tr>
</thead>
<tbody>
<tr>
<td rowspan="1">France</td>
<td>Paris</td>
</tr>
<tr>
<td rowspan="2">Canada</td>
<td>Montreal</td>
</tr>
<tr>
<td>Ottawa</td>
</tr>
</tbody>
</table>
For rawspan you have to check the count for the city not the count of the country.
You have to use two foreach one to country and one to city.
Below code is written using php7, for array it uses []. if you are using php 5.6 use the array definition as array().
<table>
<thead>
<tr>
<th>Country</th>
<th>Cities</th>
</tr>
</thead>
<tbody>
<?php $array = ['france' => ['paris'], 'canada' => ['montreal', 'ottawa']]; ?>
<?php foreach ($array as $country => $cities) { ?>
<tr>
<td rowspan="<?php echo (count($cities)) ?>"><?php echo $country ?></td>
<?php foreach ($cities as $index => $city) { ?>
<?php if ($index === 0) { ?>
<td><?php echo $city ?> </td></tr>
<?php } else { ?>
<tr>
<td><?php echo $city ?></td>
</tr>
<?php } ?>
<?php } ?>
<?php } ?>
</tbody>
</table>
<thead>
<tr>
<th>Country</th>
<th>Cities</th>
</tr>
</thead>
<tbody>
<?php $array = ['France' => [['city' => 'Paris']], 'Canada' => [['city' => 'Montreal'], ['city' => 'Ottawa']]]; ?>
<?php foreach ($array as $country => $cities) { ?>
<tr>
<td rowspan="<?php echo (count($cities)) ?>"><?php echo $country ?></td>
<?php foreach (array_values($cities) as $index => $value) { ?>
<?php if ($index === 0) { ?>
<td><?php echo $value['city'] ?> </td></tr>
<?php } else { ?>
<tr>
<td><?php echo $value['city'] ?></td>
</tr>
<?php } ?>
<?php } ?>
<?php } ?>
</tbody>
Try this. it should work fine
<?php
$france = array();
$canada = array();
$city1 = array('city'=>'Paris');
$city2 = array('city'=>'Montreal');
$city3 = array('city'=>'Ottawa');
array_push($france,$city1);
array_push($canada,$city2);
array_push($canada,$city3);
$country = array('France'=>$france,'Canada'=>$canada);
<table border="1" cellpadding="5" cellspacing="0">
<thead>
<th>Country </th><th> City </th>
</thead>
<tbody><?php
$flag=0;
foreach($country as $cN=>$city):?>
<tr><td rowspan="<?php echo count($city);?>"> <?php echo $cN;?></td>
<?php
if(count($city)>1)
{
foreach($city as $ckey=>$name):
if($flag)
{
?>
<tr><td> <?php echo $name['city'];?></td></tr><?php
}
else
{?>
<td> <?php echo $name['city'];?></td><?php
}
$flag = 1;
endforeach;
}
else
{
foreach($city as $ckey=>$name):?>
<td> <?php echo $name['city'];?></td><?php
endforeach;
}
?></tr><?php
endforeach;
?></tbody></table>

How to set different styles for different rows in html table created from php array?

I'm struggling with creating a html table from php array, while applying different css classes on different rows according to the array.
I have this code in data.php:
<?php
$data = array(
array('Title 1'=>'text11', 'Title 2'=>'text12'),
array('Title 1'=>'text21', 'Title 2'=>'text22'),
array('Title 1'=>'text31', 'Title 2'=>'text32'),
array('Title 1'=>'text41', 'Title 2'=>'text42', 'special'=>'style1'),
array('Title 1'=>'text51', 'Title 2'=>'text52', 'special'=>'style2'),
);
?>
I want to create a html table from this array, and if the array contains 'special'=>'style', it would set that style to that particular row. This is my code so far:
<?php include('data.php'); ?>
<table>
<thead>
<tr>
<th>Title 1</th>
<th>Title 2</th>
</tr>
</thead>
<tbody>
<?php foreach ($data as $key=>$row):
if ($row == 'class1') {
$class='class="style1"';
} elseif ($row == 'class1') {
$class='class="style2"';
} else {
$class='';
}?>
<tr <?php echo $class ?>>
<td><?php echo implode('</td><td>', $row); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
And this is the desired output:
<table>
<thead>
<tr>
<th>Title 1</th>
<th>Title 2</th>
</tr>
</thead>
<tbody>
<tr>
<td>text11</td><td>text12</td>
</tr>
<tr>
<td>text21</td><td>text22</td>
</tr>
<tr>
<td>text31</td><td>text32</td>
</tr>
<tr class="style1">
<td>text41</td><td>text42</td>
</tr>
<tr class="style2">
<td>text51</td><td>text52</td>
</tr>
</tbody>
</table>
Your problem is this line:
<?php foreach ($data as $key=>$row):
You're forgetting that you're looping over a multidimensional array (i.e. an array of arrays) so that $row is an array here. On the next line:
if ($row == 'class1')
You're looking for a comparison between a string and $row which is an array. This will never work! As Daniel points out in his answer, you need to look at the contents of the array.
Don't forget you'll need to remove the special element from the array before displaying it. Personally, I'd condense the code a bit, though mixing PHP and HTML like this is never a good idea.
<?php include('data.php'); ?>
<table>
<thead>
<tr>
<th>Title 1</th>
<th>Title 2</th>
</tr>
</thead>
<tbody>
<?php foreach ($data as $row): $class = $row["special"] ?? ""; unset($row["special"]);?>
<tr class="<?=$class?>">
<td><?=implode("</td><td>", $row)?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
I'm not sure I understood everything but you can try this
<?php foreach ($data as $key => $row):
$class = '';
if(isset($row['special'])){
$class = 'class="'.$row['special'].'"';
unset($row['special']);
}
?>
<tr <?php echo $class ?>>
<td><?php echo implode('</td><td>', $row); ?></td>
</tr>
<?php endforeach; ?>
If I understand your question correctly, this should do the job.
The result is as you desire, see: Online PHP shell
<table>
<thead>
<tr>
<th>Title 1</th>
<th>Title 2</th>
</tr>
</thead>
<tbody>
<?php foreach ($data as $key=>$row):
if (isset($row["special"])) {
$class = " class='" . $row["special"]. "'";
unset($row["special"]);
} else {
$class='';
}?>
<tr<?php echo $class ?>>
<td><?php echo implode('</td><td>', $row); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>

Categories