irwan Dwiyanto
irwan Dwiyanto

Reputation: 321

How to get the value uri segment in CodeIgniter

I do not understand about uri->segment on CodeIgniter. as the following example, I have a table with the fields:

| Id_comment | id_alat     | user         |
|:-----------|------------:|:------------:|
|     1      |    45       | irwan        |     

if i call $id = $this->uri->segment(4); It returns the user field.

How do I return the id_comment field instead?

Upvotes: 1

Views: 15061

Answers (4)

Marius
Marius

Reputation: 190

Since CodeIgniter 4 this is slightly changed:

$uri = service('uri');
$sLast = $uri->getSegment(4, 'any default value');

Upvotes: 0

Linesofcode
Linesofcode

Reputation: 5903

For example, your url look alike: http://localhost/yourProject/users/edit/1/john/33

So you have five parameters:

 1. users - the controller
 2. edit  - the function
 3. 1     - the id of user
 4. john  - the name of user
 5. 33    - the age of user

In your route.php file you should create the following route:

$route['users/edit/(:num)/(:any)/(:num)'] = 'users/edit/$1/$2/$3;

Finally, in your controller you are able to access the parameters from your url by uri->segment() or..

public function edit($id, $name, $age)
{
   echo $id;
}

Upvotes: 1

irwan Dwiyanto
irwan Dwiyanto

Reputation: 321

//control

function deletecom() {
    $u = $this->uri->segment(4);
    $this->mcrud->deletecomment($u);
    redirect('instrument/instrument');
}

//view

Upvotes: 0

Abdulla Nilam
Abdulla Nilam

Reputation: 38652

If your URL is this instrumentdev/instrument/instrument/detail/CT-BSC-001

then you can get data by following method

$url1 = base_url(); // Site URL
$url2 = $this->uri->segment(1); // Controller - instrument
$url3 = $this->uri->segment(2); // Method - instrument
$url4 = $this->uri->segment(3); // detail
$url5 = $this->uri->segment(4); // CT-BSC-001

Upvotes: 2

Related Questions