How to iterate an array cleanly PHP - php

Hello im having a hard time iterating my array. I don't know what to use a simple foreach or with a foreach with $key. I tried with key but im having an error of:
Illegal string offset 'payroll_employee_company_id' (View: /var/www/html/digimahouse/resources/views/member/payrollreport/loan_summary_table.blade.php)
How can i iterate it successfully?
here's my array
here's my foreach
#foreach($totals as $key => $total)
#if($total['payroll_employee_company_id'] == $comid->payroll_company_id)
<tr class="total">
<td class="text-center"><strong>TOTAL</strong></td>
<td class="text-center"></td>
<td class="text-center"></td>
<td class="text-center">{{$total['loan_total']}}</td>
<td class="text-center">{{$total['total_total_payment']}}</td>
<td class="text-center">{{$total['total_remaining_balance']}}</td>
<td class="text-center"></td>
</tr>
#endif
#endforeach
here's my controller
public function loan_summary()
{
$data["page"] = "Loan Summary";
$data["_loan_data"] = PayrollDeductionController::get_deduction($this->shop_id());
$data["_company"] = Payroll::company_heirarchy(Self::shop_id());//Tbl_payroll_company::where("shop_id", Self::shop_id())->where('payroll_parent_company_id', 0)->get();
$data['totals'] = $this->get_totals_loan_summary($data);
return view("member.payrollreport.loan_summary", $data);
}

You've said the picture is result for {{ dd($totals) }}. In this case, do this:
#foreach($totals['totals'] as $key => $total)
#if($total['payroll_employee_company_id'] == $comid->payroll_company_id)
<tr class="total">
<td class="text-center"><strong>TOTAL</strong></td>
<td class="text-center"></td>
<td class="text-center"></td>
<td class="text-center">{{$total['loan_total']}}</td>
<td class="text-center">{{$total['total_total_payment']}}</td>
<td class="text-center">{{$total['total_remaining_balance']}}</td>
<td class="text-center"></td>
</tr>
#endif
#endforeach

you foreach was not on the correct array
#foreach($data['totals'] as $key => $total)
#if($total['payroll_employee_company_id'] == $comid->payroll_company_id)
<tr class="total">
<td class="text-center"><strong>TOTAL</strong></td>
<td class="text-center"></td>
<td class="text-center"></td>
<td class="text-center">{{$total['loan_total']}}</td>
<td class="text-center">{{$total['total_total_payment']}}</td>
<td class="text-center">{{$total['total_remaining_balance']}}</td>
<td class="text-center"></td>
</tr>
#endif
#endforeach

Related

Nested foreach loop creating duplicate records with if condiotion - PHP Laravel 8

