Make Web Site Using CakePHP

November 1, 2007 by hejian

Despite CakePHP already provided the powerful Scaffolding feature and the Bake code generator, also we need to understand the following ways to finish some site make tasks:

Display a form

<?php echo $html->formTag('/datas/function') ?>
<lable>Field1:</label>
<?php echo $html->inputTag('Data/field1', array('size' => '40')) ?>
<?php echo $html->tagErrorMsg('Data/field1', 'field1 is required') ?>
<lable>Field2:</label>
<?php echo $html->inputTag('Data/field2', array('size' => '255')) ?>
<?php echo $html->tagErrorMsg('Data/field2', 'field2 is invalid') ?>
<?php echo $html->submitTag('function') ?>
</form>

Data validation

var $validate = array (
'field1' => VALID_NOT_EMPTY,
'field2' => '/^.{6,40}$/',
'field3' => VALID_EMAIL
);

Save form data to database

if (!empty($this->data)) {
if ($this->Data->validates($this->data)) {
$this->Data->save($this->data);
$this->flash('Success messages');
} else {
$this->flash('Error messages');
}
}

Display a table

<table>
<?php
echo $html->tableHeaders(array_keys($datas[0]['Data']));
foreach ($datas as $data) {
echo $html->tableCells($data['Data'], array(’bgcolor’ => ‘#ddd’));
}
?>
</table>

Leave a Reply

You must be logged in to post a comment.

Wordpress template made by HeJian