Show two array values inside one foreach loop in Laravel - php

I'm retrieving the JSON array from my DB and my array is:
{"Position":["first","second","third","fourth"],"Color":["Red",Blue,Pink,Teal]}
I want to show them in table inside a foreach loop
#foreach ($orderProduct->column as $item)
<thead>
<tr>
<th>{{ $item['Position'] }}</th>
<th>{{ $item['Color'] }}</th>
</tr>
</thead>
#endforeach
I want the output to be like this
<thead>
<tr>
<th>first</th>
<th>Red</th>
</tr>
</thead>
<thead>
<tr>
<th>second</th>
<th>Blue</th>
</tr>
</thead>
<thead>
<tr>
<th>third</th>
<th>Pink</th>
</tr>
</thead>
But it doesn't work.
Any ideas?

#for ($i = 0; $i < count($orderProduct->column['Position']); $i++)
<thead>
<tr>
<th>{{ $orderProduct->column['Position'][$i] }}</th>
<th>{{ $orderProduct->column['Color'][$i] }}</th>
</tr>
</thead>
#endfor

#foreach (array_combine($orderProduct['Position'], $orderProduct['Color']) as $position => $color)
<thead>
<tr>
<th>{{ $position }}</th>
<th>{{ $color }}</th>
</tr>
</thead>
#endforeach

Because you're using Laravel, I would suggest to make use of the Collection's zip method that "merges together the values of the given array with the values of the original collection at their corresponding index" (Docs)
You would use it like this:
$newArray = collect($array['Position'])->zip($array['Color']);
#foreach($newArray as [$position, $color])
{{ $position }}
{{ $color }}
#endforeach

Related

How to get total / sum before first row in php

Please help me, I need output total on top of data shown...
I have Looping like this in my blade.php
<table>
<thead>
<tr>
<th>Name</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
#foreach($datas $data)
<tr>
<td>{{ $data->name }}</td>
<td>{{ $data->amount }}</td>
</tr>
#endforeach
</tbody>
and output will like this:
And now how to get output total before firs row like image below?
The laravel collection has an inbuilt function called sum which returns the sum of Collection.
Learn here about collection function
<table>
<thead>
<tr>
<th>Name</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<tr>
<td>Total Pencil</td>
<td>{{ $datas->sum('amount') }}</td>
</tr>
#foreach($datas $data)
<tr>
<td>{{ $data->name }}</td>
<td>{{ $data->amount }}</td>
</tr>
#endforeach
</tbody>
convert object to array
$datasArray = json_decode(json_encode($datas), true);
collect all 'amount' in an array
$amountArray = array_column($datasArray,'amount');
Sum all element of $amountArray
$total = array_sum($amountArray );
print $total where you want

Passing array of array from phpexcel to blade laravel

i want to passing array of array to blade laravel, im using laravel 5.5 and php 7
mycontroller :
public function openexcel(Request $request, $directory)
{
$data = Exceluploads::get();
$path = 'excel/'.$directory;
$objPHPExcel = PHPExcel_IOFactory::load($path);
$sheet = $objPHPExcel->getSheetByName('mySheet1');
$a = $sheet->rangeToArray('A1:D9');
return view('excel.index_openexcel', compact('objPHPExcel,a'));
}
for example data excel:
return $a
how to display it in blade laravel
you can loop the array like this in your blade file.
<table>
<thead>
<tr>
{{-- loop the column names --}}
#foreach ($a[0] as $columnName)
<td>{{$columnName}}</td>
#endforeach
</tr>
</thead>
<tbody>
{{-- loop all the arrays except the first on --}}
#for ($i = 1; $i < count($a); $i++)
<tr>
{{-- get the data of that array --}}
#foreach ($a[$i] as $data)
<td>{{ $data }}</td>
#endforeach
</tr>
#endfor
</tbody>
</table>
hope this helps!
you can try this.
<table>
<thead>
<tr>
<th>{{ $objPHPExcel[0][0] }}</th>
<th>{{ $objPHPExcel[0][1] }}</th>
<th>{{ $objPHPExcel[0][2] }}</th>
<th>{{ $objPHPExcel[0][3] }}</th>
</tr>
</thead>
<tbody>
#for($i=1; $i<count($objPHPExcel); $i++)
<tr>
<td>{{ $objPHPExcel[$i][0] }}</td>
<td>{{ $objPHPExcel[$i][1] }}</td>
<td>{{ $objPHPExcel[$i][2] }}</td>
<td>{{ $objPHPExcel[$i][3] }}</td>
</tr>
#endfor
</tbody>
</table>
or this way.
<table>
<thead>
<tr>
#for($i=0; $i<count($objPHPExcel[0]); $i++)
<th>{{ $objPHPExcel[0][$i] }}</th>
#endfor
</tr>
</thead>
<tbody>
#for($i=1; $i<count($objPHPExcel); $i++)
<tr>
#for($j=0; $j<count($objPHPExcel[$i]); $j++)
<td>{{ $objPHPExcel[$i][$j] }}</td>
#endfor
</tr>
#endfor
</tbody>
</table>

Combine data from two mysql tables in HTML table Laravel 5.4

