I have a little problem , I hope you can help me. I want to create a table with some details, about name,surname, age etc...
One array is made by:
array:2 [▼
0 => array:9 [▼
"ID" => 424
"name" => "Alex"
"surname" => "Pippo"
"age" => 23
]
1 => array:9 [▼
"ID" => 424
"name" => "Pippo"
"surname" => "James"
"age" => "21"
]
and I have another multidimensional array as follow:
0 => array:2 [▼
0 => array:3 [▼
"class" => "VA"
"sport" => "football"
"height" => "1.55"
]
1 => array:3 [▼
"class" => "VA"
"sport" => "basketball"
"height" => "1.55"
]
]
1 => array:1 [▼
0 => array:3 [▼
"class" => "VB"
"sport" => "none"
"height" => "1,76"
]
]
]
I want to have have a table in which i have details about students and their sports. So in the view i got:
<table border="1" cellspacing="5">
<th >ID</th>
<th >Name</th>
<th >surName</th>
<th >age</th>
#foreach($data as $datas)
<tr>
<td><?php echo($datas['id']) ?></td>
<td><?php echo($datas['name']) ?></td>
<td><?php echo($datas['surname']) ?></td>
<td><?php echo($datas['age']) ?></td>
<table>
<th>class</th>
<th>sport</th>
<th>age</th>
<tr>
#foreach($data1 as $datas1)
#foreach($datas1 as $key => $value)
<td ><?php echo($value['class']) ?></td>
<td ><?php echo($value['sport']) ?> </td>
<td ><?php echo($value['height']) ?> </td>
</tr>
</table>
#endforeach
#endforeach
#endforeach
</tr>
</table>
I want to have two tables, one inside the other one.
ID Name surName Age
424 Alex Pippo 23
Class Sport Height
VA Football 1,55
VA basketball 1,55
VB none 1,76 *THIS BELONGS TO THE SECOND STUDENT
ID Name surName Age
425 Pippo James 22
Class Sport Height
VA Football 1,55 *THIS BELONGS TO THE FIRST STUDENT
VA basketball 1,55 *THIS BELONGS TO THE FIRST STUDENT
VB none 1,76
i just want for the first one the first 2 rows, and for the second one the third row.. Please help..
Based on your data, it seems that the key of $data1 is the same as the key to your student ($data). So, you need to connect both of them to get their relevant data.
The first step, you need to get the key to your student:
#foreach($data as $keys => $datas)
and only loop this student detail:
#foreach($data1[$keys] as $key => $value)
<tr>
<td ><?php echo($value['class']) ?></td>
<td ><?php echo($value['sport']) ?></td>
<td ><?php echo($value['height']) ?></td>
</tr>
#endforeach
Related
im having some issues with a array data that i got form a json file, but i guess the issue maybe is with the json structure that that i converted in array using json decode and file_get_contents.
Basicall this is the array strucutre:
#items: array:4 [▼
"Monday, 1 de Fev de 2021" => array:5 [▼
"PM" => array:7 [▼
1 => "1140-10"
2 => "8498-25"
3 => "7076-19"
4 => "3380-20"
5 => "8194-24"
6 => "8288-22"
7 => "687-22"
]
"PT" => array:7 [▼
1 => "6406-2"
2 => "2976-19"
3 => "6029-8"
4 => "8130-8"
5 => "7530-8"
6 => "1071-18"
7 => "064-16"
]
"PTV" => array:7 [▶]
"PTN" => array:7 [▶]
]
"Sat, 31 de Jan de 2021" => array:2 [▼
"PTM" => array:7 [▶]
"PT" => array:7 [▶]
]
Basically the date is the table caption, and the index with the letters eg: "PTV","PM","PT"...", are the theader th titles.
And inside of the "PM" for example there are some results:
1 => "1140-10"
2 => "8498-25"
3 => "7076-19"
4 => "3380-20"
5 => "8194-24"
6 => "8288-22"
7 => "687-22"
Where the indexes (1,2,3..) Are the prize, and the values after each index are the results.
I need to construct my table to be like this image above
This is my code:
#if(sizeof($results) > 0)
#foreach($results as $date => $result)
<div class="col-xl-12">
<table class="table">
<caption>
{{ $date }}
</caption>
<thead>
<tr>
<th id="hoje" class="tabla-header"></th>
#foreach($result as $banca => $re)
<th id="{{ $banca }}" class="tabla-header">{{ $banca }}</th>
#endforeach
</tr>
</thead>
<tbody>
{{ sizeof($result) }}
#for ($i = 0; $i < 7; $i++)
<tr>
<td>1</td>
#for ($n = 0; $n < sizeof($result); $n++)
<td>{{ $result[] }}/td>
#endfor
</tr>
#endfor
</tbody>
</table>
</div>
#endforeach
#endif
create a database and insert into table information . next with console make:controller // make a controller
make a method ...( select information of database)
$var1 = DB::table("table_name")->get('column_1');// first use DB namespace
$var2 = DB::table("table_name")->get('column_2');
$var2 = $var2[
// and other get columns table
return view('view_name',['tr1' => $var1,'tr2' => $var2 /* and etc */);
}
and view is under code
<!DOCTYPE htl>
<html>
<table class="table"> <!-- table class ; you create a table class in your code -->
<tr>
<th>{{$var1[0][0]->column_name}}</th>
<th>{{$var1[0][1]->column_name}}</th>
<th>{{$...}}</th>
</tr>
<tr>
<th> {{$avr2[0][0]->column_name}} </th>
<th> {{$var2[0][1]->coumn_name}} </th>
<!-- and continue above code -->
</tr>
</table>
</html>
first you select info of db and show them in view
I am sending 4 arrays to my controller that they need to combine before I can store them to database, here is how they're look like:
array:4 [▼
"from" => array:7 [▼
0 => "8:00"
1 => null
2 => null
3 => null
4 => null
5 => null
6 => null
]
"to" => array:7 [▼
0 => "21:00"
1 => null
2 => null
3 => null
4 => null
5 => null
6 => null
]
"closed" => array:6 [▼
0 => "on"
1 => "on"
2 => "on"
3 => "on"
4 => "on"
5 => "on"
]
"day_id" => array:7 [▼
0 => "a0275acb-c6e5-428f-8589-da0644c1f42e"
1 => "9a8d2dc5-fbe5-4a93-a279-2c6a90781777"
2 => "84d74f04-3728-4314-a6e7-8710cecaa911"
3 => "79ef140a-5de5-4b27-a286-9893cbac820e"
4 => "6d0b2858-ff96-46ce-ae1b-665c3ee353aa"
5 => "38225fe6-3bce-4ee9-bad8-c7f6cacc4854"
6 => "28cef2be-1ff6-4e80-b322-a208f4838391"
]
]
As you might notice closed array is less by 1 value as the checkbox was unchecked, so this one is vary between no value (if none are check) to 7 values (if all of them are checked)
Anyway, I need to make this arrays to something like this:
array:7 [▼
"finaldata" => array:4 [▼
from => "8:00"
to => "21:00"
closed => off
day_id => "a0275acb-c6e5-428f-8589-da0644c1f42e"
],
"finaldata" => array:4 [▼
from => null
to => null
closed => on
day_id => "9a8d2dc5-fbe5-4a93-a279-2c6a90781777"
],
// and so on...
]
If I can get such result then I'll be able to loop and store this data into my database.
Code
$schedules = [];
$schedules['from'] = $request->input('from');
$schedules['to'] = $request->input('to');
$schedules['closed'] = $request->input('closed');
$schedules['day_id'] = $request->input('day_id');
dd($schedules); // returning my results above (on top)
foreach($schedules as $schedule) {
$days = new WorkDays;
//...
$days->save();
}
Question
How can I get such result as I shared to store my data?
Update
blade
<div class="mt-4">
<div x-data="handler()">
<x-jet-label value="Schedule" />
<table class="itable align-middle w-full">
<thead class="thead-light">
<tr>
<th>Day</th>
<th>From</th>
<th>To</th>
<th>Closed</th>
</tr>
</thead>
<tbody>
<template x-for="(field, index) in {{$days}}" :key="index">
<tr>
<td x-text="field.name"></td>
<td>
<x-jet-input x-model="field.from" type="text" name="from[]" />
<x-jet-input x-model="field.id" type="hidden" value="field" name="day_id[]"/>
</td>
<td><x-jet-input x-model="field.to" type="text" name="to[]" /></td>
<td><input x-model="field.closed" class="form-checkbox" type="checkbox" name="closed[]" /></td>
</tr>
</template>
</tbody>
</table>
</div>
<script>
function handler() {
return {
fields: []
}
}
</script>
</div>
Update 2
based on iCoders answer this is what i get, which is good but the ones with unchecked checkbox will not have closed variable array 0 and 6.
"data" => array:7 [▼
0 => array:3 [▼
"from" => "54"
"day_id" => "a0275acb-c6e5-428f-8589-da0644c1f42e"
"to" => "474"
]
1 => array:4 [▼
"from" => null
"day_id" => "9a8d2dc5-fbe5-4a93-a279-2c6a90781777"
"to" => null
"closed" => "on"
]
2 => array:4 [▼
"from" => null
"day_id" => "84d74f04-3728-4314-a6e7-8710cecaa911"
"to" => null
"closed" => "on"
]
3 => array:4 [▼
"from" => null
"day_id" => "79ef140a-5de5-4b27-a286-9893cbac820e"
"to" => null
"closed" => "on"
]
4 => array:4 [▼
"from" => null
"day_id" => "6d0b2858-ff96-46ce-ae1b-665c3ee353aa"
"to" => null
"closed" => "on"
]
5 => array:4 [▼
"from" => null
"day_id" => "38225fe6-3bce-4ee9-bad8-c7f6cacc4854"
"to" => null
"closed" => "on"
]
6 => array:3 [▼
"from" => "67565"
"day_id" => "28cef2be-1ff6-4e80-b322-a208f4838391"
"to" => "6567"
]
I dont know much usage about new component in laravel 8 but usually common idea is
#foreach($days as $key=>$value)
<tr>
<td x-text="field.name"></td>
<td>
<x-jet-input x-model="field.from" type="text" name="data[{{$key}}][from]" />
<x-jet-input x-model="field.id" type="hidden" value="field" name="data[{{$key}}][day_id]"/>
</td>
<td><x-jet-input x-model="field.to" type="text" name="data[{{$key}}][to]" /></td>
<td><input x-model="field.closed" class="form-checkbox" type="checkbox" name="data[{{$key}}][closed]" /></td>
</tr>
#endforeach
Then in controller if you dd($request->data) you get merged data
Updated
As per another question answer Dynamically set name attribute of input field in loop with AlpineJS
You need to use x-bind:name with a template string:
<template x-for="(field, index) in {{$days}}" :key="index">
<tr>
<td x-text="field.name"></td>
<td>
<x-jet-input x-model="field.from" type="text" x-bind:name="`data[${index}][from]`/>
<x-jet-input x-model="field.id" type="hidden" value="field" x-bind:name="`data[${index}][day_id]`"/>
</td>
<td><x-jet-input x-model="field.to" type="text" x-bind:name="`data[${index}][to]` /></td>
<td><input x-model="field.closed" class="form-checkbox" type="checkbox" x-bind:name="`data[${index}][closed]` /></td>
</tr>
</template>
Update2:
<input x-model="field.closed" class="form-checkbox" type="checkbox" name="data[{{$key}}][closed]" #click="$event.target.value = $event.target.checked ? '1' : '2'"/>
Change your blade to something like this.
<template x-for="(field, index) in {{$days}}" :key="index">
<tr>
<td x-text="field.name"></td>
<td>
<x-jet-input x-model="field.from" type="text" name="finaldata[{{$loop->iteration}}]['from']" />
<x-jet-input x-model="field.id" type="hidden" value="field" name="finaldata[{{$loop->iteration}}]['day_id']"/>
</td>
<td><x-jet-input x-model="field.to" type="text" name="finaldata[{{$loop->iteration}}]['to']" /></td>
<td><input x-model="field.closed" class="form-checkbox" type="checkbox" name="finaldata[{{$loop->iteration}}]['closed']" /></td>
</tr>
</template>
I have the below arrays and I want to get it in laravel blade as HTML table
array:2 [▼
"chest" => array:2 [▼
"Chest Press" => array:1 [▼
0 => "1"
]
"Flys" => array:2 [▼
0 => "3"
1 => "4"
]
]
"hints" => array:2 [▼
"Chest Press" => array:1 [▼
0 => "test1"
]
"Flys" => array:1 [▼
0 => "test2"
]
]
]
I try the below but I dont get right HTML table, its correct the first two columns but the third columnt is not, any ideas how to print it on HTML table
<table class="table table-striped table-hover table-reflow">
<thead>
<tr>
<th>Exercises</th>
<th>Days</th>
<th>Hints</th>
</tr>
</thead>
<tbody>
#foreach($chests['chest'] as $chest => $exc)
<tr>
<td>{{$chest}}</td>
#foreach($exc as $key => $value)
<td>
<strong>{{$value}},</strong>
</td>
#endforeach
#endforeach
#foreach($chests['hints'] as $hint => $hin)
#foreach($hin as $key => $value)
<td>
<strong>{{$value}}</strong>
</td>
#endforeach
</tr>
#endforeach
</tbody>
Answered
<tbody>
#foreach ($chests['chest'] as $exerciseName => $daysArray)
<tr>
<td>{{ $exerciseName }}</td>
#foreach ($daysArray as $day)
<td >{{ $day }}</td>
#endforeach
<td>{{ $chests['hints'][$exerciseName][0] }}</td>
</tr>
#endforeach
</tbody>
https://laracasts.com/discuss/channels/laravel/laravel-blade-multidimensional-array
I have completely changed the format of my array now, and I had what I hope is a simple misunderstanding on my part. So my array now looks like the following
array:9 [▼
0 => array:4 [▼
"leadData" => array:7 [▼
"LeadID" => "1232806"
"Client" => "Some Client"
"LeadName" => "Test"
"Owner" => "Someone"
"Value" => "2160.00"
]
"clientData" => array:2 [▼
"Prospect" => "No"
]
"quoteData" => array:8 [▼
"QuoteID" => "Q0020"
"ProjectName" => "Test"
"Amount" => "1234"
]
"customData" => array:2 [▼
0 => array:1 [▼
"Type" => "New"
]
1 => array:1 [▼
"Month" => "June 16"
]
]
]
2 => array:4 [
...
]
]
So it is essentially now 4 inner arrays. Now if I do the following, I can print out all the data for the leadData inner array
foreach($leadArray as $array)
<tr>
foreach($array['leadData'] as $leadKey => $leadData)
<td>
{{ $leadData }}
</td>
endforeach
</tr>
endforeach
That works fine. However, I only want to display certain parts of this array. I would have presumed doing something like the following would work
foreach($leadArray as $array)
<tr>
foreach($array['leadData'] as $leadKey => $leadData)
<td>
{{ $leadData['LeadID'] }}
</td>
<td>
{{ $leadData['LeadName'] }}
</td>
endforeach
</tr>
endforeach
However if I do this I get and Illegal String Offset error. Is this not how I would access this data?
p.s. Ignore the way I do the foreach loop etc, this is because I am using a template engine.
Thanks
You don't have to loop over the second array, you can use the keys to get the data.
foreach($leadArray as $array)
<tr>
<td>
{{ $array['leadData']['LeadID'] }}
</td>
<td>
{{ $array['leadData']['LeadName'] }}
</td>
</tr>
endforeach
Your main array will be like this, where you want to work, right?
<pre>
$aMainArray = array(
0 => array(
"leadData" => array(
"LeadID" => "1232806",
"Client" => "Some Client",
"LeadName" => "Test",
"Owner" => "Someone",
"Value" => "2160.00",
)
)
);
foreach ($aMainArray AS $aSubArray) {
print_r($aSubArray);
// You can echo your required values like below
echo $aSubArray['leadData']['LeadID'];
echo $aSubArray['leadData']['LeadName'];
// OR like this one
foreach ($aSubArray AS $value) {
echo $value['LeadID'];
echo $value['LeadName'];
}
}
</pre>
I have an array like so:
array:55 [▼
0 => array:13 [▼
"product_id" => "1"
"name" => "Glowing Antique Multi-Color Necklace And Hangings"
"product_code" => "DIV-COL-0001-0001"
"option_code" => ""
"net_price" => "693.00"
"sellerCommission" => "450.00"
"moderatorCommission" => "14.00"
"principalModeratorCommission" => "17.00"
"affiliateCommission" => "28.00"
"accountType" => "affiliate"
]
25 => array:13 [▼
"product_id" => "24"
"name" => "Blue Colored Cotton Zip Pouch (3 Nos.)"
"product_code" => "PRA-KEN-0002-0006"
"option_code" => ""
"net_price" => "184.62"
"sellerCommission" => "120.00"
"moderatorCommission" => "4.00"
"principalModeratorCommission" => "5.00"
"affiliateCommission" => "7.00"
"accountType" => "affiliate"
]
26 => array:13 [▼
"product_id" => "25"
"name" => "Side Hanging Purse (2 Nos.)"
"product_code" => "PRA-KEN-0002-0007"
"option_code" => ""
"net_price" => "184.62"
"sellerCommission" => "120.00"
"moderatorCommission" => "4.00"
"principalModeratorCommission" => "5.00"
"affiliateCommission" => "7.00"
"accountType" => "affiliate"
]
44 => array:13 [▼
"product_id" => "24"
"name" => "Blue Colored Cotton Zip Pouch (3 Nos.)"
"product_code" => "PRA-KEN-0002-0006"
"option_code" => ""
"net_price" => "184.62"
"sellerCommission" => "120.00"
"moderatorCommission" => "4.00"
"principalModeratorCommission" => "5.00"
"affiliateCommission" => "7.00"
"accountType" => "principal_moderator"
]
// And the list continues ...
]
The code that I have tried so far, works correctly to a certain extent.
<tr>
<th style="vertical-align: top;">Id</th>
<th style="vertical-align: top;">Details</th>
<th style="vertical-align: top;">Net Price</th>
<th style="vertical-align: top;">Invoicer<br /> Commission</th>
<th style="vertical-align: top;">Moderator Commission</th>
<th style="vertical-align: top;">Principal Moderator Commission</th>
<th style="vertical-align: top;">Affiliate Commission</th>
</tr>
#foreach($products as $product)
<tr>
<td>{{ $product['product_id'] }}</td>
<td>
<img src="{{ $product['image'] }}" alt="{{ $product['name'] }}" class="pull-left" style="margin-right: 15px">
{{ $product['name'] }}<br />
Product Code: {{ $product['product_code'] }}<br />
#if($product['option_code'] !== '')
Option Code: {{ $product['option_code'] }}
#endif
</td>
<td class="text-right">{{ $product['net_price'] }}</td>
#if($product['accountType'] === 'seller')
<td class="text-right">{{ number_format($product['sellerCommission'], 2) }}</td>
#else
<td class="text-right">--</td>
#endif
#if($product['accountType'] === 'moderator')
<td class="text-right">{{ number_format($product['moderatorCommission'], 2) }}</td>
#else
<td class="text-right">--</td>
#endif
#if($product['accountType'] === 'principal_moderator')
<td class="text-right">{{ number_format($product['principalModeratorCommission'], 2) }}</td>
#else
<td class="text-right">--</td>
#endif
#if($product['accountType'] === 'affiliate')
<td class="text-right">{{ number_format($product['affiliateCommission'], 2) }}</td>
#else
<td class="text-right">--</td>
#endif
</tr>
#endforeach
The above code works, but it adds a new row to the table which is what I am not looking for. I do not want to add a new row, instead I would like to display the required data in that row only.
Meaning, for the product_id = 24, (except for the option code), there are 2 accountType attached viz., affiliate and principal_moderator. I would like to populate a single row with that data instead of 2 different rows.
How do I achieve it ??
I know this must be far easy, but I have failed to solve it, because I am still at the learning stage.
Any help is highly appreciated. Thanks.
P.S.: All the values are coming from the database, and the number of child arrays can be infinite.
You should group rows and make accountType property an array. Basically create new array, loop through products and add them into new array if they are not already added and if they are then add accountType to it. Something like this:
$grouped = array();
foreach($products as $product) {
if(!array_key_exists($product['product_id'], $grouped)) {
$grouped[$product['product_id']] = $product;
$grouped[$product['product_id']]['accountTypes'] = array($product['accountType']);
}else {
$grouped[$product['product_id']]['accountTypes'][] = $product['accountType'];
}
}
Then you use grouped data to display data and on accountType check you check if that type exists in accountTypes property.