how to fix the paggination problem in codeigniter - codeigniter

I wan't to create pagination in my project, but I have some problems.
I'm using a .htaccess in CodeIgniter. It makes my url not use index.php
and then I have created the pagination rules in the controller: $config['base_url'] = 'http://localhost/project/member'.
It makes my project unable to use the pagination, but if I use $config['base_url'] = 'http://localhost/project/member/index', the pagination works.
The case is my page can't show the css program, because I'm using bootstrap in this case, how can I fix the problem when I'm using the "index" and not using "index" in the base_url?

i've clear the problem
this is a silly mistake
in the header, i not make the link using <?= base_url('')?>
then, i use all <link ref to the css using <?= base_url('')?>, well done
its work
thanks for all respon here

Use below code, it will work
$this->load->library("pagination");
$config = array();
$config["base_url"] = base_url() . "review";
$config["total_rows"] = $this->blog->getReviewCount();
$config["per_page"] = 10;
$config["uri_segment"] = 2;
$config['full_tag_open'] = "<ul class='pagination'>";
$config['full_tag_close'] = '</ul>';
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$config['cur_tag_open'] = '<li class="active"><a href="#">';
$config['cur_tag_close'] = '</a></li>';
$config['prev_tag_open'] = '<li>';
$config['prev_tag_close'] = '</li>';
$config['first_tag_open'] = '<li>';
$config['first_tag_close'] = '</li>';
$config['last_tag_open'] = '<li>';
$config['last_tag_close'] = '</li>';
$config['prev_link'] = 'Previous';
$config['prev_tag_open'] = '<li>';
$config['prev_tag_close'] = '</li>';
$config['next_link'] = 'Next';
$config['next_tag_open'] = '<li>';
$config['next_tag_close'] = '</li>';
$this->pagination->initialize($config);
$page = ($this->uri->segment(2)) ? $this->uri->segment(2) : 0;
$data['title'] = "Review";
$data["links"] = $this->pagination->create_links();
$data['review'] = $this->blog->getReviews($config["per_page"], $page);
$this->load->view("review", $data);

#Lin when you take out index.php from $config[base_url] you will need to place an .htaccess file on your root folder(I mean where your index.php file is found)
Create a .htacces file and put in the code below:
RewriteEngine On
#RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php/$1
When it doesn't fix it let me know. It might be your pagination configuration is not set well. Looking at #Justin-j gave it should have worked.

in this case, can i access using different url with same page?
example:
localhost/project/index.php/member
localhost/project/member
with use the .htaccess like this in my root folder
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

Related

code igniter redirect page not working as expected

if(){
redirect('Login');
}
I have a condition above that redirect the page to log in depending on some values. I can see in url that I get redirect because when I enter
http://www.myapp.com it changes to http://www.myapp.com/Login
Now In my route I have:
$route['default_controller'] = 'welcome';
$route['Login'] = 'login';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
My Login controller looks like:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Login extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->library('session');
$this->load->model('Login_model');
}
public function index(){
$data['title'] = 'GYM';
$this->load->view('login',$data);
}
}
In my view I have
But why Im getting
Object not found!
The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again.
If you think this is a server error, please contact the webmaster.
Error 404
www.myapp.com
Apache/2.4.33 (Win32) OpenSSL/1.1.0h PHP/7.2.7
I cant figure out where I got configuration wrong
Update:
change config file:
$config['base_url'] = 'http://'.$_SERVER['HTTP_HOST'].'';
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';//'AUTO';
$config['url_suffix'] = '';
$config['language'] = 'english';
$config['charset'] = 'UTF-8';
$config['enable_hooks'] = FALSE;
$config['subclass_prefix'] = 'MY_';
$config['composer_autoload'] = FALSE;
$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-';
htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
and
RewriteEngine on
RewriteCond $1 !^(index\.php|[Javascript / CSS / Image root Folder name(s)]|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
accessing:
http://www.myapp.com/index.php/Login
Works but all the above does not. I not getting why
Try including below lines on the .htaccess at the root level of the codeigniter app folder :
RewriteCond $1 !^(index\.php)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?/$1 [L,QSA]
change your value like following
$route['login'] = 'Login';
$config['base_url'] = 'http://'.$_SERVER['HTTP_HOST'].'/';
if not work, replace this code in htaccess
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]

