What's new? | Help | Directory | Sign in
Google
wephp
Web.py philosophy brought to PHP.
  
  
  
  
    
Search
for
Updated Aug 11, 2007 by lmatteis
SimpleBlog  

Every framework has a tutorial on how to create a blog, so here it is:

index.php

<?php

require('wephp/web.php');

$urls = array(
    '/' => 'index',
    '/edit/*' => 'edit',
    '/delete/*' => 'delete',
    '/add' => 'add',
);

$db_parameters = array(
    'user'=> 'root',
    'pw' => '',
    'db' => 'wephp_blog'
);

//For the base url, change this
function base_url() {
    return '/news/wephp/index.php';
}

class index {
    function GET($path,$db) {
        $query = $db->query('SELECT * FROM posts');
        $ins['posts'] = $query->fetchall();
        echo web::render('index.php', $ins);
    }
}

class edit {
	function GET($path, $db) {
        $seg = $path->pathVars[1];
        $query = $db->query('SELECT * FROM posts WHERE id = '.$seg);
        //only need to fetch 1 here
        $ins['post'] = $query->fetch();
        echo web::render('edit.php', $ins);
    }
    
    function POST($path, $db) {
        $db->query("UPDATE posts SET title = '".$_POST['title']."', date = '".$_POST['date']."', text = '".$_POST['text']."' WHERE id = ".$_POST['id']);
        web::redirect(base_url());
    }

}

class delete {
    function GET($path, $db) {
        $seg = $path->pathVars[1];
        $db->query("DELETE FROM posts WHERE id = ".$seg);
        web::redirect(base_url());
    }
}

class add {
    function GET($path) {
        echo web::render('add.php');
    }
    
    function POST($path, $db) {
        $db->query("INSERT INTO posts (title, date, text) VALUES ('".$_POST['title']."', '".$_POST['date']."', '".$_POST['text']."')");
        web::redirect(base_url());
    }
}

web::run($urls, $db_parameters);

?>

templates/index.php

<? foreach($posts as $post): ?>

<h2><?=$post['title']?></h2>

<p>
<?=$post['text']?>
</p>

<a href="<?=base_url()?>/edit/<?=$post['id']?>">Edit</a> - <a href="<?=base_url()?>/delete/<?=$post['id']?>">Delete</a>

<? endforeach; ?>

<p><a href="<?=base_url()?>/add">Add new</a></p>

templates/add.php

<h2>Add new post</h2>

<form method="post" action="<?=base_url()?>/add">
Title <br/>
<input type="text" name="title" value="" /><br/>
Date <br/>
<input type="text" name="date" value="" /><br/>
Text<br/>
<textarea name="text"></textarea><br/>
<input type="submit" value="Edit" />
</form>

templates/edit.php

<h2>Edit post <?=$post['title']?></h2>

<form method="post" action="<?=base_url()?>/edit/<?=$post['id']?>">
Title <br/>
<input type="text" name="title" value="<?=$post['title']?>" /><br/>
Date <br/>
<input type="text" name="date" value="<?=$post['date']?>" /><br/>
Text<br/>
<textarea name="text"><?=$post['text']?></textarea><br/>
<input type="hidden" name="id" value="<?=$post['id']?>" />
<input type="submit" value="Edit" />
</form>

wephp_blog.sql

CREATE TABLE `posts` (
  `id` int(11) NOT NULL auto_increment,
  `title` varchar(255) NOT NULL,
  `text` text NOT NULL,
  `date` date NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;

Comment by bayuadji, Oct 11, 2007

Hi, I got a question,

when I called /edit/ i get 404 exception, so Should I make an change in .htaccess?

Comment by hackbard.celine, May 25, 2008

full of XSS and SQL injections ...


Sign in to add a comment