ShamerOC
ShamerOC

Reputation: 25

multiple variables in url codeigniter

I want to pass variables to a method. for example:

http://localhost/project/user/username/posts/postid

and I want to start 'posts' method and have 2 variables, or an assoc table

$user = username;
$posts = postid;

if link is http://localhost/project/user/username I want to start 'index' method and have variable $user = username

Upvotes: 1

Views: 56

Answers (1)

Pradeep
Pradeep

Reputation: 9717

Hope this will help you :

pass your variable like this , URL structure should be like this :

http://localhost/base_folder/controller_name/method_name/username/post_id

suppose ur controller name is welcome and method name is index, with username shamer and post id 4; it will become like this :

http://localhost/base_folder/welcome/index/shamer/4

in anchor should be like this :

<a href="<?=site_url('welcome/index/shamer/4');?>">your link</a>

In Welcome controller get username and post id in your index method like this :

public function index($username, $post_id)
{
   echo $username;
   echo $post_id;
}

For more : https://www.codeigniter.com/user_guide/general/controllers.html#passing-uri-segments-to-your-methods

Upvotes: 1

Related Questions