search in codeigniter if my input data is match either not - codeigniter

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.

Related

Echo an error message if query result is empty instead of ERRORS (Codeigniter)

My target is to show an echo "No data" when the query result is empty. What happening currently is that when my query is empty, it shows an error. (Please see the picture below for the error) I provided my codes below and my screenshot. Any help will be appreciated. Thank you
Controller:
public function new1($arenaID=0) {
$data['activeNav'] = "";
$data['subnav'] = "arenas";
$this->header($data);
$this->nav();
$data['k1']=$this->arenas->meron1();
$this->load->view('new', $data);
$this->footer();
}
Model:
function meron1(){
$query = $this->db->query("SELECT fightID FROM fight_entries where position='meron' ORDER BY fightID DESC LIMIT 1");
return $query->row()->fightID+1;
}
A better way to do this is just to check the number of rows being returned.
function meron1(){
$query = $this->db->query("SELECT fightID FROM fight_entries where position='meron' ORDER BY fightID DESC LIMIT 1");
if($query->num_rows() > 0){
$query = $query->rows();
return $query->fightID+1;
}else{
$message = "No Data";
return $message;
}

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

retrieve data from database in for loop

my controller is
function shiftstudentdetailsmultiple(){
$data['Show'] = $this->input->post('show[]');
$roll = $this->input->post('roll[]');
//echo sizeof($data1);
for($i=0;$i<sizeof($roll);$i++)
{
$data['results'] = $this->FetchData->getstudentsdetailscouseandmultiple($roll[$i]);
}
$this->load->view('registrar/shift_student_view', $data);
}
and model is
function getstudentsdetailscouseandmultiple($Uniq_Id){
$this->db->select("sprd.Uniq_Id, sprd.Name, sprd.Uni_Roll_No, cd.Course_Name, bd.Branch_Name, sprd.Year, sprd.Session, sprd.Section, sprd.Fee_Status, sprd.Curr_Status");
$this->db->where("sprd.Uniq_Id",$Uniq_Id);
$this->db->from("Students_Prim_Detals sprd");
$this->db->join('Course_Details cd', 'cd.Course_Id=sprd.Course_id');
$this->db->join('Branch_Details bd', 'bd.Branch_Id=sprd.Branch_id');
$query = $this->db->get();
if ($query->num_rows() > 0){
return $query->result();
}
else {
return false;
}
}
when showing in view the fetching data showing only one time.. but in my database i have more then 4 entry when i print_r
$data['results']=this->FetchData->getstudentsdetailscouseandmultiple($roll[$i]);
the data showing correct .. when load only showing one data..the last entry..
$this->load->view('registrar/shift_student_view', $data);
Each time you call...
$data['results'] = $this->FetchData->getstudentsdetailscouseandmultiple($roll[$i]);
you replace the value stored in $data['results'] with the new return. So only one return is sent to the view.
You will need to revise your controller along these lines.
$student_detail = array();
for($i=0;$i<sizeof($roll);$i++)
{
$student_detail[] = $this->FetchData->getstudentsdetailscouseandmultiple($roll[$i]);
}
$data['results'] = $student_detail;
Then in your view you will have to loop through $results to display each item in the array.
foreach($results as $detail){
echo "Name: ". $detail->name;
echo "Course: ". $detail->Course_Name;
//etc...
}

Call to a member function row() on a non-object

i am getting error Call to a member function row() on a non-object in codeigniter my controller is
public function edit_survey_pro($id)
{
$id = intval($id);
$survey = $this->model->get("surveys",array("ID" => $id),100000);
if (sizeof($survey) == 0) $this->template->error(lang("error_32"));
$this->template->loadContent("user/edit_survey_pro", array(
"survey" => $survey->row()
)
);
}
my model is
function get($table,$where='',$perpage=0,$start=0,$order_by='',$arr='')
{
$this->db->from($table);
if($perpage != 0 && $perpage != NULL)
$this->db->limit($perpage,$start);
if($where){
$this->db->where($where);
}
if($order_by){
$this->db->order_by($order_by);
}
if($arr=='')
$query = $this->db->get()->result();
else
$query = $this->db->get()->result('array');
if(!empty($query))
if($perpage != 0 && $perpage != NULL)
$result = $query;
else
$result = $query[0];
else
$result = array();
return $result;
}
here loadContent() is just load the content with view path
public function loadContent($view,$data=array(),$die=0){
//something to load the content
}
in my model I am getting the result as an array of object in $query and then it is returned as $result like this -
$query = $this->db->get()->result(); but at the controller $survey stores array of object and i want to show the content of that array of object ,previously I use
$this->template->loadContent("user/edit_survey_pro", array(
"survey" => $survey->row()
)
);
to get that data but the problem is $survey->row() cannot return that data bcoz it is not an object it is array of object so it can't be returned through row() method
so instead of this I just call the first element of that data like this-
$this->template->loadContent("user/edit_survey_pro", array(
"survey" => $survey[0]
)
);
Somehow its works for me bcoz I want to show the first row of the data
if sembody wants to show all data then I think he shuld try logic to increment the key value of that array of object for me it is $survey[] you can use foreach loop for increment the of value of the key element
The problems i see are your model, I will dissect it and add comments to your original code to point out the issues:
function get($table,$where='',$perpage=0,$start=0,$order_by='',$arr='')
//above there are problems, you are setting some of your parameters to
//equal blank, but below, in your conditionals, you are checking if they
// exist. They will always exist if they are set to blank. Fix them by
// setting them = NULL like this:
// get($table,$where=null,$perpage=0,$start=0,$order_by=null,$arr=null)
{
$this->db->select();// <-- you forgot this
$this->db->from($table);
if($perpage != 0 && $perpage != NULL)
//when will $perpage = null? , if never, then you dont need it.
$this->db->limit($perpage,$start);
if($where){
//change this to if(isset($where)). Also why do you use
//curly braces here, but not in the above if statement if only
//one line is affected in your if. I would remove the
//curly braces here.
$this->db->where($where);
}
if($order_by){
// change this to if(isset($order_by)). Same thing as
//above about the curly braces here
$this->db->order_by($order_by);
}
if($arr=='')
// change this to if(isset($arr)).
$query = $this->db->get()->result();
else
$query = $this->db->get()->result('array');
//change this to: $query = $this->db->get()->result_array();
if(!empty($query))
//change the above to if($query->num_rows > 0). Also, here since
//your code body is longer then one line, you will need curly braces
//around your if statement
if($perpage != 0 && $perpage != NULL)
//again, will $perpage ever be NULL? However, why do you need
//this conditional at all, if the limit above is already
//doing this job?
$result = $query;
else
$result = $query[0];
else
$result = array();
return $result;
}
after applying all the changes:
MODEL:
function get($table, $where=null, $perpage=0, $start=0, $order_by=null, $arr=null)
{
$this->db->select();
$this->db->from($table);
if($perpage != 0)
$this->db->limit($perpage, $start);
if(isset($where))
$this->db->where($where);
if(isset($order_by))
$this->db->order_by($order_by);
if(isset($arr)) {
$result = $this->db->get()->result_array();
} else {
$result = $this->db->get()->result();
}
return $result;
}
CONTROLLER
public function edit_survey_pro($id) {
$id = intval($id);
$survey = $this->model->get("surveys",array("ID" => $id),100000);
if (!$survey) {
$this->template->error(lang("error_32"));
} else {
$data["survey"] = $survey;
$this->template->loadContent("user/edit_survey_pro", $data);
}
}
I think when you use $this->db->get(), you need to pass the table name as param like this:
$this->db->get('table_name')->result();

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

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

Resources