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>
Related
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++;
}
?>
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>
I'm kind of new in php and i'm searching a way to get some words from a specific column that i get from a sql server stored proc. These 'words' will me to use them as style for my css
Example :
Column HTML_CODE has sometime 'bold', 'green' or something else. Sometime they are in the same column, not always in the same order. I don't know how to use them in my loop. See what i has until now :
$result = sqlsrv_query($conn, $sql);
while ($row = sqlsrv_fetch_array($result))
{
//print_r( $row ); // debug code
if ($row['HTML_CODE'] == 'BOLD(), Green()'){
$couleur='green';
$font= 'bold';
}
else {
$couleur='black';
$font= 'normal';
}
?>
<tbody>
<tr>
<?php echo "<tr style=\"font-weight:$font; color:$couleur;\">"; ?>
<td><?php echo ($row['Nom']); ?></td>
<td><?php echo ($row['Quantite']); ?></td>
<td><?php echo ($row['Montant']); if (is_numeric($row['Montant'])) {
echo ' $';
}?></td>
<td><?php echo ($row['#GL']); ?></td>
<td><?php echo ($row['Debit']); if (is_numeric($row['Debit'])) {
echo ' $';
} ?></td>
<td><?php echo ($row['Credit']); if (is_numeric($row['Credit'])) {
echo ' $';
}?></td>
</tr>
</tbody>
if( strstr($row['HTML_CODE'], "BOLD()")){ $font= 'bold'; }
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 want to populate all the data from my database to a table using <?php foreach; ?> but I can't get it to work. Please check my code:
controller
$data['query'] = $this->db->query('SELECT * FROM users');
$this->load->view('users_view',$data);
View
<?php foreach ($query->result_array() as $row)
{ ;?>
<tr>
<td><?php echo $row['usrID'];?></td>
<td><?php echo $row['usrName'];?></td>
<td><?php echo $row['usrPassword'];?></td>
</tr>
<?php } ?>
<?php endforeach; ?>
I would reccommend the alternative syntax for your foreach in the view...
It looks like you should be having some syntax errors the way you have it.
<?php foreach ($query->result_array() as $row): ?>
<tr>
<td><?php echo $row['usrID'];?></td>
<td><?php echo $row['usrName'];?></td>
<td><?php echo $row['usrPassword'];?></td>
</tr>
<?php endforeach; ?>
EDIT - to point out your syntax issues.
<?php foreach ($query->result_array() as $row)
{ ;?> //<---- semicolon?
<tr>
<td><?php echo $row['usrID'];?></td>
<td><?php echo $row['usrName'];?></td>
<td><?php echo $row['usrPassword'];?></td>
</tr>
<?php } ?>
<?php endforeach; ?> //<--- no need for this when using regular foreach
This first semicolon would be like writing a foreach like this...
foreach ($array as $ele) { ; //<--this would throw an error just like it should above.
//do stuff
}
Opinion aside from the point of the question
You should be processing your data in the controller and simply sending an array of data to be displayed in the view, not the whole query object.
so...
Controller
$query = $this->db->query('SELECT * FROM users');
$data['users'] = $query->result_array();
$this->load->view('users_view',$data);