There are some groups which holds some data for example empty cars, reservated cars and cars which will return.. So if user want to see a cross table between car groups and dates,
I want to create a cross html table like this:
<table class="table table-striped table-bordered table-hover">
<tr>
<th colspan="2"></th>
<th>Date1</th>
<th>Date2</th>
<th>Date3</th>
</tr>
<!--Group1-->
<tr>
<td rowspan="4">Group1</td>
<td>Empty</td>
<td>5</td>
<td>12</td>
<td>3</td>
</tr>
<tr>
<td>On the Res</td>
<td>8</td>
<td>6</td>
<td>6</td>
</tr>
<tr>
<td>Will Return</td>
<td>8</td>
<td>6</td>
<td>6</td>
</tr>
<tr>
<td>Remains</td>
<td>-11</td>
<td>0</td>
<td>-9</td>
</tr>
<!--Group1 Ends -->
<!--Group1-->
<tr>
<td rowspan="4">Group2</td>
<td>Empty</td>
<td>5</td>
<td>12</td>
<td>3</td>
</tr>
<tr>
<td>On the Res</td>
<td>8</td>
<td>6</td>
<td>6</td>
</tr>
<tr>
<td>Will Return</td>
<td>8</td>
<td>6</td>
<td>6</td>
</tr>
<tr>
<td>Remains</td>
<td>-11</td>
<td>0</td>
<td>-9</td>
</tr>
<!--Group1 Ends -->
</table>
And here is the output of this html code:
What is the best php array structure for this kind cross table?
Thanks.
This is bound to get a variety of opinions. For what it's worth, this is one possible structure that would make sense and would be fairly straightforward to code against.
The "groups" could be the top-level keys, and the value for each group would be an array of arrays:
$table_data = [
'Group1' => [
[ 'Title' => 'Empty',
'Date1' => 5,
'Date2' => 12,
'Date3' => 3
],
[ 'Title' => 'On the Res',
'Date1' => 8,
'Date2' => 6,
'Date3' => 6
],
// etc...
],
'Group2' => [
[ 'Title' => 'Empty',
'Date1' => 5,
'Date2' => 12,
'Date3' => 3
],
[ 'Title' => 'On the Res',
'Date1' => 8,
'Date2' => 6,
'Date3' => 6
],
// etc...
],
// etc...
];
My thoughts on the PHP loop would be something like so:
foreach( $table_data AS $group => $rows ) {
$rowspan = count( $rows );
echo '<tr><td rowspan="' . $rowspan . '">' . $group . '</td>';
$tr = '';
foreach( $rows AS $row ) {
echo $tr . '<td>' . implode( '</td><td>', array_values( $row ) ) . '</td></tr>';
$tr = '<tr>';
}
}
Related
Hello please i have a collection grouped as multidimensional arrays with keys
for example :
"book1" {
{
"id" : "1",
"group" : "book1",
"name" : "Book X",
"buy" : "140",
"test" : "test 1",
} ,
{
"id" : "2",
"group" : "book1",
"name" : "Book y",
"buy" : "1200",
"test" : "test 2",
} ,
{
"id" : "3",
"group" : "book1",
"name" : "Book Z",
"buy" : "1330",
"test" : "344",
}
},
"book2" {
{
"id" : "6",
"group" : "book2",
"name" : "Book N",
"buy" : "1220",
"test" : "233",
}
....
}
i want to make a table with spliting rows as the first column has keys (books) and other columns has splited rows for sub array dynamically, for example : (this a html code)
<table>
<tr>
<th>categorie</th>
<th>Name</th>
<th>Buy</th>
</tr>
<tr>
<td>Book 1</td>
<td>Book AA</td>
<td>14</td>
</tr>
<tr>
<td rowspan="2">Book 2</td>
<td>Book BA</td>
<td>12</td>
</tr>
<tr>
<td>Book BB</td>
<td>14</td>
</tr>
<tr>
<td rowspan="2">Book 3</td>
<td>Book CA</td>
<td>22</td>
</tr>
<tr>
<td>Book CB</td>
<td>ยงยง</td>
</tr>
<tr>
<td rowspan="3">Book 4</td>
<td>Book DA</td>
<td>12</td>
</tr>
<tr>
<td>Book DB</td>
<td>122</td>
</tr>
<tr>
<td>Book DC</td>
<td>11</td>
</tr>
</table>
Could you help to make this html example to dynamic table depend of collection and sub arrays ...
And thank you !
i fix it by using this code :
<table class="table table-striped table-inverse table-responsive">
<thead>
<tr>
<th>Book</th>
<th>Name</th>
<th>Category</th>
<th>Buy</th>
</tr>
</thead>
<tbody>
#foreach($collection as $key => $items)
#foreach($items->books->groupBy('categorie') as $book)
#foreach ($book as $index => $item)
#if ($index == "0")
<tr >
<td class="align-middle" rowspan="{{count($book)}}">{{$item->book->category}}</td>
<td >{{$item->name}}</td>
<td>{{$item->buy}}</td>
<td>{{$item->book}}</td>
<td class="align-middle text-center" rowspan="{{count($book)}}">16</td>
</tr>
#else
<tr >
<td >{{$item->name}}</td>
<td>{{$item->buy}}</td>
<td>{{$item->book}}</td>
</tr>
#endif
#endforeach
#endforeach
#endforeach
</tbody>
</table>
And thank for each try to help me
maybe you can use laravel collection and GroupBy method,
like this
$books = Books::all();
$books->groupby('group');
ex :
$collection = collect([
['account_id' => 'account-x10', 'product' => 'Chair'],
['account_id' => 'account-x10', 'product' => 'Bookcase'],
['account_id' => 'account-x11', 'product' => 'Desk'],
]);
$grouped = $collection->groupBy('account_id');
$grouped->toArray();
/*
[
'account-x10' => [
['account_id' => 'account-x10', 'product' => 'Chair'],
['account_id' => 'account-x10', 'product' => 'Bookcase'],
],
'account-x11' => [
['account_id' => 'account-x11', 'product' => 'Desk'],
],
]
*/
and in your blade like this
<table>
<thead>
<tr>
<th>categorie</th>
<th>Name</th>
<th>Buy</th>
</tr>
</thead>
<tbody>
#foreach($grouped as $key => $items)
#foreach(array_chunk($items, 2) as $chunk)
<tr>
#foreach($chunk as $index => $item)
<td #if($index == 0) rowspan="2" #endif>$key</td>
<td>$book->name</td>
<td>$book->buy</td>
#endforeach
</tr>
#endforeach
#endforeach
</tbody>
</table>
I have an html in variable. In this variable I have a SN_NO,CUST_REF_NO,NAME in td. I need to replace this variable with dynamic values from array. The array value like this
$result = array(
array(
'sn_no' => '1',
'cust_ref_no' => 'A123',
'name' => "AAA",
'id'=>'111'
),
array(
'sn_no' => '2',
'cust_ref_no' => 'B123',
'name' => "BBB",
'id'=>'222'
),
array(
'sn_no' => '3',
'cust_ref_no' => 'C123',
'name' => "CCC",
'id'=>'333'
),
);
$tr_html ='<tr>
<td class="text-left" id="abc" width="3%">SN_NO</td>
<td class="text-left" id="abc" style="font-size: 0.6rem;" width="10%">CUST_REF_NO</td>
<td class="text-left" id="abc" style="font-size: 0.6rem;" width="15%">NAME</td>
</tr> ';
I need a final result should be like this
$tr_html ='<tr>
<td class="text-left" id="abc" width="3%">1</td>
<td class="text-left" id="abc" style="font-size: 0.6rem;" width="10%">A123</td>
<td class="text-left" id="abc" style="font-size: 0.6rem;" width="15%">AAA</td>
</tr>
<tr>
<td class="text-left" id="abc" width="3%">2</td>
<td class="text-left" id="abc" style="font-size: 0.6rem;" width="10%">B123</td>
<td class="text-left" id="abc" style="font-size: 0.6rem;" width="15%">BBB</td>
</tr>
<tr>
<td class="text-left" id="abc" width="3%">3</td>
<td class="text-left" id="abc" style="font-size: 0.6rem;" width="10%">C123</td>
<td class="text-left" id="abc" style="font-size: 0.6rem;" width="15%">CCC</td>
</tr>
';
I tried the below code
foreach ($result as $key => $val){
$test = str_replace('SN_NO', $val['sn_no'], $tr_html);
$test .= str_replace('CUST_REF_NO', $val['cust_ref_no'], $tr_html);
$test .= str_replace('NAME', $val['name'], $tr_html);
}
echo $test;
Please help me on this. Thanks in advance
<?php
$result = array(
array(
'sn_no' => '1',
'cust_ref_no' => 'A123',
'name' => "AAA",
'id'=>'111'
),
array(
'sn_no' => '2',
'cust_ref_no' => 'B123',
'name' => "BBB",
'id'=>'222'
),
array(
'sn_no' => '3',
'cust_ref_no' => 'C123',
'name' => "CCC",
'id'=>'333'
),
);
$tr_html ='<tr>
<td class="text-left" id="abc" width="3%">SN_NO</td>
<td class="text-left" id="abc" style="font-size: 0.6rem;" width="10%">CUST_REF_NO</td>
<td class="text-left" id="abc" style="font-size: 0.6rem;" width="15%">NAME</td>
</tr> ';
$finalResult = '';
foreach ($result as $row) {
$tmp = $tr_html;
foreach ($row as $key => $value) {
$tmp = str_replace(strtoupper($key), $value, $tmp);
}
$finalResult .= $tmp;
}
echo $finalResult;
You can use arrays in str_replace() to replace multiple values at once.
$test = '';
foreach ($result as $key => $val){
$test .= str_replace(
array('SN_NO', 'CUST_REF_NO', 'NAME'),
array($val['sn_no'], $val['cust_ref_no'], $val['name']),
$tr_html);
}
echo $test;
I have an array like this:-
$str = array(
array(
'amount' => 1.87,
'user' => 'hello',
),
array(
'amount' => 0.9,
'user' => 'test',
),
array(
'amount' => 9,
'user' => 'hello',
),
array(
'amount' => 1.4,
'user' => 'test',
)
);
Now I show this data in HTML table like this for user 'test' :-
<thead>
<tr>
<th>Amount</th>
<th>User</th>
</thead>
<tbody>
<tr>
<td><?php
foreach ($str as $new_str) {
if ($new_str['user'] == "test") {
echo $new_str['amount'];
echo "<br />";
}
}
?></td><td><?php
foreach ($str as $new_str) {
if ($new_str['user'] == "test") {
echo $new_str['user'];
echo "<br />";
}
}
?></td>
</tr>
</tbody>
But now the problem is that when I use this code it shows the amount and user as a whole instead of two different rows. How can I fix this? Any help?
You just need to move your foreach loop outside of the <tr>...</tr> structure. This should work:
<?php foreach($str as $new_str){
if($new_str['user']=="test"){
echo "<tr><td>" . $new_str['amount'] . "</td><td>" . $new_str['user'] . "</td></tr>";
}
}
?>
Output (for your data)
<tr><td>0.9</td><td>test</td></tr>
<tr><td>1.4</td><td>test</td></tr>
Your tr was not repeating. output image I hope this will help.
<?php
$str = array(
array(
'amount' => 1.87,
'user' => 'hello',
),
array(
'amount' => 0.9,
'user' => 'test' ,
),
array(
'amount' => 9,
'user' => 'hello',
),
array(
'amount' => 1.4,
'user' => 'test',
)
);
?>
<table>
<thead>
<tr>
<th>Amount</th>
<th>User</th>
</tr>
</thead>
<tbody>
<?php foreach($str as $new_str) {
if($new_str['user'] == "test"){
echo '<tr>';
echo '<td>'.$new_str['amount'].'</td>';
echo '<td>'.$new_str['user'].'</td>';
echo '</tr>';
}
} ?>
</tbody>
</table>
I have an array that includes some other arrays. All values which are inside that array, should be placed in a HTML table. I get all the values but my HTML table looks horrible!
I have a code that looks like this:
<?php
$data = array(
'name' => array('Tom', 'Robert', 'Julia'),
'age' => array(32, 45, 21),
'city' => array('New York', 'Toronto', 'Los Angeles')
);
?>
<table>
<tr>
<td>Name</td>
<td>Age</td>
<td>City</td>
</tr>
<tr>
<?php
foreach($data as $values) {
foreach($values as $value) {
echo '<td>' . $value . '</td>';
}
}
?>
</tr>
</table>
The output of this code will be:
<table>
<tr>
<td>Name</td>
<td>Age</td>
<td>City</td>
</tr>
<tr>
<td>Tom</td>
<td>Robert</td>
<td>Julia</td>
<td>32</td>
<td>45</td>
<td>21</td>
<td>New York</td>
<td>Toronto</td>
<td>Los Angeles</td>
</tr>
</table>
This is exactly the output that I need:
<table>
<tr>
<td>Name</td>
<td>Age</td>
<td>City</td>
</tr>
<tr>
<td>Tom</td>
<td>32</td>
<td>New York</td>
</tr>
<tr>
<td>Robert</td>
<td>45</td>
<td>Toronto</td>
</tr>
<tr>
<td>Julia</td>
<td>21</td>
<td>Los Angeles</td>
</tr>
</table>
How can I do this?
Short solution with array_map function:
<?php
$data = array(
'name' => array('Tom', 'Robert', 'Julia'),
'age' => array(32, 45, 21),
'city' => array('New York', 'Toronto', 'Los Angeles')
);
$items = array_map(null, $data['name'], $data['age'], $data['city']);
?>
<table border='1'>
<tr><th>Name</th><th>Age</th><th>City</th></tr>
<?php
foreach ($items as $v) {
echo '<tr><td>' . implode('</td><td>', $v) . '</td></tr>';
}
?>
</table>
The output (push Run code snippet button):
<table border="1">
<tbody><tr><th>Name</th><th>Age</th><th>City</th></tr>
<tr><td>Tom</td><td>32</td><td>New York</td></tr><tr><td>Robert</td><td>45</td><td>Toronto</td></tr><tr><td>Julia</td><td>21</td><td>Los Angeles</td></tr></tbody></table>
Consider reformatting of the $data array to keep related values close together. It will make your foreach then easier.
$data = array(
array('name' => 'Tom', 'age' => 32, 'city' => 'New York'),
array('name' => 'Robert', 'age' => 45, 'city' => 'Toronto'),
//...
);
This way you get the one table data row within one iteration of foreach.
I am writing a word press plugin using short code.
I have a array like this:
$data = array(
0 => array('name' => 'Jonh', 'birth' => 1985, 'number' => 6),
1 => array('name' => 'Marry', 'birth' => 1991, 'number' => 10),
2 => array(same above),
3 => array(same above)
.......................
);
How can I use foreach loop or any ways to return (not echo) a table like this:
<table>
<tr>
<th>Name</th>
<th>Birth</th>
<th>Number</th>
</tr>
<tr>
<td>Show $data[0]['name']</td>
<td>Show $data[0]['birth']</td>
<td>Show $data[0]['number']</td>
</tr>
<tr>
Show all keys and all values of $data same above into each <tr> tags
</tr>
</table>
Thanks a lot!
You can do something like this
$data = array(
0 => array('name' => 'Jonh', 'birth' => 1985, 'number' => 6),
1 => array('name' => 'Marry', 'birth' => 1991, 'number' => 10),
2 => array(same above),
3 => array(same above)
);
$str="<table><tr>";
$str.="<th>Name</th>";
$str.="<th>Birth</th>";
$str.="<th>Number</th></tr>";
foreach ($data as $key => $value) {
# code...
$str.="<tr>";
$str.="<td>Show $data['name']</td>";
$str.="<td>Show $data['birth']</td>";
$str.="<td>Show $data['number']</td></tr>";
}
return $str;
For me the best way is to put your table inside var and create it row by row, like this :
`
$data = array(
0 => array('name' => 'Jonh', 'birth' => 1985, 'number' => 6),
1 => array('name' => 'Marry', 'birth' => 1991, 'number' => 10)
);
$myTable = "
<table>
<tr>
<th>Name</th>
<th>Birth</th>
<th>Number</th>
</tr>";
foreach($data as $key => $value)
{
$myTable .= "
<td>Show ".$value['name']."</td>
<td>Show ".$value['birth']."</td>
<td>Show ".$value['number']."</td>
</tr>";
}
$myTable.="</table>";
echo $myTable; //You can also return it
`