Tutorial
This is a tutorial of creating blog.
Use database.
Cheetan can deal with mysql, postgresql, textsql by default. You
can use any sql by easy modify. I use mysql in this tutorial. You set up database
in function 'config_database' which is called by framework. Example is as follows.
You usually write this function in config.php in order to refer from other sources.
| config.php |
<?php
function config_database( &$db )
{
$db->add( "", "localhost", "user", "password", "dbname" );
}
?>
|
Use Model
Model is class for deal width table. You can deal with table without write
source by use Model. Let's try. First of all, create table for blog like this.
CREATE TABLE `blog_data` (
`id` int(11) NOT NULL auto_increment,
`title` text NOT NULL,
`body` text NOT NULL,
`modified` timestamp NOT NULL
default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
);
|
Next, create Model. I put it in models directory in this example.
| models/blog_data.php |
<?php
class CBlog_data extends CModel
{
}
?>
|
Oh, nothing is written...? Yes. You can deal with table by only this code.
Look at latter explanation to know how to use this. It's very easy. Then, write
next function called by framework for regist this model in config.php.
| config.php |
function config_models( &$controller )
{
$controller->AddModel( dirname(__FILE__) . "/models/blog_data.php" );
}
|
Use View
View is a part of actual HTML code. This framework provide you next four way
to write HTML.
- Write in PHP file directly
- Separate PHP file and template like smarty.( It's faster than smarty because
of no parsing. )
- Prepare common template for all pages and write in PHP file directly.
- Prepare common template for all pages and separate PHP file and template
file.
Look at the part to know detail. I use the fourth way in this tutorial.
First, create common template
| template.html |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-JP">
<title>Cheetan blog</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
<?php print $this->content(); ?>
</body>
</html>
|
Each pages contents is displayed at $this->content(); in this case.
Next, create page to post blog data. You only write as follows because common
template is used.
| add.html |
<form method="post" action="add.php">
title<br>
<input type="text" name="blog/title"><br>
body<br>
<textarea cols=40 rows=8 name="blog/body"></textarea><br>
<input type="submit" value="write">
</form>
|
Use Controller
Controller chain Model and View you created and make them work. Filename of
Controller is 'filename of view'.php First of all look at how small source code
to save post data.
| add.php |
<?php
require_once( "config.php" );
require_once( "cheetan.php" );
function action( &$c )
{
if( count( $_POST ) )
{
$c->blog_data->insert( $c->data["blog"] );
}
}
?>
|
You can see Model blog_data is used here. Framework automatically put them
in array '$data["blog"]' that post data which name is specified like
'blog/'.
Like this, you collect necessary data in array and call funtion 'insert'.
Display posts
Let's display list of posts.
| view.php |
<?php
require_once( "config.php" );
require_once( "cheetan.php" );
function action( &$c )
{
$c->set( "datas", $c->blog_data->find() );
}
?>
|
| view.html |
<?php foreach( $data["datas"] as $data ){ ?>
<table>
<tr>
<td><?php print $data["title"]; ?></td>
<td><?php print str_replace( "\n", "<br>", $data["body"] ); ?></td>
<td><?php print $data["modified"]; ?></td>
</tr>
</table>
<?php } ?>
|
You can refer value in View as $data by function 'set'.
Edit post
Let's edit data.
| edit.php |
<?php
require_once( "config.php" );
require_once( "cheetan.php" );
function action( &$c )
{
if( count( $_POST ) )
{
$c->blog_data->update( $c->data["blog"] );
}
$c->set( "data", $c->blog_data->find( "id=" . $_GET["id"] ) );
}
?>
|
| edit.html |
<form method="post" action="edit.php">
title<br>
<input type="text" name="blog/title"
value="<?php print $data["data"]["title"]; ?>"><br>
body<br>
<textarea cols=40 rows=6 name="blog/body">
<?php print $data["data"]["body"]; ?></textarea><br>
<input type="hidden" name="blog/id"
value="<?php print $data["data"]["id"]; ?>">
<input type="submit" value="modify">
</form>
|
Delete data
Let's delete data.
| del.php |
<?php
require_once( "config.php" );
require_once( "cheetan.php" );
function action( &$c )
{
if( count( $_POST ) )
{
$c->blog_data->del( "id=" . $_POST["id"] );
}
$c->set( "data", $c->blog_data->find( "id=" . $_GET["id"] ) );
}
?>
|
| del.html |
<form method="post" action="edit.php">
Can I delete data?<br>
title<br>
<?php print $data["data"]["title"]; ?><br>
body<br>
<?php print $data["data"]["body"]; ?><br>
<input type="hidden" name="id" value="<?php print $data["data"]["id"]; ?>">
<input type="submit" value="del">
</form>
|
End
You could create blog. You can create application easily like this. You must
enable to create program without framework.
Other useful functions are prepared in Cheetan. Try others with the manual.
Sample
Access here to view or get sample.
|