Having issues with count record in codeigniter - php

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 >

Related

An uncaught Exception was encountered Error

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();

Multiple filter using codeigniter, i need only approved product?

i am doing multiple search using codeigniter, but all product are display from database. i only need approved product.
when i tried to display in product view page all the product are display from product table.
Controller:
public function product()
{
$searchText="";
if($this->input->post('searchText'))
{
$searchText = $this->input->post('searchText');
}
$like = array(
'ProductName'=>$searchText
);
$orlike = array(
'CategoryName'=>$searchText,
'SubCategoryName'=>$searchText,
'BrandName'=>$searchText
);
$where = array{
'ProductIsActive'=>1,
'ProductStatus'=>'Approved'
);
$jointables = array(
'categories'=>'CategorySlug=ProductCategory',
'subcategories'=>'SubCategorySlug=ProductSubCategory',
'brands'=>'BrandSlug=ProductBrand'
);
$data['product']= $this->
Custom_model-> getproduct('products',$jointables,$where,$like,$orlike,array());
}
Model:
function getproduct($primarytable,$jointables,$where,$like,$orlike)
{
$this->db->select('*');
$this->db->from($primarytable);
$this->db->where($where);
$this->db->like($like);
foreach($orlike as $key=>$value)
{
if($value!="")
{
$this->db->or_like($key,$value,'both');
}
}
foreach($jointables as $key=>$value)
{
$this->db->join($key,$value,'left');
}
$query = $this->db->get();
if($query->num_rows() > 0)
{
$row = $query->result();
return $row;
}else{
return FALSE;
}
}
The thing is in your configuration - if you have fields whiche are required you have to group your query
the following should do the job
your controller
public function product()
{
$searchText="";
if($this->input->post('searchText'))
{
$searchText = $this->input->post('searchText');
}
$arrFilter = [
'like' => [
'ProductName' => $searchText
],
'orlike' => [
'CategoryName' => $searchText,
'SubCategoryName' => $searchText,
'BrandName' => $searchText
],
];
$jointables = [
'categories'=>'CategorySlug=ProductCategory',
'subcategories'=>'SubCategorySlug=ProductSubCategory',
'brands'=>'BrandSlug=ProductBrand'
];
$where = [
'ProductIsActive'=>1,
'ProductStatus'=>'Approved'
];
$data['product']= $this->Custom_model-> getproduct('products',$jointables,$where,$arrFilter);
}
your model
function getproduct($primarytable, $jointables, $where, $arrFilter = [])
{
$this->db->select('*');
$this->db->from($primarytable);
$this->db->where($where);
if (is_array($arrFilter) && count($arrFilter) > 0)
{
$this->db->group_start();
if (isset($arrFilter['like']))
{
$this->db->like($arrFilter['like']));
}
if (isset($arrFilter['orlike']))
{
$this->db->group_start();
foreach($arrFilter['orlike'] as $key=>$value)
{
if($value!="")
{
$this->db->or_like($key,$value,'both');
}
}
$this->db->group_end();
}
$this->db->group_end();
}
foreach($jointables as $key=>$value)
{
$this->db->join($key,$value,'left');
}
$query = $this->db->get();
if($query->num_rows() > 0)
{
$row = $query->result();
return $row;
}
else
{
return FALSE;
}
}

PHP CODEIGNITER UPDATE issue

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();

Codeigniter Form Not Updating And Not Redirecting

