Neden bu kod id eklese = "Etkin"

1 Cevap php

Aşağıdaki gibi aşağıdaki görünüm kod html kodu üretir.

Q1. ilk satırı ne yapıyor?

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

Q2. Why

id = "aktif" üreten?

Q3. Amacı nedir

<?= 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.

Bu üçlü operatörü kullanıyor?

Görünüm kodu

<?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 kodu

<ul id="navlist">
<li id="active"><a href="http://127.0.0.1/ci_freak_auth/index.php">home</a></li>
<li><a href="http://127.0.0.1/ci_freak_auth/index.php/example.html">examples</a></li>
<li><a href="http://127.0.0.1/ci_freak_auth/index.php/auth/login.html">login</a></li>
<li><a href="http://127.0.0.1/ci_freak_auth/index.php/auth/register.html">register</a></li>
<li><a href="http://127.0.0.1/ci_freak_auth/index.php/auth/forgotten_password.html">forgotten password</a></li>
<li><a href="http://127.0.0.1/ci_freak_auth/index.php/auth/changepassword.html">change password</a></li>
<li><a href="http://127.0.0.1/ci_freak_auth/index.php/admin.html">admin</a></li>
</ul>

1 Cevap

  1. O kadar "/ foo / bar /" "foo / bar" olacak, lider ve kaldırıldı bölü işareti ile, mevcut URL'ye $ ci_uri ayarlıyor. Bu daha sonra kullanılmak üzere, bu dize olmak $ att ayarlıyor.

  2. Dize daha sonra bir HTML etiketinin içine enjekte edilecek - muhtemelen bu yüzden CSS ile farklı tarz olabilir

  3. Temelde o baskı oluyor bağlantı geçerli URL ise seyir ve eğer aktif niteliği ekleyerek oluyor.

Özetle, sürü farklı geçerli sayfaya bağlantı stili için.

Kodu:

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

şu anlama gelir:

<?= // 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
''
?>

Hemen hemen yapmanın kısa bir yolu if / else - $x ? $y : $z sözdizimi bir ternary operator olduğunu