How to display data in table based on the year in PHP - 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.

Related

Trying to get property of non-object using OCI connection

I'm new in PHP with OCI connection. I want to retrieve some data from database and insert it into a table form. But it keep show an error
Trying to get property 'attribute' of non-object
I have try to use oci_fetch object / oci_fetch_array, but it still the same. I also have followed some tutorial but it doesn't help.
This is for my mini project for this semester.
Here my source code:
$sql="SELECT borrow.book_id, book_title, borrow.stud_id, stud_name, book_bdate, return_date, due_date
FROM book
JOIN borrow
ON book.book_id = borrow.book_id
JOIN student
ON borrow.stud_id = student.stud_id
where borrow.stud_id = '$stud_id'
ORDER BY 5 DESC ";
$query=oci_parse($link,$sql) or die ("error here!");
oci_execute($query);
while (($row = oci_fetch_array($query, OCI_ASSOC)) != false) {
?>
<td><?php echo $row->stud_id; ?></td>
<td><?php echo $row->book_id; ?></td>
<td><?php echo $row->book_title; ?></td>
<td><?php echo $row->book_bdate; ?></td>
<td><?php echo $row->due_date; ?></td>
<td><?php echo $row->return_date; ?></td>
<td>
<center><a href='return-book.php?book_id=<?php echo $row->book_id; ?>'>Update</a></center>
</td>
<tr>
<?php
}
}
oci_close($link);
?>
Oracle returns field names as uppercase by default so you need to use uppercase indexes like so:
Here the solution where I got.
Btw thank you everyone for helping me.
Correct Your while loop You are fetching data as array:
oci_fetch_array($query, OCI_ASSOC) // return associated array
while (($row = oci_fetch_array($query, OCI_ASSOC)) != false) {
?>
<td><?php echo $row['stud_id']; ?></td>
<td><?php echo $row['book_id']; ?></td>
<td><?php echo $row['book_title']; ?></td>
<td><?php echo $row['book_bdate']; ?></td>
<td><?php echo $row['due_date']; ?></td>
<td><?php echo $row['return_date']; ?></td>
<td>
<center><a href='return-book.php?book_id=<?php echo $row['book_id']; ?>'>Update</a></center>
</td>
<tr>
<?php
}

Print variable in different php file

Hi I have shopping cart and i want print item name in different php file. I try this.
<?php
if(!empty($_SESSION["cart"]))
{
$total = 0;
foreach($_SESSION["cart"] as $keys => $values)
{
$product_cart = $values["item_name"];
?>
<tr>
<td><?php echo $values["item_name"]; ?></td> <?php session_start(); $_SESSION['produktik'] = $values["item_name"] ; ?>
<td><?php echo $values["item_quantity"] ?></td>
<td><?php echo $values["product_price"]; ?>€</td>
<td><?php echo number_format($values["item_quantity"] * $values["product_price"], 2); ?>€</td>
<td><span class="text-danger">X</span></td>
</tr>
and second php file is
<pre>
<?php
session_start();
echo $_SESSION['produktik'];
?>
</pre>

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 } } ?>

Displaying the nested foreach with if else in php codeigniter

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.

Categories