douhai5835 2012-09-26 00:30
浏览 21
已采纳

如何连接PHP变量以在菜单中使用

I'm not a PHP guy, however I believe PHP can be used to accomplish this.

Basically I'm using the same absolute path in my main menu + /about, /blog etc so figured well my files are already PHP why not use a Var to do this :) (I come from a Flash AS background)

Testing link: http://s433108212.onlinehome.us/

This is what I've tried below, but to no avail :(

<div id="nav_bar">

<?php $athenasweburl = 'http://s433108212.onlinehome.us/'; ?>

<ul class="nav">
    <li class="<?php echo $page === 'home' ? "selected" : "" ?>"><a href="<?php $athenasweburl; ?>">Home</a></li>
    <li class="<?php echo $page === 'about' ? "selected" : "" ?>"><a href="<?php echo ($athenasweburl+'about'); ?>">About</a></li>
    <li class="<?php echo $page === 'blog' ? "selected" : "" ?>"><a href="<?php $athenasweburl+'blog'; ?>">Blog</a></li>
    <li class="<?php echo $page === 'book' ? "selected" : "" ?>"><a href="<?php $athenasweburl+'book'; ?>">Book</a></li>
    <li class="<?php echo $page === 'events' ? "selected" : "" ?>"><a href="<?php $athenasweburl+'events'; ?>">Events</a></li>
    <li class="<?php echo $page === 'services' ? "selected" : "" ?>"><a href="<?php $athenasweburl+'services'; ?>">Services</a></li>
    <li class="<?php echo $page === 'contact' ? "selected" : "" ?>"><a href="#dialog" name="modal">Contact</a></li>
    <li class="search"><input type="text" onfocus="if(this.value == 'Search') { this.value = ''; }" value="Search" /></li>
    <li class="search_btn"><a href="#" title="Lets find it!"><div class="search_go">Go</div></a></li>
</ul>

thoughts anyone?

  • 写回答

4条回答 默认 最新

  • droi5225 2012-09-26 00:35
    关注

    In PHP + adds numbers together.. What you looking for is gluing strings.. Replace the + signs with . (dot), and an echo in front of each string, and it should work.

    <div id="nav_bar">
    
    <?php
    $menu = array("home","about","blog","book","events","services");
    function echoListItem($item){
        global $page;
        $url = 'http://s433108212.onlinehome.us/';
        if($item != "home") $url .= $item;
        $selected = $item == $page ? 'selected' : '';
        echo '<li class="'.$selected.'"><a href="'.$url.'">'.ucfirst($item).'</a></li>';
    }
    ?>
    
    <ul class="nav">
        <?php array_walk($menu, 'echoListItem'); ?>
        <li class="<?php echo $page === "contact" ? "selected" : "" ?>"><a href="#dialog" name="modal">Contact</a></li>
        <li class="search"><input type="text" onfocus="if(this.value == 'Search') { this.value = ''; }" value="Search" /></li>
        <li class="search_btn"><a href="#" title="Lets find it!"><div class="search_go">Go</div></a></li>
    </ul>
    

    This should do just about the same!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?