Continuation of My First Simple CakePhp Webpage

June 18, 2007 at 5:38 am | Posted in tutorial | 1 Comment

Now we will be removing the var $scaffold. And create our own file in app/view. The file must be .thtml, the “t” in these thtml files indicates that these files are Cake templates. The default view is index.thtml.

1. Create a folder in cake\app\views. The name of the folder must be in plural form. (example: guestbooks). Each table has its own folder in cake\app\views and has its corresponding files inside those folders.

2. Create the file index.thtml.

Example code: (for guestbooks)

<html >

<head>

<title>Untitled Document</title>

</head>

<body>

<h1>My Guestbooks</h1>

<p>

<?php echo $html->link(‘Add Guest’, ‘/guestbooks/add’) ?></p>

<table>

<tr>

<th></th>

<th>Firstname</th>

<th>Lastname</th>

<th>Address</th>

<th>Description</th>

<th>Created</th>

<th>Modified</th>

</tr>

<?php foreach ($guestbooks as $guestbook): ?>

<tr>

<td><?php echo $guestbook‘Guesbook’][‘firstname’]; ?></td>

<td>

<a href=”/guestbooks/view/

<?php echo $guestbook[‘Guestbook‘][‘firstname’]?>”>

<?php echo $guestbook[‘Guestbook‘][‘lastname’]?>

</a>

</td>

<td>

<?php echo $guestbook[‘Guestbook‘][‘address’]?>

</td>

<td>

<?php echo $guestbook[‘Guestbook‘][‘description’]?>

</td>

<td>

<?php echo $guestbook[‘Guestbook‘][‘created’]; ?>

</td>

<td>

<?php echo $guestbook[‘Guestbook‘][‘modified’]; ?>

</td>

</tr>

<?php endforeach; ?>

</table>

</body>

</html>

3. Add a function index() in app/controllers/guestbooks_controller.php

function index()

{

$this->set(‘guestbooks’, $this->Guestbook->findAll());

}

4. Create an “ADD” function. Create the add.thtml in app/view/guestbooks.

<head>

<title>add a guest</title>

</head>

<body>

<h1>Add Guest/h1>

<form action=”<?php echo $html->url(“/guestbooks/add”); ?>” method=”post”>

<p>

Firstname:

<?php echo $html->input(‘Guestbook/firstname’, array(‘size’ => ’40’))?>

</p>

<p>

Lastname:

<?php echo $html->input(‘Guestbook/lastname’, array(‘size’ => ’40’))?>

</p>

<p>

Address:

<?php echo $html->input(‘Guestbook/address’, array(‘size’ => ’40’))?>

</p>

<p>

Description:

<?php echo $html->textarea(‘Guestbook/description’) ?>

</p>

<p>

<?php echo $html->submit(‘Save’) ?>

</p>

</form>

5. Add the function add.thtml in app/controllers/guestbook_controller.php

function add()

{

if (!empty($this->data[‘Guestbook’]))

{

if($this->Note->save($this->data[‘Guestbook’]))

{

$this->flash(‘Your guestbook has been updated.’,’/guestbooks/’);

}

}

}

6. Create the app\view\guestbooks\view.thtml

<html>

<head>

<title>View Guestbook</title>

</head>

<body>

<h1><?php echo $data[‘Guestbook’][‘firstname’]?></h1>

<h1><?php echo $data[‘Guestbook’][‘lastname’]?></h1>

<h1><?php echo $data[‘Guestbook’][‘address’]?></h1>

<h1><?php echo $data[‘Guestbook’][‘description’]?></h1>

<p><small>

Created: <?php echo $data[‘Guestbook’][‘created’]?>

</small></p>

<p><?php echo $data[‘Guestbook’][‘modified’]?></p>

</body>

</html>

7. Create the app\view\guestbooks\view.thtml

function view($id)

{

$this->Guestbook->id = $id;

$this->set(‘data’, $this->Guestbook->read());

}

8. Creating the app\\view\guestbooks\edit.thtml

<h1>Edit</h1>
<form action="<?php echo $html->url('/guestbooks/edit')?>" method="post">
<?php echo $html->hidden('Guestbook/id'); ?>
<p>
Firstname:
<?php echo $html->input('Guestbook/firstname', array('size' => '40'))?>
</p>

<p>
Lastname:
<?php echo $html->input('Guestbook/lastname', array('size' => '40'))?>
</p>

<p>
Address:
<?php echo $html->input('Guestbook/address', array('size' => '40'))?>
</p>

<p>
Description:
<?php echo $html->textarea('Guestbook/description') ?>
</p>
<p>
<?php echo $html->submit('Save') ?>
/p>
</form>

9. Add the function in /app/controllers/guestbooks_controller.php

 

function edit($id = null)
{
if (empty($this->data['Guestbook']))
{
$this->Guestbook->id = $id;
$this->data = $this->Guestbook->read();
}
else

{
if($this->
Guestbook->save($this->data['Guestbook']))
{
$this->flash('It has been updated.','/guestbooks/');
}
}
}

10. Add the function delete() in app\\controllers\guestbooks_controller.php

 

function delete($id)
{
if ($this->Guestbook->
del($id))
{
$this->flash('The guest with id: '.$id.' has been deleted.', '/guestbooks');
}
}

 

 

Advertisements

1 Comment »

RSS feed for comments on this post. TrackBack URI

  1. Hey All ! ! !
    Successful people say “Time is money”,
    go Clock ,
    and make sure that …


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.
Entries and comments feeds.

%d bloggers like this: