i have a problem with my code , i want to filter by in 2 category . but in the work i did , just filter with one category can work , and filter with other category didnt work. anyone can help me to solve this ?
and this is my controller
public function index($state = null , $category = null)
{
if (!empty($state)) {
$this->load->model('M_kegiatan');
$data['username'] = $this->session->userdata('username');
$data['kegiatan'] = $this->M_kegiatan->get_status($state);
$this->load->view('beranda', $data);
}else if (!empty($category)) {
$this->load->model('M_kegiatan');
$data['username'] = $this->session->userdata('username');
$data['kegiatan'] = $this->M_kegiatan->get_kategori($category);
$this->load->view('beranda', $data);
} else {
$this->load->model('M_kegiatan');
$data['username'] = $this->session->userdata('username');
$data['kegiatan'] = $this->M_kegiatan->get_kegiatan();
$this->load->view('beranda', $data);
}
this is my model :
public function get_status($status) {
$this->db->order_by('tanggal','desc');
$this->db->where('status',$status);
$query = $this->db->get('kegiatan');
if ($query->num_rows() > 0) {
return $query -> result();
}
}
public function get_kategori ($kategori) {
$this->db->order_by('tanggal','desc');
$this->db->where('kategori',$kategori);
$query = $this->db->get('kegiatan');
if ($query->num_rows() > 0) {
return $query -> result();
}
}
from this code , just filter by get_status () can work , anyone can help me ?
If I understand correctly, I think this is what you are looking for.
public function index($state = null, $category = null)
{
$this->load->model('M_kegiatan');
$data['username'] = $this->session->userdata('username');
if (!empty($state) || !empty ($category))
{
$data['kegiatan'] = $this->M_kegiatan->get_filtered($state, $category);
}
else
{
$data['kegiatan'] = $this->M_kegiatan->get_kegiatan();
}
$this->load->view('beranda', $data);
}
Add this function to the model
public function get_filtered ($status, $category)
{
$this->db->order_by('tanggal','desc');
If (!empty($status))
{
$this->db->where('status',$status);
}
If (!empty($category))
{
$this->db->where('kategori',$category);
}
$query = $this->db->get('kegiatan');
return ($query->num_rows() > 0) ? $query->result() : NULL;
Related
I wanted to pass some values to the view.
The $result may be 0 or 1 or '' and I tried to use this below code:
public function whatwedo($result='')
{
$result = array();
$result['status'] = $result;
$this->load->view('admin/whatwedo',$result);
}
public function add_whatwedo()
{
//$this->load->library('form_validation');
$this->form_validation->set_rules('text-input','Title','required');
if($this->form_validation->run() != true)
{
$result = 0;
$this->whatwedo($result);
}
else
{
$this->load->model('admin_model');
$result = $this->admin_model->ins_whatwedo($this->input->post());
//print_r($result);exit();
$this->whatwedo($result);
}
}
And in the view:
<?php
print_r($status);
?>
But, the $status is Array ( )
The problem is this line:
$result = array();
Because now the $result variable is an empty array so when you create the index status on result you assign it an empty array.
To fix you can do something like:
public function whatwedo($input = '')
{
$result['status'] = $input;
$this->load->view('admin/whatwedo', $result);
}
or even...
public function whatwedo($input = '')
{
$this->load->view('admin/whatwedo', ["status" => $input]);
}
I'm using codeigniter and this is located in my model. I have this error Unknown column 'postTable' in 'field list' . I hope you can help me and thank you in advance.
public function getManufacturer()
{
$condition = array('manufacturer_id' => $this->manufacturer_id);
$query = $this->db->get_where('manufacturer', $condition);
return $query->row_array();
}
public function updateManufacturer()
{
$this->db->where('manufacturer_id',$this->manufacturer_id);
$query = $this->db->update('manufacturer', $this);
return $query;
}
And this is my controller
public function updateForm()
{
$data = array();
$this->load->model('asset_model');
$p = new asset_model();
$p->manufacturer_id = $this->input->post('manufacturer_id');
$data = $p->getManufacturer();
$this->load->view('updateForm',$data);
}
public function update()
{
$this->load->model('asset_model');
$p = new asset_model();
$p->manufacturer_id = $this->input->post('manufacturer_id');
$p->manufacturer_name = $this->input->post('manufacturer_name');
$status = $p->updateManufacturer();
if ($status == true) {
redirect('asset_management/manufacturers');
}
}
Try this
In your controller
public function updateForm()
{
$data = array();
$this->load->model('asset_model');
$manufacturer_id = $this->input->post('manufacturer_id');
$data = $this->asset_model->getManufacturer($manufacturer_id);
$this->load->view('updateForm',$data);
}
public function update()
{
$this->load->model('asset_model');
$manufacturer_id = $this->input->post('manufacturer_id');
$manufacturer_name = $this->input->post('manufacturer_name');
$status = $this->asset_model->getManufacturer($manufacturer_id);
if ($status == true) {
redirect('asset_management/manufacturers');
}
}
In Model
public function getManufacturer($manufacturer_id)
{
$condition = array('manufacturer_id' =>$manufacturer_id);
$query = $this->db->get_where('manufacturer', $condition);
return $query->row_array();
}
public function updateManufacturer($manufacturer_id)
{
$condition = array('manufacturer_id' =>$manufacturer_id);
$query = $this->db->update('manufacturer', $condition);
return $query;
}
I am creating an API where I'm inserting product_id, Product_Size, quantity, product_name in an array called product-info.
Now I want to update the other values when product id is already existing.
my code for insertion in controller:
if(!empty($product_info1)){
foreach ($product_info1 as $value) {
$product_id = isset($value->product_id) ? $value->product_id : 0;
$Product_Size = isset($value->Product_Size) ? $value->Product_Size : 0;
$quantity = isset($value->quantity) ? $value->quantity : 0;
$product_name = isset($value->product_name) ? $value->product_name : 0;
if( $ok = 1)
$this->api_details_model->productinventory_track($product_id,$Product_Size,$quantity,$product_name);
{
$api_status="true";
$api_message="Done";
}
// $this->api_details_model->checkProductid($product_id);
}
}
model:
function productinventory_track($product_id,$Product_Size='',$quantity='',$product_name='')
{
if($product_id>=0)
{
$data = array('product_name'=>$product_name,'size_id'=>$Product_Size,'stock_count'=>$quantity,'product_id'=>$product_id);
if($this->db->insert('product_inventory', $data)){ return true; }else{ return false; }
}
please tell me the code for update
Controller:
$productInfo = $this->db->api_details_model->get($product_id);
$productArray['product_name'] = 'yourValue';
$productArray['size_id'] = 'yourValue';
$productArray['stock_count'] = 'yourValue';
// Means that there is no product with that ID in the database
if($productInfo == null)
$this->db->api_details_model->add($productArray);
// It was found an ID in the database
else
$this->db->api_details_model->edit($product_id, $productArray);
Functions in model:
public function add($data)
{
$this->db->insert('product_inventory', $data);
}
public function edit($id, $data)
{
$this->db->where('id', $id)
->update('product_inventory', $data);
}
public function get($id)
{
$this->db->where('id', $id);
return $this->db->get('product_inventory')->row();
}
I have 100 tables or can be more than that,I always need to fetch the record all the time in my application from various table.So writing functions for each and every query its not good coding standard.
$this->db->select();
$this->db->from("table1");
$query = $this->db->get();
return $query->result_array();
$this->db->select();
$this->db->from("table2");
$query = $this->db->get();
return $query->result_array();
$this->db->select();
$this->db->from("table1");
$this->db->where("id",10);
$query = $this->db->get();
return $query->result_array();
I want the good coding standard for this.
Write this code in controller.
$data = $this->common_model->getRecords("table_name", "*", array("field1" => $this->input->post('user_name')));
This will be your function in model (that is common_model).
public function getRecords($table, $fields = '', $condition = '', $order_by = '', $limit = '', $debug = 0) {
$str_sql = '';
if (is_array($fields)) { #$fields passed as array
$str_sql.=implode(",", $fields);
} elseif ($fields != "") { #$fields passed as string
$str_sql .= $fields;
} else {
$str_sql .= '*'; #$fields passed blank
}
$this->db->select($str_sql, FALSE);
if (is_array($condition)) { #$condition passed as array
if (count($condition) > 0) {
foreach ($condition as $field_name => $field_value) {
if ($field_name != '' && $field_value != '') {
$this->db->where($field_name, $field_value);
}
}
}
} else if ($condition != "") { #$condition passed as string
$this->db->where($condition);
}
if ($limit != "")
$this->db->limit($limit);#limit is not blank
if (is_array($order_by)) {
$this->db->order_by($order_by[0], $order_by[1]); #$order_by is not blank
} else if ($order_by != "") {
$this->db->order_by($order_by); #$order_by is not blank
}
$this->db->from($table); #getting record from table name passed
$query = $this->db->get();
if ($debug) {
die($this->db->last_query());
}
$error = $this->db->_error_message();
$error_number = $this->db->_error_number();
if ($error) {
$controller = $this->router->fetch_class();
$method = $this->router->fetch_method();
$error_details = array(
'error_name' => $error,
'error_number' => $error_number,
'model_name' => 'common_model',
'model_method_name' => 'getRecords',
'controller_name' => $controller,
'controller_method_name' => $method
);
$this->common_model->errorSendEmail($error_details);
redirect(base_url() . 'page-not-found');
}
return $query->result_array();
}
I used this code for fetching the record. you just need to pass the table name ,fields name ,the condition and limit.
your codes can actually be in 1 line
$query= $this->db->get('table1')->result_array();
$query= $this->db->get_where('table1', array('id'=>3,'name'=>'tom'))->result_array();
or if you really want a function
function getData($table){
$query=$this->db->get($table)->result_array();
return $query;
}
$data= getData('table1');
I am having trouble and since more than a week trying to find a solution to disallow duplicate form content if it is already exists in database.
So it will check all rows excluding the id (row) what currently I am editing and if same value exists it should give error message.
Here is my Code.
Position Controller
public function position_edit($id = NULL)
{
$this->data['title'] = '<i class="fa fa-user"></i> ' . lang('position_edit');
$this->data['position'] = $this->positions_model->get($id);
count($this->data['position']) || $this->data['errors'][] = 'position could not be found';
$id = $this->uri->segment(4);
$this->db->where('position', $this->input->post('position'));
!$id || $this->db->where('id !=', $id);
$pos = $this->positions_model->get();
echo '<pre>', print_r($pos), '</pre>';
if (count($pos) > 0) {
$this->form_validation->set_rules('position', 'lang:position_code', 'trim|required|max_length[10]|is_unique[positions.position]|xss_clean');
$this->form_validation->set_message('is_unique', lang('error_position_exists'));
}
if ($this->form_validation->run() === TRUE) {
$data = $this->positions_model->array_from_post(array('position', 'label'));
$this->positions_model->save($data, $id);
$this->session->set_flashdata('message', lang('position_record_updated'));
$this->data['message'] = $this->session->flashdata('message');
$this->session->set_flashdata('message_type', 'success');
$this->data['message_type'] = $this->session->flashdata('message_type');
//redirect('admin/hr/positions', 'refresh');
}
// Load the view
$this->load->view('hr/positions/edit', $this->data);
}
Position Model
class Positions_Model extends MY_Model
{
protected $_table_name = 'positions';
protected $_order_by = 'label ASC';
// This $rules currently not in use since it has been
// set directly to the controller edit method code
public $rules = array(
'position' => array(
'field' => 'position',
'label' => 'Position Code',
'rules' => 'trim|required|max_length[10]|xss_clean'
),
'label' => array(
'field' => 'label',
'label' => 'Position Label',
'rules' => 'trim|required|max_length[50]|xss_clean'
),
);
public function get_new()
{
$position = new stdClass();
$position->position = '';
$position->label = '';
return $position;
}
public function get_positions($id = NULL, $single = FALSE)
{
$this->db->get($this->_table_name);
return parent::get($id, $single);
}
public function get_positions_array($id = NULL, $single = FALSE)
{
$this->db->get($this->_table_name);
$positions = parent::get($id, $single);
$array = array();
foreach($positions as $pos){
$array[] = get_object_vars($pos);
}
return $array;
}
public function delete($id)
{
// Delete a position
parent::delete($id);
}
}
DB Model
class MY_Model extends CI_Model
{
protected $_table_name = '';
protected $_primary_key = 'id';
protected $_primary_filter = 'intval';
protected $_order_by = '';
public $rules = array();
protected $_timestamps = FALSE;
function __construct()
{
parent::__construct();
}
public function array_from_post($fields)
{
$data = array();
foreach ($fields as $field) {
$data[$field] = $this->input->post($field);
}
return $data;
}
public function get($id = NULL, $single = FALSE)
{
if($id != NULL) {
$filter = $this->_primary_filter;
$id = $filter($id);
$this->db->where($this->_primary_key, $id);
$method = 'row';
} elseif($single == TRUE) {
$method = 'row';
} else {
$method = 'result';
}
if(!count($this->db->ar_orderby)) {
$this->db->Order_by($this->_order_by);
}
return $this->db->get($this->_table_name)->$method();
}
public function get_by($where, $single = FALSE)
{
$this->db->where($where);
return $this->get(NULL, $single);
}
public function save($data, $id = NULL)
{
// Set timestamps
if ($this->_timestamps == TRUE) {
$now = date('Y-m-d H:i:s');
$id || $data['created'] = $now;
$data['modified'] = $now;
}
// Insert
if ($id === NULL) {
!isset($data[$this->_primary_key]) || $data[$this->_primary_key] = NULL;
$this->db->set($data);
$this->db->insert($this->_table_name);
$id = $this->db->insert_id();
} else {
// Update
$filter = $this->_primary_filter;
$id = $filter($id);
$this->db->set($data);
$this->db->where($this->_primary_key, $id);
$this->db->update($this->_table_name);
}
return $id;
}
public function delete($id)
{
$filter = $this->_primary_filter;
$id = $filter($id);
if (!$id) {
return FALSE;
}
$this->db->where($this->_primary_key, $id);
$this->db->limit(1);
$this->db->delete($this->_table_name);
}
}
I have tried callback function also but it is not working at all and couldn't find what causing the issue.
EDIT:
Please note it The above code is giving message if I am inserting the value which already exists but it is not validating and storing the data if the row is not exists
Updated
if ($this->form_validation->run() === TRUE) {
//print_r($this->positions_model->unique_value('position', $this->uri->segment(4)));
if($this->positions_model->unique_value('position', $this->uri->segment(4))) {
$this->form_validation->set_message('unique_value', lang('error_position_exists'));
} else {
$data = $this->positions_model->array_from_post(array('position', 'label'));
$this->positions_model->save($data, $id);
$this->session->set_flashdata('message', lang('position_record_updated'));
$this->data['message'] = $this->session->flashdata('message');
$this->session->set_flashdata('message_type', 'success');
$this->data['message_type'] = $this->session->flashdata('message_type');
redirect('admin/hr/positions', 'refresh');
}
}
In Controller
public function unique_value($field, $id)
{
$id = $this->uri->segment(4);
$this->db->where($field, $this->input->post($field));
!$id || $this->db->where('id !=', $id);
$position = $this->positions_model->get();
if (count($position)) {
return TRUE;
}
return FALSE;
}
Please Note: I don't know what exactly happens to your code, but I wouldn't check between insert or update into the model, just in the controller
I would solve it using a checkExist Model function, that would check if all the values you want to check exists into the DDBB, and work according to it. I would also do it in the controller instead of the model. First, you validate the fields, and then you check if values exists excluding the edit id:
$values_from_post = $this->positions_model->array_from_post(array('position', 'label'));
// $editId is to avoid the id of the row you were editing
if ($this->form_validation->run() === TRUE ) {
if ( !$this->positions_model->check_duplicate( $values_from_post, $editId ) ) ){
// Insert Value
} else {
// Update Value
}
And in your model, you check the duplicate via a where if the values exists:
public function check_duplicate( $values_from_post, $editId ) {
foreach ( $values_from_post as $key => $value ) {
$this->db->where( $key, $value );
}
$this->db->where('id !=', $editId );
$result = $this->db->get($this->_table_name);
return ( ( $result->num_rows > 0 ) ? true : false );
}
Please, I didn't check the code, but that is the idea, instead of doing it in the model, control it in the controller and then insert or update depending of what happens.
Here is the default validation rule of CodeIgniter for checking the duplicate entries in two columns.
$this->form_validation->set_rules('form_field_name','Title here','required|max_length[255]|unique[table.table_column1,table.table_column2]');