I have an array with data and I have to put them into a table.
Array:
array(
0=>1
1=>Jon
2=>jon#email.com
3=>2
4=>Doe
5=>doe#email.com
6=>3
7=>Foo
8=>foo#email.com)
So Table head is:
<table>
<head>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
</tr>
</thead>
</table>
If I go and loop through the array:
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
#foreach($array as $data)
<tr>
<td>
{{$data}}
</td>
</tr>
#endforeach
<tbody>
</table>
Current Output:
Id Name Email
1 Jon jon#email.com 2 Doe doe#email.com 3 Foo foo#email.com
But My desired output is:
Id Name Email
1 Jon jon#email.com
2 Doe doe#email.com
3 Foo foo#email.com
I tried to chunk array into arrays with array_chunk() but the result was the same.
I am on laravel but I can go with plain php also.
Any tips would be much apreciated.
<tbody>
#foreach(array_chunk($array, 3) as $data)
<tr>
<td>
{{ $data[0] }}
</td>
<td>
{{ $data[1] }}
</td>
<td>
{{ $data[2] }}
</td>
</tr>
#endforeach
<tbody>
Hope this will help you.
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
#for($i = 0; $i < count($array); $i= $i+3)
<tr>
<td>
{{ $array[$i] }}
</td>
<td>
{{ $array[$i + 1] }}
</td>
<td>
{{ $array[$i + 2] }}
</td>
</tr>
#endforeach
<tbody>
</table>
First of all no way your current code will give that output you're showing. It should be vertical, each element in its separate row. But let's fix the problem.
You don't have proper objects to work on so here condition is that after 3 iterations, new data is coming. So you can take mod of 3 and make it work like this:
#foreach ( $array as $key => $value )
// you have a new dataset
#if ( ($key % 3) == 0 )
<tr>
#endif
<td>{{ $value }}</td>
#if ( ($key % 3) == 0 )
</tr>
#endif
#endforeach
I have not tested this code above but it should work. Though I would always prefer to use proper List of objects of User class or something else.
EDIT: I would prefer #Sand Of Vega's answer. Its better.
Try This
Re-place your with following.
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr>
#foreach($array as $key => $value)
if($key % 3 == 0){
echo "</tr>";
}
echo "<td>".$value."</td>";
#endforeach
<tbody>
</table>
I am not so familiar with laravel butI hope following works for you.
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
$length = (int) count($array)/3;
#for($i = 0; $i < $length; $i+=3)
<tr>
<td>{{$data[$i]}}</td>
<td>{{$data[$i+1]}}</td>
<td>{{$data[$i+2]}}</td>
</tr>
#endfor
<tbody>
</table>
Related
On my controller take data via foreach where the $upcoming->order_id is same I am checked by dd($notes) data is show but i can't get on my view php. before I am tried $note->get('amount') method it is also not working
public function index()
{ $upcomings = Booking_informations::Upcoming();
$notes = [];
foreach ($upcomings as $upcoming) {
$notes[] = Notes :: where('order_id',$upcoming->order_id)-> orderBy('id', 'ASC')->get();
};
return View('upcoming',compact('upcomings','notes',));
}
upcoming.blade.php
<table class="table table-bordered mb-0">
<tbody>
#foreach($notes as $note )
<tr>
<th scope="row">1</th>
<td>{{date('d-M-Y', strtotime($note->created_at))}}</td>
<td>{{$note->details}} </td>
<td>{{$note->amount}} </td>
</tr>
#endforeach
</tbody>
</table>
I have got answer while wrote below method have any more simple way for get results..
<table class="table table-bordered mb-0">
<thead>
<tr>
<th>#</th>
<th>Date</th>
<th>Content</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
#foreach($notes as $note )
#foreach($note as $id )
<tr>
<td scope="row">1</td>
<td>{{$id->details}} </td>
<td>{{$id->amount}} </td>
</tr>
#endforeach
#endforeach
</tbody>
</table>
Try referencing $note instead of $notes in your foreach loop and see if that works.
I have also noticed that you have named your view file View.blade.php. Should this be upcoming.blade.php to match the name you have provided in the returned view? Just a thought.
Hello i am a laravel beginner , when i do foreach its shows double of all single items what should i do with it code and ss are below
Brower image
code
<div >
<h1 style="text-align: center">Lists of Outlets</h1>
#foreach($outlets as $outlet)
<table class="table table-striped">
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr>
<td>John</td>
<td>Doe</td>
<td>john#example.com</td>
</tr>
</tbody>
</table>
#endforeach
</div>
The issue is, you have put the entire table inside the loop, which is wrong. You have to only put the tr inside the loop.
Try this:
#foreach($outlets as $outlet)
<tr>
<td>John</td>
<td>Doe</td>
<td>john#example.com</td>
</tr>
#endforeach
and one more thing, you are also using the static content inside the loop instead of using the loop variable value. So instead of:
<td>john#example.com</td>
it is something like:
<td>{{ $outlet['name'] }}</td> <!-- where name is the index in outlet array -->
#foreach should be inside <tbody> and instead of hard-coded content, you should be using $outlet variable to get firstname, lastname and Email:
<div>
<h1 style="text-align: center">Lists of Outlets</h1>
<table class="table table-striped">
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>
</thead>
<tbody>
#foreach($outlets as $outlet)
<tr>
<td>John</td>
<td>Doe</td>
<td>john#example.com</td>
</tr>
#endforeach
</tbody>
</table>
</div>
Currently im using Laravel-Excel (MaatWebsite) and wanted to check whether excel data is same with data in database "furnitures" table
Controller
// assume $id is retrieve from parameter
$check_furnitures = Furniture::where('company_no', $id)->get();
$rows = Excel::load('storage\\app\\public\\upload\\furniture.xlsx', function($reader){$reader->noHeading();$reader->skipRows(1)})->get();
View
<table>
<thead>
<tr>
<td>Chair Name></td>
<td>Desk Name></td>
<td>Carpet Name></td>
<td>Status</td>
</tr>
</thead>
<tbody>
<?php $arr = array(); ?>
#foreach($rows as $key => $value)
#foreach($check_furnitures as $fur)
#if(
$fur->chairs == $value[1] &&
$fur->desks == $value[2] &&
$fur->carpets == $pvalue[3]
)
<?php $temp_array[] = $value[0] ?>
#endif
#endforeach
#endforeach
<tr>
<td>{{$value[1]}}</td>
<td>{{$value[2]}}</td>
<td>{{$value[3]}}</td>
<td>
#foreach($arr as $test)
#if($test == $value[0])
DUPLICATED VALUE
#endif
#endforeach
</td>
<tr>
<table>
Excel File
The most efficient way is to index your database results first instead of looping. Laravel has a $collection->keyBy('id') so that you can $collection->get($id) later.
Having said that, you can do it this way:
<table>
<thead>
<tr>
<td>Chair Name></td>
<td>Desk Name></td>
<td>Carpet Name></td>
<td>Status</td>
</tr>
</thead>
<tbody>
#php
$arr = array();
$check_furnitures->keyBy('number');
#endphp
#foreach ($rows as $key => $row)
<tr>
<td>{{ $row->chair }}</td>
<td>{{ $row->desk }}</td>
<td>{{ $row->table }}</td>
<td>
#if($check_furnitures->get($row->number) !== null)
DUPLICATED VALUE
#endif
</td>
</tr>
#endforeach
</tbody>
</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>
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