0

Just a simple quetsion?

How to make CI

<?= base_url();?>

working with 'String'

The full codes as follows:

<?php if($_SESSION['admin'] == 1||$_SESSION['admin']== 0){
    echo "<a class='btn-common' href='<?= base_url();?>usr/logout'>LOGOUT</a>";
      }
      else{
          echo "<a class='btn-common2'></a>";
      }

Base_url is not woking

<?base_url();?>
Hermes Djohar
  • 111
  • 1
  • 3
  • 16

4 Answers4

2

You can't use a php method inside this string, you need to concatenate the string with the result instead:

    echo "<a class='btn-common' href='" . base_url() . "usr/logout'>LOGOUT</a>";
Antony
  • 1,253
  • 11
  • 19
2

Also like this. pass your path to base_url() as argument:

 <a class='btn-common' href='<?php echo base_url("usr/logout");?>'>LOGOUT</a>;

Don't forget to load url helper.using

$this->load->helper('url');
Hikmat Sijapati
  • 6,869
  • 1
  • 9
  • 19
  • In order to use PHP tags in the echo, he would need to close them before starting the tags – Antony Jan 27 '17 at 13:48
  • I'm not telling one way is better than the other, but in his case as he was using `echo` he already was in the php tags. For your example to work, he would need to replace the echo with `?>` and start a new php tag after printing the HTML. – Antony Jan 28 '17 at 19:39
0

You need to concatenate the string.

 echo "<a class='btn-common' href='".site_url('usr/logout')."'>LOGOUT</a>";

I would recommend to use site_url with your controller in parameter (which will use your config file to build url automatically). With Codeigniter, you can use $this->session->userdata('admin') for retrieving session data too.

  if($this->session->userdata('admin')===1 || $this->session->userdata('admin')===0){
      echo "<a class='btn-common' href='".site_url('usr/logout')."'>LOGOUT</a>";
  }else{
      echo "<a class='btn-common2'></a>";
  }
Luis
  • 11
  • 2
0

You should be using codeigniter links

<?php echo anchor("usr/logout",'Logout', 'title="Logout"');?>

You dont need to use base url, Codeigniter enters that for you

Brad
  • 1,685
  • 1
  • 27
  • 38