Get the value of arrays in array in twig - php

I have this array dataPP_ids coming from a controller
array:3 [▼
0 => array:1 [▼
"plan_participate_id " => 47
]
1 => array:1 [▼
"plan_participate_id " => 47
]
2 => array:1 [▼
"plan_participate_id " => 48
]
]
I just want to show the "plan_participate_id" values in twig file.
I tried
{% for details in dataPP_ids %}
<tr>
<td class="pl-0 align-middle text-center" rowspan="2">{{ details.plan_participate_id }}</td>
<td>---</td>
</tr>
values of details.plan_participate_id are not showing in the frontend. How am I able to display it in twig?

You seem to have a trailing space here.
"plan_participate_id "
Instead of
"plan_participate_id"
You should either remove it or access it in twig like that.
{{ details['plan_participate_id '] }}

Related

How to use nested loop for different arrays in laravel blade template?

I am trying to display the distance for each of my drivers on my database in my view.
When I do die or dump for $pickupDistanceAndTime2 it returns the arrays below, so I need each of the drivers to have its own distance.
0 => array:2 [▶]
1 => array:2 [▶]
2 => array:2 [▶]
return view('driverspane', [
'drivers' => $driver,
'rideDistancenTime' => $pickupDistanceAndTime2,
]);
In my view, I am looping but not getting the way I want it to appear.
#foreach ($drivers as $item)
#foreach ($rideDistancenTime as $data)
{{ $data[0] }}
#endforeach
#endforeach
But the above code keeps repeating distance and time for each of the driver, but I want each of the driver to have its own distance and time only, please see the result I am getting below attached.
The array for $driver returns
#items: array:3 [▼
0 => App\Driver {#317 ▶}
1 => App\Driver {#300 ▶}
2 => App\Driver {#281 ▶}
]
From what you did above, you are just repeating the same values for the first index of the array. You should not do a nested foreach statement.
Rather you could do it this way(using a for statement, provided that the arrays all have same number of keys:
#for ($i=0; $i<count($drivers); $i++)
{{$rideDistancenTime[$i][1]}}
#endfor
Now it should loop properly.
You can use below code to achieve your goal:
$rideDistancenTime;
#foreach ($drivers as $key => $item)
{{ $rideDistancenTime[$key] }}
#endforeach```

Convert Array data to html table

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

undefined index 'value' in laravel

im using laravel 7 and trying to get datas from api use http.
when i use dd('$datas'),i got this
array:1 [▼"manajemen_sdm" => array:4 [▼ 0 => array:9 [▼ "id_karyawan" => "2" "nama" => "Koko Hendriko" "gender" => "Laki-Laki" "ttl" => "1998-04-07" "alamat" => "Kp.Palasari " "no_telp" => "821863141" "email" => "kokohendriko#gmail.com" "npwp" => "89678923652" "golongan" => "B" ] 1 => array:9 [▶] 2 => array:9 [▶] 3 => array:9 [▶] ] ]
my controller
public function index()
{
$datas = Http::get('https://projectsoadenis.000webhostapp.com/api')->json();
return view('sdm.index',compact('datas'));
}
view
#foreach ($datas as $datasdm)
<tr>
<td>{{$no++}}</td>
<td>{{$datasdm['id_karyawan']}}</td>
<td>{{$datasdm['nama']}}</td>
<td>{{$datasdm['gender']}}</td>
<td>{{$datasdm['ttl']}}</td>
<td>{{$datasdm['alamat']}}</td>
<td>{{$datasdm['no_telp']}}</td>
<td>{{$datasdm['email']}}</td>
<td>{{$datasdm['npwp']}}</td>
<td>{{$datasdm['golongan']}}</td>
</tr>
#endforeach
If you look at the data you are getting back from the API, your data is actually nested inside objects that live under a manajemen_sdm property.
Your loop should work correctly if you update it to loop over the manajemen_sdm key:
#foreach ($datas['manajemen_sdm'] as $datasdm)
<tr>
<td>{{$no++}}</td>
<td>{{$datasdm['id_karyawan']}}</td>
<td>{{$datasdm['nama']}}</td>
<td>{{$datasdm['gender']}}</td>
<td>{{$datasdm['ttl']}}</td>
<td>{{$datasdm['alamat']}}</td>
<td>{{$datasdm['no_telp']}}</td>
<td>{{$datasdm['email']}}</td>
<td>{{$datasdm['npwp']}}</td>
<td>{{$datasdm['golongan']}}</td>
</tr>
#endforeach

How to show list of names under the title on Twig

I am trying to show the list of users that are members of a same groups with a title.
I have several groups with different titles.
All that information I have collected in 'evals' => $evals parameter that I pass to TWIG.
When I dump my $evals I can see the following:
RolesController.php on line 168:
array:774 [▼
0 => array:2 [▼
"title" => "2018"
"name" => "Jack"
]
1 => array:2 [▼
"title" => "2018"
"name" => "Peter"
]
2 => array:2 [▼
"title" => "2018"
"name" => "Mary"
3 => array:2 [▼
"title" => "School"
"name" => "Fred"
]
4 => array:2 [▼
"title" => "School"
"name" => "Neal"
========================================
What I would like to see in Twig is:
2018
Jack
Peter
Mary
School
Fred
Neal
========================================
What I have already (using the code below) is
2018
SOMETHING
School
SOMETHING
========================================
<Table>
{% set newArray = [] %}
{% for row in evals %}
{% if row.title not in newArray %}
<tr >
<th> {{ row.title | upper }}</th>
</tr>
<td align="center">
SOMETHING
</td>
{% set newArray = newArray|merge([row.title]) %}
{% endif %}
{% endfor %}
Can anyone help me with how can I get names from my evals and put it into order under a title, please?

Looping through array containing arrays

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>

Categories