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;
}
Related
I have comeup with strange problem in cakephp 3.4. I am running filter query on i18n content like this.
if($this->request->query("q")){
$this->paginate["conditions"][$this->ContractTypes->translationField('title').' LIKE'] = '%'.$this->request->query("q").'%';
}
but following call is ending up in Database error
$records = $this->paginate($this->ContractTypes);
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'ContractTypes_title_translation.content' in 'where clause' SELECT (COUNT(*)) AS `count` FROM contract_types ContractTypes WHERE ContractTypes_title_translation.content like :c0
The paginator's count query is not joing i18n table. What is the best approach to solve this problem.
Thanks in advance,
I have solved this by creating my custom paginator component by editing the paginate function. My paginator contains following code incase somebody else is facing the same problem.
namespace Console\Controller\Component;
use Cake\Controller\Component\PaginatorComponent as BasePaginator;
class PaginatorComponent extends BasePaginator
{
public function paginate($object, array $settings = [])
{
$query = null;
if ($object instanceof QueryInterface) {
$query = $object;
$object = $query->repository();
}
$alias = $object->alias();
$options = $this->mergeOptions($alias, $settings);
$options = $this->validateSort($object, $options);
$options = $this->checkLimit($options);
$options += ['page' => 1, 'scope' => null];
$options['page'] = (int)$options['page'] < 1 ? 1 : (int)$options['page'];
list($finder, $options) = $this->_extractFinder($options);
if (empty($query)) {
$query = $object->find($finder, $options);
} else {
$query->applyOptions($options);
}
$cleanQuery = clone $query;
// My Modification Starts Here
$table = $cleanQuery->repository();
$results = $query->all();
$numResults = count($results);
$count = $numResults ? $cleanQuery->select([
"count"=>$cleanQuery
->func()
->count($table->alias().'.'.$table->primaryKey())
])->first()->count : 0;
// My Modification ends Here
$defaults = $this->getDefaults($alias, $settings);
unset($defaults[0]);
$page = $options['page'];
$limit = $options['limit'];
$pageCount = (int)ceil($count / $limit);
$requestedPage = $page;
$page = max(min($page, $pageCount), 1);
$request = $this->_registry->getController()->request;
$order = (array)$options['order'];
$sortDefault = $directionDefault = false;
if (!empty($defaults['order']) && count($defaults['order']) == 1) {
$sortDefault = key($defaults['order']);
$directionDefault = current($defaults['order']);
}
$paging = [
'finder' => $finder,
'page' => $page,
'current' => $numResults,
'count' => $count,
'perPage' => $limit,
'prevPage' => $page > 1,
'nextPage' => $count > ($page * $limit),
'pageCount' => $pageCount,
'sort' => key($order),
'direction' => current($order),
'limit' => $defaults['limit'] != $limit ? $limit : null,
'sortDefault' => $sortDefault,
'directionDefault' => $directionDefault,
'scope' => $options['scope'],
];
if (!$request->getParam('paging')) {
$request->params['paging'] = [];
}
$request->params['paging'] = [$alias => $paging] + (array)$request->getParam('paging');
if ($requestedPage > $page) {
throw new NotFoundException();
}
return $results;
}
}
I want to calculate hargaLama and hargaBaru, then insert it into database. To do so, I retrieve hargaLama from a view in mysql to my controller while hargaBaru is a user input. Even though I'm using foreach I got Undefined variable hargaLama and I also got error
Unknown column 'kodeProduksi' in 'field list'.
Here's my controller:
public function proses_tambahBarang(){
$kode = $_POST['kode'];
$kodeProduksi = $_POST['kodeProduksi'];
$nama = $_POST['nama'];
$tipe = $_POST['tipe'];
$ukuran = $_POST['ukuran'];
$merk = $_POST['merk'];
$satuan = $_POST['satuan'];
$jumlah = $_POST['jumlah'];
$harga = $_POST['hargaSatuan'];
// echo "proses_tambahBarang";
$data_insert = array(
'kodeBarang' => $kode,
'kodeProduksi' => $kodeProduksi,
'namaBarang' => $nama,
'tipeBarang' => $tipe,
'ukuran' => $ukuran,
'merk' => $merk,
'satuan' => $satuan,
'jumlah' => $jumlah,
'hargaSatuan' => $harga,
'keterangan' => 'n/a',
'idUser' => $this->session->userdata('username'),
'waktuMasuk' => 'n/a',
'waktuEdit' => 'n/a'
);
//$cek = $this->mhome->Barang("where kodeBarang = $data_insert[kodeBarang]");
// if($cek >= 1)
// {
$cek = $this->mhome->BarangHistory("where kodeProduksi = '$data_insert[kodeProduksi]'");
// $cek = $this->db->get_where('baranghistory',array('kodeProduksi' =>$data_insert['kodeProduksi']));
if($cek >= 1);
{
$query = $this->mhome->TableSelect('listBarang',"where kodeProduksi = '$data_insert[kodeProduksi]'");
foreach ($query as $row) {
$hargaLama = $row[0]['hargaSatuan'];
$jumlahLama = $row[0]['jumlah'];
}
$hargaBaru = $data_insert['hargaSatuan'];
$jumlahBaru = $data_insert['jumlah'];
$jumlahBaru = $jumlahBaru + $jumlahLama;
$data_insert['jumlah'] = $jumlahBaru;
$data_insert['waktuEdit'] = date("Y-m-d h:i:sa");
$data_insert['keterangan'] = "Updated";
$this->mhome->UpdateData('baranghistory',$data_insert,array("kodeProduksi" => $data_insert['kodeProduksi']));
$this->mhome->UpdateData('barang',$data_insert,array("kodeBarang" => $data_insert['kodeBarang']));
}
// $this->mhome->hitungHargaSatuan("where kodeBarang = '$data_insert[kodeBarang]'");
$hitung = $this->mhome->hitungHargaSatuan($data_insert['kodeBarang']);
if($hitung){
$this->session->set_flashdata('pesan','Tambah Barang Sukses');
redirect('userhome/index');
}
if($cek == 0) {
$data_insert['waktuMasuk'] = date("Y-m-d h:i:sa");
$data_insert['keterangan'] = "Baru";
$res = $this->mhome->InsertData('barang',$data_insert);
$res2 = $this->mhome->InsertData('baranghistory',$data_insert);
}
if($res >= 1 && $res2 >=1)
{
$this->session->set_flashdata('pesan','Tambah Barang Sukses');
redirect('userhome/index');
}
else {
echo "Tambah barang gagal";
}
}
And here's my model:
public function TableSelect($table,$where="")
{
$stmt = $this->db->query('select * from '.$table.' '.$where);
return $stmt->result_array();
}
I am sure you need not to put 0 here
foreach ($query as $row) {
$hargaLama = $row['hargaSatuan'];//remove [0] from here
$jumlahLama = $row['jumlah'];//remove [0] from here
}
like this.you need to use count on codition because this is returned array.and use result_array() for getting result in array format.
$cek = $this->mhome->BarangHistory("where kodeProduksi = $data_insert['kodeProduksi']")->result_array();
// $cek = $this->db->get_where('baranghistory',array('kodeProduksi' =>$data_insert['kodeProduksi']))->result_array();
if(count($cek) >= 1);
{
$query = $this->mhome->TableSelect('listBarang',"where kodeProduksi = $data_insert['kodeProduksi']");
foreach ($query as $row) {
$hargaLama = $row['hargaSatuan'];
$jumlahLama = $row['jumlah'];
}
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);
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
if I use controller it works fine but if I use CI_Controller it gives this error
Fatal error: Call to undefined method CI_Input::load_query() in C:\wamp\www\films\application\controllers\documents.php on line 124
Controller
function display($query_id = 0, $sort_by = 'title', $sort_order = 'asc', $offset = 0) {
$limit = 20;
$data['fields'] = array('FID' => 'ID', 'title' => 'Title', 'category' => 'Category', 'date_added' => 'date_added', 'department' => 'department', 'type' => 'type');
$this -> input -> load_query($query_id);
$query_array = array('title' => $this -> input -> get('title'), 'category' => $this -> input -> get('category'), 'date_added_comparison' => $this -> input -> get('date_added_comparison'), 'date_added' => $this -> input -> get('date_added'), );
$data['query_id'] = $query_id;
$this -> load -> model('document_m');
$results = $this -> document_m -> search($query_array, $limit, $offset, $sort_by, $sort_order);
$data['documents'] = $results['rows'];
$data['num_results'] = $results['num_rows'];
// pagination
$this -> load -> library('pagination');
$config = array();
$config['base_url'] = site_url("documents/display/$query_id/$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;
$data['category_options'] = $this -> document_m -> category_options();
$this -> load -> view('header');
$this -> load -> view('document_index.php', $data);
$this -> load -> view('footer');
}
function search() {
$query_array = array('title' => $this -> input -> post('title'), 'category' => $this -> input -> post('category'), 'date_added_comparison' => $this -> input -> post('date_added_comparison'), 'date_added' => $this -> input -> post('date_added'), );
$query_id = $this -> input -> save_query($query_array);
redirect("documents/display/$query_id");
}
Model
function search($query_array, $limit, $offset, $sort_by, $sort_order) {
$sort_order = ($sort_order == 'desc') ? 'desc' : 'asc';
$sort_columns = array('FID', 'title', 'category', 'date_added', 'department', 'type');
$sort_by = (in_array($sort_by, $sort_columns)) ? $sort_by : 'title';
// results query
$q = $this->db->select('FID, title, category, date_added, department, type')
->from('documents')
->limit($limit, $offset)
->order_by($sort_by, $sort_order);
if (strlen($query_array['title'])) {
$q->like('title', $query_array['title']);
}
if (strlen($query_array['category'])) {
$q->where('category', $query_array['category']);
}
if (strlen($query_array['date_added'])) {
$operators = array('gt' => '>', 'gte' => '>=', 'eq' => '=', 'lte' => '<=', 'lt' => '<');
$operator = $operators[$query_array['date_added_comparison']];
$q->where("date_added $operator", $query_array['date_added']);
}
$ret['rows'] = $q->get()->result();
// count query
$q = $this->db->select('COUNT(*) as count', FALSE)
->from('documents');
if (strlen($query_array['title'])) {
$q->like('title', $query_array['title']);
}
if (strlen($query_array['category'])) {
$q->where('category', $query_array['category']);
}
if (strlen($query_array['date_added'])) {
$operators = array('gt' => '>', 'gte' => '>=', 'eq' => '=', 'lte' => '<=', 'lt' => '<');
$operator = $operators[$query_array['date_added_comparison']];
$q->where("date_added $operator", $query_array['date_added']);
}
$tmp = $q->get()->result();
$ret['num_rows'] = $tmp[0]->count;
return $ret;
}
function category_options() {
$rows = $this->db->select('name')
->from('category')
->get()->result();
$category_options = array('' => '');
foreach ($rows as $row) {
$category_options[$row->name] = $row->name;
}
return $category_options;
}
function get_category(){
$this->db->select()->from('category');
$query=$this->db->get();
return $query->result_array();
}
}
View
<?php echo form_open('documents/search'); ?>
<table width="" border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
<?php echo form_input('title', set_value('title'), 'id="title"'); ?>
</td>
<td>
<?php echo form_dropdown('category', $category_options,
set_value('category'), 'id="category"'); ?>
</td>
<td>
<?php echo form_dropdown('date_added_comparison',
array('gt' => '>', 'gte' => '>=', 'eq' => '=', 'lte' => '<=', 'lt' => '<') ,
set_value('date_added_comparison'), 'id="date_added_comparison"'); ?>
<td>
<?php echo form_input('date_added', set_value('date_added'), 'id="date_added"'); ?>
</td>
<td valign="top">
<input type="submit" value="search" class="btn"/>
</td>
</tr>
</table>
<?php echo form_close(); ?>
<div>
Found <?php echo $num_results; ?> document
</div>
<table class="table table-striped">
<thead>
<?php foreach($fields as $field_name => $field_display): ?>
<th <?php if ($sort_by == $field_name) echo "class=\"sort_$sort_order\"" ?>>
<?php echo anchor("documents/display/$query_id/$field_name/" .
(($sort_order == 'asc' && $sort_by == $field_name) ? 'desc' : 'asc') ,
$field_display); ?>
</th>
<?php endforeach; ?>
</thead>
<tbody>
<?php foreach($documents as $row): ?>
<tr>
<?php foreach($fields as $field_name => $field_display): ?>
<td>
<?php echo $row->$field_name; ?>
</td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php if (strlen($pagination)): ?>
<div>
Pages: <?php echo $pagination; ?>
</div>
<?php endif; ?>
table(documents{FID,title,category,date_added,department,type,active}
table(category{id,name}
table(ci_query{id,query_string}
this works fine with the codeigniter if I user controller I mean the old version of code igniter but this new one I downloaded it' not working and give the error I have post on up.
regards thanks in advance
you can extend the input class
class MY_Input extends CI_Input {
function __construct()
{
parent::__construct();
}
function load_query(){
//here what the method did
}
}
save this as my_input.php and put it under system/libraries/
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.