嘿伙计们我正在写一个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;
?
答案 0 :(得分: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;
?>