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);
Related
I have archive.php in my content management system which allows to search for articles like mywebsite.com/archive?s=test. Now i tried to show this posts of the search query in another page of my page. Like <?php insert('archive.php?s=test'); ?> .
I am not an expert on PHP coding. But maybe you can show me a way how i could import this post of a search query with archive.php loop. Or tell me that this is not possible :-) Thank you.
This is the code my archive page.
<?php if (!$t['page_heading']) : ?>
<?php endif; ?>
<?php if (has_items($t['latest_posts'])) : ?>
<div class="row">
<?php foreach ($t['latest_posts'] as $t['loop']) : ?>
<div class="col-12 mb-3">
<?php insert('partials/loop.php'); ?>
</div>
<?php endforeach; ?>
</div> </div> </div>
<?= $t['pagination_html']; ?>
<?php else : ?>
<?php
insert(
'partials/empty.php',
[
'empty_message' => __('nothing-found-archive', _T)
]
);
?>
<?php endif; ?>
This is the loop.php
<?= e(limit_string($t['loop.post_title'], 65), false); ?>
</h3></a>
<div class="post-footer text-muted">
<div id="hidden-xs"> <?= e(limit_string($t['loop.post_excerpt'], 145), false); ?></div>
<div class="mb-1 mt-1"> <span class="badge badge-cat"><i class="fas fa-newspaper"></i> <?= e_attr($t['loop.feed_name']) ?></span> <span class="badge badge-cat"><i class="far fa-folder-open"></i> <?= e_attr($t['loop.category_name']) ?></span>
<span class="badge badge-cat"><i class="far fa-clock"></i> vor <?= time_ago($t['loop.post_pubdate'], _T); ?></span>
search function
public function archive()
{
$app = app();
$postModel = new PostModel;
$feedModel = new FeedModel;
$engineModel = new EngineModel;
$categoryModel = new CategoryModel;
$data = [
'title' => __('archive', _T),
'meta.description' => __('archive-description', _T),
'body_class' => 'archive',
'searching_site' => false,
'by_feed' => false,
];
$query = sp_strip_tags(trim($app->request->get('s')), true);
$feedID = (int) $app->request->get('feed', 0);
$feedsTable = $feedModel->getTable();
$postsTable = $postModel->getTable();
$categoriesTable = $categoryModel->getTable();
// Current page number
$currentPage = (int) $app->request->get('page', 1);
$filters['sort'] = 'post-publish-date';
if (mb_strlen($query) > 3) {
$filters['where'][] = ["{$postsTable}.post_title", 'LIKE', "%$query%"];
$filters['where'][] = ["{$postsTable}.post_content", 'LIKE', "%$query%", 'OR'];
$data['site_search_query'] = $query;
$data['searching_site'] = true;
$data['page_heading'] = sprintf(__('search-results-for', _T), $query);
$data['title'] = sprintf(__('search-results-for-title', _T), $query);
$data['meta.description'] = sprintf(__('search-results-for-desc', _T), $query);
$data['body_class'] .= ' search-results';
}
if ($feedID) {
$feed = $feedModel->read($feedID, ['feed_id', 'feed_name']);
if ($feed) {
$filters['where'][] = ["{$postsTable}.post_feed_id", '=', $feed['feed_id']];
$data['by_feed'] = true;
$data['feed'] = $feed;
$data['title'] = $feed['feed_name'];
$data['page_heading'] = $feed['feed_name'];
}
}
$itemsPerPage = (int) get_option('latest_posts_count', 10);
// Total item count
$totalCount = $postModel->countRows(null, $filters);
$pageQuery = [];
if ($data['searching_site']) {
$pageQuery['s'] = $query;
}
if ($data['by_feed']) {
$pageQuery['feed'] = $feedID;
}
$queryStr = http_build_query($pageQuery);
if (!empty($queryStr)) {
$queryStr = "&{$queryStr}";
}
// Pagination instance
$pagination = new Pagination($totalCount, $currentPage, $itemsPerPage);
$pagination->setUrl("?page=#id#{$queryStr}");
// Generated HTML
$paginationHtml = $pagination->renderHtml();
// Offset value based on current page
$offset = $pagination->offset();
// Fields to query
$fields[] = "{$postsTable}.post_id, {$postsTable}.post_category_id, {$postsTable}.post_title, {$postsTable}.post_excerpt, {$postsTable}.post_featured_image, {$postsTable}.post_type, {$postsTable}.post_source, {$postsTable}.post_pubdate, {$postsTable}.created_at, {$postsTable}.post_hits";
$fields[] = "{$feedsTable}.feed_name, {$feedsTable}.feed_id, {$feedsTable}.feed_logo_url";
$fields[] = "{$categoriesTable}.category_name, {$categoriesTable}.category_slug, {$categoriesTable}.category_icon";
// Query to fetch the users and their respective role names
$sql = $postModel->select($fields)
->leftJoin(
$feedsTable,
"{$postsTable}.post_feed_id",
'=',
"{$feedsTable}.feed_id"
)->leftJoin(
$categoriesTable,
"{$postsTable}.post_category_id",
'=',
"{$categoriesTable}.category_id"
)->limit($itemsPerPage, $offset);
// Apply Filters
$sql = $postModel->applyModelFilters($sql, $filters);
$stmt = $sql->execute();
// List entries
$latestPosts = $stmt->fetchAll();
$maxSliderItems = get_option('max_slider_items', 10);
$sliderPosts = array_slice($latestPosts, 0, $maxSliderItems);
$engines = $engineModel->readMany(['engine_id', 'engine_name']);
$data['default_engine'] = (int) get_option('default_engine');
$data['engines'] = $engines;
$data['archive_active'] = 'active';
$data['slider_posts'] = $sliderPosts;
$data['latest_posts'] = $latestPosts;
$data['pagination_html'] = $paginationHtml;
return view('archive.php', $data);
}
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.
In pagination I am getting error when try to go to any pages than first page (offset=0) here is my code
URI: http://localhost/the-site/admin/hr/career_new/
URI with offset: http://localhost/the-site/admin/hr/career_new/employee_id/desc/3
Error1
A PHP Error was encountered
Severity: Notice
Message: Undefined offset: 0
Filename: .../career_model.php
Line Number: 127
Error2
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: .../career_model.php
Line Number: 127
The error line I have commented This is the error line #127 in below model code. It is right before return $ret
Model
public function sort($limit, $offset, $sort_by, $sort_order)
{
$type = $this->input->post('type');
$emp_id = $this->input->post('employee_id');
$old_operation = $this->input->post('old_operation');
$old_position = $this->input->post('old_position');
$new_operation = $this->input->post('new_operation');
$new_position = $this->input->post('new_position');
$created = $this->input->post('record_date');
$effect = $this->input->post('effective_date');
$sort_order = ($sort_order == 'desc') ? 'desc' : 'asc';
$sort_columns = array(
'type',
'employee_id',
'old_operation',
'old_position',
'new_operation',
'new_position',
'record_date',
'effective_date',
'reason',
);
$sort_by = (in_array($sort_by, $sort_columns)) ? $sort_by : 'id';
// results query
$q = $this->db
->select('*')
->from('career_path')
->like('type', $type)
->like('employee_id', $emp_id)
->like('old_operation', $old_operation)
->like('old_position', $old_position)
->like('new_operation', $new_operation)
->like('new_position', $new_position)
->like('record_date', $created)
->like('effective_date', $effect)
->limit($limit, $offset)
->order_by($sort_by, $sort_order);
$ret['rows'] = $q->get()->result();
// count query
$q = $this->db
->select('COUNT(*) as count', FALSE)
->from('career_path')
->like('type', $type)
->like('employee_id', $emp_id)
->like('old_operation', $old_operation)
->like('old_position', $old_position)
->like('new_operation', $new_operation)
->like('new_position', $new_position)
->like('record_date', $created)
->like('effective_date', $effect)
->limit($limit, $offset)
->order_by($sort_by, $sort_order);
$temp = $q->get()->result();
$ret['num_rows'] = $temp[0]->count; // This is the error line #127
return $ret;
}
Controller
public function career_new($sort_by = 'employee_id', $sort_order = 'desc', $offset = 0)
{
$this->data['title'] = '<i class="fa fa-briefcase"></i> ' . lang('crr_add');
/* * ***********************************************************
* load loop with pagination
* ****************************************************************** */
$limit = get_option('per_page');
// fields
$this->data['columns'] = array(
'id' => 'ID',
'type' => 'Type',
'employee_id' => 'Employee ID',
'old_operation' => 'Old Operation',
'old_position' => 'Old Position',
'new_operation' => 'New Operation',
'new_position' => 'New Position',
'record_date' => 'Created',
'effective_date' => 'Effected',
);
$results = $this->career_model->sort($limit, $offset, $sort_by, $sort_order);
$this->data['careers'] = $results['rows'];
$this->data['num_results'] = $results['num_rows'];
$base_url = base_url() . 'admin/hr/career_new/' . $sort_by . '/' . $sort_order;
//$numbs = $this->employees_model->filter_count();
$total_rows = $this->data['num_results'];
get_pagination($base_url, $total_rows, $limit, 6, 2, TRUE);
//$this->pagination->initialize($config);
$this->data['pagination'] = $this->pagination->create_links();
$this->data['sort_by'] = $sort_by;
$this->data['sort_order'] = $sort_order;
/* * ***********************************************************
* End: load loop with pagination
* ****************************************************************** */
$this->data['career'] = $this->career_model->get_new();
$rules = $this->career_model->rules;
$this->form_validation->set_rules($rules);
if ($this->form_validation->run() == TRUE) {
//db fields/columns to insert value
$crr_fields = array(
'type',
'employee_id',
'old_operation',
'old_position',
'new_operation',
'new_position',
'record_date',
'effective_date',
'reason',
);
$data = $this->career_model->array_from_post($crr_fields);
if (empty($data['reason'])) {
$data['reason'] = NULL;
}
$this->career_model->save($data);
$data['old_position'] = get_employee_id_field($data['employee_id'], 'position');
$data['old_operation'] = get_employee_id_field($data['employee_id'], 'operation');
/* ----------------------------------------------------------------
* Sending email once form validation runs true
--------------------------------------------------------------- */
$email_hr = $this->config->item('hr_email_templates');
$email_template = $this->config->item('crr_new_email');
$message = $this->load->view($email_hr . $email_template, $data, true);
$this->email->clear();
$this->email->from('atlassystem#apolloblake.com', get_config_option('site_name'));
$this->email->to(get_user_data('email', 1));
$this->email->subject(get_config_option('site_name') . ' - ' . $this->lang->line('crr_new_subject'));
$this->email->message($message);
if ($this->email->send()) {
// set session notification message
$this->session->set_flashdata('message', sprintf(lang('crr_record_added'), '#' . $data['employee_id']));
$this->data['message'] = $this->session->flashdata('message');
$this->session->set_flashdata('message_type', 'success');
$this->data['message_type'] = $this->session->flashdata('message_type');
redirect(current_url(), 'refresh');
} else {
$this->load->view('hr/career/form', $this->data);
}
}
$this->load->view('hr/career/form', $this->data);
}
View
<div class="panel-body">
<?php
if (validation_errors()):
echo show_alert(validation_errors(), TRUE, 'danger', 'glyphicon glyphicon-warning-sign');
endif;
?>
<div class="table-responsive">
<table cellpadding = "0" cellspacing = "0" border = "0" class = "table table-hover table-bordered datatables center" id = "">
<thead>
<tr class = "active center">
<?php foreach ($columns as $field_name => $field_display): ?>
<th <?= ($sort_by == $field_name ) ? "class=\"sort_$sort_order\"" : NULL ?>>
<?= anchor("admin/hr/career_new/$field_name/" . (($sort_order == 'asc' && $sort_by == $field_name) ? 'desc' : 'asc'), $field_display); ?>
<?= (($sort_order == 'asc' && $sort_by == $field_name) ? '<span class="glyphicon glyphicon-sort-by-attributes"></span>' : '<span class="glyphicon glyphicon-sort-by-attributes-alt"></span>'); ?>
</th>
<?php endforeach; ?>
</tr>
</thead>
<tbody>
<?php
foreach ($careers as $crr):
echo '<tr>';
foreach ($columns as $field_name => $field_display):
echo '<td>', $crr->$field_name, '</td>';
endforeach;
echo '</tr>';
endforeach;
?>
</tbody>
</table><!--end table-->
</div>
<?php if (strlen($pagination)): ?>
<div>
<?= $pagination; ?>
</div>
<?php endif; ?>
</div>
var_dump output
using var_dump($temp); in model I am getting below on $offset=0 means first page
array (size=1)
0 =>
object(stdClass)[37]
public 'count' => string '7' (length=1)
and var_dump($ret['num_rows']);
tring '7' (length=1)
On second page with offset=3 getting error with below var_dump output
var_dump($temp);
array (size=0)
empty
and
var_dump($ret['num_rows']);
null
Okay so I have fixed the issue. It was a small and simple yet took my 4-5 hours to find and fix it.
I just did copy and paste for result and count query in model and there I made a mistake. I just overlooked the last two ->limit(), and ->order_by() function which was not required for count total rows. This was creating issue. When use pagination it was counting limit and offset. So final fixed code is as below
Model
public function sort($limit, $offset, $sort_by, $sort_order)
{
$type = $this->input->post('type');
$emp_id = $this->input->post('employee_id');
$old_operation = $this->input->post('old_operation');
$old_position = $this->input->post('old_position');
$new_operation = $this->input->post('new_operation');
$new_position = $this->input->post('new_position');
$created = $this->input->post('record_date');
$effect = $this->input->post('effective_date');
$sort_order = ($sort_order == 'desc') ? 'desc' : 'asc';
$sort_columns = array(
'type',
'employee_id',
'old_operation',
'old_position',
'new_operation',
'new_position',
'record_date',
'effective_date',
'reason',
);
$sort_by = (in_array($sort_by, $sort_columns)) ? $sort_by : 'id';
// results query
$q = $this->db
->select('*')
->from('career_path')
->like('type', $type)
->like('employee_id', $emp_id)
->like('old_operation', $old_operation)
->like('old_position', $old_position)
->like('new_operation', $new_operation)
->like('new_position', $new_position)
->like('record_date', $created)
->like('effective_date', $effect)
->limit($limit, $offset)
->order_by($sort_by, $sort_order);
$ret['rows'] = $q->get()->result();
// count query
$q = $this->db
->select('COUNT(*) as count', FALSE)
->from('career_path')
->like('type', $type)
->like('employee_id', $emp_id)
->like('old_operation', $old_operation)
->like('old_position', $old_position)
->like('new_operation', $new_operation)
->like('new_position', $new_position)
->like('record_date', $created)
->like('effective_date', $effect); // removed limit and order_by
$temp = $q->get()->result();
$ret['num_rows'] = $temp[0]->count; // This is the error line #127
return $ret;
}
Can anyone explain why I cannot get a variable called $siteID to pass to another function within the same controller?
In the third function called "get_orders_by_site" I have loaded a different model, which returns information about 'orders' raised at the currently viewed building/site/property.
The sites controller works perfectly, first function lists a table with all my properties, then when one is clicked - the second function gets the siteID of that selection, and returns with further 'detail/data' - sites controller function1/2 all relate to the same model, and return information from the SAME table.
I'm trying to implement a third function, which will do a similar task, but return with information/data from a different table (the site.siteID, is also a FK in the orders.siteID table i've created in phpmyadmin).
If I need to explain further please let me know - Many thanks!
amended code
Sites Controller
<?php
class Sites extends CI_Controller {
//Searches for a list of sites
public function search($sort_by = 'site_title', $sort_order = 'asc', $offset = 0)
{
$limit = 20;
$data['columns'] = array(
'site_title' => 'Site Name',
'site_uprn' => 'Unique Property Reference'
);
$this->load->model('site_model');
$results = $this->site_model->get_sites($limit, $offset, $sort_by, $sort_order);
$data['sites'] = $results['rows'];
$data['num_results'] = $results['num_rows'];
//pagination for list returned
$this->load->library('pagination');
$config = array ();
$config['base_url'] = site_url("Sites/search/$sort_by/$sort_order");
$config['total_rows'] = $data['num_results'];
$config['per_page'] = $limit;
$config['uri_segment'] = 5;
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
$data['sort_by'] = $sort_by;
$data['sort_order'] = $sort_order;
$this->load->view('search', $data);
}
//Displays individual site details
//passes selected siteID to the model, and returns only database/info for that particular building/property
public function details($siteID){
$this->load->model('site_model');
$data['site']=$this->site_model->get_site($siteID);
$this->load->view('site', $data);
$this->load->view('orders', $data);
}
// this second function should do a similar method as above, however I've loaded a different model, as i'm getting information from a different database table - but I still want the data returned to be limited by the building/site ID which the user selects.
public function orders_by_site($siteID, $sort_by = 'orderID', $sort_order = 'asc', $offset = 0)
{
$this->load->model('site_model');
$this->load->model('order_model');
$limit = 20;
$data['columns'] = array(
'orderID' => 'Order No.',
'initiated_date' => 'Initiated Date',
'target_date' => 'Target Date',
'status' => 'Status',
'priority' => 'Priority',
'trade_type' => 'Trade Type'
);
$results = $this->site_model->get_site($siteID);
$results = $this->order_model->get_orders($siteID, $limit, $offset, $sort_by, $sort_order);
$data['orders'] = $results['rows'];
$data['num_results'] = $results['num_rows'];
//pagination for orders table
$this->load->library('pagination');
$config = array ();
$config['base_url'] = site_url("Orders/orders_by_site/$sort_by/$sort_order");
$config['total_rows'] = $data['num_results'];
$config['per_page'] = $limit;
$config['uri_segment'] = 6;
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
$data['sort_by'] = $sort_by;
$data['sort_order'] = $sort_order;
$this->load->view('orders', $data);
}
}
End Sites Controller
Site Model
//Get all site data
function get_sites($limit, $offset, $sort_by, $sort_order){
$sort_order = ($sort_order == 'desc') ? 'desc' : 'asc';
$sort_columns = array('site_title', 'site_uprn');
$sort_by = (in_array($sort_by, $sort_columns)) ? $sort_by : 'site_title';
$q = $this->db->select('siteID, site_title, site_uprn')
->from('sites')
->limit($limit, $offset)
->order_by($sort_by, $sort_order);
$ret['rows'] = $q->get()->result();
//count query for sites
$q = $this->db->select('COUNT(*) as count', FALSE)
->from('sites');
$tmp = $q->get()->result();
$ret['num_rows'] = $tmp[0]->count;
return $ret;
}
//Get individual site data
function get_site($siteID){
$this->db->select()->from('sites')->where(array('siteID' => $siteID));
$query = $this->db->get();
return $query->first_row('array');
}
}
Orders Model
//order table
function get_orders($siteID, $orderID, $limit, $offset, $sort_by, $sort_order){
$sort_order = ($sort_order == 'desc') ? 'desc' : 'asc';
$sort_columns = array('orderID', 'initiated_date', 'target_date','completion_date','status','priority','total_amount','job_description','requestor_name','requestor_telno','trade_type');
$sort_by = (in_array($sort_by, $sort_columns)) ? $sort_by : 'orderID';
$q = $this->db->select()->from('orders')->where(array('siteID' => $siteID))->limit($limit, $offset)->order_by($sort_by, $sort_order);
$ret['rows'] = $q->get()->result();
}
//order details
function get_order($orderID){
$this->db->select()->from('orders')->where(array('orderID' => $orderID));
$query = $this->db->get();
return $query->first_row('array');
}
}
Site View - only showing the extract where I'm trying to embed the orders view
<h5>Order Details</h5>
<?php include('orders.php')?>
</div>
</div>
</div>
Orders View
<div id="site_filter">
<div class="result_counter">
<h5>Found <?php echo $num_results; ?> Orders</h5>
</div>
<div class="pagination">
<?php if(strlen($pagination)): ?>
Page: <?php echo $pagination; ?>
<?php endif; ?>
</div>
</div>
<div class="clear_float"></div>
<table class="table">
<thead>
<?php foreach($columns as $column_name => $column_display): ?>
<th <?php if ($sort_by == $column_name) echo "class=\"sort_$sort_order\"" ?>>
<?php echo anchor("Sites/orders_by_site/$column_name/" .
(($sort_order == 'asc' && $sort_by == $column_name) ? 'desc' : 'asc') ,
$column_display); ?>
</th>
<?php endforeach; ?>
</thead>
<tbody>
<?php foreach($orders as $order): ?>
<tr>
<td><?php echo $order->orderID; ?></td>
<td><?php echo $order->initiated_date; ?></td>
<td><?php echo $order->target_date; ?></td>
<td><?php echo $order->status; ?></td>
<td><?php echo $order->priority; ?></td>
<td><?php echo $order->trade_type; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
OK, a couple of things here - and I'm gonna need some help
In the orders_by_site method, you're overwriting the results variable:
$results = $this->site_model->get_site($siteID);
$results = $this->order_model->get_orders($siteID, $limit, $offset, $sort_by, $sort_order);
Also, the way you're loading views is incorrect. You're loading your views like this:
$this->load->view('site', $data);
$this->load->view('orders', $data);
And what you need to be doing is this:
// you need to get the "contents" of the `orders` view in a variable
// and pass that to the `site` view
$data['orders'] = $this->load->view('orders', $data, TRUE);
$this->load->view('site', $data);
And change your site view to this:
<h5>Order Details</h5>
<?php echo $orders; ?>
</div>
</div>
</div>
I'm sure there's more going on than that, but that's all I can gather from what I've seen in your question and comments.
#swatkins -
Thank you very much for your help, you highlighted some issues I had overlooked - I've gone about this in a different fashion now.
Originally I was trying to use Active Record (i believe) to select data from one table, based on the selection of a record from a different table - and then pass this selection to another model and use it in a get_where statement.
I've managed to get this to work using the $this->uri->segment() method in my controller, and then passing this to the corresponding model.
Now I'm able to utilise the user selection of a 'building/propery' name, with it's address etc - and then I have a second model, which retrieves the 'orders/jobs' that have been raised at that building.
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 :