Codeigniter 3 pagination links and single post link throw 404

I have read other articles on this from here but could not solve this problem. My problems are 2.
Pagination shows but clicking links of pagination generates 404.
I have list of articles in db that are being displayed but when I
click single article link for single article page, it shows 404.
I am on localhost wamp. Here is my code. The commented code is what I have been trying but did not work for me.
.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
</IfModule>
routes.php
$route['default_controller'] = 'home';
$route['404_override'] = 'not_found';
$route['translate_uri_dashes'] = FALSE;
$route['articles/(:any)'] = 'articles/$1';
//$route['articles'] = 'articles';
config / baseurl
$config['base_url'] = 'http://localhost/practice/project-code-igniter/';
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';
Controller - Articles.php
class Articles extends CI_Controller {
public function index($start=0)
{
// $this->output->cache('86400');
$this->load->view('header');
//load model
// $this->load->model('articles_model');
// load 'get_articles' function from 'articles_model' model and store it in data
$this->load->library('pagination');
$data['articles']=$this->articles_model->get_articles(5,$start);
$config['base_url'] = base_url().'articles/';
$config['total_rows'] = $this->db->get('articles')->num_rows();
$config['per_page'] = 5;
$config["uri_segment"] = 3;
$config['use_page_numbers'] = TRUE;
$config['num_links'] = 2; //NUMBER OF LINKS BEFORE AND AFTER CURRENT PAGE IF ON PAGE ONE WILL SHOW 4 PAGES AFTERWARDS IF YOU HAVE ENOUGH RESULTS TO FILL THAT MANY
//config for bootstrap pagination class integration
$config['full_tag_open'] = '<ul class="pagination">';
$config['full_tag_close'] = '</ul>';
$config['first_link'] = "<< First";
$config['last_link'] = "Last >>";
$config['first_tag_open'] = '<li>';
$config['first_tag_close'] = '</li>';
$config['prev_link'] = '&laquo';
$config['prev_tag_open'] = '<li class="prev">';
$config['prev_tag_close'] = '</li>';
$config['next_link'] = '&raquo';
$config['next_tag_open'] = '<li>';
$config['next_tag_close'] = '</li>';
$config['last_tag_open'] = '<li>';
$config['last_tag_close'] = '</li>';
$config['cur_tag_open'] = '<li class="active"><a href="#">';
$config['cur_tag_close'] = '</a></li>';
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$this->pagination->initialize($config);
//$data['pages'] = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
//$page = ($this->uri->segment(3))? $this->uri->segment(3) : 0;
// //call the model function to get the department data
//$data['pages'] = $this->articles_model->get_articles($config["per_page"], $data['page']);
$data['pages'] = $this->pagination->create_links();
//$this->load->view('page_articles');
// load view and load model data with it
$this->load->view('page_articles',$data);
$this->load->view('footer');
}
// single article
// function post($perma){
// $this->load->view('header');
// $this->load->model('articles_model');
// $data['articles']=$this->articles_model->get_single_article($perma);
// $this->load->view('page_article',$data);
// $this->load->view('footer');
// }
}
Model / Articles_model
class Articles_model extends CI_Model {
// function get_articles() {
public function get_articles($limit, $start) {
// $query = $this->db->query("SELECT * FROM articles");
// $this->db->select()->from('articles');
// $this->db->select()->from('articles')->where('active',1)->order_by('date_added','desc')->limit(0,20);
$this->db->select()->from('articles')->where('status','1')->order_by('date_added','desc')->limit($limit, $start);
$query = $this->db->get();
// return object
// return $query->result();
// return array
return $query->result_array();
}
function get_articles_count(){
$this->db->select('id')->from('articles')->where('status',1);
$query = $this->db->get();
return $query->num_rows();
}
// for single article
// function get_single_article($perma){
// $this->db->select()->from('articles')->where(array('status'=>1,'permalink'=>$perma));
// $query = $this->db->get();
// return $query->first_row('array');
// }
}
View / page_articles
<?php
if(!isset($articles)) { ?> <div class="alert alert-info">No records</div> <?php } else
{
?>
<ol>
<?php
foreach($articles as $row)
{ ?>
<li>
<h4><a href="<?php echo base_url(); ?>articles/<?php echo $row['id']; ?>/<?php echo $row['permalink']; ?>/">
<?php echo $row['name']; ?>
</a></h4>
<p><?php echo substr(strip_tags($row['detail']),0,100).".."; ?>
<br>
Read more
</p>
<br>
</li>
<?php } ?>
</ol>
<?php
}
?>
</p>
<div>
<?php echo $pages; ?>
</div>
One problem...
public function index($start=0)
you seem to use $start for your page number and again for your offset value...
$data['articles']=$this->articles_model->get_articles(5,$start);
If $start is representing your page number, then you cannot also use it for your offset value.
Your offset needs to be calculated...
$offset = ($start - 1) * 5;
$data['articles']=$this->articles_model->get_articles(5, $offset);
Example:
With 5 records per page, page 4 would start with record #16. So 15 is the correct offset to get records #16, 17, 18, 19, & 20 for page 4.
Another potential issue...
$config["uri_segment"] = 3;
You've set the page number as segment 3, but your route is showing the page number at segment 2...
$route['articles/(:any)'] = 'articles/$1';
It also does not match this setting, which would also put your page number at segment 2...
$config['base_url'] = base_url().'articles/';
Based on your comment,
waow. What I did just now... I changed $config['base_url'] = base_url().'articles/'; to $config['base_url'] = base_url().'articles/index/'; and the pagination links show the page now but records are not changing. The 404 goes away and page is changing - records are first 5 on all pages....
then the route should be this...
$route['articles/index/(:any)'] = 'articles/$1';
So it matches the new base_url you describe...
$config['base_url'] = base_url().'articles/index/';

