|
|
|
Do you have 15 minutes? That will be
enough to write a simple guestbook with Celeroo Frame. So, assuming you
have installed Celeroo Frame, here are the next steps.
Let's create the database structure. We will need just one table. For
this example we have used the standard celeroo-frame_table prefix, but
you can use any:
CREATE TABLE `celerooframe_guestbooks` (
`id` int(10) unsigned NOT NULL auto_increment,
`name` varchar(100) NOT NULL default '',
`date` date NOT NULL default '0000-00-00',
`ip` varchar(14) NOT NULL default '',
`message` text NOT NULL,
`url` varchar(255) NOT NULL default '',
PRIMARY KEY (`id`)
);
We'll store the poster name, their IP (just in case), the message
posted and url of their website if entered.
Now, let's create the variable in defines.inc.php so you don't have to
use the table names directly in the code:
//names of the tables
$T_SETTINGS=$prefix.'settings';
$T_MESSAGES=$prefix.'guestbooks';
Now, let's create the model guestbook.php, which will
handle all the database operations: |
| |
class Guestbook extends Basic
{
//
}
For this simple guestbook you don't need to do anything in the class.
Note that we have named it with the same name as the database table
name, but in singular and starting with a capital letter. This will
ensure that the model will work with the right DB table. |
| |
That's everything you'll need in the
model. You see the Celeroo Frame database object is automatically
fetching arrays or single variables where you need them.
Now, it's time to create the controllers. You can go
with just one controller, but to keep the things cleaner and simpler,
we'll use two. The first will be loaded by default, therefore called main.php:
<?php
// default controller - list guestbook entries
$guestbook=new Guestbook();
list($messages,$count)=$guestbook->find();
$view="views/main.html";
?>
All we need to do here is to create an instance of the Guesbook model
and get the messages for the current along with the total number.
What happened? We used the built-in method find() of the Basic model
which is inherited in our Message model. The find() method allows you
to retrieve a single record by ID or other field, or retrieve all
records from the table if you don't pass an ID parameter.
The second parameter of the find() method is the $options array which
lets you define things like ordering, pagination and offset. We didn't
pass this array, because the default behavior of the method is to
return paginated results and the count of all results. That's why we
used list () to retrieve both values. In case you need a single record
or need no pagination, the find() method will not select the $count and
will return only the results of the query.
Then at the end of the controller we define the view. There is hard
rule for the name of the view, but it's always a good idea to use the
same or similar to the controller name.
Now, let's allow the users post messages with the controller post.php: |
| |
<?php
// post new guestbook entry
$guestbook=new Guestbook();
if(!empty($_POST["ok"]))
{
$_POST[ip]=$_SERVER['REMOTE_ADDR'];
$_POST[date]=date('Y-m-d');
$guestbook->add($_POST);
alert("Your
message is posted!");
jsredirect(SITE_URL);
}
else
{
$view="views/post.html";
}
?>
|
| |
This controller is
pretty straightforward. In the view we will have a field called "ok"
whose value will come when the form is submitted. Then we'll insert the
post in the database, alert the user that it's done and do a javascript
redirect to guestbook list page. Note that we didn't define the add()
method in our model, because it is inherited by the Basic object.
Before the form is submitted, the controller just displays the view.
You may notice there is no cleanup on the user's input. This is because
defines.inc.php already takes care for dangerous input like
<script> tags. If you with, you can strip all HTML tags, but in
the example we'll let the users enter them.
Now, here are the view main.html:
<div style="margin:100px;">
<h1>My Guestbook</h1>
<p align="center">
<a href="<?=SITE_URL?>post/">Leave Your
Message</a></p>
<?php if(!sizeof($messages)): ?>
<p align="center">There are no messages yet. You can be the
first!</p>
<?php endif; ?>
<?php foreach($messages as $cnt=>$message):
if($cnt%2==0)
$bgcolor='#eeeeee';
else
$bgcolor='#ffffff';
?>
<div
style="background:<?=$bgcolor?>;padding:10px;border:1pt solid
#aaaaaa; margin:5px;">
At
<?=dateformat($message[date])?>
<?php
if(!empty($message["url"])):?>
<a
href="<?=$message[url]?>"
target=_blank><?=$message[name]?></a>
<?php else: echo
"<strong>$message[name]</strong>";
endif; ?>
says:<br
/><br />
<?=nl2br($message[message])?>
</div>
<?php endforeach; ?>
<?php
echo nav_pages_se_friendly(SITE_URL."main",$count,$offset,10);
?>
</div>
There are few things to note here:
1. The alternative PHP Syntax is used for cleaner code. You are free to
use the standard PHP syntax if you wish. We don't use any template
language because there is no real benefit of that.
2. nav_pages() passes URL along with the controller action, although
even going to index.php will work. But nav pages does not prepend "?"
to the URL, so you need to give some arguments yourself.
Here is the view post.html:
<script language="javascript">
function validateForm(frm)
{
<?php
echo
jsvalidate(array("name","message"));
?>
return true;
}
</script>
<div style="margin:100px;">
<h1>Leave Your Message</h1>
<form method="post" onsubmit="return validateForm(this);">
<table align="center">
<tr>
<td width="120">Your
Name:</td>
<td width="260"><input
type="text" name="name" size="30"></td>
</tr>
<tr>
<td>Website (Optional):</td>
<td><input type="text"
name="url" size="30"></td>
</tr>
<tr>
<td>Your Message:</td>
<td><textarea name="message"
rows="5" cols="45"></textarea></td>
</tr>
<tr>
<td colspan="2" align="center">
<input
type="hidden" name="ok" value="1">
<input
type="submit" value="Post Message">
<input
type="button" value="Cancel"
onclick="window.location='<?=SITE_URL?>'">
</td>
</tr>
</table>
</form>
|
| |
Here you can see how the jsvalidate
function is doing basic form validation ensuring the required fields
are not empty.
Another thing to notice is that the form field names match exactly the
fields of the database. This is not required in the current
example. However, after we finish this one, we'll rewrite the
guestbook in a simple manner using the same views. Then this will be
important. Just read below.
So, that's all you need for a guestbook application if you follow the
MVC concepts. But the guestbooks are such simple things, can't we do
the things even quicker? |
| |
| Even
Quicker Guestbook Example
|
Fortunately Celeroo
Frame does not require you to use models if you don't want. You can
handle the simple operations in the controller and with the functions
in the DB helper to finish the task in 10 minutes. So if you want to
delete those models now, go ahead. Here is how your controllers will
look in this case:
main.php
<?php
// default controller - list guestbook
$q="SELECT * FROM $T_MESSAGES ORDER BY id DESC LIMIT $offset,10";
$messages=$DB->aq($q);
$q="SELECT COUNT(id) FROM $T_MESSAGES";
$count=$DB->oq($q);
$view="views/main.html";
?>
Pretty short, isn't it? Note that here we have used the variables for
the table names, because they are automatically available in the
controller. For anyone who doesn't like globals, well, there are always
constants. |
| |
The second controller
is also short and a bit more interesting:
post.php
<?php
// post new guestbook entry
if(!empty($_POST["ok"]))
{
$_POST["date"]=date("Y-m-d");
dumpto_table($T_MESSAGES,$_POST);
alert("Your
message is posted!");
jsredirect(SITE_URL);
}
else
{
$view="views/post.html";
}
?>
Here we have used dumpto_table to avoid writing a database query.
Because the web form fields match the database field names, using the
function is possible. See how we have explicitly put the current date
into $_POST so it gets entered in the DB too.
So if we exclude the views, how much actual PHP code have we written?
For the MVC example it took us 55 short lines of PHP
code.
The example without model was even less - 16 lines
PHP. This is how quick and light the applications written with Celeroo
Frame are. |
| |
|
 |
|
| Need
Help? Send us your queries on the framework
and/or the manual, and we will respond
promptly. |
| |
|
|