Codeigniter: Can I return multiple values from the same function? - codeigniter

I would like to return the query results along w/ the row count without having to run multiple queries. Is this possible in Codeigniter? The following works for returning the query results, but is it possible to also count the number of entries found?
Controller:
$data['records'] = $this->item_model->searchItem($item_name);
Model:
$query = $this->db->query($sql, array($this->user_id, '%'.$item_name.'%'));
return $query->result();

$bindings = array($this->user_id, '%'.$item_name.'%');
$records = $this->db->query($sql, $bindings)->result();
return array(
'records' => $records,
'count' => count($records),
);
Then, in your controller:
$query = $this->item_model->searchItem($item_name);
$data['records'] = $query['records'];
$data['count'] = $query['count'];

Option one
$query = $this->db->query($sql, array($this->user_id, '%'.$item_name.'%'));
$data['result'] = $query->result();
$data['rows'] = $query->num_rows();
return $data;
Option two
// model
$query = $this->db->query($sql, array($this->user_id, '%'.$item_name.'%'));
return $query;
// controller
$data['query'] = $this->item_model->searchItem($item_name);
// then you have $data['query']->result();
// and $data['query']->num_rows();

You can send as many variables from a function as you want ..However plz remember that a function is supposed to do one UNIT of work . thus this rule must not be violated.
we can used contrel structure
if this {return A;} elseif () {return B;} else {return C;}
ALso we can send (bundle) variables in an array and send

Related

search in codeigniter if my input data is match either not

i want to search in codeigniter if my input data is matched then shows result but couldn't match couldn't shows data..
Here is my model:
public function get_search(){
$p_cat = $this->input->post('p_category');
$p_place = $this->input->post('p_place');
$p_price = $this->input->post('p_price');
$search = $this->input->post('search');
$this->db->like('p_category',$p_cat,$search);
$this->db->or_like('p_place',$p_place,$search);
$this->db->or_like('p_price',$p_price,$search);
$query = $this->db->get('tbl_property');
if($query->num_rows() > 0)
return $query->result_array();
else
return FALSE;
}
But this model result match or mismatch it shows any data...
You have issue in your or_like method calling of query builder class. It should be like:
public function get_search(){
// Below three lines is not useful until you are using any other conditions to fetch data from DB
$p_cat = $this->input->post('p_category');
$p_place = $this->input->post('p_place');
$p_price = $this->input->post('p_price');
// I guess this string should be search in three columns
$search = $this->input->post('search');
$this->db->like('p_category',$search);
$this->db->or_like('p_place',$search);
$this->db->or_like('p_price',$search);
$query = $this->db->get('tbl_property');
if($query->num_rows() > 0)
return $query->result_array();
else
return FALSE;
}
Let me know if you still face any issue.

laravel error "strtolower() expects parameter 1 to be string"?