upload project in web host and 404 page not found

i have problem with codeigniter 3 , the project work fine in localhost , but when i upload my project in host , i see
404 Page Not Found
The page you requested was not found.
i remove .httaccess file , remove all of my routes , but dont work.
i have two controller :
class test extends CI_Controller {}
class testen extends CI_Controller {}
my htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
# !IMPORTANT! Set your RewriteBase here and don't forget trailing and leading
# slashes.
# If your page resides at
# http://www.example.com/mypage/test1
# then use
# RewriteBase /mypage/test1/
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule .* http://%1%{REQUEST_URI} [R=301,L]
</IfModule>
<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin
ErrorDocument 404 /index.php
</IfModule>
edit:
my routes:
$route['default_controller'] = 'isogamsharghedonya';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route["en"] = "isogamsharghedonyaen";
$route["fa"] = "isogamsharghedonya";
$route["en"] = "isogamsharghedonyaen/index";
$route["en/isogam-sharghe-donya-service"] = "isogamsharghedonyaen/service";
$route["en/isogam-sharghe-donya-news"] = "isogamsharghedonyaen/news";
$route["en/isogam-sharghe-donya-tvc"] = "isogamsharghedonyaen/video";
$route["en/isogam-sharghe-donya-certificate"] = "isogamsharghedonyaen/certificate";
$route["en/isogam-sharghe-donya-honor"] = "isogamsharghedonyaen/honor";
$route["en/isogam-sharghe-donya-ceo"] = "isogamsharghedonyaen/aboutCeo";
$route["en/isogam-sharghe-donya-about-company"] = "isogamsharghedonyaen/aboutCompany";
$route["en/isogam-sharghe-donya-team"] = "isogamsharghedonyaen/aboutTeam";
$route["en/isogam-sharghe-donya-contact"] = "isogamsharghedonyaen/contact";
$route["en/send-message"] = "isogamsharghedonyaen/sendMail";
$route["en/insulation-orders"] = "isogamsharghedonyaen/order";
$route["en/new-insulation-orders"] = "isogamsharghedonyaen/newOrder";
$route["fa"] = "isogamsharghedonya/index";
$route["fa/" . rawurlencode("خدمات-ایزوگام-شرق-دنیا")] = "isogamsharghedonya/service";
$route["fa/" . rawurlencode("اخبار-ایزوگام-شرق-دنیا")] = "isogamsharghedonya/news";
$route["fa/" . rawurlencode("تیزر-های-تبلیغاتی-ایزوگام-شرق-دنیا")] = "isogamsharghedonya/video";
$route["fa/" . rawurlencode("گواهینامه-ایزوگام-شرق-دنیا")] = "isogamsharghedonya/certificate";
$route["fa/" . rawurlencode("افتخارات-و-جوایز-ایزوگام-شرق-دنیا")] = "isogamsharghedonya/honor";
$route["fa/" . rawurlencode("مدیر-عامل-ایزوگام-شرق-دنیا")] = "isogamsharghedonya/aboutCeo";
$route["fa/" . rawurlencode("درباره-شرکت-ایزوگام-شرق-دنیا")] = "isogamsharghedonya/aboutCompany";
$route["fa/" . rawurlencode("تیم-ایزوگام-شرق-دنیا")] = "isogamsharghedonya/aboutTeam";
$route["fa/" . rawurlencode("تماس-با-ایزوگام-شرق-دنیا")] = "isogamsharghedonya/contact";
$route["fa/" . rawurlencode("ارسال-پیام")] = "isogamsharghedonya/sendMail";
$route["fa/" . rawurlencode("سفارش-ایزوگام")] = "isogamsharghedonya/order";
$route["fa/" . rawurlencode("سفارش-جدید-ایزوگام")] = "isogamsharghedonya/newOrder";
require_once (BASEPATH . 'database/DB' . EXT);
require_once (BASEPATH . 'helpers/url_helper' . EXT);
require_once (BASEPATH . 'helpers/text_helper' . EXT);
$db = &DB();
$query = $db -> get('news');
$result = $query -> result();
foreach ($result as $row) {
$string = rawurlencode(str_replace(' ', '-', strtolower($row -> subjectFA)));
$route["fa/news/" . $string] = "isogamsharghedonya/newsDetails/$row->id";
}
$query = $db -> get('news');
$result = $query -> result();
foreach ($result as $row) {
$string = rawurlencode(str_replace(' ', '-', strtolower($row -> subjectEN)));
$route["en/news/" . $string] = "isogamsharghedonyaen/newsDetails/$row->id";
}
$query = $db -> get('product');
$result = $query -> result();
foreach ($result as $row) {
$string = rawurlencode(str_replace(' ', '-', strtolower($row -> nameFA)));
$route["fa/product/" . $string] = "isogamsharghedonya/productDetails/$row->id";
}
$query = $db -> get('product');
$result = $query -> result();
foreach ($result as $row) {
$string = rawurlencode(str_replace(' ', '-', strtolower($row -> nameEN)));
$route["en/product/" . $string] = "isogamsharghedonyaen/productDetails/$row->id";
}
$query = $db -> get('trailer');
$result = $query -> result();
foreach ($result as $row) {
$string = rawurlencode(str_replace(' ', '-', strtolower($row -> nameFA)));
$route["fa/video/" . $string] = "isogamsharghedonya/videoDetails/$row->id";
}
$query = $db -> get('trailer');
$result = $query -> result();
foreach ($result as $row) {
$string = rawurlencode(str_replace(' ', '-', strtolower($row -> nameEN)));
$route["en/video/" . $string] = "isogamsharghedonyaen/videoDetails/$row->id";
}
Your controller and model files must begin with a capital letter.
An example: controllers/Index.php, models/Index_model.php
code in CI 3 core for controllers load (/system/core/router.php):
protected function _set_default_controller()
{
//... blah-blah-blah ...
if ( ! file_exists(APPPATH.'controllers/'.$this->directory.ucfirst($class).'.php'))
{
// This will trigger 404 later
return;
}
//...blah-blah-blah...
}
It work in localhost, because you system is Windows. Unix like systems is case sensitive in work with files.
As i reviewed your code the first thing that you have rectify is that the way you have declare your controller since it is a good practice to define your controller as class Test extends CI_Controller instead of class test extends CI_Controller as you have declared some time it might create error and the second thing is that you have to check your .htaccess file is uploaded to your root directory of the project and as well as you also check in your routes file whether you have written the appropriate routing rule for your declared controller as well as the is the method is present in your class which is defined in your routes for the same

