custom foreach with respective title - php

I have the loop here see the image for reference..
I have the column called Supplier Name
For example there are two row at the same supplier name
so i need the one heading
my expected ouptut is
ABS Agencies
Supplier name - here should be the heading
ABS Agencies - heading
ABS Agencies
ABS Agencies
mycode here
#foreach($prsdkgrids as $prsdkgrid)
<?php if($prsdkgrid->SupplierName == $prsdkgrid->SupplierName){ $SupplierName[] = $prsdkgrid->SupplierName; } ?>
#endforeach
#foreach($prsdkgrids as $prsdkgrid)
<?php if(count($SupplierName) == $SupplierName[$f]) { }else{ echo $f."<tr><td><h3 style='font-size: 15px;'>$prsdkgrid->SupplierName</h3></td></tr>"; } ?>
<tr class="psr_table1_tr" id="PSupdetID<?php echo $prsdkgrid->PSupdetID; ?>" <?php if($prsdkgrid->ApprovalStatus == 1){ echo 'style="background: #d9edf7;"';} ?> >
<td> {{ Form::label('', $prsdkgrid->ArticleNo) }} </td>
<td> {{ Form::label('', $prsdkgrid->Size) }} </td>
<td> {{ Form::label('', $prsdkgrid->SpecialDesc) }} </td>
<td> {{ Form::label('', $prsdkgrid->QtyExp) }} </td>
<td> {{ Form::label('', $prsdkgrid->Coloring) }} </td>
<td> {{ Form::label('', $prsdkgrid->Packaging) }} </td>
<td> {{ Form::label('', $prsdkgrid->Packing) }} </td>
<td> {{ Form::label('', $prsdkgrid->TargetPrice) }} </td>
<td> {{ Form::label('', $prsdkgrid->SupPrice) }} </td>
<td> {{ Form::label('', $prsdkgrid->SupplierName) }}</td>
<td> {{ Form::label('', $prsdkgrid->SupComments) }} </td>
<td> <?php if($prsdkgrid->ApprovalStatus == 0){ ?><div align="center" class="floatleft approvedk" id="ApprvedbyResp<?php echo $prsdkgrid->PSupdetID; ?>">
<li><a class="prs_add_link" href="#"><input class="save_btn Approve" type="button" onclick="ApprvedbyResp(<?php echo $prsdkgrid->PSupdetID; ?>)" value="Approve"></a></li></div><?php } ?> </td>
</tr> <?php $f++; ?>
#endforeach

You can store the supplier_name in a variable and in the loop you can just compare the variable and supplier name. A simple approach is like this since you're using foreach()
$tmp_supplier_name = '';
foreach($prsdkgrids as $prsdkgrid){
echo ($tmp_supplier_name != $prsdkgrid->supplier_name) ? '<tr>SUppliername</tr>' : ''; //compare
$tmp_supplier_name = $prsdkgrid->supplier_name; //store the suppliername that was compared
}
Or another approach is try to manipulate your array and it should be something like this , much readable.
Array(
['supplierA'] =>
Array(
[0] => array(.....)
[1] => array(.....)
),
['supplierB'] =>
Array(
[0] => array(.....)
[1] => array(.....)
)
)
Recreate the array example:
$new_array = [];
$tmp_supplier_name = '';
foreach($prsdkgrids as $prsdkgrid){
if($tmp_supplier_name != $prsdkgrid->supplier_name){
$tmp_supplier_name = $prsdkgrid->supplier_name;
}else{
$new_array[$prsdkgrid->supplier_name]['opt1'] = $prsdkgrid->opt1;
$new_array[$prsdkgrid->supplier_name]['opt2'] = $prsdkgrid->opt2;
//and so on
}
}

Related

Foreach Loop Through Array for Datatables With Eloquent Relations

