I want to create paging on my content managed site - php

I'm trying to load a blog type page, I load the entries from my database with this controller
public function blog($page){
$this->load->model("model_get");
$this->load->view("site_header");
$this->load->view("site_nav");
$counter = $this->model_get->getBlogcount();
for($counter; $counter > 0; $counter --){
$data["results"] = $this->model_get->getBlog($counter);
$this->load->view("content_blog", $data);
}
}
$this->load->view("site_footer");
}
and this model
function getBlogcount(){
$result = $this->db->count_all("blog");
return $result;
}
I count the entries in the database where I call them out by their ID. But now I'm trying to create multiple pages that expand automatically everytime I enter a new entry. So lets say I have 27 entries, and want to have no more than 5 entries on a single page, how do I make it so that it creates the necessary 6 pages to show them without loading the other 3 empty entries and stuff.
I'm new to codeigniter and have always worked with ASP .NET, any help would be helpfull.
Thanks in advance!
p.s. english isn't my first language

CodeIgniter have his own pagination Class. Take a look here : http://ellislab.com/codeigniter/user-guide/libraries/pagination.html
You can try this firstly and adapt to your project :
public function blog($page = 0)
{
$this->load->library('pagination');
$this->load->helper('url');
$config['base_url'] = base_url('blog/'. $page);
$config['total_rows'] = $this->model_get->getBlogcount();
$config['per_page'] = 5;
$this->pagination->initialize($config);
$data['results'] = $this->model_get->getBlog($config['per_page'], $page);
$this->load->view("content_blog", $data);
}
Edit your "getBlog" function model to get results with limit clause, like this :
function getBlog($limit, $start)
{
$results = $this->db->limit($limit, $start)->get('your_blog_table');
if ($results)
{
return $results->result();
}
return FALSE;
}
And use in your view this code to create your pagination links :
echo $this->pagination->create_links();

Related

How to print count in view using Codeigniter

I am new to Codeigniter and I am facing an issue with printing count in admin dashboard.
The following code is what I am trying now. Please tell me what I am doing wrong.
my model
function todayorder(){
$sql=$this->db->query("select count(*) as count from orders where order_date_time >= CURDATE()");
return $sql->row();
}
controller
public function orderlist(){
if(isset($_SESSION['admin_id'])){
$data["row"]=$this->picShuModel->orderlist();
$this->load->view('admin/index',$data);
$query = $this->picShuModel->todayorder();
$data['count'] = $query->count;
$this->load->view('admin/index',$data);
}else{
$this->load->view('admin/login');
}
}
and my view* in the dashboard
<?php foreach($data as $count){echo $count;}?>
In your view you can directly use $count as a variable.
Change
<?php foreach($data as $count){echo $count;}?>
To
echo $count;
Update
public function orderlist(){
if(isset($_SESSION['admin_id'])){
$data["row"]=$this->picShuModel->orderlist();
$query = $this->picShuModel->todayorder();
$data['count'] = $query->count;
$this->load->view('admin/index',$data);
}else{
$this->load->view('admin/login');
}
}
This below line assigns count to array variable
$data['count'] = $query->count;
To access count in view page
<?php
echo $count ; // you have use element of data rather $data. this print the count value
?>
In controller you are loading view page twice, load view page once

Codeigniter pagination doesn't show page relevant data, but the whole data set

