0

I have a function in my controller that retrieves User name here:

public function getUserName(){
     $user_id = $this->session->userdata('user_id');
     return $this->user_model->sban_name($user_id);
 }

Now, what I want is to put it directly inside an HTML tag like this,

<a href="#"><?php echo getUserName();?></a>

But, it's not working. Is there any other way to do it?

5
  • What output are you getting instead? Commented Jul 4, 2015 at 11:09
  • Define a helper and load it in your controller and call it in your view. Commented Jul 4, 2015 at 11:13
  • Why don't you pass data within a view as like redirect('your_view',$data); Commented Jul 4, 2015 at 11:22
  • @IndrasinhBihola, i dont get your point sir. Commented Jul 4, 2015 at 11:22
  • CI provides a way to call your function in your view by creating your own helper but in your context simply follow a way of @GluePear. Commented Jul 4, 2015 at 11:26

1 Answer 1

2

You need to pass the username to your view via the controller. You can't access controller functions in your view. So in the controller that loads the view, do something like this:

$data['username'] = $this->getUserName();
$this->load->view('view_name',$data);

Then in your view you can simply echo the username:

<a href="#"><?php echo $username ?></a>
Sign up to request clarification or add additional context in comments.

4 Comments

something like this? $this->load->view('dashboard/inc/header_view',$data);
Yes. I'm assuming you're loading the view like that anyway? For more info on passing data to views, see the Codeigniter manual.
i tried, but it says, undefined function getUserName();
In my example, the function getUserName has to be in the same controller as the function that loads the view. Is it?

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.