number of rows dynamic in codeigniter pagination links

I am working on Codeigniter (Gocart) Project.I am stuck on one task,I need your help.
I want to show option to select number of records per page.By default controller index action is as follows
function index($order_by="id", $sort_order="DESC", $code=0, $page=0, $rows=15)
and I am having dropdown in views for number of rows.
then in controller I have written a condition.
if(isset($_REQUEST['num_rows'])){
$rows = $_REQUEST['num_rows'];
}
else{
$rows = $rows;
}
and this is the codeigniter pagination code of controller.
$this->load->library('pagination');
$config['base_url'] = site_url($this->config->item('admin_folder').'/products/index/'.$order_by.'/'.$sort_order.'/'.$code);
$config['total_rows'] = $data['total'];
$config['per_page'] = $rows;
$config['uri_segment'] = 7;
$config['first_link'] = 'First';
$config['first_tag_open'] = '<li>';
$config['first_tag_close'] = '</li>';
$config['last_link'] = 'Last';
$config['last_tag_open'] = '<li>';
$config['last_tag_close'] = '</li>';
$config['full_tag_open'] = '<div class="pagination"><ul>';
$config['full_tag_close'] = '</ul></div>';
$config['cur_tag_open'] = '<li class="active"><a href="#">';
$config['cur_tag_close'] = '</a></li>';
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$config['prev_link'] = '«';
$config['prev_tag_open'] = '<li>';
$config['prev_tag_close'] = '</li>';
$config['next_link'] = '»';
$config['next_tag_open'] = '<li>';
$config['next_tag_close'] = '</li>';
$this->pagination->initialize($config);
and in view
<?php echo $this->pagination->create_links();?>
Now how will I set number of rows to dynamic for paginate links.I hope you got my question
First of all, This should do the trick:
if(isset($_REQUEST['num_rows'])){
$rows = $_REQUEST['num_rows'];
}
Else condition isn't really required here as you are assigning $rows to itself.
Other than this, CodeIgniter purges the $_REQUEST variable for security reasons. So I believe IF condition is always false. Unless you have set 'global_xss_filtering' variable to true in config file.
You can also use $this->input->get('num_rows') or $this->input->post('num_rows') depending on which method you are using.
Everything else seems OK to me.