Problem: There are modules, users, and user_modules tables, where the admin can assign multiple modules with permissions to a user. Admin can update module's permission which is already assigned to that user, and the modules which are not assigned should be loaded on blade view on the same table.
But the issue is data is duplicating
I am posting my code with images
AdminController:
$modules = Module::all();
$user_modules = User_module::with('module')->where('user_id', $user_id)->get();
return view('admin/seller_assign_modules', compact('user','modules','user_modules'));
seller_assign_modules.blade.php
<table class="table table-striped">
<thead>
<tr>
<th>Modules</th>
<th>Add</th>
<th>Edit</th>
<th>View</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
#foreach ($user_modules as $user_mod)
#foreach ($modules as $mod)
#if ($mod->id == $user_mod->module_id)
<tr>
<td scope="row">{{$user_mod->module->name}}</td>
<td scope="row">{{$user_mod->add}}</td>
<td scope="row">{{$user_mod->edit}}</td>
<td scope="row">{{$user_mod->view}}</td>
<td scope="row">{{$user_mod->del}}</td>
</tr>
#else
<tr>
<td scope="row">{{$mod->name}}</td>
<td scope="row"></td>
<td scope="row"></td>
<td scope="row"></td>
<td scope="row"></td>
</tr>
#endif
#endforeach
#endforeach
</tbody>
</table>
modules table:
user_modules table:
result on seller_assign_modules.blade.php
I NEED THIS:
You can do something like below (this is not a good approach however)
$user_modules = User_module::with('module')->where('user_id', $user_id)->get();
//Get modules where user dont have records
$non_user_modules = Module::whereNotIn('id', (clone $user_modules)->pluck('module_id')->toArray())->get();
return view('admin/seller_assign_modules', compact('user','modules','user_modules','non_user_modules'));
Then in the blade
#foreach ($user_modules as $user_mod)
<tr>
<td scope="row">{{$user_mod->module->name}}</td>
<td scope="row">{{$user_mod->add}}</td>
<td scope="row">{{$user_mod->edit}}</td>
<td scope="row">{{$user_mod->view}}</td>
<td scope="row">{{$user_mod->del}}</td>
</tr>
#endforeach
#foreach ($non_user_modules as $non_user_module)
<tr>
<td scope="row">{{$non_user_module->name}}</td>
<td scope="row"></td>
<td scope="row"></td>
<td scope="row"></td>
<td scope="row"></td>
</tr>
#endforeach
Best approach you could do
Make a relationship at Module model like userModules()
Then get Modules like below
$modulesWithUser = Module::with(['userModules' => function($userQuery){
$userQuery->where('user_id',$user_id);
}])->get();
Then loop $modulesWithUser in blade and access its relationship inside loop to show users permissions.
The problem with here is,Module has manyUser_modules, but you only need one.
So in this case, one approach is map a new property like user_module to each Module.
AdminController
$modules = Module::all();
$userModeules = User_module::where('user_id', $user->id)->get();
$modules = $modules->map(function($module) {
$module->user_module = $userModules->firstWhere('module_id', $module->id);
return $module;
});
return view(
'admin/seller_assign_modules',
compact('modules')
);
seller_assign_modules.blade.php
<tbody>
#foreach($mdules as $module)
<tr>
<td scope="row">{{ $module->name }}</td>
<td scope="row">{{ optional($module->user_module)->add }}</td>
<td scope="row">{{ optional($module->user_module)->edit }}</td>
<td scope="row">{{ optional($module->user_module)->view }}</td>
<td scope="row">{{ optional($module->user_module)->del }}</td>
</tr>
#endforeach
</tbody>

Display name of category in td