Trying to make one html table where is showing what login users choose.
One login auth users and one form data.
Here data is showing on page but scramled in table.
Homedata is showing twice.
Here is my index page where im trying to do:
<table class="table" id="users-info">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">First Name</th>
<th scope="col">Last Name</th>
<th scope="col">Naional Number</th>
<th scope="col">Device</th>
<th scope="col">Reimbursment</th>
<th scope="col">Warranty</th>
</tr>
</thead>
<tbody>
#if(count($users) > 0)
#foreach($users as $user)
<tr>
<th scope="row">{{ $user->id }}</th>
<td>{{ $user->firstname }}</td>
<td>{{ $user->lastname }}</td>
<td>{{ $user->phone }}
#foreach($homedata as $homedatas)
<td>{{$homedatas->device}}</td>
<td>{{$homedatas->reimburse_Pension}}</td>
<td>{{$homedatas->bonus_remainder}}</td>
#endforeach
</td>
</tr>
#endforeach
#endif
</tbody>
</table>
Please help.
if there is another way please feel free suggest.
Hey everyone who share the same problem.
I add in second foreach ->slice(0, 1) and that foreach just going once, that automaticaly means no duplicate data :)

How to include all the data in foreach loop inside a table

#foreach($lastactivity as $activity)
{{ $activity }}
#endforeach
That is the for loop.
Now this is the output in the view.
{"id":2,"log_name":"index-log","description":"I visited index","subject_id":null,"subject_type":null,"causer_id":1,"causer_type":"App\\User","properties":[],"created_at":"2017-07-18 11:05:08","updated_at":"2017-07-18 11:05:08"}
How will I be able to put that in a table with all the columns and data arranged? Thank you very much.
You should use table.
<table>
<thead>
<tr>
<th>Log Name</th>
<th>Description</th>
</tr>
</thead>
<tbody>
#foreach($lastactivity as $activity)
<tr>
<td>{{$activity->log_name}}</td>
<td>{{$activity->description}}</td>
</tr>
#endforeach
</tbody>
</table>
In the same way, Add other fields as well.
You have to define your field name
{{ $activity->id }}
{{ $activity->log_name }}
etc
Like this:
<table class="table">
<thead>
<tr>
<th>Id</th>
<th>Log Name</th>
.
.
.
<th>Updated At</th>
</tr>
</thead>
<tbody>
#foreach($lastactivity as $activity)
<tr>
<td>{{ $activity->id }}</td>
<td>{{ $activity->log_name }}</td>
.
.
.
<td>{{ $activity-> }}</td>
</tr>
#endforeach
</tbody>
</table>

Dynamic number of rows in Laravel Blade

I want a dynamic number of rows in a table like this.
number name
1 Devy
This my Blade template.
<thead>
<th>number</th>
<th>name</th>
</thead>
<tbody>
#foreach ($aaa as $value)
<tr>
<td></td>
<td>{{$value->name}}</td>
</tr>
#endforeach
</tbody>
How do I do that?
Try $loop->iteration variable.
`
<thead>
<th>number</th>
<th>name</th>
</thead>
<tbody>
#foreach ($aaa as $value)
<tr>
<td>{{$loop->iteration}}</td>
<td>{{$value}}</td>
</tr>
#endforeach
</tbody>
`
This is correct:
#foreach ($collection as $index => $element)
{{$index}} - {{$element['name']}}
#endforeach
And you must use index+1 because index starts from 0.
Using raw PHP in view is not the best solution. Example:
<tbody>
<?php $i=1; #foreach ($aaa as $value)?>
<tr>
<td><?php echo $i;?></td>
<td><?php {{$value->name}};?></td>
</tr>
<?php $i++;?>
<?php #endforeach ?>
in your case:
<thead>
<th>number</th>
<th>name</th>
</thead>
<tbody>
#foreach ($aaa as $index => $value)
<tr>
<td>{{$index}}</td> // index +1 to begin from 1
<td>{{$value}}</td>
</tr>
#endforeach
</tbody>
Use a counter and increment its value in loop:
<thead>
<th>number</th>
<th>name</th>
</thead>
<tbody>
<?php $i = 0 ?>
#foreach ($aaa as $value)
<?php $i++ ?>
<tr>
<td>{{ $i}}</td>
<td>{{$value->name}}</td>
</tr>
#endforeach
</tbody>
Use $loop variable
refer this link Loop Variable
Starting from Laravel 5.3, this has been become a lot easier. Just use the $loop object from within a given loop. You can access $loop->index or $loop->iteration. Check this answer: https://laracasts.com/discuss/channels/laravel/count-in-a-blade-foreach-loop-is-there-a-better-way/replies/305861
Just take a variable before foreach() like $i=1. And increment $i just before foreach() ends. Thus you can echo $i in the desired <td></td>
try the following:
<thead>
<th>number</th>
<th>name</th>
</thead>
<tbody>
#foreach ($aaa as $index => $value)
<tr>
<td>{{$index}}</td>
<td>{{$value}}</td>
</tr>
#endforeach
</tbody>
You can Use it like this in Blade. I hope this will help.
<thead>
<th>number</th>
<th>name</th>
</thead>
<tbody>
#foreach ($aaa as $index => $value)
<tr>
<td>{{$index +1}}</td>
<td>{{$value}}</td>
</tr>
#endforeach
</tbody>
Note: {{$index +1}} since $index starts from 0
Laravel 8 -
<?php $serial = "1"; ?> - Before #foreach loop
<td>{{ $serial++ }}</td> - Inside #foreach loop

Categories