how can send variable value through pagination links?

SOLVED
I was trying to create pagination on the home page.
my default controller :
$route['default_controller'] ="home";
problem 1:
for pagination if I write-
$config['base_url'] = base_url() . 'home/index'; // index function
$config['uri_segment'] = 3;
$config['per_page'] = 10;
$limit = array($config['per_page'] => $this->uri->segment(3));
then it works but it shows 'localhost/baseurl/home/index/10' on browser address bar.I want to show just 'localhost/baseurl/10' (while 'localhost/baseurl/' is my homepage and I want pagination on there).so I wrote
$config['base_url'] = base_url(); // index function
$config['uri_segment'] = 1;
$config['per_page'] = 10;
$limitt = array($config['per_page'] => $this->uri->segment(1));
but that doesn't work. how can I do that ?
problem 2:
how can I send a variable value through pagination link like
localhost/baseurl/home/index?search=y/10
I wrote:
$config['base_url'] = base_url() . 'home/index?search=y';
but this doesn't work. it receives search=y/10. not only y and the pagination doesn't find 10 on $this->uri->segment(3))
so whats the correct way to do that?
Edit:
my htaccess code is:-
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./mysite/index.php [L]
</IfModule>
this code shows my site address like 'localhost/mysite/'. but if I set the base url of the pagination like
$config['base_url'] = base_url();
$config['uri_segment'] = 1;
$config['per_page'] = 10;
$limitt = array($config['per_page'] => $this->uri->segment(1));
then the number shows on pagination link like 'localhost/mysite/10' but the value '10' not received by the script. if I write-
$config['base_url'] = base_url().'home/index'; // 'index' is the function in 'home' class
$config['uri_segment'] = 3;
$config['per_page'] = 10;
$limitt = array($config['per_page'] => $this->uri->segment(3));
then it works but the browser address line becomes 'localhost/mysite/home/index/10' what I don't like. whats the way to solve the first problem ?
For the first problem you will have to define a rewrite rule as in the first comment.
For the second problem do like this,
if($this->uri->segment(3)){
$keyword = $this->uri->segment(3);
}
else if($this->input->get('search')){
$keyword = $this->input->get('search');
}
$config['base_url'] = base_url().'home/index/'.$keyword.'/page/';
$config["uri_segment"] = 5;

Resources