how can send variable value through pagination links? - codeigniter

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;

Related

how to fix the paggination problem in 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]

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]

Query string in codeigniter

After I remove index.php, I enable query string in codeigniter . But I have some trouble with redirect link. Detail , I have login form (login/index) and when login success redirect to "welcome/index" and save email in session .
But when login success only load view of "welcome/index" and wrong link , now link is :
"?login/index" And session don't save . Please help me .
Here's my code
Login.php (Controller)
if($this->input->post('email') != '' && $this->input->post('password') != ''){
if ($this->user->CheckLogin($this->input->post('email'),$this->input->post('password')) == true)
{
$this->load->library('session');
$this->session->set_flashdata('email', $this->input->post('email'));
redirect('welcome/index', 'refresh');
}
else
{
$data['error_message'] = '* Email or Password is incorrect';
$this->load->view('login',$data);
}
}else{
$this->load->view('login',$data);
}
Welcome.php (Controller)
class Welcome extends CI_Controller {
public function index()
{
$this->load->model('user');
$this->load->helper('url');
$this->load->library('session');
$data['email'] = $this->session->flashdata('email');
$this->load->view('welcome_message',$data);
}
}
welcome_message.php (View)
<?php
// Cant print email
echo $email;
?>
.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
If you enable Query Strings then change the redirect like this
redirect('c=welcome &m=index', 'refresh');
Additionally it may required to write index.php? or only ? before c
Or if change configuation in config.php then you need change the c and m
like
$config['enable_query_strings'] = FALSE;
$config['controller_trigger'] = 'c';
$config['function_trigger'] = 'm';
for more info http://www.codeigniter.com/user_guide/general/urls.html#enabling-query-strings

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

Codeigniter Pagination having page number in the middle of url

I'm trying to use pagination class in codeigniter and my url looks something like this:
blah.com/posts/browse/page/1/item_per_page/10
is there anyway to keep the page number in the middle of url?
Thanks
EDIT:
$this->load->library('pagination');
$uri = $this->uri->uri_to_assoc();
$page = null;
$item_per_page = null;
if (count($uri))
{
foreach ($uri as $key => $value)
{
$$key = $value;
}
}
$config['base_url'] = base_url('posts/browse/page//item_per_page/1');
$config['uri_segment'] = 4;
$config['per_page'] = '1';
After digging through code of Pagination class, I found a way to do this, but it wasn't mentioned anywhere in the tutorial.
$config['base_url'] = base_url('posts/browse');
$config['prefix'] = '/page/';
$config['suffix'] = '/item_per_page/1';
$config['uri_segment'] = 4;
this can generate urls with page number in the middle of the url.
eg. http://www.blah.com/posts/browse/page/2/item_per_page/1;
The documentation clearly explains how to do this.
Short version: use $config['uri_segment'] = 4; in your pagination config. uri_segment tells the pagination class which uri segment contains the page #.

Resources