views:

89

answers:

1

The following view code produces html code as follows.

Q1. what is the first line doing?

<?php $ci_uri = trim($this->uri->uri_string(), '/'); $att = ' id="active"';?>

Q2. Why

producing id="active"?

Q3. What are the purpose of

<?= substr($ci_uri, 0, 7) == 'example'? $att: ''?>
<?= $ci_uri == $this->config->item('FAL_login_uri')? $att: ''?>
...
...
<?= $ci_uri == $this->config->item('FAL_changePassword_uri')? $att: ''?>
etc.

Is it using ternary operator?

View code

<?php $ci_uri = trim($this->uri->uri_string(), '/'); $att = ' id="active"';?>
<ul id="navlist">
<li<?= $ci_uri == ''? $att: ''?>><?=anchor('', 'home')?></li>
<li<?= substr($ci_uri, 0, 7) == 'example'? $att: ''?>><?=anchor('example', 'examples')?></li>
<li<?= $ci_uri == $this->config->item('FAL_login_uri')? $att: ''?>><?=anchor($this->config->item('FAL_login_uri'), 'login')?></li>
<li<?= $ci_uri == $this->config->item('FAL_register_uri')? $att: ''?>><?=anchor($this->config->item('FAL_register_uri'), 'register')?></li>
<li<?= $ci_uri == $this->config->item('FAL_forgottenPassword_uri')? $att: ''?>><?=anchor($this->config->item('FAL_forgottenPassword_uri'), 'forgotten password')?></li>
<li<?= $ci_uri == $this->config->item('FAL_changePassword_uri')? $att: ''?>><?=anchor($this->config->item('FAL_changePassword_uri'), 'change password')?></li>
<li<?= substr($ci_uri, 0, 5) == 'admin'? $att: ''?>><?=anchor('admin', 'admin')?></li>
</ul>

HTML code

<ul id="navlist">
<li id="active"><a href="http://127.0.0.1/ci_freak_auth/index.php"&gt;home&lt;/a&gt;&lt;/li&gt;
<li><a href="http://127.0.0.1/ci_freak_auth/index.php/example.html"&gt;examples&lt;/a&gt;&lt;/li&gt;
<li><a href="http://127.0.0.1/ci_freak_auth/index.php/auth/login.html"&gt;login&lt;/a&gt;&lt;/li&gt;
<li><a href="http://127.0.0.1/ci_freak_auth/index.php/auth/register.html"&gt;register&lt;/a&gt;&lt;/li&gt;
<li><a href="http://127.0.0.1/ci_freak_auth/index.php/auth/forgotten_password.html"&gt;forgotten password</a></li>
<li><a href="http://127.0.0.1/ci_freak_auth/index.php/auth/changepassword.html"&gt;change password</a></li>
<li><a href="http://127.0.0.1/ci_freak_auth/index.php/admin.html"&gt;admin&lt;/a&gt;&lt;/li&gt;
</ul>
+2  A: 
  1. It's setting $ci_uri to the current URL, with leading and trailing slashes removed, so "/foo/bar/" would become "foo/bar". It's then setting $att to be that string, for use later.

  2. The string will be injected into an HTML tag later - probably so it can be styled differently with CSS

  3. Basically it's looking if the link it's printing is the current URL and adding the active attribute if it is.

In summary, the whole lot is to style the link to the current page differently.

The code:

<?= $ci_uri == $this->config->item('FAL_login_uri')? $att: ''?>

means:

<?= // this is a shortcut for <?php echo

// If the current url is the same as the url for "FAL_login_uri"
$ci_uri == $this->config->item('FAL_login_uri')

// then
?

// use $att in the echo
$att

// else
:

// use an empty string in the echo
''
?>

The $x ? $y : $z syntax is a ternary operator - it's pretty much a short way of doing if / else

Greg