Displaying the nested foreach with if else in php codeigniter - php

Controller
public function admin()
{
// get the current date employee logged in details
$data['get_attendance'] = $this->attendance_model->get_attendance();
// get the active employee details
$data['get_employee'] = $this->attendance_model->get_employee();
//print_r($data['get_employee']); exit();
// attendance page
$data['header'] = "Employee";
$data['sub_header'] = "Attendance employee";
$data['main_content'] = 'attendance/admin_list';
$this->load->view('employeelayout/main',$data);
}
Model
public function get_attendance()
{
return $this->db->where('date',date('Y-m-d'))->get('attendance')->result_array();
}
public function get_employee()
{
return $this->db->where('is_active',1)->get('employee')->result_array();
}
Views
<?php $count = 1 ?>
<?php foreach ($get_employee as $employee) { ?>
<tr class="gradeX">
<td><?php echo $count++ ?></td>
<td><?php echo $employee['first_name'] . ' ' . $employee['last_name'] ?></td>
<td><?php echo $employee['employee_number'] ?></td>
<?php foreach ($get_attendance as $attendance) : ?>
<!-- if employee exists -->
<?php if($employee['employee_id'] == $attendance['employee_id'] ) { ?>
<td><?php echo date('M-Dj-Y',strtotime($attendance['date'])) ?></td>
<td><?php echo $attendance['mark_in_time'] ?></td>
<td>mark out going</td>
<td>Active</td>
<?php break; ?>
<?php } elseif($employee['employee_id'] != $attendance['employee_id'] ) { ?>
<td><?php echo "i'm absent" ?></td>
<?php break; ?>
<?php } ?>
<?php endforeach; ?>
</tr>
<?php } ?>
I want to display the currently logged in (present) employee and display the absent employees too. I'm getting the employee details from employee table.
and attendance details from attendance table. if an employee is absent and I want to display the absent else display the login details. where here foreach loop not displaying properly with if condition. what's wrong with the loop.

try this:
<?php foreach ($get_employee as $employee) : ?>
<tr class="gradeX">
<td><?php echo $count++ ?></td>
<td><?php echo $employee['first_name'] . ' ' . $employee['last_name'] ?></td>
<td><?php echo $employee['employee_number'] ?></td>
<?php foreach ($get_attendance as $attendance) : ?>
<!-- if employee exists -->
<?php if($employee['employee_id'] == $attendance['employee_id']) : ?>
<td><?php echo date('M-Dj-Y',strtotime($attendance['date'])) ?></td>
<td><?php echo $attendance['mark_in_time'] ?></td>
<td>mark out going</td>
<td>Active</td>
<?php elseif ($employee['employee_id'] != $attendance['employee_id']) : ?>
<td><?php echo "i'm absent" ?></td>
<?php else : ?>
<?php endif; ?>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>

Apply left join in place of two different queries.
Because currently you have applied two loops and their comparisons, will consume a lot of execution time And I don't think this is the good code If you get thousands of data than definitely you can analyse the execution time.
So it's better to apply join query.

Related

How to avoid duplicates in nested foreach loop php

Why are values are getting printed multiple times (see images below). I need to print them only once.
<?php foreach($patchData3 as $tes3){?>
<?php foreach($patchData1 as $tes){?>
<tr class="<?php if($tes->PATCH == $tes3->PATCH) {echo "red_color"; } ?>">
<td><?php echo $tes->HOSTNAME;?></td>
<td><?php echo $tes->VERSION;?></td>
<td><?php echo $tes->PATCH;?></td> <!--bgcolor="#FF0000"-->
</tr>
<?php } ?>
<?php }?>
<?php foreach($patchData1 as $tes){ ?>
<tr class="<?php if(checkfunction($tes->PATCH,$patchData3) == TRUE) { echo "red_color"; } ?>">
<td><?php echo $tes->HOSTNAME;?></td>
<td><?php echo $tes->VERSION;?></td>
<td><?php echo $tes->PATCH;?></td> <!--bgcolor="#FF0000"-->
</tr>
<?php } ?>
<?php
function checkfunction($patch,$patchData3){
foreach($patchData3 as $tes3){
if($patch == $tes3->PATCH){
return true;
}
}
}
?>
I used a function to overcome the duplication. Please comment if it does not work.

