blog::editContentsByTitle PHP Method

editContentsByTitle() public method

public editContentsByTitle ( $originalTitle, $contents )
    function editContentsByTitle($originalTitle, $contents)
    {
        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->xpath("blogPost[title='" . $originalTitle . "']") as $blogPost) {
                $blogPost->contents = html_entity_decode($contents, null, "UTF-8");
            }
            file_put_contents($_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . "data" . DIRECTORY_SEPARATOR . "blogPosts.xml", $blogPosts->asXML());
        } else {
            // PDO
            require_once $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . "classes" . DIRECTORY_SEPARATOR . "common.class.php";
            $common = new common();
            $dbh = $common->pdoOpen();
            $sql = "UPDATE " . $settings::db_prefix . "blogPosts SET contents = :contents WHERE title = :title";
            $sth = $dbh->prepare($sql);
            $sth->bindParam(':title', $originalTitle, PDO::PARAM_STR, 100);
            $sth->bindParam(':contents', $contents, PDO::PARAM_STR, 20000);
            $sth->execute();
            $sth = NULL;
            $dbh = NULL;
        }
    }

Usage Example

示例#1
0
require_once $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . "classes" . DIRECTORY_SEPARATOR . "common.class.php";
require_once $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . "classes" . DIRECTORY_SEPARATOR . "account.class.php";
require_once $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . "classes" . DIRECTORY_SEPARATOR . "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");
}
// Set updated variable to FALSE.
$updated = FALSE;
if ($common->postBack()) {
    // Update the contents of the blog post.
    $blog->editContentsByTitle($_POST['originalTitle'], $_POST['contents']);
    // Set updated to TRUE since settings were updated.
    $updated = TRUE;
}
// Get titles and dates for all blog posts.
$post = $blog->getPostByTitle(urldecode($_GET['title']));
////////////////
// BEGIN HTML
require_once '../includes/header.inc.php';
// Display the updated message if settings were updated.
if ($updated) {
    ?>
        <div id="contents-saved" class="alert alert-success fade in" role="alert">
            <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                <span aria-hidden="true">&times;</span>
            </button>
All Usage Examples Of blog::editContentsByTitle