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 in many languages with a database query
In the last example there was just one row from the database. Now we want a list of more rows. Therefore we need an array, a while statement and a foreach loop.
The table with the messages needs more data:
USE mtb; INSERT INTO `hello` (`id`, `message`, `user`, `language`) VALUES (2, 'salut, soit bienvenue!', 'Sebastien', 'French'), (3, 'ciao, benvenuti!', 'Francesca', 'Italian'), (4, 'hallo, sei willkommen!', 'Angela', 'German'), (5, 'oi, bem vindo!', 'Joao', 'Portuguese');
Instead of only one single message with a certain id, this time a list of several messages are selected and stored in an array. The function to do this is called getMessages().
| hello.model.php | comments |
| <?php class Hello extends PAppModel { public function __construct() { parent::__construct(); } | |
| public function getMessages() { $result=""; $str = "SELECT message, user, language FROM hello WHERE id>=1"; $qry = $this->dao->query($str); while ($result = $qry->fetch(PDB::FETCH_OBJ)){ $hellolist[]=$result; }; return $hellolist; } | the query selects every row from the table hello. hellolist is an array. As long as there is a result, every fetched row is written into the array hellolist is returned |
| } ?> |
Now we need a place where we can call the function getMessages(). This will be done from the controller file hello.ctrl.php with this->_model->getMessage():
| hello.ctrl.php | comments |
| <?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(); $hellotext=$this->_model->getMessages(); $this->_view->hello($hellotext); | All messages shall be fetched from the database. No need to define an message-id. The result of the function getMessages() 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 list with the messages on the website |
| $str = ob_get_contents(); ob_end_clean(); $Page = PVars::getObj('page'); $Page->content .= $str; } | |
} ?> |
The file hello.view.php stays as it is.
The file bewelcome/templates/apps/hell/hello.php will echo the list of messages with a foreach loop.
<?php
foreach ($hellotext as $cnt=>$eachhello){
//[id]=> 1 [user]=>peter [message]=> hello world,be welcome
?>
<table bgcolor=#FFA100>
<tr>
<td width=250> <b> <?php echo $eachhello->user ?> </b> speaks <b> <?php echo $eachhello->language ?> </b> and says: </td>
</tr>
<tr>
<td width=250> <i><?php echo $eachhello->message ?></i> </td>
</tr>
</table> <br>
<?php
}
?>
todo: - missing screenshot...
Well, that was just simple example, but maybe it was a help to get started. Now it is up to you to build beautiful applications! Make meeting calendars, bike sharing apps, geosystems, blogs , wikis, rss feeds and so on.
Create a simple event & meeting calendar in BWRox
(still needs to be documented)


