I put my problem, I created a table "comments" then I maked a controller which two functions are implemented, function for reading and the one other for insertion.
What I need now is to realize function for managing comments, ie beside each comments there is an options delete and modify.
So,I add a new function that I named manage (), then, to avoid some repetition I think keep read (used to list the comments) and using it into manage() then add these stufs(modify,delete) beside each comment.
But I don't know how to proceed?
I need your advice help!
<?php
class Site extends CI_Controller {
public function __construct()
{
parent::__construct();
// Chargement des ressources pour tout le contrĂ´leur
$this->load->database();
$this->load->helper('url');
$this->load->model('comments_model', 'livreorManager');
}
function index() {
$this->read();
// $this->add();
}
function read() {
$this->load->library('pagination');
$this->load->library('table');
$this->load->library('mytable');
$config['base_url'] = 'http://localhost/ci/index.php/site/index';
$config['total_rows'] = $this->db->get('comments')->num_rows();
$config['per_page'] = 5;
$config['num_links'] = 20;
$tmpl = array ( 'table_open' => '<table class="table table-striped">' );
$this->table->set_template($tmpl);
$config['full_tag_open'] = '<div class="pagination"><ul>';
$config['full_tag_close'] = '</ul></div>';
$this->pagination->initialize($config);
$this->db->order_by("id", "desc");
$data['records'] = $this->db->get('comments',$config['per_page'],$this->uri->segment(3));
$data['donnees'] = $data['records']->result_array();
$this->load->view('site_view', $data);
}
function add() {
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<p class="form_erreur">', '</p>');
$this->form_validation->set_rules('pseudo', '"Pseudo"', 'trim|required|min_length[3]|max_length[25]|alpha_dash');
$this->form_validation->set_rules('comment', '"Comment"', 'trim|required|min_length[3]|max_length[3000]');
if($this->form_validation->run()) {
$this->livreorManager->add_comment($this->input->post('pseudo'),$this->input->post('comment'));
$this->load->view('success');
$this->read();
}
else {
$this->load->view('add');
}
}
function manage() {
// implément options into read
$this->read();
}
}
Related
My pagination of search results is not working. It's showing result but pagination making it random. Please help me. Search is working but can't apply pagination in it, it's getting complicated
search_products_model
<?php
class search_products_model extends CI_Model {
public function __construct()
{
parent::__construct();
}
public function get_results($search_term,$limit, $offset)
{
// Use the Active Record class for safer queries.
$this->db->like('name',$search_term);
// Execute the query.
$query = $this->db->get('tbl_products',$limit, $offset);
if ( $query->num_rows() > 0 )
{
return $query->result_array();
}
else
{
return FALSE;
}
// Return the results.
}
function get_products_id($id)
{
$this->db->where('prod_id',$id);
$query = $this->db->get('tbl_products');
return $query->result();
}
}
?>
controller
<?php
class search_products extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper(array('form','url'));
$this->load->library(array('session', 'form_validation', 'email','pagination'));
$this->load->database();
$this->load->model('search_products_model');
}
function index()
{
$this->search_products();
$this->load->view('search_products_view');
}
function display_registration_form()
{
$this->load->view('search_products_view');
}
function execute_search($offset = 0)
{
$config['base_url'] = '/mehmaa/index.php/search_products/execute_search/';
$config['total_rows'] = $this->db->count_all_results('tbl_products');
$config['per_page'] = 6;
$config['next_link'] = 'Next';
$config['prev_link'] = 'Previous';
$this->pagination->initialize($config);
// Retrieve the posted search term.
$id = $this->input->post('id');
$search_term = $this->input->post('term');
// Use a model to retrieve the results.
$data['results'] = $this->search_products_model->get_results($search_term,$config['per_page'], $offset);
if ( empty($data['results']) )
{
$this->load->view('search_products_view');
$this->session->set_flashdata('message', 'No results found');
}
else{
// Pass the results to`enter code here` the view.
$this->load->view('search_results',$data);
}
}
hi in this link you are not checking the search condition
$config['total_rows'] = $this->db->count_all_results('tbl_products');
change this line to
$config['total_rows'] = $this->db->count_all_results($search_term);
and write the proper query
$config['total_rows'] = $this->db->count_all_results('tbl_products');
Problem in the this files, u need write another query which will be collect result by search without offsets and limits.
For example
function search($text, $offset, $limits){
//this u do search
}
function count_search($text){
//This same logic with search but return Integer value for example
//return $this->db->query()->num_rows();
}
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
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I am stuck with this problem.
I get 404 error when try to access admin pages in codeigniter, I can only access the fronted pages.
Here you have my route.php file:
$route['default_controller'] = "page";
$route['404_override'] = '';
$route[':any'] = "page/index/$1"; **WORKS**
$route['admin/page'] = "admin/page"; **WORKS**
$route['admin/page/order'] = "admin/page/order"; **NOT WORKING** <!-- this i my issue -->
$route['admin/page/edit'] = "admin/page/edit"; **WORKS**
$route['admin/page/edit/(:num)'] = "admin/page/edit/$1"; **NOT WORKING** <!-- this i my issue -->
Admin Controller
class Admin_Controller extends MY_Controller
{
function __construct ()
{
parent::__construct();
$this->data['meta_title'] = 'My awesome CMS';
$this->load->helper('form');
$this->load->library('form_validation');
$this->load->library('session');
$this->load->model('user_m');
// Login check
$exception_uris = array(
'admin/user/login',
'admin/user/logout'
);
if (in_array(uri_string(), $exception_uris) == FALSE) {
if ($this->user_m->loggedin() == FALSE) {
redirect('admin/user/login');
}
}
}
}
Frontend pages format
http://www.my_site.com/index.php/about
Backend format
http://www.my_site.com/admin/page
http://www.my_site.com/index.php/admin/page/edit/1
Please, can anyone help me?
Thanks in advance.
Guys thanks for the reply
Here you have my Page Admin Controller and also I have amended the route.php above.
<?php
class Page extends Admin_Controller
{
public function __construct ()
{
parent::__construct();
$this->load->model('page_m');
}
public function index ()
{
// Fetch all pages
$this->data['pages'] = $this->page_m->get_with_parent();
// Load view
$this->data['subview'] = 'admin/page/index';
$this->load->view('admin/_layout_main', $this->data);
}
public function order ()
{
$this->data['sortable'] = TRUE;
$this->data['subview'] = 'admin/page/order';
$this->load->view('admin/_layout_main', $this->data);
}
public function order_ajax ()
{
// Save order from ajax call
if (isset($_POST['sortable'])) {
$this->page_m->save_order($_POST['sortable']);
}
// Fetch all pages
$this->data['pages'] = $this->page_m->get_nested();
// Load view
$this->load->view('admin/page/order_ajax', $this->data);
}
public function edit ($id = NULL)
{
// Fetch a page or set a new one
if ($id) {
$this->data['page'] = $this->page_m->get($id);
count($this->data['page']) || $this->data['errors'][] = 'page could not be found';
}
else {
$this->data['page'] = $this->page_m->get_new();
}
// Pages for dropdown
$this->data['pages_no_parents'] = $this->page_m->get_no_parents();
// Set up the form
$rules = $this->page_m->rules;
$this->form_validation->set_rules($rules);
// Process the form
if ($this->form_validation->run() == TRUE) {
$data = $this->page_m->array_from_post(array(
'title',
'slug',
'body',
'template',
'parent_id'
));
$this->page_m->save($data, $id);
redirect('admin/page');
}
// Load the view
$this->data['subview'] = 'admin/page/edit';
$this->load->view('admin/_layout_main', $this->data);
}
public function delete ($id)
{
$this->page_m->delete($id);
redirect('admin/page');
}
public function _unique_slug ($str)
{
// Do NOT validate if slug already exists
// UNLESS it's the slug for the current page
$id = $this->uri->segment(4);
$this->db->where('slug', $this->input->post('slug'));
! $id || $this->db->where('id !=', $id);
$page = $this->page_m->get();
if (count($page)) {
$this->form_validation->set_message('_unique_slug', '%s should be unique');
return FALSE;
}
return TRUE;
}
}
Front End Page Controller
<?php
class Page extends Frontend_Controller{
public function __construct(){
parent::__construct();
$this->load->model('page_m');
}
public function index(){
// Fetch the page template
$this->data['page'] = $this->page_m->get_by(array('slug' => (string) $this->uri->segment(1)), TRUE);
count($this->data['page']) || show_404(current_url());
// Fetch the page data
$method = '_' . $this->data['page']->template;
if (method_exists($this, $method)) {
$this->$method();
}
else {
log_message('error', 'Could not load template ' . $method .' in file ' . __FILE__ . ' at line ' . __LINE__);
show_error('Could not load template ' . $method);
}
//Fetch the view
$this->data['subview'] = $this->data['page']->template;
$this->load->view('_main_layout', $this->data);
}
private function _page(){
dump('welcome from the page template');
}
private function _homepage(){
$this->load->model('article_m');
$this->db->where('pubdate <=', date('Y-m-d'));
$this->db->limit(6);
$this->data['articles'] = $this->article_m->get();
}
private function _news_archive(){
$this->load->model('article_m');
// Count all articles
$this->db->where('pubdate <=', date('Y-m-d'));
$count = $this->db->count_all_results('articles');
// Set up pagination
$perpage = 4;
if ($count > $perpage) {
$this->load->library('pagination');
$config['base_url'] = site_url($this->uri->segment(1) . '/');
$config['total_rows'] = $count;
$config['per_page'] = $perpage;
$config['uri_segment'] = 2;
$this->pagination->initialize($config);
$this->data['pagination'] = $this->pagination->create_links();
$offset = $this->uri->segment(2);
}
else {
$this->data['pagination'] = '';
$offset = 0;
}
// Fetch articles
$this->db->where('pubdate <=', date('Y-m-d'));
$this->db->limit($perpage, $offset);
$this->data['articles'] = $this->article_m->get();
}
}
Hope you can have help me
Thanks
I dont see any routes nor URLs that point to your Admin_Controller . If a user types http://.../admin/page Codeigniter will try to look for a controller named Admin not Admin_controller unless you have routes set up.
Also, you have this route: $route[':any'] = "page/index/$1"; This route will take any URL given and redirect the user to the Page controller (which you have not provided any code on). I would get rid of it, or what is its function?
What you need to decide is whether your admin URL should be this: http://www.my_site.com/admin/page
or this: http://www.my_site.com/admin_controller/page
Personally I would choose the first one since it looks cleaner. Which then means you have to decide whether your controller should stay named as Admin_Controller or just Admin. I would chose Admin so that you dont have to deal with routes.
The final result should be this:
Your admin URL: http://www.my_site.com/admin/page
Your Controller: application/controllers/Admin.php
class Admin extends MY_Controller {
public function __construct() {
//... your code here
}
public function page() {
//... your code here
}
}
You don't seem to have
function index()
in your admin controller.
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
I have a little problem with pagination class of codeigniter
Here is my controller
function index()
{
$this->load->library('pagination');
$config['base_url'] = base_url().'index.php/books/index/';
$config['total_rows'] = $this->db->count_all('books');
$config['per_page'] = '5';
$config['full_tag_open'] = '<p>';
$config['full_tag_close'] = '</p>';
$this->pagination->initialize($config);
echo "Here";
//load the model and get results
$this->load->model('books_model');
$data['results'] = $this->books_model->get_books($config['per_page'],$this->uri->segment(3));
// load the HTML Table Class
$this->load->library('table');
$this->table->set_heading('Title');
// load the view
$this->load->view('books_view', $data);
}
and here is the view
<body>
<h1>Books</h1>
<?php echo $this->table->generate($results); ?>
<?php echo $this->pagination->create_links(); ?>
</body>
and the model
function get_books($num, $offset) {
$query = $this->db->get('books', $num, $offset);
return $query;
}
I setup my database, and routes (default->books) already but the page doesn't show anything. My database table is books.
I don't know if $config is a variable of your own class. If it is the global config, maybe your should set its parameters using $this->config->set_item function.
Furthermore, to avoid troubles like #Henesnarfel mentioned before, try to echo the number of results when you execute the query.
function index()
{
$this->load->library('pagination');
$this->config->set_item('base_url',base_url().'index.php/books/index/');
$this->config->set_item('total_rows',$this->db->count_all('books'));
// ECHO to make me sure that the query is succesfully done
echo("Found ".$this->db->count_all('books')." books");
$this->config->set_item('per_page','5');
$this->config->set_item('full_tag_open','<p>');
$this->config->set_item('full_tag_close','</p>');
$this->pagination->initialize($config);
echo "Here";
//load the model and get results
$this->load->model('books_model');
$data['results'] = $this->books_model->get_books($config['per_page'],$this->uri->segment(3));
// load the HTML Table Class
$this->load->library('table');
$this->table->set_heading('Title');
// load the view
$this->load->view('books_view', $data);
}