1

好的,我一遍又一遍地查看了这段代码,但我似乎找不到问题以及为什么会出现这个错误,这是文件的结尾:

function ouputMainSlider() {

    global $USER;

    // Get details
    $query = "SELECT id, bigimage, heading, fullarticle, dateadded FROM news WHERE status = 1 ";
    $query .= "AND (state = '" . $USER->state . "' OR state = 'ALL') AND newstype != 1 and bigimage != '' ";
    $query .= "ORDER BY dateadded DESC LIMIT 10";
    $restresult = mysql_query($query);

    while ($restaurant = mysql_fetch_array($restresult)) :

        // Trim article for preview
        $preview = trim_str($restaurant['fullarticle'], 270);

        ?>

        <li>

            <img alt="<?=fixup($restaurant['heading'])?>" src="images/frontpage/<?=$restaurant['bigimage']?>" width="615" height="309" />
            <a href="#" class="read-more">Read More</a>
            <p>         
                <strong><a href="#"><?=fixup($restaurant['heading'])?></a></strong>
                <em><?=fixup($preview)?></em>               
            </p>

        </li>

    <?php endwhile; ?>

}

?>

如果我取出该功能,问题就会消失。

4

1 回答 1

3

这是由于您的<?php endwhile; ?>,它?>在关闭功能之前关闭},而无需重新打开<?php。由于内部没有后续代码<?php ?>,PHP 解析器将外部的剩余内容视为纯文本输出,并假定您没有正确关闭}. 碰巧那是文件的结尾,这就是报告错误的方式。

while ($restaurant = mysql_fetch_array($restresult)) :

        // Trim article for preview
        $preview = trim_str($restaurant['fullarticle'], 270);

        ?>

        <li>

            <img alt="<?=fixup($restaurant['heading'])?>" src="images/frontpage/<?=$restaurant['bigimage']?>" width="615" height="309" />
            <a href="#" class="read-more">Read More</a>
            <p>         
                <strong><a href="#"><?=fixup($restaurant['heading'])?></a></strong>
                <em><?=fixup($preview)?></em>               
            </p>

        </li>

    <?php
     endwhile; // Don't close ?> here!

while: / endwhile语法对于主要将 HTML 与 PHP 代码混合的模板很有用,但在这样的函数中使用时可能会造成混淆,因为您会失去开放组和封闭{}组提供的视觉提示。我有点建议不要以这种方式混合语法。

真的,我建议不要<?php ?>在函数内关闭和重新打开,但这是风格问题。相反,构造函数输出的字符串和echo/或return它们。

function ouputMainSlider() {
    global $USER;

    // Get details
    $query = "SELECT id, bigimage, heading, fullarticle, dateadded FROM news WHERE status = 1 ";
    $query .= "AND (state = '" . $USER->state . "' OR state = 'ALL') AND newstype != 1 and bigimage != '' ";
    $query .= "ORDER BY dateadded DESC LIMIT 10";
    $restresult = mysql_query($query);

    $html = "";
    while ($restaurant = mysql_fetch_array($restresult)) {
        // Trim article for preview 
        // You can't call functions in the HEREDOC, so call them here first
        $preview = fixup(trim_str($restaurant['fullarticle'], 270));
        $heading = fixup($restaurant['heading']);

        // Build the string with a HEREDOC, insead of directly sending it to the output buffer
        // by closing ?> and reopening <?php
        $html .=<<<HTMLSTRING
        <li>
            <img alt="$heading" src="images/frontpage/{$restaurant['bigimage']}" width="615" height="309" />
            <a href="#" class="read-more">Read More</a>
            <p>         
                <strong><a href="#">$heading</a></strong>
                <em>$preview</em>               
            </p>
        </li>
HTMLSTRING;
// No whitespace before the closing of the HEREDOC!
    }
    // Then echo the HTML output
    echo $html;
}
于 2012-12-08T17:51:38.940 回答