I have a query in my controller which searches for the term/keyword that is in the url.
Example /search/keyword
Then through my controller I perform the search doing:
$viewdata['search_results'] =
$this->Search_model->search(strtolower($this->uri->segment(2)),$limit,$offset);
then I check the database from my model like so:
function search($searchquery, $limit, $offset) {
$this->db->from('content');
$this->db->like('title', $searchquery);
$this->db->or_like('content', $searchquery);
$query = $this->db->limit($limit, $offset)->get();
$results = Array();
foreach ($query->result() as $row) {
$results[] = Array(
'title' => '' . strip_tags($row->title) . '',
'link' => base_url() . $row->anchor_url,
'text' => str_ireplace($searchquery, '<b>' . $searchquery . '</b>', strip_tags(neatest_trim($row->content,220,$searchquery,120,120)))
);
}
return $results;
}
}
I would like to know how I can return the number of rows found in my search result to the controller. I will then use the count of rows found for pagination.
How can I get the row count into the controller????
You can add a function in your model that returns the count and call that.
Example:
// model
public function getAffectedRows()
{
return $this->db->affected_rows();
}
...
// controller
$this->Search_model->doQuery();
$numRows = $this->Search_model->getAffectedRows();
You can either just use
$viewdata['search_results'] =
$this->Search_model->search(strtolower($this->uri->segment(2)),$limit,$offset);
$viewdata['search_result_count'] = count( $viewdata['search_results'] );
Or you could return a structured array from your model which includes the count. Something like
return array( 'results' => $result, 'num_results' => $this->db->num_rows() );
Related
How do I get the number of rows here? When I use $count = $flights->count(); I get this error Count all elements in an array, or something in an object
public function index(Request $request)
{
$airline = airline::all();
$search = $request->get('search');
if($search!=""){
$flights = DB::table('flights')
->join('airlines', 'flights.AirlineId', '=', 'airlines.id')
->select('flights.*', 'airlines.name', 'airlines.country','airlines.logo')
->where(DB::raw("CONCAT(flights.id,' ',flights.flightDesignator,' ',airlines.country,' ',airlines.name,' ',flights.departureFrom,' ',flights.arriveTo,' ',flights.departureTime,' ',flights.ArrivalTime)"),'LIKE','%'.$search.'%')
->paginate(4);
$flights->appends(['search' => $search]);
$count = $flights->count();
if($count == 0)
return view('admin.flights')->with(['flights' => $flights,'airline' => $airline, 'NoFound' => 'There is no result 😔']);
else
return view('admin.flights')->with(['flights' => $flights,'airline' => $airline,'found' => ' records founded']);
}
}
There is pagination is used so just change
$count = $flights->count(); to $count = $flights->total();
try to use collection
$data = collect($flights);
and then you can count it with
$data->count();
I'm trying to select rows from my DB table based on information I get from the other rows(previous query). The trouble I'm having is converting the $query->result_array(); in the controller to use it in the model again and subsequently the view.
I've tried to do a foreach loop and returning the $query->result_array(); from the model, this turned out to be problematic as it didn't fit the different stages I have on my website(I have several stages of content).
controller.php
public function firststage() {
$this->load->model('Model');
$get_stage = $_GET["stg"];
$get_results = $this->Model->get_stage_id($get_stage);
foreach ($get_results as $results) {
$id = $results["id"];
}
$data['result'] = $this->Model->stage_results($get_stage, $id);
$this->load->view('stage_view', $data);
}
model.php
public function get_stage_id($get_stage) {
$this->db->where('stage_parent', $get_stage);
$query = $this->db->get('stages');
return $query->result_array();
}
public function stage_results($get_stage, $id) {
$this->db->where('stage_id', $get_stage);
$this->db->where('stage_id', $id);
$query = $this->db->get('stage_contents');
foreach ($query->result_array() as $row) {
$rows[] = array(
'id' => $row["id"],
'name' => $row["name"]
);
}
return $rows;
}
Expected the output selection to be based on the first query result, instead I get the Undefined variable: rows when I run all of it. I don't think my view is relevant to this question, but please let me know if you think otherwise!
you get the error
Undefined variable: $rows
because your query doesn't return any result, therefore $rows is not defined
you can resolve this checking if there are rows returned:
if($query->num_rows()){
foreach ($query->result_array() as $row) {
$rows[] = array(
'id' => $row["ID"],
'name' => $row["name"]
);
}
}else {$rows='no records found';}
print_r($rows);die;
this prints either the array (if there are results), or 'no records'
or simply do:
$rows = array();
foreach ($query->result_array() as $row) {
$rows[] = array(
'id' => $row["ID"],
'name' => $row["name"]
);
}
then you get an empty array if there are no results
I want to pass multiple id in where condition how to do it?
A query fetches org_id from database.
now I want to pass that in my where condition, so how to do it?
I tried foreach loop:
Below is my code:
$devices = $this->activation_m->get_activated_devices();
$org_id = array(); // create org_id array
foreach ($devices as $row)
{
$org_id[]= $row['org_id']; // assign ids into array
//$companys =$this->data['devices'] = $this->activation_m->get_company($org_id); // don't call again and again
}
//Now $org_id is array
$companys =$this->data['devices'] = $this->activation_m->get_company($org_id);
echo $this->db->last_query();
Model Code
public function get_activated_devices()
{
$this->db->select('*');
$this->db->join('sitekey','sitekey.site_key = activation.site_key');
$this->db->from('activation');
$query =$this->db->get();
$result = $query->result_array();
return $result;
}
public function get_company($org_id)
{
$this->db->select('*');
$this->db->join('sitekey','sitekey.org_id = company.id');
$this->db->join('activation','sitekey.site_key = activation.site_key');
$this->db->where('company.id IN',(implode(',',$org_id))); // see the change here
$this->db->from('company');
$query =$this->db->get();
$result = $query->result_array();
return $result;
}
now currently my query passes only one org_id , I want to pass all the org_id I get from my first query.
You can use where_in from active-records codeigniter
as
$devices = $this->activation_m->get_activated_devices();
$org_id = array();
foreach ($devices as $row)
{
$org_id[] = $row['org_id'];
}
$companys =$this->data['devices'] = $this->activation_m->get_company($org_id);
if( $companys->num_rows() > 0 )
{
echo "<pre>";
print_r( $companys->result());
echo "</pre>";
}
And for Model
public function get_company($org_ids = array())
{
$this->db->select('*');
$this->db->join('sitekey','sitekey.org_id = company.id');
$this->db->join('activation','sitekey.site_key = activation.site_key');
$this->db->where_in('company.id', $org_ids ); //this is condition
$this->db->from('company');
return $this->db->get();
}
You can use codeigniter's $this->db->or_where() for the purpose. Just traverse the array of organization id's and apply or_where conditions.
$this->db->select('*');
$this->db->join('sitekey','sitekey.org_id = company.id');
$this->db->join('activation','sitekey.site_key = activation.site_key');
foreach($org_id as $org)
{ // where $org is the instance of one object of active record
$this->db->or_where('company.id',$org);
}
$this->db->from('company');
$query =$this->db->get();
$result = $query->result_array();
return $result;
Another way to do this is to make a custom query by traversing the array like this and appending where clauses in string.
I have been searching for long time but I'm new in CI so it's really hard to find the answer.
So here is the problem: I want to get all of my records without the last one from the database using CI pagination ... I know that I need to do something with my model but I don't know what :/ This is how it looks like:
public function get_results($search_term = 'default', $offset = 0, $limit = 0) {
$this->db->select('SQL_CALC_FOUND_ROWS id,blog_time,blog_title,blog_text,image', false);
$this->db->from('blog');
$this->db->like('blog_title', $search_term);
$this->db->or_like('blog_text', $search_term);
$this->db->order_by('blog_time', 'DESC');
$this->db->limit($limit, $offset);
$data = $this->db->get()->result();
$count = $this->db->query('SELECT FOUND_ROWS() count;')->row()->count;
return array('data' => $data, 'count' => $count);
}
You will need to modify the Model code to this:
public function get_results($search_term = 'default', $offset = 0, $limit = 0) {
$this->db->select('SQL_CALC_FOUND_ROWS id,blog_time,blog_title,blog_text,image', false);
$this->db->from('blog');
$this->db->like('blog_title', $search_term);
$this->db->or_like('blog_text', $search_term);
$this->db->order_by('blog_time', 'DESC');
$this->db->limit($limit, $offset);
$data = $this->db->get()->result();
$count = $this->db->query('SELECT FOUND_ROWS() count;')->row()->count;
return array('data' => $data, 'count' => $count -1);
}
We simply added -1 to the $count.
you can use the unset function of php
for your result
unset($result[0]);
as you are using descending last record will come first
and in your count
$count = $count-1;
I am doing a project in Codeigniter. Here I fetch the latest 10 mails from my gmail id using imap.Here I want to take the from field of fetched mail and i want to check whether the from field of fetched mail is in my database table('clients'). Here I stored the 10 from field of fetched mail to array and passed it to model,where the checking is takingplace and returns the matching field name. But it is not working for me.
My controller function is:
if ($mbox = imap_open($authhost, $user, $pass)) {
$emails = imap_search($mbox, 'ALL');
$some = imap_search($mbox, 'SUBJECT "Suspicious sign in prevented"', SE_UID);
$MC = imap_check($mbox);
$inbox = $MC->Nmsgs;
$inboxs = $inbox - 9;
if ($emails) {
$data['overview'] = imap_fetch_overview($mbox, "$inboxs,$inboxs:$inbox", 0);
$i = 0;
foreach ($data['overview'] as $i => $val) {
$from[$i] = $val->from;
$i++;
}
$data['result'] = $this->crm_model->get_names($from);
foreach ($data['result'] as $row) {
echo $row->name;echo "<br/>";
}
}
imap_close($mbox);
}
And my model function is:
function get_names($from) {
$this->db->select('name');
$this->db->from('clients');
$this->db->where_in('name', $from);
$query = $this->db->get();
return $query->result();
}
But when I used the above model function like below, it returns the value
function get_names() {
$options = array(
'0' => 'Job Openings',
'1' => 'Offers',
'2' => 'Techgig',
'3' => 'Australia',
);
$this->db->select('name');
$this->db->from('clients');
$this->db->where_in('name', $options);
$query = $this->db->get();
return $query->result();
}
I think the problem is with passing value from controller to model. Can anyone help me. Thanks in advance
While executing any query in database using where_in (list of comma separated values),
the list of values should be like this array:
$options = array('Hello', 'World!', 'Beautiful', 'Day!');
Not like this:
$options = array('0' => 'Job Openings','1' => 'Offers','2' => 'Techgig','3' =>'Australia',);
to do this you should implode this array!
$opt=implode(",",$options);
and pass this $opt array to WHERE_IN()
Hope this will solve your problem !
You don't have to use $i..
if ($emails) {
$data['overview'] = imap_fetch_overview($mbox, "$inboxs,$inboxs:$inbox", 0);
// $i = 0;
foreach ($data['overview'] as $val) {
$from[] = $val->from;
//$i++;
}
$data['result'] = $this->crm_model->get_names($from);
foreach ($data['result'] as $row) {
echo $row->name;echo "<br/>";
}
}
Do these changes and check