I am using codeigniter form validation lib and for some reason form is not updating this particular row. And there for not redirecting when form Submitted.
On my controller I use function like this
$this->load->model('admin/setting/model_setting');
$config_meta_title = $this->model_setting->edit_meta_title($this->input->post('config_meta_title'));
if (!empty($config_meta_title)) {
$data['config_meta_title'] = $this->input->post('config_meta_title');
} else {
$data['config_meta_title'] = $this->configs->get('config_meta_title');
}
But not updating database.
Model
<?php
class Model_setting extends CI_Model {
public function edit_meta_title() {
$data = array(
'group' => "config",
'key' => "config_meta_title",
'value' => $this->input->post('config_meta_title')
);
$this->db->where('setting_id', "2");
$this->db->update('setting', $data);
}
}
Controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Setting extends MY_Controller {
public function __construct() {
parent::__construct();
$this->lang->load('admin/setting/setting', 'english');
$this->lang->load('admin/english', 'english');
if ($this->session->userdata('user_id') == true) {
return true;
} else {
redirect('admin');
}
}
public function index() {
$this->load->library('form_validation');
$data['text_yes'] = $this->lang->line('text_yes');
$data['text_no'] = $this->lang->line('text_no');
$data['entry_meta_title'] = $this->lang->line('entry_meta_title');
$data['entry_maintenance'] = $this->lang->line('entry_maintenance');
$data['button_save'] = $this->lang->line('button_save');
$data['button_cancel'] = $this->lang->line('button_cancel');
$data['tab_store'] = $this->lang->line('tab_store');
$data['action'] = site_url('admin/setting');
$data['logout'] = site_url('admin/logout');
$data['cancel'] = site_url('admin/dashboard');
$this->load->model('admin/setting/model_setting');
$config_meta_title = $this->model_setting->edit_meta_title($this->input->post('config_meta_title'));
if (!empty($config_meta_title)) {
$data['config_meta_title'] = $this->input->post('config_meta_title');
} else {
$data['config_meta_title'] = $this->configs->get('config_meta_title');
}
if ($this->form_validation->run() == FALSE) {
return $this->load->view('setting/settings', $data);
} else {
redirect('admin/dashboard');
}
}
}
In your model, kindly try to pass it as a parameter:
public function edit_meta_title($config_meta_title) {
$data = array(
'group' => "config",
'key' => "config_meta_title",
'value' => $config_meta_title,
);
$this->db->where('setting_id', "2");
$this->db->update('setting', $data);
return $this->db->affected_rows();
}
I have fixed my issues now All working perfect
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Setting extends MY_Controller {
public function __construct() {
parent::__construct();
$this->lang->load('admin/setting/setting', 'english');
$this->lang->load('admin/english', 'english');
if ($this->session->userdata('user_id') == true) {
return true;
} else {
redirect('admin');
}
}
public function index() {
$data = array();
$data['text_yes'] = $this->lang->line('text_yes');
$data['text_no'] = $this->lang->line('text_no');
$data['entry_meta_title'] = $this->lang->line('entry_meta_title');
$data['entry_template'] = $this->lang->line('entry_template');
$data['entry_maintenance'] = $this->lang->line('entry_maintenance');
$data['button_save'] = $this->lang->line('button_save');
$data['button_cancel'] = $this->lang->line('button_cancel');
$data['tab_store'] = $this->lang->line('tab_store');
$data['action'] = site_url('admin/setting');
$data['logout'] = site_url('admin/logout');
$data['cancel'] = site_url('admin/dashboard');
$this->load->model('admin/setting/model_setting');
if (empty($config_meta_title)) {
$data['config_meta_title'] = $this->configs->get('config_meta_title');
}
if (empty($config_template)) {
$data['config_template'] = $this->configs->get('config_template');
}
$data['templates'] = array();
$directories = glob(APPPATH . 'modules/catalog/views/theme/*', GLOB_ONLYDIR);
foreach ($directories as $directory) {
$data['templates'][] = basename($directory);
}
if (empty($config_maintenance)) {
$data['config_maintenance'] = $this->configs->get('config_maintenance');
}
$this->load->library('form_validation');
$this->form_validation->set_rules('config_meta_title', 'Meta Title');
$this->form_validation->set_rules('config_template', 'Template');
$this->form_validation->set_rules('config_maintenance', 'Maintenance');
if ($this->form_validation->run() == FALSE) {
return $this->load->view('setting/settings', $data);
} else {
$config_meta_title = $this->model_setting->edit_meta_title($this->input->post('config_meta_title'));
$config_template = $this->model_setting->edit_template($this->input->post('config_template'));
$config_maintenance = $this->model_setting->edit_maintenance($this->input->post('config_maintenance'));
redirect('admin/dashboard');
}
}
}
Model
<?php
class Model_setting extends CI_Model {
public function edit_maintenance($config_maintenance) {
$data = array(
'group' => "config",
'key' => "config_maintenance",
'value' => $config_maintenance,
);
$this->db->where('setting_id', "1");
$this->db->update('setting', $data);
}
public function edit_meta_title($config_meta_title) {
$data = array(
'group' => "config",
'key' => "config_meta_title",
'value' => $config_meta_title,
);
$this->db->where('setting_id', "2");
$this->db->update('setting', $data);
}
public function edit_template($config_template) {
$data = array(
'group' => "config",
'key' => "config_template",
'value' => $config_template,
);
$this->db->where('setting_id', "3");
$this->db->update('setting', $data);
}
}

Add or edit data if already exist in database

Im trying to add or edit data depending if its already stored in the database. As you can see below im checking if user already exist with getuserid($id) loading the corresponding view but the problem is when i trigger add_user() or edit_user() as none of these methods are redirecting back as it should be. Any suggestions? This is the complete code http://pastebin.com/2G7D8ie4
public function getuserid($id = NULL){
if(!$id)
{
show_404();
}
$query = $this->Users_model->getuserid($id);
if($query == false){
$data = array('title'=>'Admin ::LxFPanamá::',
'content'=>'users/add_users_view',
'id'=>$id);
}else{
$data = array('title'=>'Admin ::LxFPanamá::',
'content'=>'users/edit_users_view',
'id'=> $query->id,
'staff_id'=> $query->staff_id,
'login'=> $query->login,
'password'=> $query->password
);
}
$this->load->view('themes/'.$this->config->item('theme_front').'.php', $data);
}
Model
public function getuserid($id){
$query = $this->db->get_where('users', array('staff_id' => $id));
if($query->num_rows() > 0){
return $query->row();
}else{
return false;
}
}
can you try making your model
public function getuserid($id){
$query = $this->db->get_where('users', array('staff_id' => $id));
$result=array();
foreach ($query->result() as $rows){
$result[]=$rows;
}
return $result;
}
and controller
if(count($query)>0){
$data = array('title'=>'Admin ::LxFPanamá::',
'content'=>'users/edit_users_view',
'id'=> $query->id,
'staff_id'=> $query->staff_id,
'login'=> $query->login,
'password'=> $query->password
);
}
else
{
$data = array('title'=>'Admin ::LxFPanamá::',
'content'=>'users/add_users_view',
'id'=>$id
);
}

Categories