blog::titleExists PHP Method

titleExists() public method

public titleExists ( $newTitle )
    function titleExists($newTitle)
    {
        require_once $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . "classes" . DIRECTORY_SEPARATOR . "settings.class.php";
        $settings = new settings();
        if ($settings::db_driver == "xml") {
            // XML
            $blogPosts = simplexml_load_file($_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . "data" . DIRECTORY_SEPARATOR . "blogPosts.xml");
            foreach ($blogPosts as $blogPost) {
                if ($blogPost->title == $newTitle) {
                    return TRUE;
                }
            }
            return FALSE;
        } else {
            // PDO
            require_once $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . "classes" . DIRECTORY_SEPARATOR . "common.class.php";
            $common = new common();
            $dbh = $common->pdoOpen();
            $sql = "SELECT COUNT(*) FROM " . $settings::db_prefix . "blogPosts WHERE title = :title";
            $sth = $dbh->prepare($sql);
            $sth->bindParam(':title', $title, PDO::PARAM_STR, 100);
            $sth->execute();
            $count = $sth->fetchColumn();
            $sth = NULL;
            $dbh = NULL;
            if ($count > 0) {
                return TRUE;
            }
            return FALSE;
        }
    }

Usage Example

示例#1
0
// Load the require PHP classes.
require_once '../../classes/common.class.php';
require_once '../../classes/account.class.php';
require_once '../../classes/blog.class.php';
$common = new common();
$account = new account();
$blog = new blog();
// Check if the user is logged in.
if (!$account->isAuthenticated()) {
    // The user is not logged in so forward them to the login page.
    header("Location: login.php");
}
$titleExists = FALSE;
if ($common->postBack()) {
    // Check if title already exists.
    $titleExists = $blog->titleExists($_POST['title']);
    if (!$titleExists) {
        // Update the contents of the blog post.
        $blog->addPost($_SESSION['login'], $_POST['title'], $_POST['contents']);
        // Forward the user to the blog management index page.
        header("Location: /admin/blog/");
    }
}
////////////////
// BEGIN HTML
require_once '../includes/header.inc.php';
?>
            <h1>Blog Management</h1>
            <hr />
            <h2>Add Blog Post</h2>
            <form id="add-blog-post" method="post" action="add.php">