I want to display subcategory in td but I like the result with some formatting. The output is going on weight column so need help to improve code. How can I display one by one product_name? for each loop create the second td & it will go to the weigh column. I want to display this product_name with auto-increment $i
Excepted result:
product_name
1)face wash
2)face cream
my result:
face wash face cream
Blade file code:
<div class="row">
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title"><strong>Order summary</strong></h3>
</div>
<div class="panel-body">
<div class="table-responsive">
<table class="table table-condensed">
<thead>
<tr>
<td><strong>Product Name</strong></td>
<td class="text-center"><strong>Weight</strong></td>
<td class="text-center"><strong>Quantity</strong></td>
<td class="text-right"><strong>Price</strong></td>
<td class="text-right"><strong>SubTotal</strong></td>
</tr>
</thead>
<tbody>
<!-- foreach ($order->lineItems as $line) or some such thing here -->
<tr>
<?php
$subcategory=explode(',', $bill->name_of_subcategory);
$weight=explode(',', $bill->weight);
$orders_qty=explode(',', $bill->orders_qty);
$price=explode(',', $bill->price);
$orders_subtotal=explode(',', $bill->orders_subtotal);
?>
<td><?php
$subcategory=explode(',', $bill->name_of_subcategory);
for($i=0;$i<count($subcategory);$i++)
{
echo $subcategory[$i];
}
?></td>
<td class="text-center">{{ $bill->weight }}</td>
<td class="text-center">{{ $bill->orders_qty }}</td>
<td class="text-center">{{ $bill->price }}</td>
<td class="text-center">{{ $bill->orders_subtotal }}</td>
</tr>
<tr>
<td class="thick-line"></td>
<td class="thick-line"></td>
<td class="thick-line"></td>
<td class="thick-line text-center"><strong>Subtotal</strong></td>
<td class="thick-line text-right">{{ $bill->total }}</td>
</tr>
<tr>
<td class="no-line"></td>
<td class="no-line"></td>
<td class="no-line"></td>
<td class="no-line text-center"><strong>SGST</strong></td>
<td class="no-line text-right">{{ $bill->sgst }}%</td>
</tr>
<tr>
<td class="no-line"></td>
<td class="no-line"></td>
<td class="no-line"></td>
<td class="no-line text-center"><strong>CGST</strong></td>
<td class="no-line text-right">{{ $bill->cgst }}%</td>
</tr>
<tr>
<td class="no-line"></td>
<td class="no-line"></td>
<td class="no-line"></td>
<td class="no-line text-center"><strong>IGST</strong></td>
<td class="no-line text-right">{{ $bill->igst }}%</td>
</tr>
<tr>
<td class="no-line"></td>
<td class="no-line"></td>
<td class="no-line"></td>
<td class="no-line text-center"><strong>Total</strong></td>
<td class="no-line text-right">{{ $bill->grand_total }}</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
Try this
<table>
<tr>
<?php
$subcategory=explode(',', $bill->name_of_subcategory);
foreach($subcategory as $row){ ?>
<td>{{$row}}</td>
<?php } ?>
</tr>
</table>
you should create a <td> foreach result.
<table>
<?php
$subcategory=explode(',', $bill->name_of_subcategory);
for($i=0;$i<count($subcategory);$i++)
{
echo "<tr><td>".$subcategory[$i]."</td></tr>";
}
?>
</table>

Trying to get property of non-object Laravel 5.4

Hi Guy's Im always getting an error from laravel 5.4.
Im creating a list of members... but when i use #foreach it says
ErrorException in 6163a8b030c7474bc8eaad359ab99eb61ebdb127.php line 38:
Trying to get property of non-object (View: E:\wamp64\www\gplspring2017\resources\views\admin\memberlist.blade.php)
Here's my controller
public function listmember(Request $request, $idteam)
{
$teams = DB::table('gpl_team')->where('gpl_team_id', $idteam)->first();
$count = count($teams);
if (!$count) {
return redirect('404');
} else {
return view('/admin/memberlist', ['team' => $teams]);
}
}
and here's my view code:
<table class="table table-striped">
<tr>
<td style="width:15%;"></td>
<td style="width:25%;">Summoners Name</td>
<td style="width:25%;">Name</td>
<td style="width:25%;">Role</td>
<td style="width:10%;"></td>
</tr>
<?php
$qmember = DB::table('team_member')->where('gpl_team_id', $team->gpl_team_id)->first();
$counting = count($qmember);
?>
#if (! $counting)
<tr>
<td colspan="4"> No Recored! </td>
</tr>
#else
#foreach($qmember as $get_member)
<tr>
<td><img src="{{ $get_member->member_pic }}" /></td>
<td></td>
<td></td>
<td></td>
<td>
<div class="glyphicon glyphicon-pencil"></div> |
<div class="glyphicon glyphicon-trash"></div>
</td>
</tr>
#endforeach
#endif
</table>
When i remove the foreach the code is working.. but i try to add the #foreach($qmember as $get_member) it not working anymore...
Check this line:
$qmember = DB::table('team_member')->where('gpl_team_id', $team->gpl_team_id)->first();
when you are using first() then it does not return an Std Class object. If you want it so then use get()
Using first() method you are fetching only one record from DB that match with the ID (First one matched). You need to use get().
Controller:
public function listmember(Request $request, $idteam)
{
$teams = DB::table('gpl_team')->where('gpl_team_id', $idteam)->get(); //Use get() if you expect more than one result not first()
$count = count($teams);
if(! $count)
{
return redirect('404');
}
else
{
return view('/admin/memberlist', ['teams' => $teams]);// this team variable must be used in the blade
}
}
View:
<table class="table table-striped">
<tr>
<td style="width:15%;"></td>
<td style="width:25%;">Summoners Name</td>
<td style="width:25%;">Name</td>
<td style="width:25%;">Role</td>
<td style="width:10%;"></td>
</tr>
<?php
$qmember = DB::table('team_member')->where('gpl_team_id', $team->gpl_team_id)->first();
$counting = count($qmember);
?>
#if (! $counting)
<tr>
<td colspan="4"> No Recored! </td>
</tr>
#else
#foreach($teams as $team)
<tr>
<td><img src="{{ $team['member_pic'] }}" /></td>
<td></td>
<td></td>
<td></td>
<td>
<div class="glyphicon glyphicon-pencil"></div> |
<div class="glyphicon glyphicon-trash"></div>
</td>
</tr>
#endforeach
#endif
</table>