Display two arrays in foreach loop in Php?

I want to display data in single foreach loop. I have two tables dailystats and monthlystats both of them have same columns like calls,minutes,incomingcalls etc Im using Yii PHP Framework Here is my controller
public function actionViewStats(){
$model = new Company();
$id = $_GET['id'];
$modelMonthly = $model->getCompanyUsageMonthly($id);
$modelDaily = $model->getCompanyUsageDaily($id);
$this->renderPartial('/shared/_company_stats', array(
'modelDaily' => $modelDaily,
'modelMonthly'=>$modelMonthly
));
}
Here is my view of table.
<?PHP if(isset($modelMonthly) && sizeof($modelMonthly)!=0): ?>
<div class="ibox-content col-md-12 col-xs-12">
<div class="title col-md-2">
<h3>Current Usage</h3>
</div>
<div class="col-md-10">
<div class="col-md-12 col-xs-12">
<table class="table">
<thead>
<th>Minutes</th>
<th>Incoming Minutes</th>
<th>Calls</th>
<th>Incoming Calls</th>
</thead>
<tbody>
<?PHP
if(isset($modelMonthly)){
foreach($modelMonthly as $monthlystats){
?>
<tr>
<td><?php echo $monthlystats->minutes?></td>
<td><?PHP echo $monthlystats->incoming_minutes; ?></td>
<td><?PHP echo $monthlystats->calls; ?></td>
<td><?PHP echo $monthlystats->incoming_calls; ?></td>
</tr>
<?PHP } } ?>
</tbody>
</table>
</div>
</div>
</div>
<?PHP endif;?>
This is showing only monthly stats modelMonthly but i want to show daily stats modelDaily too in the same foreach loop.. How do i do that? I have tried array_combine etc but unable to do it. Searched on SOF but unable to find solution for it.
My code above shows only Monthly stats like this
But I want to show like this below. I have already made the table but im not able to use both modelDaily and modelMonthly in same foreach loop. I have tried different things but unable to do it..
<?PHP
if(isset($modelMonthly)){
foreach($modelMonthly as $usage){
?>
<tr>
<td><?php echo $usage->minutes?></td>
<td><?PHP echo $usage->incoming_minutes; ?></td>
<td><?PHP echo $usage->calls; ?></td>
<td><?PHP echo $usage->incoming_calls; ?></td>
</tr>
<?PHP } } ?>
If both your arrays have numerical indexes, you can use a regular for loop:
for ($i = 0; $i < max(count($modelMonthly), count($modelDaily)); $i) {
if (isset($modelDaily[$i]) {
// Add the daily columns
} else {
// Add as much empty columns
}
if (isset($modelMonthly[$i]) {
// Add the monthly columns
} else {
// Add as much empty columns
}
}
The next step is adding the required headers th in the thead, and adding the extra td inside the loop.
Alternatively, you can create both table independantly, and position them using CSS. It is not in the scope of this question, but this is what I would recommend.
You shoudl use this tecnique
foreach($modelMonthly as $index => $usage){
?>
<tr>
<td><?php echo $usage->minutes?></td>
<td><?PHP echo $usage->incoming_minutes; ?></td>
<td><?PHP echo $usage->calls; ?></td>
<td><?PHP echo $usage->incoming_calls; ?></td>
<td><?php if (isset($dailystats[$index]->minute)) echo $dailystats[$index]->minutes?></td>
<td><?PHP if (isset($dailystats[$index]->incoming_minutes)) echo $dailystats[$index]->incoming_minutes; ?></td>
<td><?PHP if (isset($dailystats[$index]->calls)) echo $dailystats[$index]->calls; ?></td>
<td><?PHP if (isset($dailystats[$index]->incoming_calls)) echo $dailystats[$index]->incoming_calls; ?></td>
</tr>
<?PHP } } ?>

iterating in two arrays in the same for loop

I'm learning php and I want to know how to iterate in two arrays in the same for loop? This is the code I have, line 8 to line 14 for example, but it gives an array instead of an element, what to do? please be easy on me I'm still new to this field.
<tr>
<td><?php echo $val->title ?></td>
<td><?php echo $val->content?></td>
<td><?php echo $val->owner ?></td>
<?php } ?>
<?php
foreach ($users as $val) {
?>
<td><?php echo str_replace("-", "%", $val->email) ?></td>
<?php }?>
<?php foreach ($maincat as $val) {?>
<td><?php echo str_replace("-","%",$val->maincat_name) ?></td>
<?php }?>
<?php foreach ($category as $val) {?>
<td><?php echo str_replace("-","%",$val->category_name) ?></td>
<?php }?>
<?php foreach ($sub_category as $val) {?>
<td><?php echo str_replace("-","%",$val->sub_category_name) ?></td>
<?php }?>
<?php foreach ($sub_sub_category as $val) {?>
<td><?php echo str_replace("-","%",$val->sub_sub_category_name) ?></td>
<?php }?>
</tr>
In this image, it gives the whole cells
instead of the two cells {sajeda#sajeda.com and email2} , I want the cell sajeda#sajeda.com to be in the bottom and the other one *email*2 to be above.
This is not an html question, when iterating, I want the first value to be sajeda#sajdea.com and the other iteration to give email2
can somebody help ?
EDIT :
this is how i define users
$data['users'] = $this->model->join_users($table)->result();
I don't know is this what you're looking for?
I used a couple shorthands such as <?=
to replace <?php echo and using foreach(...) : endforeach;
<?php $i = 0; ?>
<?php foreach ($users as $val) : ?>
<td><?= str_replace("-", "%", $val->email) ?></td>
<td><?= str_replace("-", "%", $maincat[$i]->maincat_name) ?></td>
<td><?= str_replace("-", "%", $category[$i]->category_name) ?></td>
<td><?= str_replace("-", "%", $sub_category[$i]->sub_category_name) ?></td>
<td><?= str_replace("-", "%", $sub_sub_category[$i]->sub_sub_category_name) ?></td>
<?php $i++; ?>
<?php endforeach; ?>
result() as $msg):?>
db->select("lastname, firstname,mi")->from("tb_employee")->where("employee_id",$msg->sender)->get(); ?>
<a href=<?php echo base_url() ."mainx/show_msg_id/". $msg->msg_id; ?> >
<?php foreach($s->result() as $t):?>
<div>
<strong><?php echo $t->lastname . ' ' . $t->firstname;?></strong>
<span class="pull-right text-muted">
<em><?php echo date('m/d/Y H:i:s' ,$msg->timestamp); ?></em>
</span>
</div>
<div><?php echo $msg->message;?></div>
</a>
<?php endforeach; ?>
<?php endforeach; ?>
What i do in my example is that i am loop to foreach. On my first loop is the name of employee and in between of my employee loop is the message foreach loop.

