PHP HTML CSS Acemi USE CASE / ANAHTARI / ELSE IF?

4 Cevap php

Bir PHP tabanlı ticaret ürünü Magento kullanarak bir web sitesi oluşturmak.

Ben sorun ben sekmeli navigasyon kullanmak istiyor.

Benim fikrim URL tabanlı ilgili Navigasyon menü öğesi üzerinde TAB göstermek için CSS kullanmak oldu.

Ancak bir URL her zaman değişir, bu yüzden ben bir şekilde bir ifelse deyimini kullanmak istedim.

Ben işe yarayabilir düşünüyorum iki yöntem, herhangi uzmanlar ne düşünürdüm iyi olduğunu söyle ve bunu nasıl uygulayacağını ile geldim?

Method 1

<div id="nav">
<ul id="mainnav">
<li><a href="index.php" title="Welcome page" <?php if ($page == 'index.php') { ?>class="active"<?php } ?>>Welcome</a></li>
<li><a href="about_us.php" title="About us page" <?php if ($page == 'about_us.php') { ?>class="active"<?php } ?>>About us</a></li>
<li><a href="services.php" title="Services page" <?php if ($page == 'services.php') { ?>class="active"<?php } ?>>Services</a></li>
<li><a href="testimonials.php" title="Testimonials page" <?php if ($page == 'testimonials.php') { ?>class="active"<?php } ?>>Testimonials</a></li>
<li><a href="contact_us.php" title="Contact us page" <?php if ($page == 'contact_us.php') { ?>class="active"<?php } ?>>Contact us</a></li>
else
  <li><a href="store.php" title="Store Page" <?php ($page == 'store.php') { ?>class="active"<?php } ?>>Store</a></li>
</ul>
</div>

Method 2

$URL = store.php; 
SWITCH ($sample) { 
CASE home.php: 
<li><a href="index.php" title="Welcome page" <?php if ($page == 'index.php') { ?>class="active"<?php } ?>>Welcome</a></li>
  break; 
CASE services.php: 
<li><a href="services.php" title="Services page" <?php if ($page == 'services.php') { ?>class="active"<?php } ?>>Services</a></li>
  break; 
CASE aboutus.php: 
<li><a href="about_us.php" title="About us page" <?php if ($page == 'about_us.php') { ?>class="active"<?php } ?>>About us</a></li>
  break; 
DEFAULT: 
<li><a href="store.php" title="Store Page" <?php ($page == 'store.php') { ?>class="active"<?php } ?>>Store</a></li>
}

4 Cevap

Bu nasıl bir dizi soyutlayarak ilgili menü, son şekli ise?

$menu = array("index" =>        "Index", 
              "about_us" =>     "About us",
              "services" =>     "Services",
              "testimonials" => "Testimonials",
              "contact_us" =>   "Contact us",
              "store" =>        "Store");

/* Render the menu */
$activeFound = false;
foreach ($menu as $url => $item)
 { 
   if ($url == $page) 
   { $class = "active"; 
     $activeFound = true; 
   }
   else $class = "";
   echo "<li><a href='$url.php' $class>".htmlentities($item)."</a></li>";
  }

 if (!$activeFound)
  /* Render your changing store URL here */ 
  echo "store URL";

Bu şekilde, uzun vadede dizi düzenlemek için yalnızca olurdu.

Şahsen ben bu şekilde yapmak istiyorum:

Lütfen head olarak

<style>
a#<?= $currentPage ?> {
    color:red;
}
</style>

Eğer ($ _SERVER yararlı) bir şekilde CurrentPage değişkeni almak gerekir nerede.

Sonra sadece her linke sayfa adının bir id ekleyin.

Pekkas cevabını takiben, ben sprintf üçüncül deyimi içerecek şekilde değiştirmek istiyorum ve (kıvrımlara görünüyor) foreach döngüsü için php şablon kodu kullanmak için:

$menu = array("index" => "Index", 
              "about_us" => "About us",
              "services" => "Services",
              "testimonials" => "Testimonials",
              "contact_us" => "Contact us",
              "store" => "Store");

/* Render the menu */
foreach ($menu as $url => $item):
    echo sprintf("<li><a href="%s.php" class="%s">%s</a></li>", $url, (($url==$page) ? "active" : ""), htmlentities($item));
endforeach;

Şahsen, ben bu şekilde yapmak istiyorum:

<?php $act[$page] = 'active'; ?>
<div id="nav">
<ul id="mainnav">
<li><a href="index.php" title="Welcome page" class="<?= $act['index.php'] ?>">Welcome</a></li>
<li><a href="about_us.php" title="About us page" class="<?= $act['about_us.php'] ?>>About us</a></li>
<!-- and so on-->
</ul>
</div>