-1

I am using codeigniter 2.xx, and this is my view code:

<a  href="users/user_info/<?=$row->user_name;?>/<?php echo $row->user_id;?>"></a>

And this is my controller code:

function user_info($name, $id)
{   
    $info['info']       =   $this->user_model->user_info($id);
    $data['content']    =   $this->load->view('home',$info,true);
    $this->load->view('template',$data);
}

When I click on the link I get this url: http://localhost/users/user_info/name/id

I want to remove last id segment so my url becomes: http://localhost/users/user_info/name. How can I do this? I read about uri segment but it doesn't solve my question.

davehale23
  • 4,374
  • 2
  • 27
  • 40
Azam Alvi
  • 6,918
  • 8
  • 62
  • 89

2 Answers2

0

If you're trying to get the current URL and remove the /id from it, simply:

$url = $_SERVER["REQUEST_URI"];

$paths = explode('/', trim(parse_url($url, PHP_URL_PATH), '/'));

unset($paths[count($paths)-1]);

$new_url = '/' . implode('/', $paths);

echo $new_url;

Which removes the last URL segment, outputting /users/user_info/name if visiting the page /users/user_info/name/id

Alternatively, you could change $url to the string in the href="...", but then again couldn't you just remove the code directly from the href?

sman591
  • 540
  • 6
  • 19
  • this will do well but it will not solve because the link from href is getting name and id so how this will work.and thanks for ur ans – Azam Alvi Apr 06 '13 at 05:37
  • So you don't want the name to be outputted into the href="" either? – sman591 Apr 06 '13 at 05:40
  • sorry i can't understand what u r saying. actually i mean to say that how to use this code because i am just using `href` so if change is required then here will be done, my question is that where to use your code.thanks – Azam Alvi Apr 06 '13 at 05:43
  • Can you not just change `href="users/user_info/=$row->user_name;?>/user_id;?>"` to `href="users/user_info/=$row->user_name;?>"`? – sman591 Apr 06 '13 at 05:46
  • no i cant because i am using unique id for fetching data and for displaying in url also including name, so i can't change. – Azam Alvi Apr 06 '13 at 05:48
  • So you want the URL to hide the /id, but want the website to handle the request with a /id? – sman591 Apr 06 '13 at 05:50
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/27684/discussion-between-sman591-and-azam-alvi) – sman591 Apr 06 '13 at 05:50
0
<a  href="/users/user_info/<?=$row->user_name;?>"></a>

(At href add slash at the start) And at controller..

function user_info($name)
{   
    $info['info']       =   $this->user_model->user_info($name);
    $data['content']    =   $this->load->view('home',$info,true);
    $this->load->view('template',$data);
}

At your user Model set function user_info to catch user by username, not by user id. And you are ready..

Svetoslav
  • 4,686
  • 2
  • 28
  • 43