how to use count() in controller? - php

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

Related

How to increment a count in model and save in database using codeigniter?

I have use a count to but it will save only 1 in database what did i do wrong
public function create_invoice()
{
$count = 1;
$data = array(
'invoice_no' => $count
);
$count++;
return $this->db->insert('invoice', $data);
}

Function not returning all database rows

I have this function in Code igniter PHP where I am trying to get all rows from the database that match a input value.
How should I change the code to get all rows that match my search criteria?
Thank you
Model search function:
function findquestion($searchvalue){
$this->db->where('answer1', $searchvalue);
$res = $this->db->get('questions');
$count = $res->num_rows();
if($res->num_rows()==0){
return "...";
}
$moviearray = $res->row_array();
return $moviearray;
}
Controller Function:
public function getdatabasedata()
{
if($this->input->server('REQUEST_METHOD') == 'GET')
{
$year = $this->input->get('searchvalue');
if($year != ''){
$movie = $this->adminmodel->findquestion($year);
echo json_encode($movie);
}
}
}
When I look at Postman to check what the JSON object holds, this is what I get. But in fact I have three more rows in my database that match my search criteria.
{
"id": "10",
"question": "test questions",
"image": "test image",
"answer1": "answer1",
"answer2": "answer2",
"answer3": "answer3",
"answer4": "answer4"
}
function findquestion($searchvalue){
1) define array.
$data = array();
$this->db->where('answer1', $searchvalue);
$res = $this->db->get('questions');
if($res->num_rows()==0){
return "...";
}
2) loop trough results and store in array.
foreach ($res->result() as $row)
{
$data[] = array(
'id' => $row->id,
'question' => $row->question,
'image' => $row->image,
'answer1' => $row->answer1,
'answer2' => $row->answer2,
'answer3' => $row->answer3,
'answer4' => $row->answer4
);
}
//$moviearray = $res->row_array();
return $data;
}

Pass an array from controller to model in codeigniter

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

PHP Recursive Function Breaks at Return

I've written a recursive function, which generates an organizational diagram by looking at each employees manager. The function works, however I have an issue with values being returned. The code uses two functions, which are both recursive. They are build using the same mindset.
My problem lies within function get_top_org, which breaks out of the loop when getting to the first employee, who do not have any employees. The function should continue by mapping all employees for the manager. I can get the function to do this by removing the recursive return (return get_top_org($emp->username, $organisation, $level, $limit);). Then the function will go through all the employees, but returns null.
Any help is appreciated!
function get_top_managers($username, $account, $layer, $managers){
global $DB;
$manager = $DB->get_record('user_info_data', array('fieldid' => 2, 'userid' => (String)$account->id));
if($manager->data != ""){
$managers[$username][] = $manager->data;
$manager_account = $DB->get_record('user', array('username' => (String)$manager->data));
return get_top_managers($username, $manager_account, $layer + 1, $managers);
}else{
return $managers;
}
}
function get_top_org($username, $organisation, $level, $limit){
global $DB;
$employees = $DB->get_records_select('user_info_data', "fieldid = 2 AND data LIKE '$username'");
if(count($employees) != 0){
foreach($employees as $emp){
if($emp = $DB->get_record('user', array('id' => $emp->userid))){
$managers = array();
$last = "";
$managers = get_top_managers($emp->username, $emp, 0, $managers);
$managers = $managers[$emp->username];
foreach($managers as $manager){
$merger = array();
if($last != ""){
$merger[$manager] = $last;
$last = $merger;
}
}
$organisation = array_merge_recursive($organisation, $merger);
//This return statement gets the function to stop running, however - if removed the function returns null but runs as it should.
return get_top_org($emp->username, $organisation, $level, $limit);
}
}
}else{
if($non_manager = $DB->get_record('user', array('username' => $username))){
$managers = array();
$last = "";
$managers = get_top_managers($non_manager->username, $non_manager, 0, $managers);
$managers = $managers[$non_manager->username];
foreach($managers as $manager){
$merger = array();
if($last != ""){
$merger[$manager] = $last;
$last = $merger;
}else{
$last = array($manager => $non_manager->username);
}
}
return array_merge_recursive($organisation, $merger);
}
}
}
You're looping over a list of employees, but then returning from within that loop. So the function as you have written it here will only visit one employee.
Instead of returning the results of your (recursive) call to get_top_org directly, you should combine the results of each call and return that after the for loop has finished.

Codeigniter get number of row from search query

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

Categories