1

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

1
  • pls always response to the answers by giving some comments or ,if it helps you, by marking it as green and upvoting, it is the best way to thanks all the programmers Commented Jul 17, 2018 at 12:01

1 Answer 1

1

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

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

1 Comment

if my answer helps u pls don't forget to check it as green and do upvote also

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.