我的包含文件中有一个导航菜单,但我无法在我的网站上回显菜单。如果我尝试按原样放置代码,页面将标记出导航菜单的位置。
如果我不在菜单之前添加<?php
函数,代码会有所作为。
我想知道如何在函数中回显此代码。我已经尝试将单引号更改为double,但我认为我对菜单中li
内的回声感到困惑。
解决方案会很棒。
由于
<?php
function nav() {
echo
<nav class='container'>
<ul>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'current-journal')) echo 'class="current"';?> href="current-journal">CURRENT JOURNAL<span class="sub-nav">Our latest and greatest!</span></a></li>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'submit')) echo 'class="current"';?> href="submit">SUBMIT<span class="sub-nav">Your writing</span></a></li>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'submission-guidelines')) echo 'class="current"';?> href="submission-guidelines">SUBMISSION GUIDELINES<span class="sub-nav">Everything you need to know is here</span></a></li>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'contributors')) echo 'class="current"';?> href="contributors">CONTRIBUTORS<span class="sub-nav">See who\'s in our magazine\'s latest issue</span></a></li>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'past-journals')) echo 'class="current"';?> href="past-journals">PAST JOURNALS<span class="sub-nav">Browse our issue archives</span></a></li>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'blog')) echo 'class="current"';?> href="blog">BLOG<span class="sub-nav">Just a blog</span></a></li>
</ul>
</nav>
}?>
答案 0 :(得分:1)
替换为:
<?php
function nav() { ?>
<nav class='container'>
<ul>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'current-journal')) echo 'class="current"';?> href="current-journal">CURRENT JOURNAL<span class="sub-nav">Our latest and greatest!</span></a></li>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'submit')) echo 'class="current"';?> href="submit">SUBMIT<span class="sub-nav">Your writing</span></a></li>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'submission-guidelines')) echo 'class="current"';?> href="submission-guidelines">SUBMISSION GUIDELINES<span class="sub-nav">Everything you need to know is here</span></a></li>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'contributors')) echo 'class="current"';?> href="contributors">CONTRIBUTORS<span class="sub-nav">See who\'s in our magazine\'s latest issue</span></a></li>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'past-journals')) echo 'class="current"';?> href="past-journals">PAST JOURNALS<span class="sub-nav">Browse our issue archives</span></a></li>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'blog')) echo 'class="current"';?> href="blog">BLOG<span class="sub-nav">Just a blog</span></a></li>
</ul>
</nav>
你不需要echo来显示html只是关闭html之前的php标签
希望这会有所帮助!!
答案 1 :(得分:1)
最简单的是不使用初始回声。请注意?>
名称之后的function
,并在底部再次添加<?php
作为结束函数大括号。
<?php
function nav() {
?>
<nav class='container'>
<ul>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'current-journal')) echo 'class="current"';?> href="current-journal">CURRENT JOURNAL<span class="sub-nav">Our latest and greatest!</span></a></li>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'submit')) echo 'class="current"';?> href="submit">SUBMIT<span class="sub-nav">Your writing</span></a></li>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'submission-guidelines')) echo 'class="current"';?> href="submission-guidelines">SUBMISSION GUIDELINES<span class="sub-nav">Everything you need to know is here</span></a></li>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'contributors')) echo 'class="current"';?> href="contributors">CONTRIBUTORS<span class="sub-nav">See who\'s in our magazine\'s latest issue</span></a></li>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'past-journals')) echo 'class="current"';?> href="past-journals">PAST JOURNALS<span class="sub-nav">Browse our issue archives</span></a></li>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'blog')) echo 'class="current"';?> href="blog">BLOG<span class="sub-nav">Just a blog</span></a></li>
</ul>
</nav>
<?php
}?>
答案 2 :(得分:1)
<?php
function nav() {
?>
<nav class='container'>
<ul>
<?php if(strpos($_SERVER['PHP_SELF'],'current-journal' )) {echo "<li><a class='current' href='current-journal'>CURRENT JOURNAL<span class='sub-nav'>Our latest and greatest!</span></a></li>"; }?>
<?php if(strpos($_SERVER['PHP_SELF'],'submit' )) {echo "<li> <a class='current' href='submit'>SUBMIT<span class='sub-nav'>Your writing</span></a></li>"}?>
<?php if(strpos($_SERVER['PHP_SELF'],'submission-guidelines' )) {echo "<li> <a class='current' href="submission-guidelines'>SUBMISSION GUIDELINES<span class="sub-nav">Everything you need to know is here</span></a></li>";}?>
<?php if(strpos($_SERVER['PHP_SELF'],'contributors' )) {echo "<li> <a class='current' href='contributors'>CONTRIBUTORS<span class='sub-nav'>See who\'s in our magazine\'s latest issue</span></a></li>";}?>
<?php if(strpos($_SERVER['PHP_SELF'],'past-journals' )) {echo "<li> <a class='current' href='past-journals'>PAST JOURNALS<span class='sub-nav'>Browse our issue archives</span></a></li> ";}?>
<?php if(strpos($_SERVER['PHP_SELF'],'blog' )) {echo "<li> <a class='current' href='blog'>BLOG<span class='sub-nav'>Just a blog</span></a></li>";}?>
</ul>
</nav>
<?php
}
?>
这应该做的工作。