I have 2 tables in my database - Bots and Results. The bots table is to have information on each specific bot alone. Each bot should have several results attached to it. I will simplify what I have in my columns:
Bots Table
Device | Info1 | Info2 | Info3
Results Table
Device | Data1 | Data2 | Data3
As you can see, each table has the column named Device. Now, I have 2 Models of course for each table - the regarding info is as follows:
Bot Model
public function results()
{
return $this->hasMany('App\Result', 'device', 'device');
}
Result Model
public function bots()
{
return $this->belongsTo('App\Bot', 'device', 'device');
}
I have a view with a Datatable in it which I am trying to list every Device and Every Result corresponding to that Device. What I have here is an example of what was made before eloquent relations was brought in. I use to only have the Results table, so therefore it was farely simple to do a foreach and loop throught every result I had to display on the Datatabes
Datatables View
#foreach ($results as $result)
<tr>
<td>
{{ $result->device }}
</td>
<td>
{{ $result->info1 }}
</td>
<td>
{{ $result->info2 }}
</td>
<td>
{{ $result->info3 }}
</td>
<td>
{{ $result->data1 }}
</td>
<td>
{{ $result->data2 }}
</td>
<td>
{{ $result->data3 }}
</td>
</tr>
#endforeach
How can I get this to work now that the device name and information is actually stored in a separate table?
You can fetch your device information with its associated results using eager load like this
$bots = Bot::with('results')->get();
Now in view you can do it like this
#foreach ($bots as $bot)
<tr>
<td>
{{ $bot->device }}
</td>
<td>
{{ $bot->info1 }}
</td>
<td>
{{ $bot->info2 }}
</td>
<td>
{{ $bot->info3 }}
</td>
</tr>
#foreach ($bot->results as $result)
<tr>
<td>
{{ $result->data1 }}
</td>
<td>
{{ $result->data2 }}
</td>
<td colspan="2">
{{ $result->data3 }}
</td>
</tr>
#endforeach
#endforeach

Laravel view, sort based on two criteria

I am trying to order the out but based on two criteria's, the dawn one should come at the top and then the up and last the no active once. I am trying to do it in the view in laravel. this is my code. it is able to sort them based on up or dawn but not on active.
#foreach($notes->sortBy('values') as $notification)
<tbody>
<tr>
<td> {{ $notification->website_url }} </td>
#if($notification->status('health') == 'up')
<td> {{ $notification->status('health') }} <span class="up"></span></td>
#else
<td> {{ $notification->status('health') }} <span class="down"></span></td>
#endif
<td> </td>
<td class="<?php echo $notification->active == 1 ? 'active' : '' ?>">
{{ $notification->active == 1 ? 'Yes' : 'No' }}
</td>
<td>
<a href="{{ route('notifications.edit', [$notification->id]) }}">
<input type="submit" name="showmore" value="show more"/>
</a>
</td>
<td></td>
</tr>
</tbody>
#endforeach
Argument of sortBy() method can be actually a function, so you can use custom value as a sort criteria. Preferably in the Controller.
$notes = $notes->sortBy(function ($note) {
return ($note->active ? '0' : '1') . ($note->status('health') == 'down' ? '0' : '1');
});
And $notes will be sorted by this value in the following order:
00 for down active
01 for up active
10 for down not active
11 for up not active
To be sincere though I'm not really sure if I understand what order you want to achieve. If that is the case just adjust the return value of sorting function.

Displaying multiple columns of data [from a PHP list] in a table in Laravel View

I currently am using a Laravel View with the following HTML that displays data onto a table using the <td> tag.
#if($unrostered)
<table class="table table-hover table-striped">
<thead>
<tr>
<th>#</th>
<th>Name</th>
</tr>
</thead>
<tbody>
<?php $index = 1; ?>
#foreach($unrostered as $name => $profile)
<tr>
<td>
<?= $index; $index++; ?>
</td>
<td>
<a
href="{{ $profile }}">{{ $name }}
</a>
</td>
</tr>
#endforeach
</tbody>
</table>
#endif
I am currently just using one associative list called $unrostered which contains the key and value called name and profile. This shows two columns of data, like this:
As you can see, the name variable is the name of the person, and the profile is a URL that links to their profile.
My HTML skills aren't good. I want to pass in multiple lists [of the same length] into my HTML table, so each list occupies their own column.
I tried juggling the row and data tags, however, all I was able to achieve were that all the data points occupied only one row instead of columns.
Assume the name of your other lists are $list1, $list2, ... and they have the same format as your $unrostered. Then your foreach code should be changed to:
<?php $index = 1; ?>
#foreach($unrostered as $name => $profile)
<tr>
<td>
<?= $index; $index++; ?>
</td>
<td>
<a
href="{{ $profile }}">{{ $name }}
</a>
</td>
<td>
<a
href="{{ $list1[$name] }}">{{ $name }}
</a>
</td>
<td>
<a
href="{{ $list2[$name] }}">{{ $name }}
</a>
</td>
</tr>
#endforeach

2 Foreach Loop Inside A Table

