public function get_orderProduct($loginuserID) {
$this->db->select('client_order.*,product.image_gallery,product.name');
$this->db->from('client_order');
$this->db->join('product','client_order.productID = product.productID','LEFT');
$this->db->where('client_order.clientID',$loginuserID);
$this->db->where('client_order.status',1);
$this->db->order_by('client_order.id','desc');
$sql = $this->db->get();
$result = $sql->result();
return $result;
}
<?php
if(count($result) > 0){
$i=1;
foreach($result as $row){
$img = explode(",", $row->image_gallery);
?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $row->orderID; ?></td>
<td><?php echo $row->name; ?></td>
</tr>
<?php
$i++;
}
}else{
?>
<div class="alert alert-danger alert-dismissible fade show" role="alert">
No order available!
</div>
<?php
}
?>
I have two product with same orderID and I want to show same orderID product in single row. Now, Here it show in different different row. So, How can I show in single row? Please help me.
Thank You
To solve this, you have to create an empty array to store orderids.Before you print the orderid you have to search the array using in_array method, if orderid exists ignore printing it, otherwise print and push the orderID to the array using array_push method.
<?php
$ids = array();
$i=1;
foreach($result as $row){
$img = explode(",", $row->image_gallery);
?>
<tr>
<td><?php echo $i; ?></td>
<td>
<?php
if(!in_array($row->orderID,$ids))
{
echo $row->orderID;
array_push($ids,$row->orderID);
}
?>
</td>
<td><?php echo $row->name; ?></td>
</tr>
<?php
$i++;
}
?>
Related
im having trouble getting a result value of a query and the error says.
Notice: Undefined index: prelim in C:\xampp\htdocs\gradingxworking\teacher\student.php on line 85 can someone help me to fix this or some clue to fix this? im just starting to learn php.
<tbody>
<?php $c=1; ?>
<?php foreach($mystudent as $row): ?>
<tr>
<td><?php echo $c; ?></td>
<td class="text-center"><?php echo $row['studid']; ?></td>
<td class="text-center"><?php echo $row['lname'].', '.$row['fname']; ?></td>
<?php $grade = $student->getstudentgrade($row['studid']);?>
//code that cause error line 85--> <td class="text-center"><?php echo $grade['prelim']; ?></td>
</tr>
<?php $c++; ?>
<?php endforeach; ?>
<?php if(!$mystudent): ?>
<tr><td colspan="8" class="text-center text-danger"><strong>*** No Result ***</strong></td></tr>
<?php endif; ?>
</tbody>
the function:
function getstudentgrade($studid){
$q = "select * from studentsubject where studid=$studid";
$r = mysql_query($q);
$data = array();
while($row = mysql_fetch_array($r)){
$data[] = array(
'prelim' => $row['prelim']
);
}
return $data;
}
As #Bara suggested, you need to check the array first before accessing it.
if(isset($grade['prelim']))
I suspect, there is no data for specific studid. Lets see you function.
$data = array();
while($row = mysql_fetch_array($r)){
$data[] = array(
'prelim' => $row['prelim']
);
}
Now, you have created new array $data. But, if there is no record ? your while loop won't be executed and your $data array won't have anything. right ? So, to handle that, you need to check whether there is any data in your array.
Now, second point, which #Mossavari made is also correct. You need to use
$grade[0]['prelim'];
instead of
$grade['prelim'];
after doing
<?php $grade = $student->getstudentgrade($row['studid']);?>
you need to check what $grade holdes. and its better before trying to fetch data from array to make a check like this :
if(isset($grade['prelim']))
Based on your getstudentgrade function you'll have multidimensional array result, you may need to change $grade['prelim'] to $grade[0]['prelim']
Since, every student will have only 1 grade. So, why to use array.
<?php
function getstudentgrade($studid){
$q = "select * from studentsubject where studid=$studid LIMIT 0,1";
$data = "";
$r = mysql_query($q);
while($row = mysql_fetch_array($r)){
$data = $row['prelim'];
}
return $data;
}?>
PHP
<tbody>
<?php $c=1; ?>
<?php foreach($mystudent as $row): ?>
<tr>
<td><?php echo $c; ?></td>
<td class="text-center"><?php echo $row['studid']; ?></td>
<td class="text-center"><?php echo $row['lname'].', '.$row['fname']; ?></td>
<td class="text-center"><?php echo $grade = $student->getstudentgrade($row['studid']);?></td>
</tr>
<?php $c++; ?>
<?php endforeach; ?>
<?php if(!$mystudent): ?>
<tr>
<td colspan="8" class="text-center text-danger">
<strong>*** No Result ***</strong>
</td>
</tr>
<?php endif; ?>
</tbody>
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 } } ?>
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.
I have an activity table that contain the list of activity student had joined before. So if the student is new student, there will have been no activity for that student.
<table align="center" width="1000" border="1" >
<h3>Activity List</h3>
</br>
<tr align="center" style="font-weight:bold" >
<td>ID</td>
<td>Activity</td>
<td>Sem</td>
<td>Session</td>
<td>Achievement</td>
<td>Level</td>
</tr>
<?php do { ?>
<tr align="center">
<td><?php echo $row_Recordset1['student_id']; ?></td>
<td><?php echo $row_Recordset1['activity']; ?></td>
<td><?php echo $row_Recordset1['sem']; ?></td>
<td><?php echo $row_Recordset1['session']; ?></td>
<td><?php echo $row_Recordset1['achievement']; ?></td>
<td><?php echo $row_Recordset1['level']; ?></td>
</tr>
<?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
</table>
How can I make this table only show on screen if it is not empty. Btw, im using session to display the exist record.
The best way to do this would be to get your array via mysql(i)_fetch_array() of the query you have build and then to chec to see if the query has rows like:
$qry = "SELECT `this` FROM `table`"
while ($result = mysql_fetch_array($qry)) {
if (mysql_num_rows($result) > 0) {
echo "We have rows!";
}
else {
echo "Looks like we haven't got anything here!";
}
}
I hope this helps.
Might also help to look here: PHP mysql_num_rows method.
<?php if(!empty($activity))
{
your msg ..
}
else
{
}
?>
where empty() will check a given variable is empty or not
Well that's what the if statement is used for:
if(!count($activities)) {
echo "This student has no activities yet.";
} else {
//display activities
....
}
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>