Loop into a multidimensional array and built a table with rowspan - php

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>

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>

Showing data in table row wise by using codeigniter php

How can i show data in two row separately as shown in image
I have tried this but i am not getting what i want. Also i don't want to use two loops separately.
<table class="tbl1">
</thead>
<tbody>
<tr>
<td>
<h1> date</h1> </td>
<?php $i=1; foreach ($student as $value) { $i++;?>
<td>
<?php echo $value[ 'date']; ?>
<?php } ?>
</tr>
<tr>
<td>
<h1>Status</h1> </td>
<?php $i=1; foreach ($student as $value) { $i++; ?>
<td>
<?php echo $value[ 'status']; ?>
</td>
<?php } ?>
</tr>
</tr>
</tbody>
</table>
Do a single loop and in that loop include 2 <tr>s for date and status.
<table class="tbl1">
<tbody>
<?php foreach ($student as $value) { ?>
<tr>
<td><h1> date</h1> </td>
<td><?php echo $value['date']; ?>
</tr>
<tr>
<td><h1>Status</h1></td>
<td><?php echo $value['status']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
Also i don't see the purpose of using $i here and what about the rest of the column in front of date and status.
Or if you want only 2 rows and display all data column wise then you could do it like
<?php
$dateHTML = '';
$statusHTML = '';
foreach ($student as $value) {
$dateHTML .= '<td>'.$value['date'].'</td>';
$statusHTML .= '<td>'.$value['status'].'</td>';
}
?>
<table class="tbl1">
<tbody>
<tr>
<td><h1> date</h1> </td>
<?php echo $dateHTML;?>
</tr>
<tr>
<td><h1>Status</h1></td>
<?php echo $statusHTML;?>
</tr>
</tbody>
</table>

How can i manipulate array with json encoded data?

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>

Print nested associative array keys as row numbers

I have a nested associative array that prints out users data in a table. Here is the code:
<table class="table table-striped">
<thead>
<tr>
<th>#</th>
<th>Prenume</th>
<th>Nume de familie</th>
<th>Email</th>
<th>Telefon</th>
<th>Oras</th>
<th>Adresa</th>
</tr>
</thead>
<tbody>
<?php foreach ($user_data as $arr){ ?>
<tr>
<td>
row number nedded here
</td>
<?php foreach ($arr as $key => $value){ ?>
<td><?php echo $value; ?></td>
<?php } ?>
</tr>
<?php }?>
</tbody>
</table>
I need to display the row number instead at the most left, instead of "row number nedded here"
You could just add a variable as a counter and display that:
<tbody>
<?php $counter=0; foreach ($user_data as $arr){ ?>
<tr>
<td>
<?php echo ++$counter; ?>
</td>
<?php foreach ($arr as $key => $value){ ?>
<td><?php echo $value; ?></td>
<?php } ?>
</tr>
<?php }?>
</tbody>
You can do it like this, if you have not any key you have index number starting from 0.
<tbody>
<?php foreach ($user_data as $key=>$arr){ ?>
<tr>
<td>
<?php echo $key+1 ;?>
</td>
<td>
<?php echo $arr["prenume"];?>
</td>
<td>
<?php echo $arr["nume"];?>
</td>
<td>
<?php echo $arr["email"];?>
</td>
...............
</tr>
<?php }?>
</tbody>

How to foreach and display 2 diffent type in array in same row ?

I have an array include name of the home team and away team . But i don't know how to display it in the same row . My array like this :
[8] => Array
(
[comm_match_team] => localteam
[player_name] => Fraizer Campbell
)
[9] => Array
(
[comm_match_team] => localteam
[player_name] => Jason Puncheon
)
[24] => Array
(
[comm_match_team] => visitorteam
[player_name] => Eden Hazard
)
[25] => Array
(
[comm_match_team] => visitorteam
[player_name] => Nemanja Matic
)
And my code
<tbody>
<?php foreach($teams as $team): ?>
<tr>
<?php if($team['comm_match_team'] == 'localteam'): ?>
<td class="home"><?php echo $team['player_name'] ?></td>
<?php else: ?>
<td class="away"><?php echo $team['player_name'] ?></td>
<?php endif ?>
</tr>
Everytime loop , it add tr tag too , but i want it :
<tr>
<td class="home">Fraizer Campbell</td>
<td class="away">Eden Hazard</td>
</tr>
<tr>
<td class="home">Jason Puncheon</td>
<td class="away">Nemanja Matic</td>
</tr>
Can anyone give me a solution for this problem ? Thanks
<?php
$c1 = [];
$c2 = [];
foreach($teams as $team) {
if($team['comm_match_team'] == 'localteam') {
$c1[] = $team['player_name'];
}
else
{
$c2[] = $team['player_name'];
}
}
$count = count($c1);
?>
<?php for($i = 0; $i < $count; $i++) { ?>
<tr>
<td class="home"><?php echo $c1[$i]; ?></td>
<td class="away"><?php echo $c2[$i]; ?></td>
</tr>
<?php } ?>
<tbody>
<tr>
<?php foreach($teams as $team): ?>
<?php if($team['comm_match_team'] == 'localteam'): ?>
<td class="home"><?php echo $team['player_name'] ?></td>
<?php else: ?>
<td class="away"><?php echo $team['player_name'] ?></td>
<?php endif ?>
//stop foreach here
//then close </tr>
</tr>
You should get your <tr> out of your foreach loop. Right now everytime you code goes trough the loop it adds an <tr> to your table

Categories