I dont know why its not working.i followed many articles but it seems my code is total wrong i need to erase all things.<<1,2,3>> this sign is coming but when i click anything only url chng happen nothing else.
This is my controller
function view($offset=0){
$limit=5;
//$this->uri->segment(3);
$this->load->model('emp_expenses_model');
//$result['contents']=$this->emp_expenses_model->getRows($limit,$offset);
$result['countRows']=$this->emp_expenses_model->countRows();
$this->load->library('pagination');
$this->load->library('table');
$config=array(
'base_url' =>site_url ('/view_expenses/view?'),
'total_rows' => $result['countRows'],
'per_page' => $limit,
'uri_segment' => 3,
//'num_links' => 200,
);
//var_dump($config);
$this->db->limit('5');
$this->pagination->initialize($config);
$this->load->model('emp_expenses_model');
$this->data['view_expenses'] = $this->emp_expenses_model->get_all();
//$this->data['pagination'] = $this->pagination->create_links();
//var_dump($this->data['pagination']);die("jk");
$this->data['title'] = 'Payroll System';
$this->data['message'] = $this->session->flashdata('message');
$this->load->library('pagination');
$this->load->view('view_expenses', $this->data);
/*$this->load->view('add_list', $this->data);*/
}
This is my model
<?php
class Emp_expenses_model extends CI_Model
{
function emp_expenses_model()
{
parent::__construct();
}
function get_all()
{
$this->db->select('expenses_id,id,dropdown,modeofpayment,amount');
$query=$this->db->get('emp_expenses');
return $query->result_array();
}
function get_record($list_id)
{
$this->db->select('expenses_id,id, dropdown, modeofpayment, amount');
$this->db->where('id',$list_id);
$query=$this->db->get('emp_expenses');
return $query->result_array();
}
function insert_list($data)
{
$this->db->insert('emp_expenses', $data);
$id = $this->db->insert_id();
return (isset($id)) ? $id : FALSE;
}
public function update_list($list_id, $data)
{
$this->db->where('id', $list_id);
$this->db->update('emp_expenses', $data);
}
public function del_list($list_id)
{
$this->db->where('id', $list_id);
$this->db->delete('emp_expenses');
}
function getRows($limit,$offset)
{
$query=$this->db->select('expenses_id,id,dropdown,modeofpayment,amount')
->from('emp_expenses')
->limit($limit,$offset);
$result=$query->get()->result_array();
return $result;
//var_dump($result);
}
function countRows()
{
//$query="select count(*) as count from emp_expenses";
$result = $this->db->count_all_results('emp_expenses');
//$result=$this->db->query($query);
return $result;
//var_dump('countRows');
}
and in view
*inside the table im writing*
<?php echo $this->pagination->create_links()?>
Try with this, hope it helps:
EDITED:
function view($offset=0){
$limit=5;
$this->load->model('emp_expenses_model');
$countRows=$this->emp_expenses_model->countRows();
$this->load->library('pagination');
$this->load->library('table');
$config=array(
'base_url' =>base_url('/index.php/view_expenses/view/'.$this->uri->segment(3)),
'total_rows' => $countRows,
'per_page' => $limit,
'uri_segment' => 3,
);
$this->pagination->initialize($config);
$this->data['view_expenses'] = $this->emp_expenses_model->getRows($limit,$this->uri->segment(3));
$this->data['title'] = 'Payroll System';
$this->data['message'] = $this->session->flashdata('message');
$this->load->view('view_expenses', $this->data);
}
function in controller
function users(){
if ($this->ion_auth->is_admin() && $this->ion_auth->logged_in()){
$this->load->model('reciever');
$this->load->library('uri');
$this->load->library('pagination');
$config['base_url'] = base_url(). 'users_ci/users';
$config['total_rows'] = $this->reciever->getRows();
$config['per_page'] = 4;
$config['num_links']=2;
$config['users']= $this->reciever->getUsers(4,$this->uri->segment(3));
$this->pagination->initialize($config);
$config['pages'] = $this->pagination->create_links();
$this->load->view('users',$config);
}else{
$this->load->view('login_form');
}
}
This is for the view
<?php echo $pages; ?>
You must query your database exactly with how many you want to saw in the page
Related
I have a paginated list of users where there are total 2 records with a pagination of 2. This means I should get only 1 link in pagination. But I am getting 2 links where 2nd link (i.e 2) should not be rendered.
Below is my Controller and Model code:
class Users extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('users_model');
$this->load->model('roles_model');
$this->load->model('branches_model');
}
public function index()
{
$this->data['page_title'] = 'Users';
$config["base_url"] = base_url() . "admin/users";
$config["total_rows"] = $this->users_model->record_count();
$config["per_page"] = 2;
$config["uri_segment"] = 3;
$this->pagination->initialize($config);
$page = ($this->uri->segment($config["uri_segment"])) ? $this->uri->segment($config["uri_segment"]) : 0;
$this->data['users'] = $this->users_model->get_users(NULL,$config["per_page"], $page);
$this->data["links"] = $this->pagination->create_links();
$this->render('admin/list_users_view');
}
}
class Users_model extends CI_Model {
public function __construct() {
parent::__construct();
$this->load->database();
}
public function record_count() {
return $this->db->count_all("users");
}
public function get_users($UserId = FALSE, $limit = FALSE, $start = FALSE) {
$this->db->select('users.*,roles.RoleName,branches.BranchName');
$this->db->from('users');
$this->db->join('roles', 'users.RoleId = roles.RoleId', 'inner');
$this->db->join('branches', 'users.BranchId = branches.BranchId', 'inner');
$this->db->where('users.BranchId',2);
$filtered_count = $this->db->count_all_results('', false);
$this->db->limit($limit, $start);
$query = $this->db->get()->result();
$data_array = array('total_records' => $filtered_count, 'records' => $query );
return $data_array;
}
}
pagination
Try to reordering the variables on the controllers and use the returned total_records from the model :
public function index()
{
$this->data['page_title'] = 'Users';
$config["base_url"] = base_url() . "admin/users";
$config["per_page"] = 2;
$config["uri_segment"] = 3;
$page = ($this->uri->segment($config["uri_segment"])) ? $this->uri->segment($config["uri_segment"]) : 0;
$this->data['users'] = $this->users_model->get_users(NULL,$config["per_page"], $page);
$config["total_rows"] = $this->data['users']['total_records'];
$this->pagination->initialize($config);
$this->data["links"] = $this->pagination->create_links();
$this->render('admin/list_users_view');
}
I have been trying to add pagination into my code igniter project and despite the fact that the code igniter reference material says it is "easy" I cannot get it to work. I have spent time googling and see similar stuff and I do everything that I find and still nothing works. One of my friends, who is also using codeigniter, tried to help me as well and could not get it to work. I think that I am very close and probably just forgetting everything. Anyways, I am working on a blog and everything works well with the blog except the pagination! I currently have the pagination code in my controller under my index method as such:
public function index()
{
$data['blog'] = $this->Blog_model->get_blog();
$data['title'] = 'Blog archive';
//pagination code
$this->load->library('Pagination');
$config['base_url'] = site_url('blog');
$config['total_rows'] = 1;
$config['per_page'] = 1;
$this->pagination->initialize($config);
$data['Pagination'] = $this->pagination->create_links();
//echo $this->pagination->create_links();
//END Pagination
$this->load->view('templates/header', $data);
$this->load->view('blog/index', $data);
$this->load->view('templates/footer');
}
The whole entire controller looks like this:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Blog extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('Blog_model');
$this->load->helper('url_helper');
}
public function index()
{
$data['blog'] = $this->Blog_model->get_blog();
$data['title'] = 'Blog archive';
//pagination code
$this->load->library('Pagination');
$config['base_url'] = site_url('blog');
$config['total_rows'] = 1;
$config['per_page'] = 1;
$this->pagination->initialize($config);
$data['Pagination'] = $this->pagination->create_links();
//echo $this->pagination->create_links();
//END Pagination
$this->load->view('templates/header', $data);
$this->load->view('blog/index', $data);
$this->load->view('templates/footer');
}
public function view($slug = NULL)
{
$data['blog_item'] = $this->Blog_model->get_blog($slug);
if (empty($data['blog_item']))
{
show_404();
}
$data['title'] = $data['blog_item']['title'];
$this->load->view('templates/header', $data);
$this->load->view('blog/view', $data);
$this->load->view('templates/footer');
}
public function create()
{
$this->load->helper('form');
$this->load->library('form_validation');
$data['title'] = 'Create a news item';
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('body', 'Body', 'required');
if ($this->form_validation->run() === FALSE)
{
$this->load->view('templates/header', $data);
$this->load->view('blog/create');
$this->load->view('templates/footer');
}
else
{
$this->Blog_model->set_blog();
redirect('blog');
//$this->index();
//$this->load->view('templates/header');
//$this->load->view('../../blog/index', $data);
//$this->load->view('templates/footer');
}
}
}
Over on my views, I have this:
<h1>Create A blog Entry</h1>
<?php echo $Pagination; ?>
<div class="blog">
<h2><?php echo "Blog"; ?></h2>
<div class="row">
<?php foreach ($blog as $blog_item): ?>
<div class="col-md-6">
<div class='panel panel-primary'>
<div class='panel-heading'>
<?php echo $blog_item['title']; ?>
</div>
<div class="panel-body">
<?php echo $blog_item['body']; ?>
</div>
<div class="panel-footer">
<?php echo $blog_item['entry_date']; ?>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
</div>
finally, my config/autoload.php file has this:
$autoload['libraries'] = array('database','Pagination');
I do not have anything on my model. If one wants to see the full code it is as my github page under LAMPCAMP_Project and my name there is ravenusmc. Thank you for any help with this and have a great day!
I have added the model code:
<?php
class Blog_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function get_blog($slug = FALSE)
{
if ($slug === FALSE)
{
$query = $this->db->order_by('entry_date', 'desc')->get('blog');
return $query->result_array();
}
$query = $this->db->get_where('blog', array('slug' => $slug));
return $query->row_array();
}
public function set_blog()
{
$this->load->helper('url');
$slug = url_title($this->input->post('title'), 'dash', TRUE);
$data = array(
'title' => $this->input->post('title'),
'slug' => $slug,
'body' => $this->input->post('body')
);
return $this->db->insert('blog', $data);
}
}
I would autoload url helper instead of loading it in model. And remove $this->load->database(); from model if you have already autoload it.
$autoload['helper'] = array('url');
$autoload['libraries'] = array('database','pagination');
Second I would recommend creating a count function for total rows
public function count_total() {
$query = $this->db->get($this->db->dbprefix . 'blog');
return $query->num_rows();
}
For your get function you need a get and limit and offset variable.
public function fetch_blogs($limit, $start, $slug) {
if ($slug == FALSE) {
$this->db->limit($limit, $start);
$this->db->order_by('entry_date', 'desc');
if ($query->num_rows() > 0 {
return $query->result_array();
} else {
return FALSE;
}
}
$this->db->limit($limit, $start);
$query = $this->db->get_where($this->db->dbprefix . 'blog', array('slug' => $slug));
if ($query->num_rows() > 0 {
return $query->result_array();
} else {
return FALSE;
}
}
application/config/routes.php with you blog routes I would think you would need to create I route for pagination in the blog.
$route['blog'] = 'blog/index';
$route['blog/(:any)'] = 'blog/index/$1';
Blog Controller
First off use base_url() insead of site_url()
<?php
class Blog extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('blog_model');
}
public function index() {
$config["base_url"] = base_url('blog');
$config["total_rows"] = $this->blog_model->count_total();
$config["per_page"] = 5; // Change limit to suit what you would like
$config["uri_segment"] = 2;
$this->pagination->initialize($config);
$data['Pagination'] = $this->pagination->create_links();
$start = ($this->uri->segment(2)) ? $this->uri->segment(2) : 0;
// Fetch Blogs
$slug = '' // What ever you need slug to be example uri segment()
$data['blog'] = $this->blog_model->fetch_blogs($config["total_rows"], $start, $slug);
$this->load->view('templates/header', $data);
$this->load->view('blog/blog_view', $data); // change index to different view name
$this->load->view('templates/footer');
}
}
Here are the user guide links for CI2 And CI3
CI3: http://www.codeigniter.com/user_guide/libraries/pagination.html
CI2: http://www.codeigniter.com/userguide2/libraries/pagination.html
I m using codeigniter pagination class, it works fine when a controller first load, on the next load it not change the data.
Here is my controller:
class test extends CI_Controller{
public function __construct() {
parent:: __construct();
$this->load->model("getdata");
$this->load->library("pagination");
}
public function read($page=0){
$this->load->view('header');
$config['base_url'] = base_url()."index.php/test/read";
$config["total_rows"] = $this->getdata->record_count();
$config["per_page"] = 5;
$this->pagination->initialize($config);
$data["results"] = $this->getdata->basic($config["per_page"],$page);
$data["links"] = $this->pagination->create_links();
$this->load->view('readarea',$data);
$this->load->view('footer');
}
};
?>
Here is my Model:
<?php
class getdata extends CI_Model{
public function __construct() {
parent::__construct();
}
public function record_count() {
return $this->db->count_all("table1");
}
public function basic($limit,$start){
$this->db->limit($limit,$start);
$query = $this->db->query("SELECT table1.id,table1.post_id,table1.author,table1.quest,table1.first_name,table2.last_name,table2.pic,table2.userid FROM table1,table2 WHERE table1.post_id = table2.id ORDER BY table1.id DESC");
if($query->num_rows() > 0) {
return $query->result();
}
return FALSE;
}
};
?>
NOTE: Links are created, but not get the next data from database...
Try below query in model.
public function basic($limit,$start){
$query = $this->db->query("SELECT table1.id,table1.post_id,table1.author,table1.quest,table1.first_name,table2.last_name,table2.pic,table2.userid FROM table1,table2 WHERE table1.post_id = table2.id ORDER BY table1.id DESC LIMIT $start, $limit");
if($query->num_rows() > 0) {
return $query->result();
}
return FALSE;
}
change this function to this
public function read($page = null){
$this->load->view('header');
$config['base_url'] = base_url()."index.php/test/read";
$config["total_rows"] = $this->getdata->record_count();
$config["per_page"] = 5;
$this->pagination->cur_page = $page; // add this line into it i hope this work
$this->pagination->initialize($config);
$data["results"] = $this->getdata->basic($config["per_page"],$page);
$data["links"] = $this->pagination->create_links();
$this->load->view('readarea',$data);
$this->load->view('footer');
}
};
I am using CodeIgniter and have completed the news tutorial. I am trying to add a delete and update button within the news section. I have seen your post on stackoverflow about the delete button on this tutorial but i still cant get it working.
Here is my coding that i have used from your post
news_model.php
<?php
class News_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function get_news($slug = FALSE)
{
if ($slug === FALSE)
{
$query = $this->db->get('news');
return $query->result_array();
}
$query = $this->db->get_where('news', array('slug' => $slug));
return $query->row_array();
}
public function set_news()
{
$this->load->helper('url');
$slug = url_title($this->input->post('title'), 'dash', TRUE);
$data = array(
'title' => $this->input->post('title'),
'slug' => $slug,
'text' => $this->input->post('text')
);
return $this->db->insert('news', $data);
}
public function delete_news($id) {
$this->db->delete('news', array('id' => $id));
}
}
CONTROLLER - news.php
<?php
class News extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('news_model');
}
public function index()
{
$data['news'] = $this->news_model->get_news();
$data['title'] = 'News archive';
$this->load->view('templates/header', $data);
$this->load->view('news/index', $data);
$this->load->view('templates/footer');
}
public function view($slug)
{
$data['news'] = $this->news_model->get_news($slug);
if (empty($data['news_item']))
{
show_404();
}
$data['title'] = $data['news_item']['title'];
$this->load->view('templates/header', $data);
$this->load->view('news/view', $data);
$this->load->view('templates/footer');
}
public function create()
{
$this->load->helper('form');
$this->load->library('form_validation');
$data['title'] = 'Create a news item';
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('text', 'text', 'required');
if ($this->form_validation->run() === FALSE)
{
$this->load->view('templates/header', $data);
$this->load->view('news/create');
$this->load->view('templates/footer');
}
else
{
$this->news_model->set_news();
$this->load->view('news/success');
}
}
public function delete($id) {
$this->news_model->delete_news($id);
$this->load->helper('url');
redirect('/www.shelim786.co.uk/CodeIgniter/news');
}
}
VIEWS - index.php
<?php foreach ($news as $news_item): ?>
<h2><?php echo $news_item['title'] ?></h2>
<div id="main">
<?php echo $news_item['text'] ?>
</div>
<p>View article</p>
<p>delete article</p>
<?php endforeach ?>
and ROUTING
$route['news/create'] = 'news/create';
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
$route['news/delete/(:any)'] = 'news/delete/$1';
The problem i am having is that the news article in www.shelim786.co.uk/CodeIgniter/index.php/news
doesnt delete when i click the delete button.
i would really appreciate if you could help me.
Thanks,
Shelim
You don't have slash after the word 'delete' in the link. Your link gets displayed something like http://www.shelim786.co.uk/CodeIgniter/index.php/news/delete 6. Notice the space after 'delete'?
In your view, change the 'delete article' link line to this.
<p>delete article</p>
Notice the slash after 'delete'?
you should redirect your delete link delete_news function
not delete function as the delete query is written there
I try add example from http://ellislab.com/codeigniter/user_guide/tutorial/news_section.html
to my site, this is code:
news_model
<?php
class News_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function get_news($art = FALSE)
{
if ($art === FALSE)
{
$query = $this->db->get('news');
return $query->result_array();
}
$query = $this->db->get_where('news', array('art' => $art));
return $query->row_array();
}
public function set_news()
{
$this->load->helper('url');
$slug = url_title($this->input->post('title'), 'dash', TRUE);
$data = array(
'title' => $this->input->post('title'),
'art' => $art,
'text' => $this->input->post('text'),
'image'=> $image
);
return $this->db->insert('news', $data);
}
}
?>
news_controller
<?php
class News extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('news_model');
}
public function index()
{
$session_data = $this->session->userdata('logged_in');
$data['username'] = $session_data['username'];
$data['errors_login'] = array();
$data['news'] = $this->news_model->get_news();
$data['title'] = 'News archive';
$this->load->view('main/open_news', $data);
}
public function view($art) {
$data['news_item'] = $this->news_model->get_news($art);
if (empty($data['news_item']))
{
show_404();
}
$data['title'] = $data['news_item']['title'];
$session_data = $this->session->userdata('logged_in');
$data['username'] = $session_data['username'];
$data['errors_login'] = array();
$this->load->view('main/open_one_news', $data);
}
}
open_news
<?php
$this->load->view('mains/header');
$this->load->view('main/news');
$this->load->view('mains/footer');
?>
news view
<?php foreach ($news as $news_item): ?>
<h2><?php echo $news_item['title'] ?></h2>
<div id="main">
<?php echo $news_item['text'] ?>
</div>
View article
<?php endforeach ?>
And when I click in View article
The page is not forwarding to concret page with news, only in link duplicate "news":
http://localhost/index.php/news/news/news/news/news/news
I dont know what is problem. But I think it will by may in routes.config
Because I have only:
$route['default_controller'] = 'login'; -> this is my start page
But in CodeIgniter tutorial is:
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
But when I add some from 3 first line, even the first page with list of news doesn`t open.
SOLVED:
I make stupid mistake. Because controller name news, but function: public function view($art), and the link should be: 'news/view/'.$news_item['art'].
I think problem with the below link
View article
use codeigniter anchor instead
anchor('news/'.$news_item['art'],'View article');
try this and feed me back
You forgot the echo at:
View article
You should use:
View article
or:
<?php echo anchor('news/' . $news_item['art'], 'View article');
or:
<?php echo anchor("news/{$news_item['art']}", 'View article');