开发者

If variable equals multiple strings in a navigation list

I found something similar to this question but it wasn't related to strings. I'm just looking for the best way to write the following code, I figured there has to be some short version I don't know about.

<ul id="top-nav">
    <li <?php if($filename == "index.php"){echo ("class=\"current\"");} ?>><a href="index.php" title="Home">Home</a></li>
    <li <?php if($filename == "cause.php"){echo ("class=\"current\"");} ?>><a href="cause.php" title="Cause">Cause</a></li>
    <li><a href="order.php" title="Order">Order</a></li>
    <li class="drop-down<?php if($filename == "grand-prize-one.php" | $filename == "grand-prize-two.php"){echo (" current");} ?>"><a href="grand-prize-one.php" title="Grand Prizes">Grand Prizes</a>
      <ul>
        <li><a href="grand-prize-one.php" title="Grand Prize One">Grand Prize One</a></li>
        <li><a href="grand-prize-two.php" title="Grand Prize Two">Grand Prize Two</a></li>
      </ul>
    </li>
    <li <?php if($filename == "early-bird.php"){echo ("class=\"current\"");} ?>><a href="early-bird.php" title="Early Bird">Early Bird</a></li>
    <li class="drop-down<?php if($filename == "vehicles.php" || $filename == "vacations.php" || $filename == "other-prizes.php"){echo (" current");} ?>"><a href="vehicles.php" title="Electronics &amp; more">Other Prizes</a>
      <ul>
        <li><a href="vehicles.php" title="Vehicles">Vehicles</a></li>
        <li><a href="vacations.php" title="Vacations">Vacations</a></li>
        <li><a href="other-prizes.php" title="Electronics &amp; more">Electronics &amp; more</a></li>
      </ul>
    </li>
    <li <?php if($filename == "winners.php"){echo ("class=\"current\"");} ?>><a href="winners.php" title="Winners">Winners</a></li>
  </ul>

Is there a shorter way to write $filename == "vehicles.php" || $filename == "vacations.php" || $filename == "other-prizes.php"? I doubt I'd ever need it to accommodate more the 5-10 possible strings.

Thank you

SOLUTION

For those who find this question, I ended up doing this:

With my main PHP

<?php
 // My other code
 $grandPrizes = array("grand-prize-one.php","grand-prize-two.php");
 $otherPrizes = array("vehicles.php","vacations.php","other-prizes.php");
?>

My inline PHP in the li element

<?php if (in_array($filename, $grandPrizes)) 开发者_如何学运维{echo " current";} ?>


$filenames = array("vehicles.php","vacations.php","other-prizes.php");
if (in_array($filename, $filenames)) {
    // code here
}


You could use a switch:

http://php.net/manual/en/control-structures.switch.php

switch($filename) {
    case "vehicles.php":
    case "vacations.php":
    case "other-prizes.php":
        echo "class=\"current\"";
        break;
}


Try this:

<?php 
    if(in_array($filename, array("vehicles.php", "vacations.php", "other-prizes.php")) {
        echo ("class=\"current\"");
    } 
?>

in_array function is appropriate for what you are trying to do.


You could use

if(in_array($filename, array("vehicles.php", "vacations.php", "other-prizes.php"))){
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