I am passing json to a laravel route as following and I am runnig this query sql view.
{"columns":["fname","lname","mobile"], "offset":"1", "limit":"25",
"order":[["fname","asc"],["lname","asc"]],
"filter":[["gender","=","M"]]}
And this is the function placed in controller which will going to call on route
public function fetch_contacts(Request $request){
if($request->header('content-type') == 'application/json' && !empty($request->header('Device')) && !empty($request->header('UID'))){
$query = DB::connection('mysql_freesubs')->table("contact_view")
->select($request->columns);
if(!empty($request->filter))
$query = $query->where($request->filter);
if(!empty($request->offset))
$query = $query->offset($request->offset);
if(!empty($request->limit))
$query = $query->limit($request->limit);
if(!empty($request->order))
$query = $query->orderBy($request->order);
$contacts = $query->get();
return $contacts;
}
where am I going wrong ?
You're passing multidimensional array to orderBy, try this instead:
$query = $query->orderBy($request->order[0][0], $request->order[0][1]);
$query = $query->orderBy($request->order[1][0], $request->order[1][1]);

codeigniter get array data in controller

I have sql query in controller, this query produces number of rows.
$q1 = $this->db->query("SELECT product_code,product_quantity FROM tbl_order_details WHERE order_id='$order_no' AND location='$location'")->row();
foreach($q1 as $v_barcode){
$result = $this->settings_model->update_cancel_stock($v_barcode->product_code,$v_barcode->product_quantity);
}
then i pass this data to my model,
public function update_cancel_stock($code,$qty)
{
$this->db->set('product_quantity', $qty , FALSE);
$this->db->where('product_id', $order );
$this->db->update("tbl_inventory6");
}
but no any update. please check above code. thanx
Try this
$cond = array("order_id" => $order_no, "location" => $location);
$q1 = $this->db->select("product_code,product_quantity")->from("tbl_order_details")->where($cond)->row();
$result = $this->settings_model->update_cancel_stock($q1->product_code,$q1->product_quantity);
and then model ($code was not used)
public function update_cancel_stock($code,$qty){
$this->db->set('product_quantity', $qty , FALSE);
$this->db->where('product_id', $code);
$this->db->update('tbl_inventory6');
}
You are using row() function of Active Record. It already return only one row. So there is no requirement of foreach. Try this:
$q1 = $this->db->query("SELECT product_code,product_quantity FROM tbl_order_details WHERE order_id='$order_no' AND location='$location'")->row();
$result = $this->settings_model->update_cancel_stock($q1->product_code,$q1->product_quantity);
If you are getting multiple records and you are using foreach, you should use result().
$q1 = $this->db->query("SELECT product_code,product_quantity FROM tbl_order_details WHERE order_id='$order_no' AND location='$location'")->result();
foreach($q1 as $v_barcode){
$result = $this->settings_model->update_cancel_stock($v_barcode->product_code,$v_barcode->product_quantity);
}

CodeIgniter pass variables form controller to model

Ok I want to pass two variables from a controller to a model but I get some kind of error. Am I passing variables on right way? My syntax is:
Controller:
public function add_tag(){
if(isset($_POST['id_slike']) && isset($_POST['id_taga'])){
$slika = $_POST['id_slike'];
$tag = $_POST['id_taga'];
$this->load->model("Member_model");
$res = $this->Member_model->add_tags($slike, $tag);
foreach ($res->result() as $r){
echo $r->name;
}
}
else{
echo "";
}
}
Model:
public function add_tags(){
$data = array(
'tags_id' => $tag ,
'photos_id' => $slika
);
$check = $this->db->query("SELECT tags_id,photos_id FROM bridge WHERE bridge.tags_id='{$tag}' AND bridge.photos_id={$slika} ");
if($check->num_rows()==0){
$this->db->insert('bridge',$data);
$res = $this->db->query("SELECT name FROM tags where `tags`.`id`='{$tag}' ");
return $res;
}
}
you are passing variables correctly, but do not get them correctly in the model, which should look like this:
public function add_tags($slike, $tag){
//your other code
}
The following code write on the controller file:-
$data = array();
$this->load->model('dbmodel');
$data['item'] = $this->dbmodel->getData('*','catagory',array('cat_id'=>21));
$this->load->view('listing_view', $data);
The following code write on the dbmodel file:-
public function getData($cols, $table, $where=array()){
$this->db->select($cols);
$this->db->from($table);
$this->db->where($where);
$query = $this->db->get();
$result = $query->result();
return $result;}

How can I use CodeIgniter form dropdown function?

codeIgniter form_dropdown() function can receive only associative array but I have multi dimension array by using result_array() function. How can I fetch my data on form_dropdown() function?
Let's say you want a dropdown of items, using result_array():
$query = $this->db->query("SELECT id, item_name FROM items");
$options = array();
foreach ($query->result_array() as $row){
$options[$row['id']] = $row['item_name'];
}
echo form_dropdown('my_items_dropdown', $options, '1');
I have extended the class DB_result.php in system/database with this new function
public function dropdown_array($id_field = FALSE, $list_field = FALSE)
{
$result = array();
if ($id_field === FALSE || $list_field === FALSE) return $result;
$array = $this->result_array();
foreach ($array as $value) {
$result[$value[$id_field]] = $value[$list_field];
}
return $result;
}
Now you can simply call from every Model class your new function to generate a dropdown-compliant array like this:
$customers = $this->db->get('customers');
$result = $customers->dropdown_array('id', 'name');
return $result;

Resources