为什么我会重复<a> tags?</a>

时间:2013-09-02 06:43:14

标签: php html

我正在使用分页部分中的bootstrap“pager”。

以下是我用来在PHP中加载HTML的内容:

echo "<ul class='pager'>";
echo ($total > $lim) ? "<li class='previous'>" : "<li class='previous disabled'>";
echo ($total > $lim) ? "<a href='#status' onclick='getStatuses(" . (($lim+10)/10) . ")'>&larr; Older</a>" : "<a>&larr; Older</a>";
echo "</li>";
echo ($lim == 10) ? "<li class='next disabled'>" : "<li class='next'>";
echo ($lim == 10) ? "<a>Newer &rarr;</a>" : "<a href='#status' onclick='getStatuses(" . (($lim-10)/10) . ")'>Newer &rarr;</a>";
echo "</li>";
echo "</ul>";

现在,当我实际加载它时,它会像这样返回:

<ul class="pager">
<a></a>
<li class="previous disabled">
<a></a>
<a>← Older</a>
</li>
<li class="next disabled">
<a>Newer →</a>
</li>
</ul>

有2个似乎是随机<a></a>标签。我怎么能摆脱这些?我做错了什么?

2 个答案:

答案 0 :(得分:2)

从PHP打印出一堆HTML通常会导致代码理解困难。最好退出PHP上下文,例如

// drop out of PHP
?>
<ul class="pager">
    <?php if ($total > $lim) : ?>
        <li class="previous">
            <a href="#status" onclick="getStatuses(<?= ($lim+10)/10 ?>)">&larr; Older</a>
        </li>
    <?php else : ?>
        <li class="previous disabled">
            <a>&larr; Older</a>
        </li>
    <?php endif ?>
    <?php if ($lim == 10) : ?>
        <li class="next disabled">
            <a>Newer &rarr;</a>
        </li>
    <?php else : ?>
        <li class="next">
            <a href="#status" onclick="getStatuses(<?= ($lim-10)/10 ?>)">Newer &rarr;</a>
        </li>
    <?php endif ?>
</ul>

<?php
// and now back to PHP

答案 1 :(得分:-1)

同意菲尔的意见,但我的猜测是&amp; larr或者你的三元表达式没有括号的事实。试试这个:

echo "<ul class='pager'>";
echo (($total > $lim) ? "<li class='previous'>" : "<li class='previous disabled'>");
echo (($total > $lim) ? "<a href='#status' onclick='getStatuses(" . (($lim+10)/10) . ")'> Older</a>" : "<a> Older</a>");
echo "</li>";
echo (($lim == 10) ? "<li class='next disabled'>" : "<li class='next'>");
echo (($lim == 10) ? "<a>Newer &rarr;</a>" : "<a href='#status' onclick='getStatuses(" . (($lim-10)/10) . ")'>Newer</a>");
echo "</li>";
echo "</ul>";