简体   繁体   English

如何在菜单中使用PHP变量

[英]How to concat PHP vars for use in menu

I'm not a PHP guy, however I believe PHP can be used to accomplish this. 我不是PHP专家,但是我相信可以使用PHP来实现这一目标。

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) 基本上,我在主菜单中使用了相同的绝对路径+ / about,/ blog等,因此很好地弄清楚了我的文件已经是PHP了,为什么不使用Var来做到这一点:)(我来自Flash AS背景)

Testing link: http://s433108212.onlinehome.us/ 测试链接: 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? 有人在想吗?

You don't output the URL. 您不输出URL。 Note the addition of echo in the second stanza. 注意第二个节中增加了echo

<li class="<?php echo $page === 'home' ? "selected" : "" ?>"><a href="<?php echo $athenasweburl; ?>">Home</a></li>

And concatenation uses . 并用. , not + . ,而不是+

<li class="<?php echo $page === 'about' ? "selected" : "" ?>"><a href="<?php echo $athenasweburl . 'about'; ?>">About</a></li>

In PHP + adds numbers together.. What you looking for is gluing strings.. Replace the + signs with . 在PHP +中将数字加在一起。.您要寻找的是粘合字符串。.用+代替+号。 (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! 这应该做的差不多!

PHP uses a dot notation "." PHP使用点表示法“。” To concatenate variables and strings etc. To concatenate the string "blog" to $athenasweburl, you would use 串联变量和字符串等。要将字符串“ blog”串联到$ athenasweburl,可以使用

  $athenasweburl = $athenasweburl."blog/";
  echo $athenasweburl; //http://s433108212.onlinehome.us/blog/

Hope that helps. 希望能有所帮助。

I've hit a similar issue not that long ago. 不久前,我遇到了类似的问题。

Assuming you have $page defined somewhere else as well... 假设您在其他地方也定义了$ page ...

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM