An uncaught Exception was encountered
Type: Error
Message: Call to a member function num_rows() on bool
Filename: C:\xampp\htdocs\sikan_v2\application\views\transaction\sale\cart_data.php
Line Number: 2
Backtrace:
File: C:\xampp\htdocs\sikan_v2\application\views\transaction\sale\sale_form.php
Line: 134
Function: view
File: C:\xampp\htdocs\sikan_v2\application\libraries\Template.php
Line: 14
Function: view
File: C:\xampp\htdocs\sikan_v2\application\controllers\Sale.php
Line: 26
Function: load
File: C:\xampp\htdocs\sikan_v2\index.php
Line: 315
Function: require_once
My Controllers: controllers/sale.php
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Sale extends CI_Controller {
function __construct()
{
parent::__construct();
check_not_login();
check_admin();
$this->load->model(['sale_m','item_m', 'supplier_m', 'stock_m']);
}
public function index()
{
$this->load->model(['customer_m', 'item_m']);
$customer = $this->customer_m->get()->result();
$item = $this->item_m->get()->result();
$cart = $this->sale_m->get_cart();
$data = array(
'customer' => $customer,
'item' => $item,
'cart' => $cart,
'invoice' => $this->sale_m->invoice_no(),
);
$this->template->load('template', 'transaction/sale/sale_form', $data);
}
public function process()
{
$data = $this->input->post(null, TRUE);
if(isset($_POST['add_cart'])) {
$this->sale_m->add_cart($data);
}
if($this->db->affected_rows() > 0) {
$params = array("success" => true);
} else {
$params = array("success" => false);
}
echo json_encode($params);
}
}
My Models: models/Sale_m.php
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Sale_m extends CI_Model {
public function invoice_no()
{
$sql = "SELECT MAX(MID(invoice,9,4)) AS invoice_no
FROM t_sale
WHERE MID(invoice,3,6) = DATE_FORMAT(CURDATE(), '%y%m%d')";
$query = $this->db->query($sql);
IF($query->num_rows() > 0) {
$row = $query->row();
$n = ((int)$row->invoice_no) + 1;
$no = sprintf("%'.04d", $n);
}else{
$no = "0001";
}
$invoice = "MP".date('ymd').$no;
return $invoice;
$query = $this->db->get();
return $query;
}
public function get_cart($params = null)
{
$this->db->select('*, p_item.name as item_name, t_cart.price as cart_price');
$this->db->from('cart');
$this->db->join('p_item', 't_cart.item_id = p_item.item_id');
if($params != null) {
$this->db->where($params);
}
$this->db->where('user_id', $this->session->userdata('userid'));
$query = $this->db->get();
return $query;
}
public function add_cart($post)
{
$query = $this->db->query("SELECT MAX(cart_id) AS cart_no FROM t_cart");
if($query->num_rows() > 0) {
$row = $query->row();
$car_no = ((int)$row->cart_no) + 1;
} else {
$car_no = "1";
}
$params = array(
'cart_id' => $car_no,
'item_id' => $post['item_id'],
'price' => $post['price'],
'qty' => $post['qty'],
'total' => ($post['price'] * $post['qty']),
'user_id' => $this->session->userdata('userid')
);
$this->db->insert('t_cart', $params);
}
}
My View: transaction/sale/cart_data.php
<?php $no = 1;
if($cart->num_rows() > 0) {
foreach ($cart->result() as $c =>$data) { ?>
<tr>
<td><?=$no++?>.</td>
<td><?=$data->barcode?></td>
<td><?=$data->item_name?></td>
<td class="text-right"><?=$data->cart_price?></td>
<td class="text-center"><?=$data->qty?></td>
<td class="text-right"><?=$data->discount_item?></td>
<td class="text-right"id="total"><?=$data->total?></td>
<td class="text-center" width="160px">
<button id="update_cart" data-toggle="modal" data-target="#modal-item-edit"
data-cartid="<?=$data->cart_id?>"
data-barcode="<?=$data->barcode?>"
data-product="<?=$data->item_name?>"
data-price="<?=$data->cart_price?>"
data-qty="<?=$data->qty?>"
data-discount="<?=$data->discount_item?>"
data-total="<?=$data->total?>"
class="btn btn-xs btn primary">
<i class="fa fa-pencil"></i> Update
</button>
<button id="del_cart" data-cartid="<?=$data->cart_id?>" class="btn btn-xs btn-danger">
<i class="fa fa-trash"></i> Delete
</button>
</td>
</tr>
<?php
}
} else {
echo '<tr>
<td colspan="8"> class="text-center">Tidak ada item</td>
</tr>';
} ?>
please help me solve the error
It will return bool when there's any errors in your query. Check this function again:
public function get_cart($params = null)
{
$this->db->select('*, p_item.name as item_name, t_cart.price as cart_price');
$this->db->from('cart');
$this->db->join('p_item', 't_cart.item_id = p_item.item_id');
if($params != null) {
$this->db->where($params);
}
$this->db->where('user_id', $this->session->userdata('userid'));
$query = $this->db->get();
return $query;
}
I think it should be:
$this->db->from('t_cart');
If you check again and find no errors, you can use method: last_query to get the query then try to query directly in phpmyadmin or MYSQL Workbench to check if there's any errors. Like this:
public function get_cart($params = null)
{
// above source code
$query = $this->db->get();
return $this->db->last_query();
}
You can do a
var_dump($data)
before calling this line to see if the 'cart' gets what you expect.
$this->template->load('template', 'transaction/sale/sale_form', $data);
My guess is that you are missing the result() at the end:
return $this->db->query("<your query>")->result();
Related
A PHP Error was encountered
Severity: Notice
Message: Undefined property: stdClass::$designation_id
Filename: dashboard/index.php
Line Number: 548
Backtrace:
File: /home/newtalent/public_html/demo/localhost/application/views/dashboard/index.php
Line: 548
Function: _error_handler
File: /home/newtalent/public_html/demo/localhost/application/third_party/MX/Loader.php
Line: 362
Function: include
File: /home/newtalent/public_html/demo/localhost/application/third_party/MX/Loader.php
Line: 304
Function: _ci_load
File: /home/newtalent/public_html/demo/localhost/application/controllers/Dashboard.php
Line: 86
Function: view
File: /home/newtalent/public_html/demo/localhost/index.php
Line: 292
Function: require_once
my controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Announcement extends MY_Controller {
public function __construct() {
Parent::__construct();
$this->load->library('session');
$this->load->helper('form');
$this->load->helper('url');
$this->load->helper('html');
$this->load->database();
$this->load->library('form_validation');
//load the model
$this->load->model("Announcement_model");
$this->load->model("Xin_model");
$this->load->model("Designation_model");
}
/*Function to set JSON output*/
public function output($Return=array()){
/*Set response header*/
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
/*Final JSON response*/
exit(json_encode($Return));
}
public function index()
{
$data['title'] = $this->Xin_model->site_title();
$data['all_designations'] = $this->Designation_model->all_designations();
$session = $this->session->userdata('username');
$data['breadcrumbs'] = 'Announcements';
$data['path_url'] = 'user/user_announcement';
if(!empty($session)){
$data['subview'] = $this->load->view("user/announcement_list", $data, TRUE);
$this->load->view('layout_main', $data); //page load
} else {
redirect('');
}
}
public function announcement_list()
{
$data['title'] = $this->Xin_model->site_title();
$session = $this->session->userdata('username');
if(!empty($session)){
$this->load->view("user/announcement_list", $data);
} else {
redirect('');
}
// Datatables Variables
$draw = intval($this->input->get("draw"));
$start = intval($this->input->get("start"));
$length = intval($this->input->get("length"));
$announcement = $this->Announcement_model->get_announcements();
$data = array();
foreach($announcement->result() as $r) {
// get user > added by
$user = $this->Xin_model->read_user_info($r->published_by);
// user full name
$full_name = $user[0]->first_name.' '.$user[0]->last_name;
// get date
$sdate = $this->Xin_model->set_date_format($r->start_date);
$edate = $this->Xin_model->set_date_format($r->end_date);
if($r->designation_id == '') {
$ol = 'All Designations';
} else {
$ol = '<ol class="nl">';
foreach(explode(',',$r->designation_id) as $desig_id) {
$_des_name = $this->Designation_model->read_designation_information($desig_id);
$ol .= '<li>'.$_des_name[0]->designation_name.'</li>';
}
$ol .= '</ol>';
}
$data[] = array('<span data-toggle="tooltip" data-placement="top" title="View"><button type="button" class="btn btn-secondary btn-sm m-b-0-0 waves-effect waves-light" data-toggle="modal" data-target=".view-modal-data" data-announcement_id="'. $r->announcement_id . '"><i class="fa fa-eye"></i></button></span>',
$r->title,
$r->summary,
$ol,
$sdate,
$edate,
$full_name
);
}
$output = array(
"draw" => $draw,
"recordsTotal" => $announcement->num_rows(),
"recordsFiltered" => $announcement->num_rows(),
"data" => $data
);
echo json_encode($output);
exit();
}
}
model
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class announcement_model extends CI_Model {
public function __construct()
{
parent::__construct();
$this->load->database();
}
public function get_announcements()
{
return $this->db->get("xin_announcements");
}
public function read_announcement_information($id) {
$condition = "announcement_id =" . "'" . $id . "'";
$this->db->select('*');
$this->db->from('xin_announcements');
$this->db->where($condition);
$this->db->limit(1);
$query = $this->db->get();
if ($query->num_rows() == 1) {
return $query->result();
} else {
return false;
}
}
// Function to add record in table
public function add($data){
$this->db->insert('xin_announcements', $data);
if ($this->db->affected_rows() > 0) {
return true;
} else {
return false;
}
}
// Function to Delete selected record from table
public function delete_record($id){
$this->db->where('announcement_id', $id);
$this->db->delete('xin_announcements');
}
// Function to update record in table
public function update_record($data, $id){
$this->db->where('announcement_id', $id);
if( $this->db->update('xin_announcements',$data)) {
return true;
} else {
return false;
}
}
}
?>
View
<?php
$session = $this->session->userdata('username');?>
<div class="box box-block bg-white">
<h2><strong>List All</strong> Announcements </h2>
<div class="table-responsive" data-pattern="priority-columns">
<table class="table table-striped table-bordered dataTable" id="xin_table">
<thead>
<tr>
<th>Action</th>
<th>Title</th>
<th>Summary</th>
<th>Published For</th>
<th>Start Date</th>
<th>End Date</th>
<th>Published By</th>
</tr>
</thead>
</table>
</div>
</div>
if anyone have solution please provide
thanks in advance
This error indicates that there is no property in stdClass.
For example:
<?php
$exampleObj = new stdClass();
$value = $exampleObj->designation_id; // Undefined property: stdClass::$designation_id
To avoid errors, you need to create this property:
<?php
$exampleObj = new stdClass();
$exampleObj->designation_id = 1;
$value = $exampleObj->designation_id;
I can't update data in a record in CodeIgniter .
I have posted the code below:-
//CONTROLLER
//RESERVATION.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Reservation extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('reservation_model','reservation');
}
public function index()
{
$this->load->helper('url');
$this->load->view('manage_reservation');
}
public function reservationview()
{
$this->load->helper('url');
$this->load->view('view_reservation');
}
public function ajax_list()
{
$list = $this->reservation->get_datatables();
$data = array();
$no = $_POST['start'];
foreach ($list as $reservation) {
$no++;
$row = array();
$row[] = $reservation->id;
$row[] = $reservation->dateReserved;
$row[] = $reservation->requestedBy;
$row[] = $reservation->facility;
$row[] = $reservation->dateFrom;
$row[] = $reservation->dateTo;
$row[] = $reservation->timeStart;
$row[] = $reservation->timeEnd;
$row[] = $reservation->status;
$row[] = $reservation->items;
$row[] = $reservation->purpose;
//add html for action
$row[] = '<a class="btn btn-sm btn-primary" href="javascript:void(0)" title="Edit" onclick="edit_reservation('."'".$reservation->id."'".')"><i class="glyphicon glyphicon-pencil"></i></a>
<a class="btn btn-sm btn-danger" href="javascript:void(0)" title="Hapus" onclick="delete_reservation('."'".$reservation->id."'".')"><i class="glyphicon glyphicon-trash"></i></a>';
$data[] = $row;
}
$output = array(
"draw" => $_POST['draw'],
"recordsTotal" => $this->reservation->count_all(),
"recordsFiltered" => $this->reservation->count_filtered(),
"data" => $data,
);
//output to json format
echo json_encode($output);
}
public function ajax_edit($id)
{
$data = $this->reservation->get_by_id($id);
$data->dateFrom = ($data->dateFrom == '0000-00-00') ? '' : $data->dateFrom;
$data->dateTo = ($data->dateTo == '0000-00-00') ? '' : $data->dateTo; // if 0000-00-00 set tu empty for datepicker compatibility
echo json_encode($data);
}
public function ajax_add()
{
$this->_validate();
$data = array(
'id' => $this->input->post('id'),
'dateReserved' => $this->input->post('dateReserved'),
'requestedBy' => $this->input->post('requestedBy'),
'facility' => $this->input->post('facility'),
'dateFrom' => $this->input->post('dateFrom'),
'dateTo' => $this->input->post('dateTo'),
'timeStart' => $this->input->post('timeStart'),
'timeEnd' => $this->input->post('timeEnd'),
'status' => $this->input->post('status'),
'items' => $this->input->post('items'),
'purpose' => $this->input->post('purpose'),
);
$insert = $this->reservation->save($data);
echo json_encode(array("status" => TRUE));
}
public function ajax_update()
{
$this->_validate();
$data = array(
'id' => $this->input->post('id'),
'dateReserved' => $this->input->post('dateReserved'),
'requestedBy' => $this->input->post('requestedBy'),
'facility' => $this->input->post('facility'),
'dateFrom' => $this->input->post('dateFrom'),
'dateTo' => $this->input->post('dateTo'),
'timeStart' => $this->input->post('timeStart'),
'timeEnd' => $this->input->post('timeEnd'),
'status' => $this->input->post('status'),
'items' => $this->input->post('items'),
'purpose' => $this->input->post('purpose'),
);
$this->reservation->update(array('id' => $this->input->post('id')), $data);
echo json_encode(array("status" => TRUE));
}
public function ajax_delete($id)
{
$this->reservation->delete_by_id($id);
echo json_encode(array("status" => TRUE));
}
private function _validate()
{
$data = array();
$data['error_string'] = array();
$data['inputerror'] = array();
$data['status'] = TRUE;
if($this->input->post('requestedBy') == '')
{
$data['inputerror'][] = 'requestedBy';
$data['error_string'][] = 'Requested Name is required*';
$data['status'] = FALSE;
}
if($this->input->post('dateFrom') == '')
{
$data['inputerror'][] = 'dateFrom';
$data['error_string'][] = 'Please Select a Date*';
$data['status'] = FALSE;
}
if($this->input->post('dateTo') == '')
{
$data['inputerror'][] = 'dateTo';
$data['error_string'][] = 'Please Select a Date*';
$data['status'] = FALSE;
}
if($this->input->post('timeStart') == '')
{
$data['inputerror'][] = 'timeStart';
$data['error_string'][] = 'Please select a Time*';
$data['status'] = FALSE;
}
if($this->input->post('timeEnd') == '')
{
$data['inputerror'][] = 'timeEnd';
$data['error_string'][] = 'Please select a Time*';
$data['status'] = FALSE;
}
if($this->input->post('status') == '')
{
$data['inputerror'][] = 'status';
$data['error_string'][] = 'Please Indicate Status*';
$data['status'] = FALSE;
}
if($this->input->post('items') == '')
{
$data['inputerror'][] = 'items';
$data['error_string'][] = 'Please select an Item*';
$data['status'] = FALSE;
}
if($this->input->post('purpose') == '')
{
$data['inputerror'][] = 'purpose';
$data['error_string'][] = 'Please indicate a Purpose*';
$data['status'] = FALSE;
}
if($data['status'] === FALSE)
{
echo json_encode($data);
exit();
}
}
}
//MODEL
//Reservation_model.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Reservation_model extends CI_Model {
var $table = 'tblreservation';
var $column_order = array('id','dateReserved','requestedBy','facility','dateFrom','dateTo','timeStart','timeEnd','status','items','purpose',null); //set column field database for datatable orderable
var $column_search = array('id','dateReserved','requestedBy','facility','dateFrom','dateTo','timeStart','timeEnd','status','items','purpose'); //set column field database for datatable searchable just firstname , lastname , address are searchable
var $order = array('id' => 'desc'); // default order
public function __construct()
{
parent::__construct();
$this->load->database();
}
private function _get_datatables_query()
{
$this->db->from($this->table);
$i = 0;
foreach ($this->column_search as $item) // loop column
{
if($_POST['search']['value']) // if datatable send POST for search
{
if($i===0) // first loop
{
$this->db->group_start(); // open bracket. query Where with OR clause better with bracket. because maybe can combine with other WHERE with AND.
$this->db->like($item, $_POST['search']['value']);
}
else
{
$this->db->or_like($item, $_POST['search']['value']);
}
if(count($this->column_search) - 1 == $i) //last loop
$this->db->group_end(); //close bracket
}
$i++;
}
if(isset($_POST['order'])) // here order processing
{
$this->db->order_by($this->column_order[$_POST['order']['0']['column']], $_POST['order']['0']['dir']);
}
else if(isset($this->order))
{
$order = $this->order;
$this->db->order_by(key($order), $order[key($order)]);
}
}
function get_datatables()
{
$this->_get_datatables_query();
if($_POST['length'] != -1)
$this->db->limit($_POST['length'], $_POST['start']);
$query = $this->db->get();
return $query->result();
}
function count_filtered()
{
$this->_get_datatables_query();
$query = $this->db->get();
return $query->num_rows();
}
public function count_all()
{
$this->db->from($this->table);
return $this->db->count_all_results();
}
public function get_by_id($id)
{
$this->db->from($this->table);
$this->db->where('id',$id);
$query = $this->db->get();
return $query->row();
}
public function save($data)
{
$this->db->insert($this->table, $data);
return $this->db->insert_id();
}
public function update($where, $data)
{
$this->db->update($this->table, $data, $where);
return $this->db->affected_rows();
}
public function delete_by_id($id)
{
$this->db->where('id', $id);
$this->db->delete($this->table);
}
}
VIEW = manage_reservation.php = save function
function save()
{
$('#btnSave').text('saving...'); //change button text
$('#btnSave').attr('disabled',true); //set button disable
var url;
if(save_method == 'add') {
url = "<?php echo site_url('reservation/ajax_add')?>";
} else {
url = "<?php echo site_url('reservation/ajax_update')?>";
}
// ajax adding data to database
$.ajax({
url : url,
type: "POST",
data: $('#form').serialize(),
dataType: "JSON",
success: function(data)
{
if(data.status) //if success close modal and reload ajax table
{
$('#modal_form').modal('hide');
reload_table();
}
else
{
for (var i = 0; i < data.inputerror.length; i++)
{
$('[name="'+data.inputerror[i]+'"]').parent().parent().addClass('has-error'); //select parent twice to select div form-group class and add has-error class
$('[name="'+data.inputerror[i]+'"]').next().text(data.error_string[i]); //select span help-block class set text error string
}
}
$('#btnSave').text('save'); //change button text
$('#btnSave').attr('disabled',false); //set button enable
},
error: function (jqXHR, textStatus, errorThrown)
{
alert('Error adding / update data');
$('#btnSave').text('save'); //change button text
$('#btnSave').attr('disabled',false); //set button enable
}
});
}
NEWBIE TO CODE IGNITER any help will be appreciated :)
try
public function update($where, $data){
$this->db->where($where);
$this->db->set($data); //array of new data
$this->db->update($this->table);
return $this->db->affected_rows();
}
Please print your query after the update statement, this will help you to know whether you query is correct or not.
$this->db->last_query();
Please check that query in model
Controller :
$update_id = array();
$update_id['id'] = $this->input->post('id');
$data = array(
'id' => $this->input->post('id'),
'dateReserved' => $this->input->post('dateReserved'),
'requestedBy' => $this->input->post('requestedBy'),
'facility' => $this->input->post('facility'),
'dateFrom' => $this->input->post('dateFrom'),
'dateTo' => $this->input->post('dateTo'),
'timeStart' => $this->input->post('timeStart'),
'timeEnd' => $this->input->post('timeEnd'),
'status' => $this->input->post('status'),
'items' => $this->input->post('items'),
'purpose' => $this->input->post('purpose'),
);
$result = $this->reservation->update('table_name',$update_id, $data);
Model :
$this->db->where($where);
$this->db->update($table, $data);
return $this->db->affected_rows();
I am having a syntax issue in the controller in the function index where the view is being loaded. it is saying that I have Severity: Parsing Error
Message: syntax error, unexpected ';' I cannot figure out what the issue is I am trying to get the count of all my records in the database. Any help would be appreciated.
controllers/test.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Test extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('test_model','test_table');
}
public function index()
{
$this->load->view('includes/header');
$this->load->view('includes/table_main');
$this->load->view('includes/ajax_functions');
$this->load->view('includes/add_employee_form');
$total_records = $this->test_model->count_all_records();
//$this->load->view('test_view');
$this->load->view('test_view', array('total_records' => $total_records);
}
public function ajax_list()
{
$list = $this->test_table->get_datatables();
$data = array();
$no = $_POST['start'];
foreach ($list as $test_table) {
$no++;
$row = array();
$row[] = $test_table->Name;
$row[] = $test_table->Department;
$row[] = $test_table->Manager;
//add html for action
$row[] = '<a class="btn btn-sm btn-link " href="javascript:void()" title="Edit" onclick="edit_test('."'".$test_table->id."'".')"><i class="glyphicon glyphicon-pencil"></i> Edit</a>
<a class="btn btn-sm text-warning" href="javascript:void()" title="Hapus" onclick="delete_test('."'".$test_table->id."'".')"><i class="glyphicon glyphicon-trash"></i> Delete</a>';
$data[] = $row;
}
$output = array(
"draw" => $_POST['draw'],
"recordsTotal" => $this->test_table->count_all(),
"recordsFiltered" => $this->test_table->count_filtered(),
"data" => $data,
);
//output to json format
echo json_encode($output);
}
public function ajax_edit($id)
{
$data = $this->test_table->get_by_id($id);
echo json_encode($data);
}
public function ajax_add()
{
$this->_validate();
$data = array(
'Name' => $this->input->post('Name'),
'Department' => $this->input->post('Department'),
'Manager' => $this->input->post('Manager'),
);
$insert = $this->test_table->save($data);
echo json_encode(array("status" => TRUE));
}
public function ajax_update()
{
$this->_validate();
$data = array(
'Name' => $this->input->post('Name'),
'Department' => $this->input->post('Department'),
'Manager' => $this->input->post('Manager'),
);
$this->test_table->update(array('id' => $this->input->post('id')), $data);
echo json_encode(array("status" => TRUE));
}
public function ajax_delete($id)
{
$this->test_table->delete_by_id($id);
echo json_encode(array("status" => TRUE));
}
//validation section were user must enter data in all fields
private function _validate()
{
$data = array();
$data['error_string'] = array();
$data['inputerror'] = array();
$data['status'] = TRUE;
if($this->input->post('Name') == '')
{
$data['inputerror'][] = 'Name';
$data['error_string'][] = 'Name is required';
$data['status'] = FALSE;
}
if($this->input->post('Department') == '')
{
$data['inputerror'][] = 'Department';
$data['error_string'][] = 'Department is required';
$data['status'] = FALSE;
}
if($data['status'] === FALSE)
{
echo json_encode($data);
exit();
}
}
}
Model
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class test_model extends CI_Model {
var $table = 'test_table';
var $column = array('Name','Department','Manager'); //set column field database for order and search
var $order = array('id' => 'desc'); // default order
public function __construct()
{
parent::__construct();
$this->load->database();
}
private function _get_datatables_query()
{
$this->db->from($this->table);
$i = 0;
foreach ($this->column as $item) // loop column
{
if($_POST['search']['value']) // if datatable send POST for search
{
if($i===0) // first loop
{
$this->db->group_start(); // open bracket. query Where with OR clause better with bracket. because maybe can combine with other WHERE with AND.
$this->db->like($item, $_POST['search']['value']);
}
else
{
$this->db->or_like($item, $_POST['search']['value']);
}
if(count($this->column) - 1 == $i) //last loop
$this->db->group_end(); //close bracket
}
$column[$i] = $item; // set column array variable to order processing
$i++;
}
if(isset($_POST['order'])) // here order processing
{
$this->db->order_by($column[$_POST['order']['0']['column']], $_POST['order']['0']['dir']);
}
else if(isset($this->order))
{
$order = $this->order;
$this->db->order_by(key($order), $order[key($order)]);
}
}
function count_all_records(){
$this->db->select('id');
$this->db->distinct();
$this->db->from('test_table');
$query = $this->db->get();
return $query->num_rows();
}
function get_datatables()
{
$this->_get_datatables_query();
if($_POST['length'] != -1)
$this->db->limit($_POST['length'], $_POST['start']);
$query = $this->db->get();
return $query->result();
}
function count_filtered()
{
$this->_get_datatables_query();
$query = $this->db->get();
return $query->num_rows();
}
public function count_all()
{
$this->db->from($this->table);
return $this->db->count_all_results();
}
public function get_by_id($id)
{
$this->db->from($this->table);
$this->db->where('id',$id);
$query = $this->db->get();
return $query->row();
}
public function save($data)
{
$this->db->insert($this->table, $data);
return $this->db->insert_id();
}
public function update($where, $data)
{
$this->db->update($this->table, $data, $where);
return $this->db->affected_rows();
}
public function delete_by_id($id)
{
$this->db->where('id', $id);
$this->db->delete($this->table);
}
}
view
Total Records: <?php echo $total_records ?>
In the class Test index function last statement ) is missing. Here is the correct one
$this->load->view('test_view', array('total_records' => $total_records));
It might cause due to script contain in view ,set short_open_tag 0 in your htaccess file and check if solves
<IfModule mod_php5.c >
php_value short_open_tag 0
</IfModule >
I have a problem in codeigniter.
I call a funtion in controller Articles from the view.
<li><a href="index.php/articles/Category/1" ><?php echo $item['CATEGORY'] ?></a></li>
The function
function getCategories() {
$this->db->select('ID_CAT,CATEGORY');
$this->db->from('CATEGORY');
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->result_array();
} else {
return FALSE;
}
}
function Category($id_cat) {
if (!$id_cat) {
$id_cat = 0;
}
$this->load->model('graf/home_model');
$data = array();
$data['categories'] = $this->home_model->getCategories();
$filtro_cat = $id_cat;
$data['article'] = $this->home_model->getArticles($filtro_cat);
$this->load->view('graf/articles', $data, TRUE);
}
home_model
function getArticles($filtro_cat) {
$this->db->select('*');
$this->db->from('ARTICLES');
if ($filtro_cat != 0) {
$this->db->where("ID_CAT=".$filtro_cat);
}
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->result_array();
} else {
return FALSE;
}
}
When i select the button the url changes to http://localhost/Graf/index.php/articles/Category/1 but does not load
http://localhost/Graf/index.php/articles/
Why?
When you set third argument of load->view() to true it returns string value and does not output anything
Try changing:
$this->load->view('graf/articles', $data, TRUE);
to
$this->load->view('graf/articles', $data);
set the routes.php in folder config, add this code;
$route['articles/Category/(.*)'] = 'articles/$1';
and change the link
<li><a href="<?php echo site_url('index.php/articles/'.$item['ID_CAT']); ?>" ><?php echo $item['CATEGORY'] ?></a></li>
and change your controller to this
$this->load->view('graf/articles', $data);
How can put return tow function following in one function marge() and echo it with json_encode.
function get_gr()
{
//$tourf_id = $this->input->post('toname');
$id = '102';
$query = $this->db->order_by('id','desc')->get_where('table1', array('id' => $id));
if ($query->num_rows() == 0) {
return 0;
} else {
$query = $query->row();
return array('guide' => $query->guide);
}
}
function get_res()
{
//$id = $this->input->post('name');
$id = '102';
$data = array();
$query_r = $this->db->order_by('id','desc')->get_where('table2', array('relation' => $id));
if($query_r->num_rows() > 0){
foreach ($query_r->result() as $row) {
$data[] = array(
'name_re' => $row->name_re,
'id' => $row->id
);
}
return $hotel_data;
}else{
return 0;
}
}
function marge(){
echo json_encode(get_gr().get_res()); //This line 991
}
I get this erroe from above php code:
Fatal error: Call to undefined function get_gr() in D:\xampp\htdocsapplication\controllers\faile.php on line 991
What do i do?
If inside a controller, you must refer to get_gr() as $this->get_gr()
You can do a merge as two array elements.
echo json_encode(array(get_gr(), get_res()));