Here is my controller :
function index()
{
$data['title']="Post";
$this->load->library('pagination');
$config['base_url'] = base_url().'Post/index';
$config['total_rows'] = $this->post_model->get_total_count();
$config['per_page'] = 1;
$config['uri_segment'] = 1;
$this->pagination->initialize($config);
$data['posts'] = $this->post_model->get_single_post($config['per_page'],$this->uri->segment(1));
$this->load->view('frontend/layouts/header',$data);
$this->load->view('frontend/view_post',$data);
$this->load->view('frontend/layouts/footer');
}
Model
function get_single_post(){
$query= $this->db->select('*')
->from('tbl_post,tbl_users')
->where('tbl_post.post_created=tbl_users.user_id')
->where('tbl_post.post_status',1)
->order_by('tbl_post.post_id','asc')
->get();
$data = $query->result_array();
return $data;
}
function get_total_count(){
$this->db->select('*');
$this->db->from('tbl_post');
$query = $this->db->get();
return $query->num_rows();
}
The relevant page data doesn't show. It shows all of the data in one page!
$config['uri_segment'] = 1 are you sure on this ?? Check this CI example
As you mentioned $config['base_url'] = base_url().'Post/index'; in question $config['uri_segment'] = 1 should be $config['uri_segment'] = 2
As well doo this too
In Controller
$config['uri_segment'] = 1;
$this->pagination->initialize($config);
$data['links'] = $this->pagination->create_links();
$data['posts'] = $this->post_model->get_single_post($config['per_page'],$this->uri->segment(1));
In View
echo $links;
Edit 01
In Model add this
function get_single_post($page,$limit) # Changed
{
$query= $this->db->select('*')
->from('tbl_post,tbl_users')
->where('tbl_post.post_created=tbl_users.user_id')
->where('tbl_post.post_status',1)
->limit($page,$limit) # Changed
->order_by('tbl_post.post_id','asc')
->get();
$data = $query->result_array();
return $data;
}
There are lots of problem.
$config['uri_segment'] cannot be 1. It should be 3
So fix it to
$config['uri_segment']=3;
segment 1 means controller name.
2 means function name.
3 means page number.
if you use 1 it will replace the controller name with page number.
You passing parameter to your model function but your model not receiving the parameters.So your function should be like this
function get_single_post($limit,$page_no){
Your model returning all records not the current page records. So add limit
->limit($page_no,$limit)//remember page_no should be 0 for 1,1 for 2.

Can I have $.get call in $this->pagination->create_links();

I am working on Code igniter (new at codeigniter) and I want to do pagination on $.get.
Contoller Code is here:
public function get_todo($id=null)
{
$this->_required_login();
if($id!=null)
{
$result=$this->todo_model->get([
'todo_id'=>$id,
'user_id'=>$this->session->userdata('user_id')
]);
}
else
{
$config = array();
$config["base_url"] = base_url() . "dashboard/load_todo"; // I want here is $.get instead of a link
//I have js files in which $.get is.
$total= $this->todo_model->get_rows($this->session->userdata('user_id')); //Total rows
$config["total_rows"] = $total;
$config["per_page"] = 3; // Per Page required
// I have no idea what it is.
$config["uri_segment"] = 3;
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
// no idea thing ends here
//getting results and working fine
$data["results"] = $this->todo_model->
fetch_data($config["per_page"], $page);
// Real pain in the neck
$data["links"] = $this->pagination->create_links();
}
$this->output->set_output(json_encode($data));
}
Now about js file which is creating html response
var load_todo = function() {
$.get('api/get_todo',function(o){
//api/get_todo is controller
var output='';
for (var i=0;i<o.results.length;i++)
{
output+=Template.todo(o.results[i]); // Pagination result
}
output+='<p>'+o.links+'</p>'; // Pagination links
//console.log(output);
$("#list_todo").html(output);
},'json');
};
What I want it to have o.links to have $.get.
you forgot to return your output. In your controller method get_todo() edit the last line to this:
return $this->output
->set_content_type('application/json')
->set_output(json_encode($data));
After 3 days of struggle and headache I finally got my answer own my own. For those who might come with same thinking about pagination as I did, if you want to do pagination for ajax call get or post, best way will be to change ajax_pagination library.
ajax_pagination -> create_links() -> getAJAXlink()
and edit its code as u want to.

Codeigniter post method conflicts with pagination

I have a codeigniter search form which includes a dropdown list (Car) and a checkbox array (Car types). I am using POST method to get values from database but post method conflicts with pagination. Could you please check my code and help me to find the mistake.
Here is my controller
public function search($offset = 0) {
$limit = 3;
$this->load->library('form_validation');
$this->load->model('model_x');
$this->form_validation->set_rules('car', 'Car','required');
$this->form_validation->set_rules('types', 'Car Type','required');
if($this->form_validation->run()) {
$car= $this->input->post('car');
$types = $this->input->post('types');
$this->load->library('pagination');
$config['base_url'] = 'http://localhost/abc/cont/search/';
// 'http://localhost/abc' is my base url
$config['total_rows'] = 14;
$config['per_page'] = 3;
$data['pagination'] = $this->pagination->initialize($config);
if ($this->model_x->did_search($car, $types, $limit, $offset)){
$data["results"] = $this->model_x->did_search($car, $types, $limit, $offset);
$this->load->view("search_ok",$data);
}
}
else
{
$data['message'] = 'Please select your options.';
$this->load->view("search_nok",$data);
}
}
Using get parameters for search would be better since besides making pagination easier, it will allow users to access that link directly through Bookmarks or other hyperlinks without needing to go through posting your form every time they need to repeat a "saved" search.
You can use pagination with post method search data php ci
store post data in session and on second page check session with 2?page_rows=5
get from URL.
This will work
if (session_id() && !empty($this->input->get('page_rows', 0))) {
$pData= $this->session->userdata('custumer_data');
....
then query
}

how to pass arguments in codeigniter php controller functions

hi friends i have this function to show all articles, i am writing this function again and again for different categories, because codeigniter arguments are related to url how do i pass arguments so that i can reuse this function ?
This is my controller function to show all news.
function all_news(){
//do some pagination
$this->load->library('pagination');
$config['base_url'] = 'http://localhost/news/all_news';
$config['total_rows'] = $this->db->get('articles')->num_rows();
$config['per_page'] = 10;
$config['num_links'] = 7;
//some css for pagination
$config['full_tag_open'] = '<div id="pagination">';
$config['full_tag_close'] = '</div>';
//initialize pagination
$this->pagination->initialize($config);
//end pagination
$data['title'] =" All News";
//for pagination
$data['query']= $this->db->order_by('id','desc');
$data['query'] = $this->db->get('articles',$config['per_page'],$this->uri->segment(3));
$this->load->vars($data);
$this->load->view('main/all_news');
}
Consider moving much of your controller logic into a model method. Then you'll be able send arguments to this method which will return back database results to your controller based on the arguments you send to the method.
http://example.com/index.php/news/local/metro/crime_is_up
The segment numbers would be this:
1-news
2-local
3-metro
4-crime_is_up
$product_id = $this->uri->segment(3);//metro
full info https://www.codeigniter.com/user_guide/libraries/uri.html

Categories