wiki:HelloSimpledb

Your Hello World Applicationclick here

Say hello with html formated text from an extern fileclick here

Say hello with a simple database queryclick here

Say hello in many languages with a database queryclick here



Say hello with a simple database query

Now we are going to use the database. Instead of writing the hello message directly into the code, it will be written in the mysql table. This are the mysql commands to create the table "hello" in the BeWelcome database (mtb) and to insert the message into the table:

USE mtb;
CREATE TABLE `hello`(`id` int(11) NOT NULL auto_increment, `message` text NOT NULL, `user` varchar(80) NOT NULL,
                     `language` varchar(80) NOT NULL, PRIMARY KEY (`id`)) TYPE=InnoDB;

INSERT INTO `hello` (`id`, `message`, `user`, `language`) VALUES
                    (1, 'hello world, be welcome!', 'Peter', 'English');

The table has four fields: id, message, user and language. At the moment we have the user Peter, his language is English and his message is "hello world, be welcome!". The id of this message is 1. Later we can fill the table with more messages in other languages from other users. The field id is the Primary Key: Each hello message will have an own id number to identify it and distinguish from the other messages. With the id number we can show our message on the BW site by adding a database query to the file hello.model.php. This will be done with the function getMessage(). The id number can be passed as parameter.

hello.model.phpcomments
<?php
class Hello extends PAppModel {
public function __construct() {
parent::__construct();
}
public function getMessage($id) {
$result="";
$str = "SELECT message, user, language FROM hello WHERE id='$id'";
$qry = $this->dao->query($str);
$result = $qry->fetch(PDB::FETCH_OBJ);
return $result;
}
the function which returns the message with the selected id from the database
the variable $str contains the databasequery, the result is written in the variable $result
}
?>


Now we need a place where we can call the function getMessage(). This will be done from the controller file hello.ctrl.php with this->_model->getMessage($id):

hello.ctrl.phpcomments
<?php
class HelloController extends PAppController {

private $_model;
private $_view;

public function __construct() {
parent::__construct();
$this->_model = new Hello();
$this->_view = new HelloView($this->_model);
}

public function __destruct(){
unset($this->_model);BR] unset($this->_view);[[BR? }

public function index() {
ob_start();
$messageid=1;
$hellotext=$this->_model->getMessage($messageid);
$this->_view->hello($hellotext);


The message with id=1 shall be fetched from the database.
The result of the function getMessage() from the file hello.model.php is stored in the variable $hellotext.
afterwards $hellotext is passed to the class HelloView and the function hello() will show the message on the website
$str = ob_get_contents();
ob_end_clean();
$Page = PVars::getObj('page');
$Page->content .= $str;
}

}
?>


In the file hello.view.php just a small change: instead of function hello() there is a parameter as input value: function($hellotext).

<?php

class HelloView extends PAppView {
private $_model;

public function __construct(Hello $model) {
$this->_model = $model;
}

public function hello($hellotext) {
require TEMPLATE_DIR.'apps/hello/hello.php'; ;
}
$hellotext is the array with the results from the database query

}
?>

the file bewelcome/templates/apps/hell/hello.php has to echo the object fetched from the database:

<?php
echo "<b> $hellotext->user </b> speaks <b> $hellotext->language </b> and says:<br> <i> $hellotext->message </i> ";
?>

todo: - missing screenshot...




only logged in users

If you want only logged in users to see a message or do an action, make something like this (not tested if it works): 

if (!($User = APP_User::login())) {
                        return false;
                }


next step:

Say hello in many languages with a database queryclick here