How to display data in table based on the year in PHP

I want to display based on the year with repeated first_ ids and actual ids. but actual_id is unique.
<?php while($Row=oci_fetch_assoc($Result)){
$ResultArray[$j]['First_ID']=$Row['First_ID'];
$ResultArray[$j]['Actual_ID']=$Row['Actual_ID'];
$ResultArray[$j]['Title']=$Row['Title'];
$ResultArray[$j]['START_DATE']=$Row['START_DATE'];
$startDate=explode(' ',$Row['START_DATE']);
$ResultArray[$j]['YEAR']=$startDate[0];
if(($startDate[0])==($startyear)){
$ResultArray[$j][$startyear]['Actual_ID']=$Row['Actual_ID'];
$ResultArray[$j][$startyear]['Other_ID']=$Row['Other_ID'];
$ResultArray[$j][$startyear]['Content']=$Row['Content'];
}
if(($startDate[0])==($startyear+1)){
$ResultArray[$j][$startyear+$i]['Actual_ID']=$Row['Actual_ID'];
$ResultArray[$j][$startyear+$i]['Other_ID']=$Row['Other_ID'];
$ResultArray[$j][$startyear+$i]['Content']=$Row['Content'];
}
if(($startDate[0])==($startyear+1)){
$ResultArray[$j][$startyear+$i]['Actual_ID']=$Row['Actual_ID'];
$ResultArray[$j][$startyear+$i]['Other_ID']=$Row['Other_ID'];
$ResultArray[$j][$startyear+$i]['Content']=$Row['Content'];
}
$j++;
}?>
//to display in table
it display year with asc order first and then next year etc.
<table class="tab">
<tr><th>S.No</th>
<th>first_ id</th><th>Title</th>
<?phpfor($i=0;$i<4;$i++){?><th colspan="3"><?php echo $startyear+$i; ?> </th>
<?php }?>
<tr><th></th><th></th><th></th>
<th>Actual_ID</th><th>Other_ID</th><th>Content</th>
<th>Actual_ID</th><th>Other_ID</th> <th>Content</th>
<th>Actual_ID</th><th>Other_ID</th><th>Content</th>
<th>Actual_ID</th><th>Other_ID</th><th>Content</th></tr>
<?php
foreach($ResultArray as $ResultArray1){
//print_r($abstarctResultArray1);
?><tr><td><?php echo $count; $count++;?></td>
<td><?php echo $ResultArray1['First_ID'];?></td>
<td><?php echo $ResultArray1['Title'];?></td>
<?php for($i=0;$i<4;$i++){
$startDate=explode(' ',$ResultArray1['START_DATE']);
if(($startDate[0]==$startyear)){?>
<td><?php echo $ResultArray1[$startDate[0]['Actual_ID'];?></td>
<td><?php echo $ResultArray1[$startDate[0]['Other_ID'];?></td>
<td><?php echo $ResultArray1[$startDate[0]['Content'];?></td>
<?php }
else if(($startDate[0]==$startyear+1)){?>
<td><?php echo $ResultArray1[$startDate[0]['Actual_ID'];?></td>
<td><?php echo $ResultArray1[$startDate[0]['Other_ID'];?></td>
<td><?php echo $ResultArray1[$startDate[0]['Content'];?></td>
</tr>
<?php
}
else if(($startDate[0]==$startyear+2)){?>
<td><?php echo $ResultArray1[$startDate[0]['Actual_ID'];?></td>
<td><?php echo $ResultArray1[$startDate[0]['Other_ID'];?></td>
<td><?php echo $ResultArray1[$startDate[0]['Content'];?></td>
</tr>
<?php
}
}
}
?>
I have tried with different ideas, but I can't. Please help.

parsing XML web service

i have this code controler CI:
function cek_xml() {
$response = $_SESSION ['nim'];
// fetch data
$respons = $this->curl->simple_get($url = "http://localhost/restful/index.php/restful/buku/nim/$response/format/xml");
if (empty($response)) {
show_error('can`t access :' . $response);
}
$data['cuaca'] = $this->format->factory($respons, 'xml')->to_array();
$this->load->view('data_buku_XML', $data);
}
in view :
<?php $no=1;?>
<?php //$this->benchmark->mark('rest_start'); ?>
<?php foreach ($cuaca as $row) { ?>
<?php foreach ($row as $row) { ?>
<tr class="<?php echo ($no % 2 == 0) ?>">
<td><?php echo $no; ?></td>
<td><?php echo $row['title']; ?></td>
<td><?php echo $row['loan_date'] ?></td>
<td><?php echo $row['due_date'] ?></td>
<?php $no=$no+1;?>
</tr>
<?php } ?>
<?php } ?>
The problem is when i get the title, loan date, due date more than one variable it's okay.
But if get 1 title it's will show:
Message: Illegal string offset 'title'
But if i put // like in one foreach:
// foreach ($row as $row) {
It will show 1 variable title but error in more one variable ...
You are reusing same name for variables
Change the variable name
<?php foreach ($row as $r)
and use as
<td><?php echo $r['title']; ?></td>

Categories