How to compare two arrays and show matches in columns

I have two arrays.
Array 1 - List of all dates
Array 2 - list of all dates a person is present
I want to show a table that has all the dates in the first row and at each day that a person is present on the second row, the respective column should say present.
I tried nested loop but it just shows various rows and one result per row i.e. only one match per row.
I want to accomplish something like this
<table width="100%" border="1" cellspacing="1" cellpadding="1">
<tr>
<td width="6%">160111</td>
<td width="6%">160113</td>
<td width="6%">160120</td>
<td width="6%">160127</td>
<td width="6%">160201</td>
<td width="6%">160203</td>
<td width="6%">160208</td>
<td width="6%">160210</td>
<td width="6%">160217</td>
<td width="6%">160224</td>
<td width="6%">160229</td>
<td width="6%">160302</td>
<td width="6%">160307</td>
<td width="6%">160309</td>
<td width="6%">160321</td>
<td width="5%">160323</td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td>Present</td>
<td> </td>
<td> </td>
<td> </td>
<td>Present</td>
<td> </td>
<td> </td>
<td> </td>
<td>Present</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
</table>
Suggestions? How to run loop or how can this be achieved?
Use in_array() to test whether an item is in Array 2.
foreach ($array1 as $day) {
echo "<td>";
echo in_array($day, $array2) ? "Present" : " ";
echo "</td>";
}
Iterate through Array1 ( foreach loop )
Check each value against Array2 ( in_array() )
Eg:-
echo "<br/><table border='1' style='width:100%'><tr><td>";
foreach($arr1 as $a1) {
echo "<table border='1' style='display:inline;border:0px solid'>";
echo "<tr><td>$a1</td></tr>";
if(in_array($a1,$arr2)) {
echo "<tr><td>Present</td></tr>";
} else {
echo "<tr><td> </td></tr>";
}
echo "</table>";
}
echo "</td></tr></table>";
You can try this if Array are same like I given in my answer
PHP Code:
<?php
$dates =array(
'160111','160113','160120','160127','160201','160203',
'160208','160210','160217','160224','160229','160302',
'160307','160309','160321','160323'
);
$person_present =array(
'','','','160127','','',
'','160210','','','','160302',
'','','',''
);
foreach ($dates as $pkey => $day)
{
if ( in_array($day, $person_present) ) {
?>
<td>Present</td>
<?php
} else {
?>
<td> </td>
<?php
}
}
?>
Full Code:
<table width="100%" border="1" cellspacing="1" cellpadding="1">
<tr>
<td width="6%">160111</td>
<td width="6%">160113</td>
<td width="6%">160120</td>
<td width="6%">160127</td>
<td width="6%">160201</td>
<td width="6%">160203</td>
<td width="6%">160208</td>
<td width="6%">160210</td>
<td width="6%">160217</td>
<td width="6%">160224</td>
<td width="6%">160229</td>
<td width="6%">160302</td>
<td width="6%">160307</td>
<td width="6%">160309</td>
<td width="6%">160321</td>
<td width="5%">160323</td>
</tr>
<tr>
<?php
$dates =array(
'160111','160113','160120','160127','160201','160203',
'160208','160210','160217','160224','160229','160302',
'160307','160309','160321','160323'
);
$person_present =array(
'','','','160127','','',
'','160210','','','','160302',
'','','',''
);
foreach ($dates as $pkey => $day)
{
if ( in_array($day, $person_present) ) {
?>
<td>Present</td>
<?php
} else {
?>
<td> </td>
<?php
}
}
?>
</tr>
</table>

