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>
Related
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 want students and events for week to be displayed in calendar format.I have this array as follows. Every student has 7 arrays starting from monday to sunday and inner array of each has events for day
$array = [
'Alex' => [
[
['event' => 'eventName1'],['event' => 'eventName2']
],
[
['event' => 'eventName3'],['event' => 'eventName4']
],
[
['event' => 'eventName5'],['event' => 'eventName6']
],
[
['event' => 'eventName7'],['event' => 'eventName8']
],
[],
[],
[]
],
'Allen' => [
[
['event' => 'eventName'],['event' => 'eventName']
],[
['event' => 'eventName'],['event' => 'eventName']
],
[],
[],
[],
[],
[]
],
];
I need the array to displayed as week calendar,
I don't know where it gone wrong with following code
<table>
<thead>
<tr>
<th>Participant</th>
<th>Monday</th>
<th>Tuesday</th>
<th>Wednesday</th>
<th>Thrusday</th>
<th>Friday</th>
<th>Satday</th>
<th>Sunday</th>
</tr>
</thead>
<tbody>
<?php foreach ($array as $participant => $event): ?>
<?php foreach ($event as $i => $value):?>
<tr>
<?php if ($i === 0): ?>
<td rowspan="2"><?= $participant ?></td>
<?php endif ?>
<?php foreach ($value as $index => $eventD):?>
<td><?php echo $eventD['event']; ?></td>
</tr>
<?php endforeach ?>
<?php endforeach ?>
<?php endforeach ?>
</tbody>
</table>
My problem is the loop and how to print the the events of the particular day.
Can anyone please in building the correct format. thanks for help
<?php
$array = [
'Alex' => [
[
['event' => 'eventName1'],['event' => 'eventName2']
],
[
['event' => 'eventName3'],['event' => 'eventName4']
],
[
['event' => 'eventName5'],['event' => 'eventName6']
],
[
['event' => 'eventName7'],['event' => 'eventName8']
],
[],
[],
[]
],
'Allen' => [
[
['event' => 'eventName'],['event' => 'eventName'],['event' => 'eventName'],['event' => 'eventName'],
],[
['event' => 'eventName'],['event' => 'eventName']
],
[],
[],
[],
[],
[]
],
];
$maxRows = [];
foreach ($array as $participant => $weekEvents) {
$max = 0;
foreach ($weekEvents as $dayEvents) {
if (count($dayEvents) > $max) {
$max = count($dayEvents);
}
}
$maxRows[$participant] = $max;
}
?>
<table border="1">
<thead>
<tr>
<th>Participant</th>
<th>Monday</th>
<th>Tuesday</th>
<th>Wednesday</th>
<th>Thrusday</th>
<th>Friday</th>
<th>Satday</th>
<th>Sunday</th>
</tr>
</thead>
<tbody>
<?php foreach ($array as $participant => $weeklyEvents): ?>
<?php for ($i = 0; $i < $maxRows[$participant]; $i++): ?>
<tr>
<?php if ($i === 0): ?>
<td rowspan="<?php echo $maxRows[$participant]; ?>">
<?php echo $participant; ?>
</td>
<?php endif; ?>
<?php for ($j = 0; $j < 7; $j++): ?>
<td>
<?php $dayEvents = $weeklyEvents[$j]; ?>
<?php if (isset($dayEvents[$i])): ?>
<?php echo $dayEvents[$i]['event']; ?>
<?php else: ?>
<?php endif; ?>
</td>
<?php endfor; ?>
</tr>
<?php endfor; ?>
<?php endforeach; ?>
</tbody>
</table>
Check this html
<table border="1">
<thead>
<tr>
<th>Participant</th>
<th>Monday</th>
<th>Tuesday</th>
<th>Wednesday</th>
<th>Thrusday</th>
<th>Friday</th>
<th>Satday</th>
<th>Sunday</th>
</tr>
</thead>
<tbody>
<?php foreach ($array as $participant => $event): ?>
<tr>
<td><?= $participant ?></td>
<?php foreach ($event as $i => $value): ?>
<td>
<?php foreach ($value as $index => $eventD): ?>
<?php echo $eventD['event'] . '<br/>'; ?>
<?php endforeach ?>
</td>
<?php endforeach ?>
</tr>
<?php endforeach ?>
</tbody>
</table>
Try like this if my understanding is correct
<?php
$array = [
'Alex' => [
[
['event' => 'eventName1'],['event' => 'eventName2']
],
[
['event' => 'eventName3'],['event' => 'eventName4']
],
[
['event' => 'eventName5'],['event' => 'eventName6']
],
[
['event' => 'eventName7'],['event' => 'eventName8']
],
[],
[],
[]
],
'Allen' => [
[
['event' => 'eventName'],['event' => 'eventName']
],[
['event' => 'eventName'],['event' => 'eventName']
],
[],
[],
[],
[],
[]
],
];
echo "<table border='1'>
<thead>
<tr>
<th>Participant</th>
<th>Monday</th>
<th>Tuesday</th>
<th>Wednesday</th>
<th>Thrusday</th>
<th>Friday</th>
<th>Satday</th>
<th>Sunday</th>
</tr>
</thead>
<tbody>";
foreach($array as $key => $data)
{
echo "<tr>
<td>$key</td>";
foreach($data as $data2) {
if(is_array($data2) && isset($data2[0])) {
echo "<td>";
array_walk($data2, function($val, &$dat){
echo $val['event'].', ';
});
echo "</td>";
} else {
echo "<td></td>";
}
}
echo "</tr>";
}
echo "</tbody>
</table>";
?>
fiddle link http://sandbox.onlinephpfunctions.com/
I have been trying to get data from google analytics in laravel. It will show the pathpage and the pageviews in a form of table. I have retrieved the data using spatie/laravel-analytics but when I display it on the view the page views is not tally to the google analytics. So I have been trying to sort the data using the following code but keep getting errors
Here is my controller:
public function google(MediaSite $mediaSite)
{
$ga7day = Analytics::performQuery(
Period::days(7),
'ga:sessions',
[
'metrics' => 'ga:sessions, ga:pageviews',
'dimensions' => 'ga:yearMonth, ga:pagePath',
]
);
$ga1month = Analytics::performQuery(
Period::days(30),
'ga:sessions',
[
'metrics' => 'ga:sessions, ga:pageviews',
'dimensions' => 'ga:yearMonth, ga:pagePath',
]
);
$ga3month = Analytics::performQuery(
Period::months(3),
'ga:sessions',
[
'metrics' => 'ga:sessions, ga:pageviews',
'dimensions' => 'ga:yearMonth, ga:pagePath',
]
);
$analyticsDataToday = Analytics::performQuery(
Period::days(7),
'ga:sessions',
[
'metrics' => 'ga:sessions, ga:pageviews',
'dimensions' => 'ga:yearMonth, ga:pagePath',
]
);
$mediaSite = new MediaSite;
$mediaSite = MediaSite::Where('media_owner_id','1');
foreach ($mediaSite as $site) {
$site->view7days = 0;
foreach ($ga7day as $ga) {
# code...
if($ga['1'] === '/media-site/'. $site->id);
$site->view7days = $ga['3'];
# code...
}
$site->view1month = 0;
foreach ($ga1month as $mga) {
# code...
if($mga['1'] === '/media-site/'. $site->id);
$site->view1month = $mga['3'];
# code...
}
$site->view3month = 0;
foreach ($ga3month as $nga) {
# code...
if($nga['1'] === '/media-site/' . $site->id);
$site->view3month = $nga['3'];
# code...
}
}
return
view('analytics.google',compact('ga7day','ga1month','ga3month',
'analyticsDataToday','site','mediaSite','ga','mga','nga'));
my view:
<body>
<form method="get" action="{{ action('AnalyticsController#google') }}">
{{ csrf_field() }}
<div class="input-group">
<input type="text" name="q" class="form-control" placeholder="eg: Media Ownner" required>
<span class="input-group-btn">
<button type="submit" class="btn btn-default">
Go!
</button>
</span>
</div>
</form>
<br>
<br>
<table class="table">
<tr>
<th> Media Site </th>
<th> 7 days</th>
<th> 1 month</th>
<th> 3 month</th>
<th> Overall</th>
</tr>
<tr>
#foreach ($ga as $index => $key)
<tr> <td> {{ $key['1'] }} </td>
<td> {{ $key['3'] }} </td>
<td> {{ $ga1month[$index]['3'] }}</td>
<td> {{ $ga3month[$index]['3'] }}</td>
<td> {{ $analyticsDataToday[$index]['3'] }}</td></tr>
#endforeach
</tr>
</table>
</body>
</html>
I keep getting these errors:
enter image description here
Replace your controller with the following
public function google(MediaSite $mediaSite) {
$ga7day = Analytics::performQuery( Period::days(7),
'ga:sessions',
[
'metrics' => 'ga:sessions, ga:pageviews',
'dimensions' => 'ga:yearMonth, ga:pagePath',
]
);
$ga1month = Analytics::performQuery( Period::days(30),
'ga:sessions',
[
'metrics' => 'ga:sessions, ga:pageviews',
'dimensions' => 'ga:yearMonth, ga:pagePath',
]
);
$ga3month = Analytics::performQuery( Period::months(3),
'ga:sessions',
[
'metrics' => 'ga:sessions, ga:pageviews',
'dimensions' => 'ga:yearMonth, ga:pagePath',
]
);
$analyticsDataToday = Analytics::performQuery( Period::days(7),
'ga:sessions',
[
'metrics' => 'ga:sessions, ga:pageviews',
'dimensions' => 'ga:yearMonth, ga:pagePath',
]
);
$mediaSite = new MediaSite;
$mediaSite = MediaSite::Where('media_owner_id','1');
foreach ($mediaSite as $site) {
$site->view7days = 0;
foreach ($ga7day as $ga) {
# code...
if($ga['1'] === '/media-site/'. $site->id);
$site->view7days = $ga['3'];
# code...
}
$site->view1month = 0;
foreach ($ga1month as $mga) {
# code...
if($mga['1'] === '/media-site/'. $site->id);
$site->view1month = $mga['3'];
# code...
}
$site->view3month = 0;
foreach ($ga3month as $nga) {
# code...
if($nga['1'] === '/media-site/' . $site->id);
$site->view3month = $nga['3'];
# code...
}
}
return view('analytics.google',compact('mediaSite'));
}
And in the view
#foreach ($meadiaSite as $site)
<tr>
<td> {{ $site['1'] }} </td>
<td> {{ $site->view7days }} </td>
<td> {{ $site->view1month }}</td>
<td> {{ $site->view3month }}</td>
<td> {{ /*same logic*/ }}</td>
</tr>
#endforeach
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.
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>';
}
}