PHP Classes
elePHPant
Icontem

File: blog.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Orazio Principe  >  DB Persister  >  blog.php  >  Download  
File: blog.php
Role: Example script
Content type: text/plain
Description: write your own blog with dbpersister support helper
Class: DB Persister
Store and retrieve objects in database tables
Author: By
Last change:
Date: 2 years ago
Size: 3,614 bytes
 

Contents

Class file image Download
<?php
require_once("Post.php");

$post = new Post();
if(!empty(
$_GET['idMessage'])) {
   
$post->findByPrimaryKey(@$_GET['idMessage']);
}

function
savePost()
{
    global
$post;
   
   
//Try to load the record if it's presents into posts variables
   
$post->findByPrimaryKey(@$_POST['idPost']);
   
   
//setup the object
   
$post->setFromSubmit();
   
   
//Correct dates formats
   
if(empty($post->dateCreated)) $post->dateCreated = date("Y-m-d H:i:s");
   
$post->dateEdited = date("Y-m-d H:i:s");
   
   
//Just save...
   
$post->save();
   
   
//Redirect to form/list page
   
header("Location: index.php", TRUE, 302);
   
}


//Parsing operations
if(!empty($_POST['cmdSave'])) {
   
savePost();
}

if(!empty(
$_GET['cmdDelete'])) {
   
$post->delete();
   
   
//Redirect to form/list page
   
header("Location: index.php", TRUE, 302);
}

?>
<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
        <div style="border: 1px solid black; padding: 5px; width: 500px; margin: auto">
            <h1>My DBPersister Blog</h1>
        </div>
       
        <form action="" method="post" >
           
            <input type="hidden" name="idMessage" id="idMessage" value="<?=$post->idMessage?>" />
           
            <div style="border: 1px solid black; padding: 5px; width: 500px; margin: auto">
                <div style="float: left">Subject</div>
                <div style="float: left">
                    <input type="text" name="subject" id="subject" value="<?=$post->subject?>" />
                </div>
               
                <div style="clear: both"></div>
               
                <div>Message</div>
                <div>
                    <textarea name="message" id="message" cols="50" rows="5"><?=$post->message?></textarea>
                </div>

                <div>
                    <input type="submit" name="cmdSave" id="cmdSave" value=" SAVE " />
                </div>
               
                <div style="clear: both"></div>
            </div>
        </form>
       
        <div style="margin-top: 20px"></div>
       
        <?
        $mysqli
= $post->getDbClass();
       
$sql = "SELECT * FROM POSTS";
       
$rs = $mysqli->query($sql);
        while(
$row = $rs->fetch_object()):
           
?>

            <div style="border: 1px solid black; padding: 5px; width: 500px; margin: auto">
                <div style="background-color: #CCCCCC; color: yellow">
                    <div style="float: left"><a href="index.php?idMessage=<?=$row->idMessage?>">[EDIT]</a></div>
                    <div style="float: left"><a href="index.php?idMessage=<?=$row->idMessage?>&cmdDelete=1">[DELETE]</a></div>
                    <div style="clear: both"></div>
                </div>
               
                <div style="float: left"><?=$row->subject?></div>
                <div style="clear: both"></div>
               
                <div style="float: left"><?=$row->message?></div>
                <div style="clear: both"></div>
               
                <div style="float: left">Created: <?=$row->dateCreated?></div>
                <div style="clear: both"></div>
                <div style="float: left">Edited: <?=$row->dateEdited?></div>
                <div style="clear: both"></div>
            </div>
           
            <?
       
endwhile;
       
?>

      
        <script>
            document.getElementById("subject").focus();
        </script>
    </body>
</html>