0

I need to concatenate the base url with another variable which should be public in the class. How can I do this?

Here is my code :

class site extends CI_Controller 
{
  public $site_favicon = base_url().'img/favicon.ico';
 // I tried even       public $site_favicon = base_url();.'img/favicon.ico';
}

Where i will call the variable $t publicly any where from the site.

How can i do this ?

This is how I will call:

<link rel="shortcut icon" href="<?php echo $this->site_favicon; ?>">

I am getting:

Parse error: syntax error, unexpected '(', expecting ',' or ';' in /opt/lampp/htdocs/ci/application/controllers/site.php on line 23

What mistake am I doing in concatenation and how can i fix this?

8
  • What value are you getting when you echo $site_favicon ? Commented May 23, 2014 at 6:28
  • Obviously missing a semi colon probably at the end of line 22 Commented May 23, 2014 at 6:28
  • @Jenz: I am just getting the above error Commented May 23, 2014 at 6:29
  • @ElefantPhace though i try to concatinate as public $site_favicon = base_url();.'img/favicon.ico'; i am getting the same error Commented May 23, 2014 at 6:30
  • Downvoter : May i know the reason for downvote ? Commented May 23, 2014 at 6:31

2 Answers 2

1

Try with this

class site extends CI_Controller 
{
     public function __construct()
    {
        parent::__construct();
        $this->load->helper(array('url'));
        $url=$this->config->base_url();
        $this->site_favicon=$url.'img/favicon.ico';
    }
}

Now you can access site_favicon in functions with echo $this->site_favicon;

<link rel="shortcut icon" href="<?php echo $this->site_favicon ?>">

Note : If you want variables set in the constructor accessible, then you have to set them as a class property ie., like $this->variablename and access them in the same way with $this->variablename in your functions.

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

7 Comments

can $this->site_favicon variable access on view?
I am not getting error but i am getting empty value while i use this in my view <link rel="shortcut icon" href="<?php $this->site_favicon ?>">
@Girish.Yes. In the same manner it will be available in view also
@CiiLearner..You have to echo it. <link rel="shortcut icon" href="<?php echo $this->site_favicon ?>">
@Jenz: Thanks I am getting it correct. ( If you find my question is valid. Kindly upvote it too)
|
0

You can't assign function inside class varable, base_url is helper function, that is initialized in system autoload So you can use directly in views Try this way

<link rel="shortcut icon" href="<?php echo base_url("img/favicon.ico"); ?>">

OR

<link rel="shortcut icon" href="<?php echo base_url()."img/favicon.ico"; ?>">

UPDATE: You should assign favicon value in view variable, the view variables are defined in controller -> action method

In controller

$favicon = base_url().'img/favicon.ico';
$this->load->view("viewName", array("favicon" => $favicon));

In view you can access variable

<link rel="shortcut icon" href="<?php $favicon; ?>">

NOTE: you can't access controller class variable value directly in view

2 Comments

Thanks i am getting. But i don't want to use the base_url() in the view. I just want to assign the value for $site_favicon; as there are several pages in my website. (But i am able to assign other variables just inside the calss)
Parse error: syntax error, unexpected '$favicon' (T_VARIABLE), expecting function (T_FUNCTION) in /opt/lampp/htdocs/ci/application/controllers/site.php on line 23 Same error. Let me try in few min and convey you ji

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.