I had Users table and group table, when i load a group table, it loads the user which have same group_id as the group table id. it works, but my problem is the foreach was quite a mess. the output looks like this..
however, i want to make the output look like this.
here is my code..
<tbody>
<?php $i = 0; ?>
#foreach ($auser as $res)
<?php $i += 1; ?>
<tr #if($i%2==0) class="bgcolor" #endif>
<td>{{ $res->id }}.</td>
<td id="showdet">{{ $res->nama }}</td>
<td>{{ $res->description }}</td>
<?php $user = DB::table('users')->where('devgroup',$res->id)->get();?>
#foreach($user as $mem)
<td>{{ $mem->name }}</td>
#endforeach
<td>
<a class="default-btn" href="/pms/admin/group/edit/{{ $res->id }}">Edit</a>
<a type="submit" class="default-btn del" data-id="devgroup" href="#"{{ $res->id }}">Delete</a>
</td>
</tr>
#endforeach
</tbody>
what did i do wrong? thank you for your help.
You create new TD for each member. The nested foreach has to be:
<td>
#foreach($user as $mem)
{{ $mem->name }}<br>
#endforeach
</td>
The result will be:
<td>
Name 1<br>
Name 2<br>
Name 3<br>
</td>
I don't know the template engine you used, add inside a loop condition and don't put <br> after the last one member.

can't access the variable i passed from controller to my view in laravel 4.2

i have a view that contains a form. the form's url is the address of view that the form is in it.
in controller i pass a variable to the view when the user hit the button in the form.
but my view has an error says the variable i passed to it is undefined.
here is my code
my view:
#extends('layouts.master')
#section('content')
<?php
$category = Category::lists('name','name');
$priority = Priority::lists('name','name');
$lesson = Lesson::lists('name','name');
$count = 1;
?>
{{ Form::open(array('action' => 'createQuizController#find', 'method' => 'POST', 'id' => 'search_question')) }}
<div>
{{ Form::label('quiz_cat', 'نام دسته بندی') }}
{{ Form::select('quiz_cat', array('/'=>'انتخاب کنید')+$category, null, ['id' => 'cat_select']) }}
</div>
<div>
{{ Form::label('quiz_less','نام درس') }}
{{ Form::select('quiz_less',$lesson, null, ['id' => 'les_select']) }}
</div>
<div>
{{ Form::label('quiz_prio','سطح سوال') }}
{{ Form::select('quiz_prio',array('/'=>'انتخاب کنید')+$priority, null, ['id' => 'prio_select']) }}
</div>
<div>
{{Form::label('date_start','تاریخ شروع')}}
{{Form::input('date','date_start',null,array('id' => 'date_start'))}}
</div>
<div class='form-group'>
{{ Form::submit('جستجو', ['class' => 'btn btn-primary']) }}
</div>
{{ Form::close() }}
<iframe width="690px" height="300px" scrolling="yes">
{{ Form::open(array('method' => 'POST', 'id' => 'add_question')) }}
<div class="col-lg-10 col-lg-offset-1">
<div class="table-responsive" id="question_table">
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>ردیف</th>
<th>عنوان سوال</th>
<th></th>
</tr>
</thead>
<tbody>
#foreach ($res as $quiz)
<tr>
<td align="center" width="100px">
<?php echo $count++; ?>
</td>
<td align="center" width="200px">{{ $quiz->title }}</td>
<td align="center" width="200px"><input type="checkbox" name="select_question[]" id="{{ $quiz->lesson_id }}" value="{{ $quiz->id }}"></td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
{{ Form::submit('افزودن', ['class' => 'btn btn-primary']) }}
{{ Form::close() }}
</iframe>
#stop
my controller:
<?php
class createQuizController extends BaseController {
public function find()
{
$prio = Input::get('quiz_prio');
$res1 = DB::select("select id from priority pr where '$prio' = pr.name");
$r1 = $res1[0]->id;
$cat = Input::get('quiz_cat');
$res2 = DB::select("select id from categories cat where '$cat' = cat.name");
$r2 = $res2[0]->id;
$les = Input::get('quiz_less');
$res3 = DB::select("select id from lesson less where '$les' = less.name");
$r3 = $res3[0]->id;
$sdate = Input::get('date_start');
$res = DB::select("select * from quiz where categories_id='$r2' and lesson_id='$r3' and priority_id='$r1' and date_start='$sdate'");
return View::make('cquiz')->with('res',$res);
}
public function addQuestion()
{
$a = array();
$a[] = Input::get('select_question');
var_dump($a[]);
/*foreach($a as $b) {
$c = DB::select("select name from lesson where '$b'=id" );
var_dump($c);
}*/
}
}
my route:
Route::post('/cquiz/','createQuizController#find');
Route::post('/addQuestion/','createQuizController#addQuestion');
try this:
return View::make('cquiz')->withRes($res);
and in the view you can access '$res' (or {{ $res }} etc)

Categories