6

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?

1
  • You may need to configure your routes. Commented Apr 18, 2015 at 14:29

5 Answers 5

6

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

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

Sign up to request clarification or add additional context in comments.

3 Comments

thank you, If i do that it showing page not found error
You may need to configure your routes.
@wolfgang actually i don't know how to config my routes could you help me
5

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

Comments

2

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)
   }
}
}

2 Comments

i tried it just displaying controller name but not displaying my parameter
@M.Athishkrishna the code with segments always shows last part of url. I think you are not passing parameter via uri to your application.
2

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.

Comments

0

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]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.