redirect to error page in yii2

i'm still new in yii2.
so, make a simple project which use MVC in yii2 where the project is to output examination result.
what bugging me is, how to redirect to main page as no data found in database?
another one, i get Undefined offset: 0 which i googled say mismatch array and data not NULL.
anyhow, here are those code :
controller : StudentController.php
public function actionCall()
{
$result = $_POST['semester'];
$result_explode = explode('|', $result);
$sem = $result_explode[0];
$tahun = $result_explode[0]." ".$result_explode[1];
$send = array(
'id' => $_POST['id'],
'semester' => $sem ,
'tahun' => $tahun);
$model = new Student();
if(!$data['result']= $model->getDetails($send))
{
return $this->render('detail', $data);
}
else
{
return $this->render('detail');
}
}
public function actionSearch()
{
return $this->render('searchstudent2');
}
model : Student.php
public function getDetails($send)
{
$student = student::find()
->select('s.student_name,al.level_matric_no,al.level_semester,al.level_id,s.student_mykad, s.student_address,s.student_postcode,
s.student_state,ss.subject_code,ss.subject_name,ss.subject_credit_hour,c.course_name,st.taken_session,
g.Grade_symbol,g.Grade_value,sr.semester_gpa,sr.semester_cgpa,sr.total_point,
sr.total_credit, sr.semester_count')
->from('student AS s')
->leftJoin('a_level AS al', '`s`.`student_id` = `al`.`student_id`')
->leftJoin('subject_taken AS st', '`al`.`level_id` = `st`.`level_id`')
->leftJoin('semester_result AS sr', '`al`.`level_id` = `sr`.`level_id`')
->leftJoin('grade AS g', '`g`.`grade_id` = `st`.`grade_id`')
->leftJoin('course AS c', '`al`.`level_course_offered` = `c`.`course_id`')
->leftJoin('subject AS ss', '`ss`.`subject_id` = `st`.`subject_id`')
->where(['al.level_id'=>$send['id']])
->andWhere(['sr.semester_count'=>$send['semester']])
->andWhere(['st.taken_session'=>$send['tahun']])
->asArray()
->all();
return $student;
}
view : searchstudent2.php
<div class="container">
<div class="row">
<h2>Stylish Search Box</h2>
<div id="custom-search-input">
<?php $form = ActiveForm::begin(['action' => Url::to(['student/call']),'options' => ['method' => 'post']]) ?>
<div class="input-group col-md-12">
<select name="semester">
<option value="1|2012/2013">sem1</option>
<option value="2|2013/2014">sem2</option>
<option value="3|2014/2015">sem3</option>
<option value="4|2015/2016">sem4</option>
</select>
<br><br>
<input type="text" name="id" class="search-query form-control" placeholder="Search" />
<span class="input-group-btn">
<button class="btn btn-danger" type="submit">
<span class=" glyphicon glyphicon-search"></span>
</button>
</span>
</div>
<?php ActiveForm::end() ?>
</div>
</div>
detail.php
table 1 in detail.php
<table style="width:100%">
<tr>
<th class="tg-yw4l">Nama</th>
<th class="tg-baqh" colspan="7"><?php echo $result[0]['student_name']; ?></th>
</tr>
<tr>
<td class="tg-yw4l">Alamat</td>
<td class="tg-baqh"><?php echo $result[0]['student_address']; echo " ". $result[0]['student_postcode']; echo " ".$result[0]['student_state'];?></td>
<td class="tg-yw4l" rowspan="4"></td>
<td class="tg-yw4l">Kemasukan</td>
<td class="tg-baqh"></td>
<td class="tg-yw4l" rowspan="4"></td>
<td class="tg-yw4l" colspan="2"></td>
</tr>
<tr>
<td class="tg-yw4l">No. KP</td>
<td class="tg-baqh"><?php echo $result[0]['student_mykad']; ?></td>
<td class="tg-yw4l">Sesi</td>
<td class="tg-baqh"><?php echo $result[0]['taken_session']; ?></td>
<td class="tg-yw4l">Tahun Akademik</td>
<td class="tg-yw4l"><?php echo $result[0]['taken_session']; ?></td>
</tr>
<tr>
<td class="tg-yw4l">No. Matrik</td>
<td class="tg-baqh"><?php echo $result[0]['level_matric_no']; ?></td>
<td class="tg-yw4l">Fakulti</td>
<td class="tg-baqh"></td>
<td class="tg-yw4l" colspan="2" rowspan="2"></td>
</tr>
<tr>
<td class="tg-yw4l">Program</td>
<td class="tg-baqh"><?php echo $result[0]['course_name']; ?></td>
<td class="tg-yw4l">Semester</td>
<td class="tg-baqh"></td>
</tr>
<tr>
<td class="tg-yw4l">Pinjaman</td>
<td class="tg-baqh" colspan="7"></td>
</tr>
table 2 in detail.php
<tr>
<th class="tg-031e">BIL</th>
<th class="tg-031e">KOD</th>
<th class="tg-031e">SUBJEK</th>
<th class="tg-yw4l">KREDIT</th>
<th class="tg-yw4l">GRED</th>
<th class="tg-yw4l">MATA</th>
<!-- <th class="tg-yw4l">GPA/CGPA</th> -->
</tr>
<?php
$bil=0;
foreach ($result as $details) {
$bil++;
?>
<tr>
<td class="tg-031e"><?=$bil?></td>
<td class="tg-031e"><?php echo $details['subject_code']; ?></td>
<td class="tg-031e"><?php echo $details['subject_name']; ?></td>
<td class="tg-yw4l"><?php echo $details['subject_credit_hour']; ?></td>
<td class="tg-yw4l"><?php echo $details['Grade_symbol']; ?></td>
<td class="tg-yw4l"><?php echo $details['Grade_value']; ?></td>
</tr>
<?php } ?>
<tr>
<td class="tg-031e" colspan="2"></td>
<td class="tg-031e">TOTAL KREDIT</td>
<td class="tg-yw4l"><?php echo $details['total_point']; ?></td>
<td class="tg-yw4l">JUMLAH JAM KREDIT</td>
<td class="tg-yw4l" colspan="3"><?php echo $details['total_credit']; ?></td>
</tr>
Try the following steps one by one
check $_POST['semester'] and make sure it is not empty
check $model->getDetails($send) returns any result or not.
Note: find()-> ...asArray()->all() returns an empty array if no result found. so check for empty result before for render view.
if( !empty($data=$model->getDetails($send)) )
{
return $this->render('detail', ['viewData'=>$data]);
}
else
{
return $this->render('_another_view');
// or redirect to some page or do whatever
}
Note: if you send empty $data array to the view and if you use $data[0]['anything']
you will get Undefined offset: 0 error.
so if result is empty then render another view. in your case you are rendering same view 'detail' one with $data another without $data.
So there is a high chance to get Undefined offset: 0 error.
Thanks
If getDetails functions doesn't return data, redirect to main page:
if ( ($data = $model->getDetails($send)) != null ) {
return $this->render('detail', ['result' => $data]);
} else {
// redirect no data
return $this->redirect(['site/index']);
}

Categories