Headline

Web programming in Language:PHP with the Technology:Zend framework

Motivation

Language:PHP is one of the most commonly used languages for web programming. To get a more structured and valuable implementation, it is reasonable to choose a PHP based web-application framework. The Technology:Zend framework provides a good infrastructure and a large amount of pre-assembled components and supports form-based web development. The use of PHP suggests an Language:SQL DBMS like Technology:MySQL, to allow persistence. The connection to the DBMS is provided by the database adapter included in the Zend framework.

Illustration

This section is divided into three parts. The first part describes the mapping of the entities, the second part describes the implementation of Feature:Total and Feature:Cut and the third part describes the development of the GUI.

Object-Relational mapping

Feature:Persistence is provided by a Technology:MySQL DBMS (see Contribution:mySqlMany) and the zend adapter for this specific database. The adapter is generated automatically according to the corresponding entries in the file application.ini:

<source lang="php"> resources.db.adapter = "PDO MYSQL" resources.db.params.host = "localhost" resources.db.params.username = "root" resources.db.params.password = "" resources.db.params.dbname = "test" </source>

The entities of the company are mapped by specific classes, which are extended by the class Zend Db Table Abstract. They create simple associative arrays out of the declared tables. If we assume, that associative arrays are objects, we can call this process Object-Relational mapping. To map a table, it is only necessary to specify the name of the designated table within the corresponding class. The class <source lang="php" enclose="none">Application Model DbTable Employee</source> in Employee.php illustrates the mapping of the employee table:

<source lang="php" source="zend/application/models/DbTable/Employee.php"> class Application Model DbTable Employee extends Zend Db Table Abstract { protected $ name = 'employee'; ... } </source>

Feature implementation

The features Feature:Total and Feature:Cut are both implemented in the model Employee.php in the sense of MVC. The reason is, that these methods affect only employees, depend on the corresponding company or department. The following example shows the calculation of the Feature:Total value for a company with a given $id.

<source lang="php" source="zend/application/models/DbTable/Employee.php"> class Application Model DbTable Employee extends Zend Db Table Abstract { ... public function getTotalForCompany($id) { // cast the id to integer $id = (int)$id; // get all employees from the database // - with the given company cid // - as an array composed of associative arrays for each employee $rows = $this->fetchAll('cid = ' . $id); $total = 0; // walk through the array and add every salary foreach ($rows as $row) { $total += $row->salary; } return $total; } } </source>

GUI

It is possible to create the GUI using a native Language:HTML-Language:PHP web page. The other way is to create simple forms by using the extension

Zend Form
. Technology:Zend framework offers a great support for form based websites. That means, that it is possible to generate forms with almost no Language:HTML code, because the web-application framework automatically generates the website out of the pre-defined PHP objects. The objects are initialized and added to the specific form in a developer-defined order. The following example shows the company form. The different GUI components are explained in the comments:

<source lang="php" source="zend/application/forms/Company.php"> class Application Form Company extends Zend Form { var $departmentList; public function init() { // The field helps to identify the form within the application. $this->setName('company'); // The hidden field keeps the company id for further use. $id = new Zend Form Element Hidden('id'); $id->addFilter('Int'); // The name field contains the name of the company. $name = new Zend Form Element Text('name'); $name ->setLabel('Name') ->setRequired(true) ->addFilter('StripTags') ->addFilter('StringTrim') ->addValidator('NotEmpty'); // The button invokes the function to save a new name. $submit = new Zend Form Element Submit('save'); $submit ->setAttrib('id', 'submitbutton') ->setOptions(array('class' => 'button')); // The following list shows all subdepartments of the company. $this->departmentList = new Zend Form Element Select('departments'); $this->departmentList ->setLabel('Departments'); $this->departmentList->setRegisterInArrayValidator(false); // The button is pressed to select a department out of the list. $select = new Zend Form Element Submit('select'); $select ->setAttrib('id', 'submitbutton'); // The textfield shows the total value. $total = new Zend Form Element Text('total', array("readonly" => "readonly")); $total ->setLabel('Total'); // The button is used to cut the company. $cut = new Zend Form Element Submit('cut'); $cut->setAttrib('id', 'submitbutton'); // There is a block created in the GUI for each array added in the following way: $this->addElements(array($id, $name, $submit)); $this->addElements(array($this->departmentList, $select)); $this->addElements(array($total, $cut)); } ... } </source>

If the controller wants to fill in some data into the total (

$total
) field, it must call the method
populate
of the previously initialized form. If the array parameter of the method contains a field
total
, the textfield with the identifier "total" is filled automatically with the value contained in the array:

<source lang="php" source="zend/application/forms/Company.php"> ... $c = $company->getCompany($id); $c<a href="total">total</a> = $employee->getTotalForCompany($id); $form->populate($c); ... </source>

Architecture

Usage

This project needs a running Technology:MySQL-database. We recommend Technology:XAMPP. It also needs a Web browser. You can use the company.sql and sampleCompany.sql of Contribution:mySqlMany for this project.

After the database has started:

  • Download the sources.
  • Copy the complete zend folder to the htdocs-directory of your XAMPP-installation.
  • Start your Web browser and go to http://localhost/zend/public/.
If you want to continue with the development of this contribution, please download Technology:NetBeans and import the project into the IDE.

Metadata


There are no revisions for this page.

User contributions

    This user never has never made submissions.

    User edits

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.

    10 most similar pages:

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.