1

嘿伙计们,我正在写一个 CMS,但我遇到了问题,需要一双新的眼睛。我收到此错误:解析错误:语法错误,在最后一个 else 语句上方的行上出现意外的“}”,但没有理由。

我们的想法是显示文章的子字符串,然后单击它们以获取完整文章。有人可以看看我的代码并告诉我我哪里出错了,或者这是否可行。

class MyCMS 
{
function get_content($id = "", $excerpt = FALSE)
{
if ($id != ""):
    $id = mysql_real_escape_string($id);
    $sql = "SELECT * FROM content WHERE blog_id = '$id'";
    $return = '<p><a href="index.php"> Go Back To Content Page</a></p>';

else:
    $sql = "SELECT blog_id, title, date, body FROM content ORDER BY blog_id DESC LIMIT 0, 3";
endif;

$res = mysql_query($sql) or die(mysql_error());
if(mysql_num_rows($res) !=0):
    while($row = mysql_fetch_assoc($res))
    {
        echo '<div id="roundedbox"><h2><a href="index.php?id=' . $row['blog_id'] . '">' . $row['title'] . ' </a></h2>';
        echo '<div id="date"><h5><p>' . $row['date'] . '</p></h5></div>';
        if ($excerpt):
            echo substr('<p>' . $row['body'] . '</p>',0, 90)." .... "." read more </div>";
        else:
            echo '<p>' . $row['body'] . '</p>';

    }
    else:
        echo '<p> UH OOH! THERE IS NO SUCH PAGE IT DOES\'T EXIST </p>'; 
        echo $return;
    endif;  

}


}

然后在我的主页中,我调用代码如下:

<?php
include 'cms.php';
$obj = new MyCMS(); 
?>


    <?php
    if(isset($_GET['id'])):
        echo $obj->get_content($_GET['id'], TRUE);
    else:
        echo $obj->get_content($_GET['id']);
    endif;
?
4

1 回答 1

1

您忘记添加 endif(用于循环内的 if 语句)和 }(用于关闭类)。请检查此代码。

class MyCMS 
{
function get_content($id = "", $excerpt = FALSE)
{
if ($id != ""):
    $id = mysql_real_escape_string($id);
    $sql = "SELECT * FROM content WHERE blog_id = '$id'";
    $return = '<p><a href="index.php"> Go Back To Content Page</a></p>';

else:
    $sql = "SELECT blog_id, title, date, body FROM content ORDER BY blog_id DESC LIMIT 0, 3";
endif;

$res = mysql_query($sql) or die(mysql_error());
if(mysql_num_rows($res) !=0):
    while($row = mysql_fetch_assoc($res))
    {
        echo '<div id="roundedbox"><h2><a href="index.php?id=' . $row['blog_id'] . '">' . $row['title'] . ' </a></h2>';
        echo '<div id="date"><h5><p>' . $row['date'] . '</p></h5></div>';
        if ($excerpt):
            echo substr('<p>' . $row['body'] . '</p>',0, 90)." .... "." read more </div>";
        else:
            echo '<p>' . $row['body'] . '</p>';
        endif;

    }
    else:
        echo '<p> UH OOH! THERE IS NO SUCH PAGE IT DOES\'T EXIST </p>'; 
        echo $return;
    endif;  

}

}

另请注意,第二个代码逻辑是绝对错误的..

<?php
include 'cms.php';
$obj = new MyCMS(); 
?>


    <?php
    if(isset($_GET['id'])):
        echo $obj->get_content($_GET['id'], TRUE);
    else:
      //  echo $obj->get_content($_GET['id']); // This line is executing when $_GET['id'] is undefined.So dont use $_['id'] here.
    endif;
?>
于 2013-06-13T05:47:12.643 回答