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
Related
I have some data that returns to me as a laravel array and I want to print it out in a table in a view, but I can't access that data
array:1 [▼
0 => array:2 [▼
0 => array:3 [▼
0 => "MS3939"
1 => "20G1"
2 => "20DC"
]
1 => array:3 [▼
0 => "MRK940"
1 => "20R1"
2 => "20RF"
]
]
]
this is my view code with the table
<table class="table table-bordered">
<thead>
<tr class="text-center">
<th>ID</th>
<th>ISO</th>
<th>Type</th>
</tr>
</thead>
<tbody id="filter-table">
#foreach($datos as $dato)
<tr>
<th scope="row">{{$loop->iteration}}</th>
<td>{{$dato['0']}}</td>
<td>{{$dato['1']}}</td>
<td>{{$dato['2']}}</td>
#endforeach
</tr>
</tbody>
</table>
If your $datos is a collection then do one thing.
$datos = $datos->flatten(1); //it will reduce one level of your array
If your $datos is not a collection and it just an array then convert it to collection
$datos = collect($datos); //then
$datos = $datos->flatten(1);
Then in view you are already running a loop, so you will be able to access data like...
<td>{{ $dato[0] }}</td>
<td>{{ $dato[1] }}</td>
<td>{{ $dato[2] }}</td>
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
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.