How to get the parameter from url in codeigniter?

$this->uri->segment(n); 

where n = 1 for controller, n = 2 for method and n = 3 for parameter and so on.
You need n = 3 to get parameter.
In your path localhost/log/job/php , your method name is missing.
Even if your method name is index then you route will be localhost/log/job/index/php
In case if you need to remove index.php from url then you will get parameter using localhost/log/index.php/job/index/php
To remove index.php you need to create .htaccess file by following these steps:
  1. Create a .htaccess file where index.php file is located with content
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L,QSA]
  2. Make sure that apache can access this .htaccess file. To do this edit apache configuration file. If you use ubuntu, then it is /etc/apache2/sites-available/default and then change AllowOverride none to AllowOverride all for directory and www directory.
       <Directory />
          Options FollowSymLinks
          AllowOverride all
       </Directory>
       <Directory /var/www/>
          Options Indexes FollowSymLinks MultiViews
          AllowOverride all
          Order allow,deny
          allow from all
       </Directory>
  3. Then enable mod rewrite if you don't have it, with the following command:
        `sudo a2enmod rewrite`
  4. Finally do not forget to restart apache.
 
Another Way:
 
$route['uri-(:any)/(:num)'] = "controller/function/$1/$2";
OR
$route['uri-(:any)'] = "controller/function/$1";
in your controller do some changes
<?php
 if (!defined('BASEPATH'))
   exit('No direct script access allowed');

 class controller extends CI_Controller 
 {
    function function($parameter1 = null, $parameter2 = null) 
    {
       ........
    }
 }
 
 

Comments

Popular posts from this blog

Getting started Mysql with JSON

MySQL event scheduler and how to create MySQL events to automate database tasks