user2480085
user2480085

Reputation: 33

PHP: CMS Problems

Hey guys im writting a CMS but im having problems and need a fresh pair of eyes at this. I am getting this error: Parse error: syntax error, unexpected '}' on the line just above the last else statement but there is no reason for it.

The idea is to have display a substr of the articles and then click them to get the full article. Can someone have a look at my code and tell me where im going wrong or if this will even work.

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;  

}


}

Then in my main page i am calling the code as follows:

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

Upvotes: 1

Views: 62

Answers (1)

Fasil kk
Fasil kk

Reputation: 2187

You forgot to add endif(for the if statement inside loop) and } (for closing class). Please check this code.

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;  

}

}

Also please notice that second code logic is absolutely wrong..

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

Upvotes: 1

Related Questions