M.Athish krishna
M.Athish krishna

Reputation: 227

How to get the parameter from url in codeigniter?

I couldn't get the parameter value from url in codeigniter. For instance: localhost/log/job/php here log/ is my folder, job/ is my controller and php is my parameter.

I want to get this parameter in controller 'job'. How can I do that?

Upvotes: 6

Views: 22032

Answers (5)

Nizarhdt
Nizarhdt

Reputation: 23

Use:

$param = $this->uri->segment(3);

add .htaccess on your folder (on your case is "log") :

RewriteEngine On
RewriteBase /log/

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /log/index.php/$1 [L]

Upvotes: 0

user5506189
user5506189

Reputation: 196

You can use $this->uri->segment(n);

You need to do some changes in route in order to allow the code igniter to receive the parameter in your controller

$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) 
    {
       ........
    }
 }

refer this http://www.codeigniter.com/userguide2/libraries/uri.html

Upvotes: 5

Md. Salahuddin
Md. Salahuddin

Reputation: 1072

You get it by this:

$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.

Hope this will help.

Upvotes: 2

mirza
mirza

Reputation: 5793

Assuming your parameter will be in the end always:

$segs = $this->uri->segment_array();
echo end($segs);

EDIT: For the clarify other essentials. First you need the setup your application/config/routes.php :

$route['Youruri-(:any)/(:num)'] = "yourcontroller/yourfunction/$1/$2";
$route['Youruri-(:any)'] = "yourcontroller/yourfunction/$1";

In the controller application/controllers/yourcontroller.php you need to define a function:

<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Yourcontroller extends CI_Controller {

function yourfunction($brand = null, $page = null) {
   // http://www.yoursite.com/Youruri-Microsoft
   // or http://www.yoursite.com/yourcontroller/yourfunction/Microsoft
   // do some stuff for get products of this $brand (Microsoft)
   if($page != null) {
   // http://www.yoursite.com/Youruri-Intel/2 
   // or http://www.yoursite.com/yourcontroller/yourfunction/Intel/2
   // do some other stuff get products of this $brand (Intel) of $page (2)
   }
}
}

Upvotes: 2

Jens A. Koch
Jens A. Koch

Reputation: 41786

You might use $this->uri->segment(n).

http://www.codeigniter.com/userguide2/libraries/uri.html

Upvotes: 6

Related Questions