Databases Reference
In-Depth Information
{
$db = Zend::registry('db');
$view = Zend::registry('view');
$view → title = 'Please Vote for this week's movie';
$view → sum = $db → showVote();
$view → result = $db → showData();
echo $view → render('vote.php');
}
}
?>
We would like to display the survey results currently in the database on the main
page. To do that, we created a voteAction() method, where we initialized the
database and view registries. Then, we call a database method
db
→
showVote()
which queries the movie_names table and gets the sum of the votes. Then, we
used the sum to create the percentage of the votes and called another database
method
db
→
showData()
to get the movie names. Finally, we rendered the page
to display the movie names along with the percentage of votes. We also allowed
users to add a new movie name and vote for the new suggestions. We handled
the database insert for the new movie suggestion in AddController.php. We
created a movieAction(), which uses dbAdapter method insert() to add movie into
the db2 table. The code in Example 3-9 shows AddController.php. The
movieAction() method calls database functions to perform insertions and
updates.
Example 3-9 AddController.php
<?php
Zend::loadClass('Zend_Controller_Action');
class AddController extends Zend_Controller_Action
{
function indexAction()
{
$this → _redirect('/');
}
function __call($action, $arguments)
{
$this → _redirect('/');
}
function movieAction()
{
$view → title = 'Name your favorite movie of the week';
$movie_name = $_POST['movie_name'];
$votevalue = $_POST['vote'];
$db = Zend::registry('db');
Search WWH ::
Custom Search