1

Ok, I have looked over and over this code but I can't seem to find the problem and why I am getting this error, here is the end of the file:

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

}

?>

If I take out that function the problem goes away.

2
  • A strategy for debugging parse errors is to strategically comment/delete bits of code until you find the problem. Commented Dec 8, 2012 at 17:52
  • I think that space between the closing parenthesis and the colon on your while might be the culprit. ETA: I was about to add to that there is one too many ?> tags but I see it's been answered. Commented Dec 8, 2012 at 17:54

1 Answer 1

3

It is due to your <?php endwhile; ?>, which closes ?> before closing the function's }, without re-opeining <?php. Since there is no subsequent code inside <?php ?>, the PHP parser sees the remaining stuff outside as plain text output and assumes you have no properly closed the }. It happens that that is the end of the file, and so that is how the error is reported.

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!

The while: / endwhile syntax is useful for templating where you are primarily mixing HTML with PHP code, but can be confusing when used inside a function like this, as you lose the visual cues provided by open and closed {} groups. I kind of recommend against mixing the syntax in this way.

Really, I would recommend against ever closing and reopening <?php ?> inside a function, but that is a matter of style. Instead, construct the strings that a function outputs and echo or return them.

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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Haha good spotting of the missing opening tag! :) I've recently started using syntax like while: / endwhile a lot more because it's far easier to connect the end parts then it is with just braces; but I agree, in this instance it's not really needed. :) Thanks for the other tips too!
One question..... shouldn't that be $html .=<<<HTMLSTRING so it grabs the data of EACH entry?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.