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;
Related
I am trying to get the data from database in CodeIgniter using foreach loop with the following query but I am getting the last result only.
public function get_tags($limit, $start, $tag_id)
{
$snippetstagdata = $this->getDataOneColumn("snippets_tags","tag_id",$tag_id);
foreach ($snippetstagdata as $getSnippet) {
$snippet_id = $getSnippet->snippet_id;
$this->db->limit($limit, $start);
$this->db->select('*');
$this->db->from('snippets');
$this->db->where("id", $snippet_id);
//$this->db->like('snippet_tags',$query);
$query = $this->db->get();
}
$result = $query->result();
$this->db->save_queries = false;
return $result;
}
Whereas getDataOneColumn function is
public function getDataOneColumn($table, $col1_name, $col1_value)
{
$this->db->where("$col1_name", $col1_value);
$query = $this->db->get("$table");
$result = $query->result();
$this->db->save_queries = false;
return $result;
}
This is incorrect:
foreach ($snippetstagdata as $getSnippet) {
$snippet_id = $getSnippet->snippet_id;
$this->db->limit($limit, $start);
$this->db->select('*');
$this->db->from('snippets');
$this->db->where("id", $snippet_id);
//$this->db->like('snippet_tags',$query);
$query = $this->db->get();
}
$result = $query->result();
You are looping through each item in your data and resetting $query each time but then only using $query once when you are out of the loop.
You need to move this line:
$result = $query->result();
inside the loop to get the correct output.
EDIT:
As pointed out, this isn't 100% either. $result should be an array and inside the loop the query should be pushed into the array like this:
$result[] = $query->result();
Then the final array ($result) can be looped through on its own.
Like #steve said you are replacing $query->result() each time you run the loop. so first you need to create an associative array to handle this. So your code should be:
$data = array();
foreach ($snippetstagdata as $getSnippet) {
$snippet_id = $getSnippet->snippet_id;
$this->db->limit($limit, $start);
$this->db->select('*');
$this->db->from('snippets');
$this->db->where("id", $snippet_id);
//$this->db->like('snippet_tags',$query);
$query = $this->db->get();
$result = $query->result();
$data[$snippet_id][] = $result;
}
Then you can get all the content from the $data array.
Hope that helps
You should try this
public function get_tags($limit, $start, $tag_id) {
$snippetstagdata = $this->getDataOneColumn("snippets_tags", "tag_id", $tag_id);
$review = [];
foreach ($snippetstagdata as $getSnippet) {
$snippet_id = $getSnippet->snippet_id;
$this->db->limit($limit, $start);
$this->db->select('*');
$this->db->from('snippets');
$this->db->where("id", $snippet_id);
//$this->db->like('snippet_tags',$query);
$query = $this->db->get();
$result[] = $query->result();
}
$this->db->save_queries = false;
return $result;
}
i am using code igniter and i want to get the record from db using OR operator so can anyone help me how to use OR operator in where clause
getting record from db where code is equal to 12 or 21 and status is 1
public static function front_page_ads() {
$CI = & get_instance();
$array = array('status' => 1 , 'code'=>'12' OR 'code'=>'21');
$CI->db->where($array);
$CI->db->order_by('id', 'DESC');
$CI->db->limit(3);
$q = $CI->db->get('international');
$r = $q->result();
return $r;
}
There are two ways to write OR.
$where = "name='Joe' AND status='boss' OR status='active'";
$this->db->where($where);
or you can use
$this->db->where('name', $name);
$this->db->or_where('id', $id);
In your code :
public static function front_page_ads() {
$CI = & get_instance();
$code_arr=array('12','21');
$CI->db->where('status',1);
$CI->db->where_in('code', $code_arr);
$CI->db->order_by('id', 'DESC');
$CI->db->limit(3);
$q = $CI->db->get('international');
$r = $q->result();
return $r;
}
Hope it helps.
use like this
$array = array('status' => 1);
$CI->db->where($array);
$ci->db->where("(code='12' OR code='21' )");
$CI->db->order_by('id', 'DESC');
$CI->db->limit(3);
$q = $CI->db->get('international');
$r = $q->result();
or_where : multiple instances are joined by OR
$CI->db->where($array);
$ci->db->or_where("code='12'");
$ci->db->or_where("code='21'");
simply:
$this->db->where("status","live")->or_where("status","dead");
or
$this->db->where("(status='live' OR status='dead')");
Hello i need help i am trying to pass three different variables from the controller of a codeigniter to model so i created an array and pass it then i exploded and i am getting the error explode() expects parameter 2 to be string, array given
The controller
$data = array(
'district_id' => $this->input->post('district') ,
'limit' => $limit,
'offset' => $offset,
);
$data['tubadili'] = $this->wapi_db->search_bar($data);
The Model function
public function search_bar($data){
$dataArray = explode(',' , $data);
$district_id = $dataArray[0];
$limit = $dataArray[1];
$offset = $dataArray[2];
$this->db->select('entertainment.Name,entertainment.ID,entertainment.Category,entertainment.Location,entertainment.Description,
image.ImagePath,image.Enter_ID,entertainment.DistrictID');
$this->db->from('entertainment');
$this->db->join('image', 'image.Enter_ID=entertainment.ID');
$this->db->where('entertainment.Category',"Bar");
$this->db->where('entertainment.DistrictID', $district_id);
$this->db->limit($limit, $offset);
$query = $this->db->get();
return $query->result();
}
Change:
$dataArray = explode(',' , $data);
$district_id = $dataArray[0];
$limit = $dataArray[1];
$offset = $dataArray[2];
To:
extract($data);
Basically what this does is take the associative array you have in $data and extracts them into their own variables, which is logically equal to:
$district_id = $data['district_id'];
$limit = $data['limit'];
$offset = $data['offset'];
Your issue is that you were trying to explode $data which will not work, explode works on strings and not arrays.
I am doing a complex query of a large database in my model and need to count the total rows before limiting the results for paging.
My model looks as follow:
public function get_categoryads($limit, $start, $cid, $type)
{
$this->db->start_cache();
// All your conditions without limit
$this->db->from();
$this->db->where(); // and etc...
$this->db->stop_cache();
$total_rows = $this->db->count_all_results(); // This will get the real total rows
// Limit the rows now so to return per page result
$this->db->limit($per_page, $offset);
$result = $this->db->get();
return array(
'total_rows' => $total_rows,
'result' => $result,
);
}
I don't understand how to now call it in my controller.
1. Get the results to be handed to the view
2. Get total_rows to be handed to the pagination config in the controller.
How do I call the array of results & total_rows in my controller?
In my controller:
// call the model function to get the result List data if not in an array
$data['results'] = $this->my_model->get_categoryads(10, 0, $cid, $type);
Sure this wont work as the model pushes out an array...
What should I then do in my controller to get the following:
// call the model function to get the result List data if not in an array
$data['results'] = $this->my_model->get_categoryads(10, 0, $cid, $type);
// Get total results for Pagination Config:
$config['total_rows'] = $this->db->count_all_results();
Do I do
$config['total_rows'] = $this->db->count_all_results($data['results']);
You can do like this in controller:
$results = $this->my_model->get_categoryads(10, 0, $cid, $type);
// Array of result rows to be sent to view
$data['results'] = $results['result'];
// For pagination config:
$config['total_rows'] = $results['total_rows'];
you can use codeigniter pagination concept:-
you have take page offset by uri segment like:-
$page = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0;
// it's depends on your uri segments
Now call your model with about page offset
$this->my_model->get_results($page,$something);
Now count total number of rows:-
$cnt=$this->my_model->count_results($something);
if 2nd and 3rd steps is done in one go then you have to do it like..
$temp=array();
$temp['result']=your query result;
$temp['count']=your count result;
Now insert this lines for pagination:-
$this->load->library("pagination");
$config = array();
$config["total_rows"] = $cnt; // or $result['count']
$config["per_page"] = 10;
$config['uri_segment'] = 4;
$config['num_links']=2;
$config['first_link'] = 'First';
$config['last_link'] = 'Last';
$config["base_url"] = your _controller_url
$config["display_pages"]=TRUE;
$this->pagination->initialize($config);
$data["links"] = $this->pagination->create_links();
Now call your view:-
$this-> load->view(your_view_name);
In your view you have to add div tag like:
<div class="pagination">
<?php echo $links;?>
</div>
Hope this may help you.
I think you could do this in a better code way.
Grab the total results before query filter.
Grab the total results after query filter.
Grab the results after query filter.
With that said:
public function get_categoryads($limit, $start, $cid, $type)
{
$this->db->select('COUNT(field) AS `total`')
->from('table');
$totalRows = $this->db->get()->row()->total;
$this->db->select('...')
->from('...')
->where('...')
->limit('...');
$totalRowsAfterLimit = $this->db->get()->num_rows();
$results = $this->db->get()->results();
return array('totalRows' => $totalRows,
'totalRowsAfterLimit' => $totalRowsAfterLimit,
'results' => $results);
}
Finally, in your controller you may do something like:
list($totalRows, $totalRowsAfterLimit, $results) = $this->my_model->get_categoryads('...');
And you are now able to loop through the results.
foreach($results as $result)
{
echo $result->some_field_from_database;
}
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() );