I have a array in controller I have to send it to view and then show the value
The Controller page code
public function showbookedmenu(){
$rowLength=$this->input->post('rowno');
$check=$this->input->post('checkeed');
$j=0;
for($i=1;$i<=$rowLength;$i++)
{
$check=$this->input->post('checkeed'.$i);
if($check== "checked")
{
$orderedItem[$j][$i] = $this->input->post('txtMenuItem'.$i);
$orderedItem[$j][$i+1] = $this->input->post('quantity'.$i);
$orderedItem[$j][$i+2] = $this->input->post('lebPrice'.$i);
$orderedItem[$j][$i+3]= $this->input->post('grandtotal'.$i);
$j++;
}
else{}
}
$data['order']= $orderedItem;
$this->load->view('orderpage', $data);
}
The view page
What will be the code??
First rewrite your controller action like this:
public function showbookedmenu()
{
$rowLength = $this->input->post('rowno');
$orderedItem = array();
for($i = 1; $i <= $rowLength; $i++)
{
$check = $this->input->post('checkeed'.$i);
if($check == "checked")
{
$orderedItem[] = array(
'menu_item' => $this->input->post('txtMenuItem'.$i),
'quantity' => $this->input->post('quantity'.$i),
'leb_price' => $this->input->post('lebPrice'.$i),
'grand_total' => $this->input->post('grandtotal'.$i)
);
}
}
$data['order'] = $orderedItem;
$this->load->view('orderpage', $data);
}
Then in your view you can do this:
<? foreach($order as $order_item): ?>
<? echo $order_item['menu_item']; ?>
<? echo $order_item['quantity']; ?>
<? echo $order_item['leb_price']; ?>
<? echo $order_item['grand_total']; ?>
<? endforeach; ?>
first try to print array value in your view file..
print_r($order);
if it is coming fine..
you can give it on
foreach ($order as $value):
and use it :
Related
All I want is to get some place_ids from db, put them in array and echo that array in view. Could you please check the code below and help me to find the mistake. I think the problem is in view part.
Model:
$this->db->select('place_id');
$this->db->from('table');
$this->db->where('ses_id', $ses_id);
$query = $this->db->get();
if ($query && $query->num_rows() > 0) {
return $query->result_array();
}
else {return false;}
Controller:
$ses_id = $this->uri->segment(3);
$data["results"] = $this->mymodel->did_get($ses_id);
$this->load->view("my_view", $data);
View:
<?php
$place_id = array();
foreach($results as $row){
$place_id[] = $row['place_id'];
}
print_r($place_id);
?>
checking the $results is empty or not
<?php
$place_id = array();
if(!empty($results)
{
foreach($results as $row)
{
$place_id[] = $row['place_id'];
}
}else
{ echo "no data found";}
print_r($place_id);
?>
helper.website.php
public function load_top_players() {
$this->load->lib(array('rank_db', 'db'), array(HOST, USER, PASS,CHARACTERS));
$query = $this->rank_db->query('SELECT name, access FROM users ORDER BY access DESC LIMIT 5');
while($row = $query->fetch()) {
$this->rank[] = array(
'name' => htmlspecialchars($row['name']),
'level' => (int)$row['access']
);
}
return $this->rank;
}
view.header.php
<?php
$rank = load::get('website');
$i = 1;
foreach ($rank as $pos => $player) {
$first = (in_array($i, array(1))) ? '' : '';
$second = (in_array($i, array(2))) ? '' : '';
echo '<tr style="'.$first.' '.$second.' '.$third.'">
<td >'.$i.'</td>
<td>'.$player['name'].'</td>
<td>'.$player['access'].'</td>
</tr>';
$i++;
}
?>
It shows a blank page, no errors no nothing!
helper.website.php
public function getOnlineCount(){
$this->load->lib(array('rank_db', 'db'), array(HOST, USER, PASS, CHARACTERS));
return $this->registry->rank_db->snumrows('SELECT count(name) as count from users where status <> -1 AND sub_status>= 1');
}
and in in view.header.php <?php echo load::get('website')->getOnlineCount() ; ?> works perfect ! how can i transform that to my query
It's showing a blank page because you didn't pass any data to your view file. For getting data in your view file you must load a view from your controller with some data. Syntax is-
<?php
class Your_Controller_Class extends CI_Controller {
function your_function()
{
$data['data'] = 'Your data'; //get it from your model or whatever
$this->load->view('your_view_file', $data);
}
}
?>
and then access it from your view file.
I have implemented the CI Pagination into the system correctly. i have set limit 25 result per page. but i like to let user to change the limit such as 50,75,100 if they wish. The system work perfectly in first page if the user choose to view 50 result, but when the user click on the next page link or do sort table column, it return to limit 25.
here is my code.
view:
<?php
if(isset($_POST['num']))
{
$selected_option = $_POST['num'] ;
}else{
$selected_option ='';
}
$options = array(25,50,75,100);
?>
<form id="myForm" method="post" action = "">
Show <select name="num" onchange="document.getElementById('myForm').submit()">
<?php
$selected_option = $_POST['num'];
foreach($options as $v){
if($v == $selected_option){
$selected = 'selected = "selected"';
}else{
$selected = '';
}
echo "<option value='$v' $selected>$v</option>";
}
?>
</select> entries
</form>
<table style="width:100%">
<thread>
<?php foreach ($fields as $field_name => $field_display): ?>
<th <?php if($sort_by == $field_name) echo "class=\"sort_$sort_order\"" ?>>
<?php echo anchor("rps/index/$field_name/".
(($sort_order == 'asc' && $sort_by == $field_name) ? 'desc' : 'asc'), $field_display); ?>
</th>
<?php endforeach; ?>
</thread>
<tbody>
<?php foreach ($record as $key): ?>
<tr>
<?php foreach ($fields as $field_name => $field_display): ?>
<td><?php echo $key->$field_name; ?></td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php if (strlen($pagination)): ?>
Pages: <?php echo $pagination; ?>
<?php endif; ?>
controller:
public function index($sort_by='player_item', $sort_order='asc',$offset=0)
{
if (isset($_POST['num']) && !empty($_POST['num']))
$limit = $this->input->post('num');
else
$limit= 25;
$data7['fields'] = array(
'id' => 'ID',
'time' => 'Time',
'player_item' => 'Player Choose',
'comp_item'=> 'Computer Choose',
'result'=> 'Result'
);
$this->load->model('rps_result');
$result = $this->rps_result->history($limit,$offset,$sort_by,$sort_order);
$data7['record'] = $result['rows'];
$data7['num_record'] = $result['number_rows'];
//pagination
$this->load->library('pagination');
$this->load->helper('url');
$config['base_url'] = "http://localhost/xampp/CodeIgniter/index.php/rps/index/$sort_by/$sort_order";
$config['total_rows'] = $data7['num_record'];
$config['per_page'] = $limit;
$config['uri_segment'] = 5;
$this->pagination->initialize($config);
$data7['pagination'] = $this->pagination->create_links();
$data7['sort_by'] = $sort_by;
$data7['sort_order'] = $sort_order;
$this->load->view('rps_result', $data7);
}
model:
function history($limit,$offset,$sort_by,$sort_order)
{
$sort_order = ($sort_order == 'desc') ? 'desc' : 'asc';
$sort_columns = array('id','player_item','comp_item','result','time');
$sort_by = (in_array($sort_by, $sort_columns)) ? $sort_by : 'time';
//result query
$query = $this->db->select('id,player_item,comp_item,result,time')
->from('rps')
->limit($limit,$offset)
->order_by($sort_by,$sort_order);
$ret['rows'] = $query->get()->result();
//count query
$query1 = $this->db->select('count(*) as count', FALSE)
->from('rps');
$tmp = $query1->get()->result();
$ret['number_rows'] = $tmp[0]->count;
return $ret;
}
Any help will be deeply appreciated.
You should pass 'num' from page to page. When you select the value, it gets the form submitted and the page is reloaded. But it doesn't use $_POST['num'] on the next pages, the value doesn't pass trough. The easyest way to fix this, would be to store the value in a cookie. In your controller instead of $limit = $this->input->post('num'); do
if($this->input->post('num')){
setcookie('pagination_limit',$this->input->post('num'));
$limit = $this->input->post('num');
}elseif($this->input->cookie('pagination_limit')){
$limit = $this->input->cookie('pagination_limit', true);
}else{$limit = 25;}
In your view, you should also repalace $_POST['num'] with $this->input->cookie('pagination_limit', true);
I want to make an invoice form. For this reason i need to insert multiple data at once. for example I want to input db 5 category sale or purchase product at a time. But I am not interested with insert_batch. I try but i got some null value in db.
My Model is:
<?php
class Purchase_model extends CI_Model{
public function purchase(){
$price = $this->input->post('price');
$quantity = $this->input->post('quantity');
$date = $this->input->post('date');
$vendor_name = $this->input->post('vendor_name');
$model = $this->input->post('model');
$invoice_no = $this->input->post('invoice');
//$temp = count($this->input->post('vendor_name'));
for($i=0; $i<10; $i++){
$data = array(
'date'=>$date[$i],
'vendor_name'=>$vendor_name[$i],
'model'=>$model[$i],
'price' =>$price[$i],
'purchase_quantity'=>$quantity[$i],
'amount' =>$price[$i]*$quantity[$i],
'invoice_no'=>$invoice_no[$i]
);
$insert = $this->db->insert('purchase',$data);
return $insert; }
}}
My controller is:
public function purchase(){
if($this->Purchase_model->purchase()){
$this->session->set_flashdata('Success',
'You are entered data successfully');
redirect('home/purchase_form');
}
}
My view for example:
<?php echo form_label ('Price:'); ?>
<select name="price[]" id="price" class="input-xlarge">
</select>
<?php echo form_label ('Quantity'); ?>
<?php
$data = array ('name' =>'quantity[]',
'class' =>'input-xlarge',
'value' => set_value('quantity')); ?>
<?php echo form_input ($data); ?>
<?php echo form_label ('Price:'); ?>
<select name="price[]" id="price2" class="input-xlarge">
</select>
<?php echo form_label ('Quantity'); ?>
<?php
$data = array ('name' =>'quantity[]',
'class' =>'input-xlarge',
'value' => set_value('quantity')); ?>
<?php echo form_input ($data); ?>
Please help.
The problem has been solved. My mistake in my model. The correct model is
public function purchase(){
$data = array();
$temp = count($this->input->post('quantity'));
for($i=0; $i<$temp; $i++){
$invoice_no = $this->input->post('invoice');
$date = $this->input->post('date');
$price = $this->input->post('price');
$quantity = $this->input->post('quantity');
$vendor_name = $this->input->post('vendor_name');
$model = $this->input->post('model');
if( $quantity[$i]!= '') {
$data[] = array(
'date'=>$date,
'vendor_name'=>$vendor_name[$i],
'model'=>$model[$i],
'price' =>$price[$i],
'purchase_quantity'=>$quantity[$i],
'amount' =>$price[$i]*$quantity[$i],
'invoice_no'=>$invoice_no
);} }
$insert = count($data);
if($insert)
{
$this->db->insert_batch('purchase', $data);
}
return $insert;
}
Thanks every one who try to help me.
<?php
print_r($optimum);
$dataNumRows = count($optimum);
?>
<?php for ($i = 0; $i < $dataNumRows; $i++) : ?>
<?php echo $cFirstName; ?>
<?php echo $cLastName; ?>
<?php endfor; ?>
My print_r inserted in my VIEW shows the following:
Array ( [cFirstName] => Array ( [0] => Tom [1] => Alexa ) [cLastName] => Array ( [0] => Jones [1] => Planter ) )
My MODEL is the following
//Get all the customers currently pending
//install for the user making the request.
function getAllCustomersPendingInstall()
{
$data=array();
//Need to use sessions to display proper
//records for each user. Temp set id to user #7
$id = 7;
//query the db and return all record where SalesRepId == $id
$query = $this->db->get_where('customers', array('SalesRepId' => $id));
//check logic, if rows exist RETURN all rows, else
//return message that no pending installs is available.
if($query->num_rows != 0) {
foreach($query->result() as $row) {
$data['cFirstName'][] = $row->customerFirstName;
$data['cLastName'] [] = $row->customerLastName;
}
} else {
$data = "No pending installs available!";
return $data;
}
//the following var_dump is only showing the last record.
//need to show all rows (which should be 2)
//var_dump($data); exit;
return $data;
}
My CONTROLLER is the following
{
$this->load->library('table');
$this->load->model('GetData');
$data['optimum'] = $this->GetData->getAllCustomersPendingInstall();
$this->load->view('welcome_message', $data);
}
And my question is how do I properly use the FOR loop in my VIEW so that I can loop through all the returned rows. As you can see the print_r is properly returning the proper rows- However I am unable to loop through them. Thanks for the help! Much appreciated!
Try this in your view:
<?php for ($i = 0; $i < $dataNumRows; $i++) : ?>
<?php echo $optimum['cFirstName'][$i]; ?>
<?php echo $optimum['cLastName'][$i]; ?>
<?php endfor; ?>
I think what you're trying to do is get an associative array for each row returned from the database. Correct me if I'm wrong about that.
Should fix your problem
$data = array();
$data_index = 0;
if($query->num_rows != 0) {
foreach($query->result() as $row) {
$data[$data_index]['cfirst'] = $row->customerFirstName;
$data[$data_index]['clast'] = $row->customerLastName;
$data_index++;
}
} else {
$data = "No pending installs available!";
return $data;
}
then in your view (where $customer is the $data array)
<?php foreach($customer as $c):?>
<?php echo $c['cfirst'];?>
<?php endforeach;?>