<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://docs.sandbox.joomla.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Tonie</id>
	<title>Joomla! Documentation - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://docs.sandbox.joomla.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Tonie"/>
	<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/Special:Contributions/Tonie"/>
	<updated>2026-09-08T05:01:55Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.43.0</generator>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Introduction&amp;diff=9535</id>
		<title>J1.5:Developing a MVC Component/Introduction</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Introduction&amp;diff=9535"/>
		<updated>2008-07-27T21:40:36Z</updated>

		<summary type="html">&lt;p&gt;Tonie: /* Creating the Entry Point */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
A software framework is the base of an application that can be used by a developer. The framework in Joomla! 1.5 unleashes a great deal of power for them. The Joomla! code has been completely overhauled and cleaned up. This tutorial will guide you through the process of developing a component using the framework.&lt;br /&gt;
&lt;br /&gt;
The scope of this project will be to develop a simple Hello World! component. In future tutorials, this simple framework will be built upon to show the full power and versatility of the MVC design pattern in Joomla!&lt;br /&gt;
&lt;br /&gt;
== Requirements ==&lt;br /&gt;
You need Joomla! 1.5 or greater for this tutorial.&lt;br /&gt;
&lt;br /&gt;
== Introduction to Model-View-Controller ==&lt;br /&gt;
While the idea behind a component may seem extremely simple, code can quickly become very complex as additional features are added or the interface is customized.&lt;br /&gt;
&lt;br /&gt;
Model-View-Controller (herein referred to as MVC) is a software design pattern that can be used to organize code in such a way that the business logic and data presentation are separate. The premise behind this approach is that if the business logic is grouped into one section, then the interface and user interaction that surrounds the data can revised and customized without having to reprogram the business logic.&lt;br /&gt;
&lt;br /&gt;
There are three main parts of an MVC component. They are described here in brief, but for a more thorough explanation, please refer to the links provided at the end of this tutorial.&lt;br /&gt;
&lt;br /&gt;
=== Model ===&lt;br /&gt;
The model is the part of the component that encapsulates the application&#039;s data. It will often provide routines to manage and manipulate this data in a meaningful way in addition to routines that retrieve the data from the model. In our case, the model will contain methods to add, remove and update information about the greetings in the database. It will also contain methods to retrieve the list of greetings from the database. In general, the underlying data access technique should be encapsulated in the model. In this way, if an application is to be moved from a system that utilizes a flat file to store its information to a system that uses a database, the model is the only element that needs to be changed, not the view or the controller.&lt;br /&gt;
&lt;br /&gt;
=== View ===&lt;br /&gt;
The view is the part of the component that is used to render the data from the model in a manner that is suitable for interaction. For a web-based application, the view would generally be an HTML page that is returned to the data. The view pulls data from the model (which is passed to it from the controller) and feeds the data into a template which is populated and presented to the user. The view does not cause the data to be modified in any way, it only displays data retrieved from the model.&lt;br /&gt;
&lt;br /&gt;
=== Controller ===&lt;br /&gt;
The controller is responsible for responding to user actions. In the case of a web application, a user action is (generally) a page request. The controller will determine what request is being made by the user and respond appropriately by triggering the model to manipulate the data appropriately and passing the model into the view. The controller does not display the data in the model, it only triggers methods in the model which modify the data, and then pass the model into the view which displays the data.&lt;br /&gt;
&lt;br /&gt;
== Joomla! MVC Implementation ==&lt;br /&gt;
In Joomla!, the MVC pattern is implemented using three classes: [http://api.joomla.org/Joomla-Framework/Application/JModel.html JModel], [http://api.joomla.org/Joomla-Framework/Application/JView.html JView] and [http://api.joomla.org/Joomla-Framework/Application/JController.html JController]. For more detailed information about these classes, please refer to the API reference documentation (WIP).&lt;br /&gt;
&lt;br /&gt;
== Creating a Component ==&lt;br /&gt;
For our basic component, we only require five files:&lt;br /&gt;
&lt;br /&gt;
* hello.php - this is the entry point to our component&lt;br /&gt;
* controller.php - this file contains our base controller&lt;br /&gt;
* views/hello/view.html.php - this file retrieves the necessary data and pushes it into the template&lt;br /&gt;
* views/hello/tmpl/default.php - this is the template for our output&lt;br /&gt;
* hello.xml - this is an XML file that tells Joomla! how to install our component.&lt;br /&gt;
&lt;br /&gt;
=== Creating the Entry Point ===&lt;br /&gt;
Joomla! is always accessed through a single point of entry: index.php for the Site Application or administrator/index.php for the Administrator Application. The application will then load the required component, based on the value of &#039;option&#039; in the URL or in the POST data. For our component, the URL would be:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;index.php?option=com_hello&amp;amp;view=hello&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will load our main file, which can be seen as the single point of entry for our component: components/com_hello/hello.php.&lt;br /&gt;
&lt;br /&gt;
The code for this file is fairly typical across components.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * components/com_hello/hello.php&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_1&lt;br /&gt;
 * @license    GNU/GPL&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
// No direct access&lt;br /&gt;
defined( &#039;_JEXEC&#039; ) or die( &#039;Restricted access&#039; );&lt;br /&gt;
&lt;br /&gt;
// Require the base controller&lt;br /&gt;
&lt;br /&gt;
require_once( JPATH_COMPONENT.DS.&#039;controller.php&#039; );&lt;br /&gt;
&lt;br /&gt;
// Require specific controller if requested&lt;br /&gt;
if($controller = JRequest::getWord(&#039;controller&#039;)) {&lt;br /&gt;
    $path = JPATH_COMPONENT.DS.&#039;controllers&#039;.DS.$controller.&#039;.php&#039;;&lt;br /&gt;
    if (file_exists($path)) {&lt;br /&gt;
        require_once $path;&lt;br /&gt;
    } else {&lt;br /&gt;
        $controller = &#039;&#039;;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Create the controller&lt;br /&gt;
$classname    = &#039;HelloController&#039;.$controller;&lt;br /&gt;
$controller   = new $classname( );&lt;br /&gt;
&lt;br /&gt;
// Perform the Request task&lt;br /&gt;
$controller-&amp;gt;execute( JRequest::getVar( &#039;task&#039; ) );&lt;br /&gt;
&lt;br /&gt;
// Redirect if set by the controller&lt;br /&gt;
$controller-&amp;gt;redirect();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first statement is a security check.&lt;br /&gt;
&lt;br /&gt;
JPATH_COMPONENT is the absolute path to the current component, in our case components/com_hello. If you specifically need either the Site component or the Administrator component, you can use JPATH_COMPONENT_SITE or JPATH_COMPONENT_ADMINISTRATOR.&lt;br /&gt;
&lt;br /&gt;
DS is the directory separator of your system: either &#039;/&#039; or &#039;\&#039;. This is automatically set by the framework so the developer doesn&#039;t have to worry about developing different versions for different server OSs. DS should always be used when referring to files on the local server.&lt;br /&gt;
&lt;br /&gt;
After loading the base controller, we check if a specific controller is needed. In this component, the base controller is the only controller, but we will leave this here for future use.&lt;br /&gt;
&lt;br /&gt;
JRequest:getVar() finds a variable in the URL or the POST data. So if our URL is index.php?option=com_hello&amp;gt;controller=controller_name, then we can retrieve our controller name in our component using: echo JRequest::getVar(&#039;controller&#039;);&lt;br /&gt;
&lt;br /&gt;
Now we have our base controller &#039;HelloController&#039; in com_hello/controller.php, and, if needed, additional controllers like &#039;HelloControllerController1&#039; in com_hello/controllers/controller1.php. Using this standard naming scheme will make things easy later on: &#039;{Componentname}{Controller}{Controllername}&#039;&lt;br /&gt;
&lt;br /&gt;
After the controller is created, we instruct the controller to execute the task, as defined in the URL: index.php?option=com_hello&amp;amp;task=sometask. If no task is set, the default task &#039;display&#039; will be assumed. When display is used, the &#039;view&#039; variable will decide what will be displayed. Other common tasks are save, edit, new...&lt;br /&gt;
&lt;br /&gt;
The controller might decide to redirect the page, usually after a task like &#039;save&#039; has been completed. This last statement takes care of the actual redirection.&lt;br /&gt;
&lt;br /&gt;
The main entry point (hello.php) essentially passes control to the controller, which handles performing the task that was specified in the request.&lt;br /&gt;
&lt;br /&gt;
Note that we don&#039;t use a closing php tag in this file: ?&amp;gt;. The reason for this is that we will not have any unwanted whitespace in the output code. This is default practice since Joomla! 1.5, and will be used for all php-only files.&lt;br /&gt;
&lt;br /&gt;
=== Creating the Controller ===&lt;br /&gt;
Our component only has one task - greet the world. Therefore, the controller will be very simple. No data manipulation is required. All that needs to be done is the appropriate view loaded. We will have only one method in our controller: display(). Most of the required functionality is built into the JController class, so all that we need to do is invoke the JController::display() method.&lt;br /&gt;
&lt;br /&gt;
The code for the base controller is:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_1&lt;br /&gt;
 * @license    GNU/GPL&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// No direct access&lt;br /&gt;
&lt;br /&gt;
defined( &#039;_JEXEC&#039; ) or die( &#039;Restricted access&#039; );&lt;br /&gt;
&lt;br /&gt;
jimport(&#039;joomla.application.component.controller&#039;);&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Hello World Component Controller&lt;br /&gt;
 *&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 */&lt;br /&gt;
class HelloController extends JController&lt;br /&gt;
{&lt;br /&gt;
    /**&lt;br /&gt;
     * Method to display the view&lt;br /&gt;
     *&lt;br /&gt;
     * @access    public&lt;br /&gt;
     */&lt;br /&gt;
    function display()&lt;br /&gt;
    {&lt;br /&gt;
        parent::display();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The JController constructor will always register a display() task and unless otherwise specified (using the registerDefaultTask() method), it will set it as the default task.&lt;br /&gt;
&lt;br /&gt;
This barebones display() method isn&#039;t really even necessary since all it does is invoke the parent constructor. However, it is a good visual clue to indicate what is happening in the controller.&lt;br /&gt;
&lt;br /&gt;
The JController::display() method will determine the name of the view and layout from the request and load that view and set the layout. When you create a menu item for your component, the menu manager will allow the administrator to select the view that they would like the menu link to display and to specify the layout. A view usually refers to a view of a certain set of data (i.e. a list of cars, a list of events, a single car, a single event). A layout is a way that that view is organized.&lt;br /&gt;
&lt;br /&gt;
In our component, we will have a single view called hello, and a single layout (default).&lt;br /&gt;
&lt;br /&gt;
=== Creating the View ===&lt;br /&gt;
The task of the view is very simple: It retrieves the data to be displayed and pushes it into the template. Data is pushed into the template using the JView::assignRef method.&lt;br /&gt;
&lt;br /&gt;
The code for the view is:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_1&lt;br /&gt;
 * @license    GNU/GPL&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
// no direct access&lt;br /&gt;
&lt;br /&gt;
defined( &#039;_JEXEC&#039; ) or die( &#039;Restricted access&#039; );&lt;br /&gt;
&lt;br /&gt;
jimport( &#039;joomla.application.component.view&#039;);&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * HTML View class for the HelloWorld Component&lt;br /&gt;
 *&lt;br /&gt;
 * @package    HelloWorld&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
class HelloViewHello extends JView&lt;br /&gt;
{&lt;br /&gt;
    function display($tpl = null)&lt;br /&gt;
    {&lt;br /&gt;
        $greeting = &amp;quot;Hello World!&amp;quot;;&lt;br /&gt;
        $this-&amp;gt;assignRef( &#039;greeting&#039;, $greeting );&lt;br /&gt;
&lt;br /&gt;
        parent::display($tpl);&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Creating the Template ====&lt;br /&gt;
&lt;br /&gt;
Joomla! templates/layouts are regular PHP files that are used to layout the data from the view in a particular manner. The variables assigned by the JView::assignRef method can be accessed from the template using $this-&amp;gt;{propertyname} (see the template code below for an example).&lt;br /&gt;
&lt;br /&gt;
Our template is very simple: we only want to display the greeting that was passed in from the view:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
&lt;br /&gt;
// No direct access&lt;br /&gt;
&lt;br /&gt;
defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;); ?&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;&amp;lt;?php echo $this-&amp;gt;greeting; ?&amp;gt;&amp;lt;/h1&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Wrapping It All Up - Creating the hello.xml File ===&lt;br /&gt;
It is possible to install a component manually by copying the files using an FTP client and modifying the database tables. It is more efficient to create a package file that will allow the Joomla! Installer to do this for you. This package file contains a variety of information:&lt;br /&gt;
&lt;br /&gt;
* basic descriptive details about your component (i.e. name), and optionally, a description, copyright and license information.&lt;br /&gt;
* a list of files that need to be copied.&lt;br /&gt;
* optionally, a PHP file that performs additional install and uninstall operations.&lt;br /&gt;
* optionally, an SQL file which contains database queries that should be executed upon install/uninstall&lt;br /&gt;
&lt;br /&gt;
The format of the XML file is as follows:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;&lt;br /&gt;
&amp;lt;install type=&amp;quot;component&amp;quot; version=&amp;quot;1.5.0&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;name&amp;gt;Hello&amp;lt;/name&amp;gt;&lt;br /&gt;
 &amp;lt;!-- The following elements are optional and free of formatting conttraints --&amp;gt;&lt;br /&gt;
 &amp;lt;creationDate&amp;gt;2007-02-22&amp;lt;/creationDate&amp;gt;&lt;br /&gt;
 &amp;lt;author&amp;gt;John Doe&amp;lt;/author&amp;gt;&lt;br /&gt;
 &amp;lt;authorEmail&amp;gt;john.doe@example.org&amp;lt;/authorEmail&amp;gt;&lt;br /&gt;
 &amp;lt;authorUrl&amp;gt;http://www.example.org&amp;lt;/authorUrl&amp;gt;&lt;br /&gt;
 &amp;lt;copyright&amp;gt;Copyright Info&amp;lt;/copyright&amp;gt;&lt;br /&gt;
 &amp;lt;license&amp;gt;License Info&amp;lt;/license&amp;gt;&lt;br /&gt;
 &amp;lt;!--  The version string is recorded in the components table --&amp;gt;&lt;br /&gt;
 &amp;lt;version&amp;gt;1.01&amp;lt;/version&amp;gt;&lt;br /&gt;
 &amp;lt;!-- The description is optional and defaults to the name --&amp;gt;&lt;br /&gt;
 &amp;lt;description&amp;gt;Description of the component ...&amp;lt;/description&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;!-- Site Main File Copy Section --&amp;gt;&lt;br /&gt;
 &amp;lt;!-- Note the folder attribute: This attribute describes the folder&lt;br /&gt;
      to copy FROM in the package to install therefore files copied&lt;br /&gt;
      in this section are copied from /site/ in the package --&amp;gt;&lt;br /&gt;
 &amp;lt;files folder=&amp;quot;site&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;controller.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/view.html.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/tmpl/default.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/tmpl/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
 &amp;lt;/files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;administration&amp;gt;&lt;br /&gt;
  &amp;lt;!-- Administration Menu Section --&amp;gt;&lt;br /&gt;
  &amp;lt;menu&amp;gt;Hello World!&amp;lt;/menu&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;!-- Administration Main File Copy Section --&amp;gt;&lt;br /&gt;
  &amp;lt;files folder=&amp;quot;admin&amp;quot;&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;/files&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
 &amp;lt;/administration&amp;gt;&lt;br /&gt;
&amp;lt;/install&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you look closely you will notice that there are some files that will be copied that we have not discussed. These are the index.html files. An index.html file is placed in each directory to prevent prying users from getting a directory listing. If there is no index.html file, some web servers will list the directory contents. This is often undesirable. These files have the simple line:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;html&amp;gt;&amp;lt;body bgcolor=&amp;quot;#FFFFFF&amp;quot;&amp;gt;&amp;lt;/body&amp;gt;&amp;lt;/html&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It will simply display a blank page.&lt;br /&gt;
&lt;br /&gt;
The other file is the hello.php file. This is the entry point for the admin section of our component. Since we don&#039;t have an admin section of our component, it will have the same content as the index.html files at this point in time.&lt;br /&gt;
&lt;br /&gt;
== Contributors ==&lt;br /&gt;
* mjaz&lt;br /&gt;
* staalanden&lt;br /&gt;
&lt;br /&gt;
== Download ==&lt;br /&gt;
The component can be downloaded at: [http://joomlacode.org/gf/download/frsrelease/8108/29433/com_hello1_01.zip com_hello1_01]&lt;/div&gt;</summary>
		<author><name>Tonie</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=Portal:Developers&amp;diff=9529</id>
		<title>Portal:Developers</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=Portal:Developers&amp;diff=9529"/>
		<updated>2008-07-27T20:21:32Z</updated>

		<summary type="html">&lt;p&gt;Tonie: /* Articles and Tutorials on developer.joomla.org */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Developer profile}}&lt;br /&gt;
&lt;br /&gt;
== Participation ==&lt;br /&gt;
&lt;br /&gt;
* [[Participating in the community]]: a brief description of how people can get involved.&lt;br /&gt;
* [[Coding style and standards]] (To be reviewed).&lt;br /&gt;
* [[Patch submission guidelines]].&lt;br /&gt;
* [[Filing bugs and issues]].&lt;br /&gt;
* [[How to release a distribution tarball]].&lt;br /&gt;
&lt;br /&gt;
== Reference Pages ==&lt;br /&gt;
&lt;br /&gt;
Peruse all pages in the [[:Category:Development]] category.&lt;br /&gt;
&lt;br /&gt;
When creating a new page, ensure you place the following marker at the top of the page so it is included in the category list:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;lt;nowiki&amp;gt;[[Category:Development]]&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you locate other articles you thing that are relevant to developer, please add this marker to those pages.&lt;br /&gt;
&lt;br /&gt;
== Articles and Tutorials on developer.joomla.org ==&lt;br /&gt;
&lt;br /&gt;
[http://community.joomla.org/magazine/article/508-developer-localization-advancements-in-joomla-15.html Localization Advancements in Joomla! 1.5]&lt;br /&gt;
&lt;br /&gt;
[http://developer.joomla.org/tutorials/165-understanding-output-overrides-in-joomla.html Understanding Output Overrides in Joomla! 1.5]&lt;br /&gt;
&lt;br /&gt;
[http://developer.joomla.org/tutorials/181-preventing-sql-injections.html Preventing SQL Injections]&lt;br /&gt;
&lt;br /&gt;
[http://developer.joomla.org/tutorials/184-how-to-create-a-joomla-plugin.html How to create a Joomla! Plugin]&lt;br /&gt;
&lt;br /&gt;
[http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_1 Developing a Model-View-Controller Component - Part 1]&lt;br /&gt;
&lt;br /&gt;
[http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_2_-_Adding_a_Model Developing a Model-View-Controller Component - Part 2 - Adding a Model]&lt;br /&gt;
&lt;br /&gt;
[http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_3_-_Using_the_Database Developing a Model-View-Controller Component - Part 3 - Using the Database]&lt;br /&gt;
&lt;br /&gt;
[http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_4_-_Creating_an_Administrator_Interface Developing a Model-View-Controller Component - Part 4 - Creating an Administrator Interface]&lt;br /&gt;
&lt;br /&gt;
== Cookie Jar ==&lt;br /&gt;
&lt;br /&gt;
Articles that need to be written.&lt;br /&gt;
&lt;br /&gt;
* Developer guidelines.&lt;br /&gt;
* Secure coding guidelines.&lt;br /&gt;
* Error message conventions.&lt;br /&gt;
* Exception handling.&lt;br /&gt;
* Release numbering, compatibility and deprecation.&lt;br /&gt;
* Complete/update/review the wiki API reference (assumes this has been moved from DocuWiki to MediaWiki).&lt;br /&gt;
* Update developer tutorials and how-to&#039;s currently on dev.joomla.org&lt;br /&gt;
** Review all material under the tutorials heading at http://dev.joomla.org/component/option,com_jd-wiki/Itemid,32/&lt;br /&gt;
** Recommend material to be migrated over to docs.joomla.org&lt;br /&gt;
** Update material that is to be migrated over to docs.joomla.org&lt;br /&gt;
* [[Adding AJAX to your component]].&lt;br /&gt;
** Write a document describing how to add AJAX to an MVC component.  If desired, use the MVC Hello World tutorial as a base.  Describe where various elements should go in the MVC design pattern.  Also describe how to implement MVC in a module (these need supporting components to do AJAX).&lt;br /&gt;
* [[Creating a toolbar for your component]].&lt;br /&gt;
* [[Adding configuration objects to modules and plugins]].&lt;br /&gt;
* [[Storing data in the session between page loads]].&lt;br /&gt;
* [[Using the caching system in your component]].&lt;br /&gt;
* [[Creating a file uploader in your component]].&lt;br /&gt;
* [[Suppressing output of extra HTML]].&lt;br /&gt;
* [[Adding view layout configuration parameters]].&lt;br /&gt;
** Explain how to create an XML file that will allow users to configure views.&lt;br /&gt;
* [[How to implement XML-RPC in a component]]&lt;br /&gt;
** There are two ways to do this:&lt;br /&gt;
*** Implement it using an XML-RPC plugin&lt;br /&gt;
*** Implement it in the component itself using raw views&lt;br /&gt;
* [[How to use the filesystem package]]&lt;br /&gt;
* [[How to use the filter package]]&lt;br /&gt;
** Describe how and when to use the Filter package and explain what needs to be filtered for various situations (for queries, for URLs, etc)&lt;br /&gt;
* [[How to use the registry package]]&lt;br /&gt;
* [[How to use JSimpleXML]]&lt;br /&gt;
** How to load and store XML files and how to work with them&lt;br /&gt;
* [[How to add breadcrumbs]]&lt;br /&gt;
* [[How to create component feeds]] (RSS/ATOM)&lt;br /&gt;
* [[How to create PDF views]]&lt;br /&gt;
* [[How to send email from components]]&lt;br /&gt;
* [[What&#039;s available in the JFactory class]]&lt;br /&gt;
* [[How to generate paths for client side and server side]]&lt;br /&gt;
* How to access information from the request/browser&lt;br /&gt;
** This focuses on using the JBrowser class to retrieve information about the features available in the user&#039;s browser.&lt;br /&gt;
* [[How to create an editor plugin]]&lt;br /&gt;
* [[What can be done with a user plugin]]&lt;br /&gt;
* [[How to work with parameters]]&lt;br /&gt;
* [[How to use the JToolBar class in the frontend]]&lt;br /&gt;
* [[How to use the editor in a component]]&lt;br /&gt;
* [[How to cloak email addresses]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Development]]&lt;/div&gt;</summary>
		<author><name>Tonie</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Creating_an_Administrator_Interface&amp;diff=9527</id>
		<title>J1.5:Developing a MVC Component/Creating an Administrator Interface</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Creating_an_Administrator_Interface&amp;diff=9527"/>
		<updated>2008-07-27T20:14:33Z</updated>

		<summary type="html">&lt;p&gt;Tonie: Transwiki:Developing a Model-View-Controller Component - Part 4 - Creating an Administrator Interface moved to Developing a Model-View-Controller Component - Part 4 - Creating an Administrator Interface: Finished with cleanups.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
In the first three tutorials, we have developed a MVC component that retrieves its data from a table in the database. Currently, there is no way to add data to the database except to do it manually using another tool. In this tutorial, we will develop an administrator section for our component which will make it possible to manage the entries in the database.&lt;br /&gt;
&lt;br /&gt;
== Creating the Basic Framework ==&lt;br /&gt;
&lt;br /&gt;
The basic framework of the administrator panel is very similar to the site portion. The main entry point for the administrator section of the component is hello.php. This file is identical to the hello.php file that was used in the site portion except the name of the controller it loads will be changed to HellosController. The default controller is also called controller.php and this file is identical to the default controller in the site portion, with the exception that the controller is named HellosController instead of HelloController. This difference is so that JController will by default load the hellos view, which will display a list of our greetings.&lt;br /&gt;
&lt;br /&gt;
Here is the listing for hello.php:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_4&lt;br /&gt;
 * @license    GNU/GPL&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
// No direct access&lt;br /&gt;
&lt;br /&gt;
defined( &#039;_JEXEC&#039; ) or die( &#039;Restricted access&#039; );&lt;br /&gt;
&lt;br /&gt;
// Require the base controller&lt;br /&gt;
&lt;br /&gt;
require_once( JPATH_COMPONENT.DS.&#039;controller.php&#039; );&lt;br /&gt;
&lt;br /&gt;
// Require specific controller if requested&lt;br /&gt;
if($controller = JRequest::getWord(&#039;controller&#039;)) {&lt;br /&gt;
    $path = JPATH_COMPONENT.DS.&#039;controllers&#039;.DS.$controller.&#039;.php&#039;;&lt;br /&gt;
    if (file_exists($path)) {&lt;br /&gt;
        require_once $path;&lt;br /&gt;
    } else {&lt;br /&gt;
        $controller = &#039;&#039;;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Create the controller&lt;br /&gt;
$classname    = &#039;HellosController&#039;.$controller;&lt;br /&gt;
$controller   = new $classname( );&lt;br /&gt;
&lt;br /&gt;
// Perform the Request task&lt;br /&gt;
$controller-&amp;gt;execute( JRequest::getVar( &#039;task&#039; ) );&lt;br /&gt;
&lt;br /&gt;
// Redirect if set by the controller&lt;br /&gt;
$controller-&amp;gt;redirect();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The view and model that we will start with is the hellos view and the hellos model. We will start with the model.&lt;br /&gt;
&lt;br /&gt;
==== The Hellos Model ====&lt;br /&gt;
&lt;br /&gt;
The Hellos Model will be very simple. The only operation that we currently need is the ability to retrieve the list of hellos from the database. This operation will be implemented in a method called getData().&lt;br /&gt;
&lt;br /&gt;
The JModel class has a built in protected method called _getList(). This method can be used to simplify the task of retrieving a list of records from the database. We simply need to pass it the query and it will return the list of records.&lt;br /&gt;
&lt;br /&gt;
At a later point in time, we might want to use our query from within another method. Therefore, we will create a private method called _buildQuery() which will return the query that will be passed to _getList(). This makes it easier to change the query as well since it is localized in one place.&lt;br /&gt;
&lt;br /&gt;
Therefore we need two methods in our class: getData() and _buildQuery().&lt;br /&gt;
&lt;br /&gt;
_buildQuery() simply returns the query. It looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source  lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Returns the query&lt;br /&gt;
 * @return string The query to be used to retrieve the rows from the database&lt;br /&gt;
 */&lt;br /&gt;
function _buildQuery()&lt;br /&gt;
{&lt;br /&gt;
    $query = &#039; SELECT * &#039;&lt;br /&gt;
           . &#039; FROM #__hello &#039;&lt;br /&gt;
    ;&lt;br /&gt;
&lt;br /&gt;
    return $query;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
getData() will obtain the query and retrieve the records from the database. Now it might happen that we need to retrieve this list of data twice in one page load. It would be a waste to have to query the database twice. Therefore, we will have this method store the data in a protected property so that on subsequent requests it can simply return the data it has already retrieved. This property will be called _data.&lt;br /&gt;
&lt;br /&gt;
Here is the getData() method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source  lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Retrieves the hello data&lt;br /&gt;
 * @return array Array of objects containing the data from the database&lt;br /&gt;
 */&lt;br /&gt;
function getData()&lt;br /&gt;
{&lt;br /&gt;
    // Lets load the data if it doesn&#039;t already exist&lt;br /&gt;
    if (empty( $this-&amp;gt;_data ))&lt;br /&gt;
    {&lt;br /&gt;
        $query = $this-&amp;gt;_buildQuery();&lt;br /&gt;
        $this-&amp;gt;_data = $this-&amp;gt;_getList( $query );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return $this-&amp;gt;_data;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The completed model looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * Hellos Model for Hello World Component&lt;br /&gt;
 * &lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_4&lt;br /&gt;
 * @license        GNU/GPL&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// Check to ensure this file is included in Joomla!&lt;br /&gt;
defined(&#039;_JEXEC&#039;) or die();&lt;br /&gt;
&lt;br /&gt;
jimport( &#039;joomla.application.component.model&#039; );&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Hello Model&lt;br /&gt;
 *&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 */&lt;br /&gt;
class HellosModelHellos extends JModel&lt;br /&gt;
{&lt;br /&gt;
    /**&lt;br /&gt;
     * Hellos data array&lt;br /&gt;
     *&lt;br /&gt;
     * @var array&lt;br /&gt;
     */&lt;br /&gt;
    var $_data;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Returns the query&lt;br /&gt;
     * @return string The query to be used to retrieve the rows from the database&lt;br /&gt;
     */&lt;br /&gt;
    function _buildQuery()&lt;br /&gt;
    {&lt;br /&gt;
        $query = &#039; SELECT * &#039;&lt;br /&gt;
            . &#039; FROM #__hello &#039;&lt;br /&gt;
        ;&lt;br /&gt;
        return $query;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Retrieves the hello data&lt;br /&gt;
     * @return array Array of objects containing the data from the database&lt;br /&gt;
     */&lt;br /&gt;
    function getData()&lt;br /&gt;
    {&lt;br /&gt;
        // Lets load the data if it doesn&#039;t already exist&lt;br /&gt;
        if (empty( $this-&amp;gt;_data ))&lt;br /&gt;
        {&lt;br /&gt;
            $query = $this-&amp;gt;_buildQuery();&lt;br /&gt;
            $this-&amp;gt;_data = $this-&amp;gt;_getList( $query );&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        return $this-&amp;gt;_data;&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This file is saved as models/hellos.php.&lt;br /&gt;
&lt;br /&gt;
==== The Hellos View ====&lt;br /&gt;
&lt;br /&gt;
Now that we have a model to retrieve our data, we need to display it. This view will be fairly similar to the view from the site section as well.&lt;br /&gt;
&lt;br /&gt;
Just as our model was automatically instantiated in the site, so it is in the administrator. Methods that start with get in the model can be accessed using the get() method of the JView class. So our view has three lines: one to retrieve the data from the model, one to push the data into the template, and one to invoke the display method to display the output. Thus we have:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * Hellos View for Hello World Component&lt;br /&gt;
 * &lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_4&lt;br /&gt;
 * @license        GNU/GPL&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// Check to ensure this file is included in Joomla!&lt;br /&gt;
defined(&#039;_JEXEC&#039;) or die();&lt;br /&gt;
&lt;br /&gt;
jimport( &#039;joomla.application.component.view&#039; );&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Hellos View&lt;br /&gt;
 *&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 */&lt;br /&gt;
class HellosViewHellos extends JView&lt;br /&gt;
{&lt;br /&gt;
    /**&lt;br /&gt;
     * Hellos view display method&lt;br /&gt;
     * @return void&lt;br /&gt;
     **/&lt;br /&gt;
    function display($tpl = null)&lt;br /&gt;
    {&lt;br /&gt;
        JToolBarHelper::title( JText::_( &#039;Hello Manager&#039; ), &#039;generic.png&#039; );&lt;br /&gt;
        JToolBarHelper::deleteList();&lt;br /&gt;
        JToolBarHelper::editListX();&lt;br /&gt;
        JToolBarHelper::addNewX();&lt;br /&gt;
&lt;br /&gt;
        // Get data from the model&lt;br /&gt;
        $items =&amp;amp; $this-&amp;gt;get( &#039;Data&#039;);&lt;br /&gt;
&lt;br /&gt;
        $this-&amp;gt;assignRef( &#039;items&#039;, $items );&lt;br /&gt;
&lt;br /&gt;
        parent::display($tpl);&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This file is saved as views/hellos/view.html.php.&lt;br /&gt;
&lt;br /&gt;
==== The Hellos Template ====&lt;br /&gt;
&lt;br /&gt;
The template will take the data pushed into it from the view and produce the output. We will display our output in a simple table. While the frontend template was very simple, in the administrator we will need a minimal amount of extra logic to handle looping through the data.&lt;br /&gt;
&lt;br /&gt;
Here is our template:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;); ?&amp;gt;&lt;br /&gt;
&amp;lt;form action=&amp;quot;index.php&amp;quot; method=&amp;quot;post&amp;quot; name=&amp;quot;adminForm&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;div id=&amp;quot;editcell&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;table class=&amp;quot;adminlist&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;thead&amp;gt;&lt;br /&gt;
        &amp;lt;tr&amp;gt;&lt;br /&gt;
            &amp;lt;th width=&amp;quot;5&amp;quot;&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo JText::_( &#039;ID&#039; ); ?&amp;gt;&lt;br /&gt;
            &amp;lt;/th&amp;gt;&lt;br /&gt;
            &amp;lt;th&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo JText::_( &#039;Greeting&#039; ); ?&amp;gt;&lt;br /&gt;
            &amp;lt;/th&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;            &lt;br /&gt;
    &amp;lt;/thead&amp;gt;&lt;br /&gt;
    &amp;lt;?php&lt;br /&gt;
    $k = 0;&lt;br /&gt;
    for ($i=0, $n=count( $this-&amp;gt;items ); $i &amp;lt; $n; $i++)&lt;br /&gt;
    {&lt;br /&gt;
        $row =&amp;amp; $this-&amp;gt;items[$i];&lt;br /&gt;
        ?&amp;gt;&lt;br /&gt;
        &amp;lt;tr class=&amp;quot;&amp;lt;?php echo &amp;quot;row$k&amp;quot;; ?&amp;gt;&amp;quot;&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo $row-&amp;gt;id; ?&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo $row-&amp;gt;greeting; ?&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;&lt;br /&gt;
        &amp;lt;?php&lt;br /&gt;
        $k = 1 - $k;&lt;br /&gt;
    }&lt;br /&gt;
    ?&amp;gt;&lt;br /&gt;
    &amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;option&amp;quot; value=&amp;quot;com_hello&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;task&amp;quot; value=&amp;quot;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;boxchecked&amp;quot; value=&amp;quot;0&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;controller&amp;quot; value=&amp;quot;hello&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/form&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This template is saved as views/hellos/tmpl/default.php.&lt;br /&gt;
&lt;br /&gt;
You will notice that our output is enclosed in a form. Though this is not necessary now, it will be soon.&lt;br /&gt;
&lt;br /&gt;
We have now completed the basic part of the first view. We have added five files to the admin section of our component:&lt;br /&gt;
&lt;br /&gt;
* hello.php&lt;br /&gt;
* controller.php&lt;br /&gt;
* models/hellos.php&lt;br /&gt;
* views/hellos/view.html.php&lt;br /&gt;
* views/hellos/tmpl/default.php&lt;br /&gt;
&lt;br /&gt;
You can now add these files to the XML install file and give it a try!&lt;br /&gt;
&lt;br /&gt;
== Adding Functionality ==&lt;br /&gt;
&lt;br /&gt;
So far our administrator section is pretty useless. It doesn&#039;t really do anything - all it does is display the entries that we have in our database.&lt;br /&gt;
&lt;br /&gt;
In order to make it useful, we need to add some buttons and links.&lt;br /&gt;
&lt;br /&gt;
==== The Toolbar ====&lt;br /&gt;
&lt;br /&gt;
You may have noticed the toolbar that appears at the top of other Joomla! component administrator panels. Our component needs one as well. Joomla! makes this very easy to do. We will add buttons Delete records, Edit records, and create New records. We will also add a title that will be displayed on our toolbar.&lt;br /&gt;
&lt;br /&gt;
This is done by adding code to the view. To add the buttons, we use static methods from the Joomla! JToolBarHelper class. The code looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;JToolBarHelper::title(   JText::_( &#039;Hello Manager&#039; ), &#039;generic.png&#039; );&lt;br /&gt;
JToolBarHelper::deleteList();&lt;br /&gt;
JToolBarHelper::editListX();&lt;br /&gt;
JToolBarHelper::addNewX();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
These three methods will create the appropriate buttons. The deleteList() method can optionally take up to three parameters - the first parameter is a string to display to the user to confirm that they want to delete the records. The second is the task that should be sent with the query (the default is &#039;remove&#039;), and the third is the text that should be displayed below the button.&lt;br /&gt;
&lt;br /&gt;
The editListX() and addNewX() methods can each take two optional parameters. The first is the task (which are by default edit and add, respectively), and the second is the text that should be displayed below the button.&lt;br /&gt;
&lt;br /&gt;
*You may have noticed the use of the JText::_ method in the template before and here as well. This is a handy function that makes component translation much easier. The JText::_ method will look up the string in your component language file and return the translated string. If not translation is found, it will return the string that you passed it. If you want to translate your component into another language, all you have to do is create a language file that will map the strings within the quotes to the translated version of the string.&lt;br /&gt;
&lt;br /&gt;
==== Checkboxes and Links ====&lt;br /&gt;
&lt;br /&gt;
We now have buttons. Two of those buttons operate on existing records. But how do we know which records to operate on? We have to let the user tell us. To do this, we need to add checkboxes to our table so that the user can select certain records. This is done in our template.&lt;br /&gt;
&lt;br /&gt;
In order to the add the checkboxes, we need to add an extra column into our table. We will add the column in between the two that we already have.&lt;br /&gt;
&lt;br /&gt;
In the header of the column, we will add a checkbox which can be used to toggle all the boxes below it on or off:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;th width=&amp;quot;20&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;input type=&amp;quot;checkbox&amp;quot; name=&amp;quot;toggle&amp;quot; value=&amp;quot;&amp;quot; onclick=&amp;quot;checkAll(&amp;lt;?php echo count( $this-&amp;gt;items ); ?&amp;gt;);&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/th&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Javascript checkAll function is a function that is built into the Joomla! base Javascript package that provides the functionality that we want here.&lt;br /&gt;
&lt;br /&gt;
Now we need to add the checkboxes into the individual rows. Joomla!&#039;s JHTML class has a method, JHTML::_(), which will generate our checkbox for us. We will add the following line to our loop:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$checked    = JHTML::_( &#039;grid.id&#039;, $i, $row-&amp;gt;id );&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
after the line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$row =&amp;amp; $this-&amp;gt;items[$i];&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then we will add a cell in between the two that we already have:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;td&amp;gt;&lt;br /&gt;
    &amp;lt;?php echo $checked; ?&amp;gt;&lt;br /&gt;
&amp;lt;/td&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It can be cumbersome to have to check the box that we want to edit and then move up and click the edit button. Therefore, we will add a link that it will go straight to the greeting&#039;s edit form. We will add the following line after the call to the JHTML::_() method to generate the link HTML:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$link = JRoute::_( &#039;index.php?option=com_hello&amp;gt;controller=hello&amp;gt;task=edit&amp;gt;cid[]=&#039;. $row-&amp;gt;id );&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And we include the link in the cell showing the greeting text:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;td&amp;gt;&lt;br /&gt;
    &amp;lt;a href=&amp;quot;&amp;lt;?php echo $link; ?&amp;gt;&amp;quot;&amp;gt;&amp;lt;?php echo $row-&amp;gt;greeting; ?&amp;gt;&amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;lt;/td&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You will notice that this link points to the hello controller. This controller will handle the data manipulation of our greetings.&lt;br /&gt;
&lt;br /&gt;
If you recall from above, we had four hidden input fields at the bottom of our form. The first input field was named &#039;option&#039;. This field is necessary so that we stay in our component. The second input field was task. This form property gets set when one of the buttons in the toolbar is clicked. A Javascript error will result and the buttons will not work if this input field is omitted. The third input field is the boxchecked field. This field keeps track of the number of boxes that are checked. The edit and delete buttons will check to ensure that this is greater than zero and will not allow the form to be submitted if it is not. The fourth input field is the controller field. This is used to specify that tasks fired from this form will be handled by the hello controller.&lt;br /&gt;
&lt;br /&gt;
Here is the code for the completed default.php file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;); ?&amp;gt;&lt;br /&gt;
&amp;lt;form action=&amp;quot;index.php&amp;quot; method=&amp;quot;post&amp;quot; name=&amp;quot;adminForm&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;div id=&amp;quot;editcell&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;table class=&amp;quot;adminlist&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;thead&amp;gt;&lt;br /&gt;
        &amp;lt;tr&amp;gt;&lt;br /&gt;
            &amp;lt;th width=&amp;quot;5&amp;quot;&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo JText::_( &#039;ID&#039; ); ?&amp;gt;&lt;br /&gt;
            &amp;lt;/th&amp;gt;&lt;br /&gt;
            &amp;lt;th&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo JText::_( &#039;Greeting&#039; ); ?&amp;gt;&lt;br /&gt;
            &amp;lt;/th&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;            &lt;br /&gt;
    &amp;lt;/thead&amp;gt;&lt;br /&gt;
    &amp;lt;?php&lt;br /&gt;
    $k = 0;&lt;br /&gt;
    for ($i=0, $n=count( $this-&amp;gt;items ); $i &amp;lt; $n; $i++)&lt;br /&gt;
    {&lt;br /&gt;
        $row =&amp;amp; $this-&amp;gt;items[$i];&lt;br /&gt;
        ?&amp;gt;&lt;br /&gt;
        &amp;lt;tr class=&amp;quot;&amp;lt;?php echo &amp;quot;row$k&amp;quot;; ?&amp;gt;&amp;quot;&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo $row-&amp;gt;id; ?&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo $row-&amp;gt;greeting; ?&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;&lt;br /&gt;
        &amp;lt;?php&lt;br /&gt;
        $k = 1 - $k;&lt;br /&gt;
    }&lt;br /&gt;
    ?&amp;gt;&lt;br /&gt;
    &amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;option&amp;quot; value=&amp;quot;com_hello&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;task&amp;quot; value=&amp;quot;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;boxchecked&amp;quot; value=&amp;quot;0&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;controller&amp;quot; value=&amp;quot;hello&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/form&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Our hellos view is now complete.&lt;br /&gt;
&lt;br /&gt;
== Getting Down and Dirty: Doing the Real Work ==&lt;br /&gt;
&lt;br /&gt;
Now that the Hellos view is done, it is time to move to the Hello view and model. This is where the real work will get done.&lt;br /&gt;
&lt;br /&gt;
==== The Hello Controller ====&lt;br /&gt;
&lt;br /&gt;
Our default controller just isn&#039;t going to cut it when it comes to doing work - all it is capable of doing is displaying views.&lt;br /&gt;
&lt;br /&gt;
We need to be able to handle the tasks that we are launching from the Hellos view: add, edit and remove.&lt;br /&gt;
&lt;br /&gt;
Add and edit are essentially the same task: they both display a form to the user that allows a greeting to be edited. The only difference is that new displays a blank form, and edit displays a form with data already in it. Since they are similar, we will map the add task onto the edit task handler. This is specified in our constructor:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * constructor (registers additional tasks to methods)&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function __construct()&lt;br /&gt;
{&lt;br /&gt;
    parent::__construct();&lt;br /&gt;
&lt;br /&gt;
    // Register Extra tasks&lt;br /&gt;
    $this-&amp;gt;registerTask( &#039;add&#039;  ,     &#039;edit&#039; );&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first parameter of JController::registerTask is the task to map, and the second is the method to map it to.&lt;br /&gt;
&lt;br /&gt;
We will start with handling the edit task. The controller&#039;s job is fairly simple for the edit task. All it has to do is specify the view and layout to load (the hello view and the form layout). We will also tell Joomla! to disable the mainmenu while we are editing our greeting. This prevents users from leaving unsaved records open.&lt;br /&gt;
&lt;br /&gt;
Our edit task handler looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * display the edit form&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function edit()&lt;br /&gt;
{&lt;br /&gt;
    JRequest::setVar( &#039;view&#039;, &#039;hello&#039; );&lt;br /&gt;
    JRequest::setVar( &#039;layout&#039;, &#039;form&#039;  );&lt;br /&gt;
    JRequest::setVar(&#039;hidemainmenu&#039;, 1);&lt;br /&gt;
&lt;br /&gt;
    parent::display();&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== The Hello View ====&lt;br /&gt;
&lt;br /&gt;
The Hello view will display a form which will allow the user to edit a greeting. The display method if the hello view has to do a few simple tasks:&lt;br /&gt;
&lt;br /&gt;
* retrieve the data from the model&lt;br /&gt;
* create the toolbar&lt;br /&gt;
* pass the data into the template&lt;br /&gt;
* invoke the display() method to render the template&lt;br /&gt;
&lt;br /&gt;
This becomes a bit more complicated because the one view handles both the edit and add tasks. In our toolbar we want the user to know what whether they are adding or editing, so we have to determine which task was fired.&lt;br /&gt;
&lt;br /&gt;
Since we are already retrieving the record that we want to display from the model, we can use this data to determine what task was fired. If the task was edit, then the id field of our record will have been set. If the task was new, then it will not have been set. This can be used to determine if we have a new record or an existing record.&lt;br /&gt;
&lt;br /&gt;
We will add two buttons to the toolbar: save and cancel. Though the functionality will be the same, we want to display different buttons depending on whether it is a new or existing record. If it is a new record, we will display cancel. If it already exists, we will display close.&lt;br /&gt;
&lt;br /&gt;
Thus our display method looks like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * display method of Hello view&lt;br /&gt;
 * @return void&lt;br /&gt;
 **/&lt;br /&gt;
function display($tpl = null)&lt;br /&gt;
{&lt;br /&gt;
    //get the hello&lt;br /&gt;
    $hello        =&amp;amp; $this-&amp;gt;get(&#039;Data&#039;);&lt;br /&gt;
    $isNew        = ($hello-&amp;gt;id &amp;lt; 1);&lt;br /&gt;
&lt;br /&gt;
    $text = $isNew ? JText::_( &#039;New&#039; ) : JText::_( &#039;Edit&#039; );&lt;br /&gt;
    JToolBarHelper::title(   JText::_( &#039;Hello&#039; ).&#039;: &amp;lt;small&amp;gt;&amp;lt;small&amp;gt;[ &#039; . $text.&#039; ]&amp;lt;/small&amp;gt;&amp;lt;/small&amp;gt;&#039; );&lt;br /&gt;
    JToolBarHelper::save();&lt;br /&gt;
    if ($isNew)  {&lt;br /&gt;
        JToolBarHelper::cancel();&lt;br /&gt;
    } else {&lt;br /&gt;
        // for existing items the button is renamed `close`&lt;br /&gt;
        JToolBarHelper::cancel( &#039;cancel&#039;, &#039;Close&#039; );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    $this-&amp;gt;assignRef(&#039;hello&#039;, $hello);&lt;br /&gt;
    parent::display($tpl);&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== The Hello Model ====&lt;br /&gt;
&lt;br /&gt;
Our view needs data. Therefore, we need to create a model to model a hello.&lt;br /&gt;
&lt;br /&gt;
Our model will have two properties: _id and _data. _id will hold the id of the greeting and data will hold the data.&lt;br /&gt;
&lt;br /&gt;
We will start with a constructor, which will attempt to retrieve the id from the query:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Constructor that retrieves the ID from the request&lt;br /&gt;
 *&lt;br /&gt;
 * @access    public&lt;br /&gt;
 * @return    void&lt;br /&gt;
 */&lt;br /&gt;
function __construct()&lt;br /&gt;
{&lt;br /&gt;
    parent::__construct();&lt;br /&gt;
&lt;br /&gt;
    $array = JRequest::getVar(&#039;cid&#039;,  0, &#039;&#039;, &#039;array&#039;);&lt;br /&gt;
    $this-&amp;gt;setId((int)$array[0]);&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The JRequest::getVar() method is used to retrieve data from the request. The first parameter is the name of the form variable. The second parameter is the default value to assign if there is no value found. The third parameter is the name of the hash to retrieve the value from (get, post, etc), and the last value is the data type that should be forced on the value.&lt;br /&gt;
&lt;br /&gt;
Our constructor will take the first value from the cid array and assign it to the id.&lt;br /&gt;
&lt;br /&gt;
Our setId() method can be used to set our id. Changing the id that our model points to will mean the id points to the wrong data. Therefore, when we set the id, we will clear the data property:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Method to set the hello identifier&lt;br /&gt;
 *&lt;br /&gt;
 * @access    public&lt;br /&gt;
 * @param    int Hello identifier&lt;br /&gt;
 * @return    void&lt;br /&gt;
 */&lt;br /&gt;
function setId($id)&lt;br /&gt;
{&lt;br /&gt;
    // Set id and wipe data&lt;br /&gt;
    $this-&amp;gt;_id        = $id;&lt;br /&gt;
    $this-&amp;gt;_data    = null;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finally, we need a method to retrieve our data: getData()&lt;br /&gt;
&lt;br /&gt;
getData will check if the _data property has already been set. If it has, it will simply return it. Otherwise, it will load the data from the database.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Method to get a hello&lt;br /&gt;
 * @return object with data&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
function &amp;amp;getData()&lt;br /&gt;
{&lt;br /&gt;
    // Load the data&lt;br /&gt;
    if (empty( $this-&amp;gt;_data )) {&lt;br /&gt;
        $query = &#039; SELECT * FROM #__hello &#039;.&lt;br /&gt;
                &#039;  WHERE id = &#039;.$this-&amp;gt;_id;&lt;br /&gt;
        $this-&amp;gt;_db-&amp;gt;setQuery( $query );&lt;br /&gt;
        $this-&amp;gt;_data = $this-&amp;gt;_db-&amp;gt;loadObject();&lt;br /&gt;
    }&lt;br /&gt;
    if (!$this-&amp;gt;_data) {&lt;br /&gt;
        $this-&amp;gt;_data = new stdClass();&lt;br /&gt;
        $this-&amp;gt;_data-&amp;gt;id = 0;&lt;br /&gt;
        $this-&amp;gt;_data-&amp;gt;greeting = null;&lt;br /&gt;
    }&lt;br /&gt;
    return $this-&amp;gt;_data;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== The Form ====&lt;br /&gt;
&lt;br /&gt;
Now all that is left is to create the form that the data will go into. Since we specified our layout as form, the form will go in a file in the tmpl directory of the hello view called form.php:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;); ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;form action=&amp;quot;index.php&amp;quot; method=&amp;quot;post&amp;quot; name=&amp;quot;adminForm&amp;quot; id=&amp;quot;adminForm&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;div class=&amp;quot;col100&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;fieldset class=&amp;quot;adminform&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;legend&amp;gt;&amp;lt;?php echo JText::_( &#039;Details&#039; ); ?&amp;gt;&amp;lt;/legend&amp;gt;&lt;br /&gt;
        &amp;lt;table class=&amp;quot;admintable&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;tr&amp;gt;&lt;br /&gt;
            &amp;lt;td width=&amp;quot;100&amp;quot; align=&amp;quot;right&amp;quot; class=&amp;quot;key&amp;quot;&amp;gt;&lt;br /&gt;
                &amp;lt;label for=&amp;quot;greeting&amp;quot;&amp;gt;&lt;br /&gt;
                    &amp;lt;?php echo JText::_( &#039;Greeting&#039; ); ?&amp;gt;:&lt;br /&gt;
                &amp;lt;/label&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;input class=&amp;quot;text_area&amp;quot; type=&amp;quot;text&amp;quot; name=&amp;quot;greeting&amp;quot; id=&amp;quot;greeting&amp;quot; size=&amp;quot;32&amp;quot; maxlength=&amp;quot;250&amp;quot; value=&amp;quot;&amp;lt;?php echo $this-&amp;gt;hello-&amp;gt;greeting;?&amp;gt;&amp;quot; /&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;&lt;br /&gt;
    &amp;lt;/table&amp;gt;&lt;br /&gt;
    &amp;lt;/fieldset&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;clr&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;option&amp;quot; value=&amp;quot;com_hello&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;id&amp;quot; value=&amp;quot;&amp;lt;?php echo $this-&amp;gt;hello-&amp;gt;id; ?&amp;gt;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;task&amp;quot; value=&amp;quot;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;controller&amp;quot; value=&amp;quot;hello&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/form&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in addition to the input field, there is a hidden field for the id. The user doesn&#039;t need to edit the id (and shouldn&#039;t), so we silently pass it along in the form.&lt;br /&gt;
&lt;br /&gt;
==== Implementing the Functionality ====&lt;br /&gt;
&lt;br /&gt;
So far, our controller only handles two tasks: edit and new. However, we also have buttons to save, delete and cancel records. We need to write code to handle and perform these tasks.&lt;br /&gt;
&lt;br /&gt;
=== Saving a Record ===&lt;br /&gt;
&lt;br /&gt;
The logical next step is to implement the functionality to save a record. Normally, this would require some switches and logic to handle various cases, such as the difference between creating a new record (an INSERT query), and updating an existing query (an UPDATE query). Also, there are complexities involved in getting the data from the form and putting it into the query.&lt;br /&gt;
&lt;br /&gt;
The Joomla! framework takes care of a lot of this for you. The JTable class makes it easy to manipulate records in the database without having to worry about writing the SQL code that lies behind these updates. It also makes it easy to transfer data from an HTML form into the database.&lt;br /&gt;
&lt;br /&gt;
== Creating the Table Class ==&lt;br /&gt;
&lt;br /&gt;
The JTable class is an abstract class from which you can derive child classes to work with specific tables. To use it, you simply create a class that extends the JTable class, add your database fields as properties, and override the constructor to specify the name of the table and the primary key.&lt;br /&gt;
&lt;br /&gt;
Here is our JTable class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * Hello World table class&lt;br /&gt;
 * &lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_4&lt;br /&gt;
 * @license        GNU/GPL&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// No direct access&lt;br /&gt;
defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;);&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Hello Table class&lt;br /&gt;
 *&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 */&lt;br /&gt;
class TableHello extends JTable&lt;br /&gt;
{&lt;br /&gt;
    /**&lt;br /&gt;
     * Primary Key&lt;br /&gt;
     *&lt;br /&gt;
     * @var int&lt;br /&gt;
     */&lt;br /&gt;
    var $id = null;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * @var string&lt;br /&gt;
     */&lt;br /&gt;
    var $greeting = null;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Constructor&lt;br /&gt;
     *&lt;br /&gt;
     * @param object Database connector object&lt;br /&gt;
     */&lt;br /&gt;
    function TableHello( &amp;amp;$db ) {&lt;br /&gt;
        parent::__construct(&#039;#__hello&#039;, &#039;id&#039;, $db);&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You will see here that we have defined our two fields: the id field and the greeting field. Then we have defined a constructor, which will call the constructor of the parent class and pass it the name of the table (hello), the name of the field which is the primary key (id), and the database connector object.&lt;br /&gt;
&lt;br /&gt;
This file should be called hello.php and it will go in a directory called tables in the administrator section of our component.&lt;br /&gt;
&lt;br /&gt;
== Implementing the Function in our Model ==&lt;br /&gt;
&lt;br /&gt;
We are now ready to add the method to the model which will save our record. We will call this method store. Our store() method will do three things: bind the data from the form to the TableHello object, check to ensure that the record is properly formed, and store the record in the database.&lt;br /&gt;
&lt;br /&gt;
Our method looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Method to store a record&lt;br /&gt;
 *&lt;br /&gt;
 * @access    public&lt;br /&gt;
 * @return    boolean    True on success&lt;br /&gt;
 */&lt;br /&gt;
function store()&lt;br /&gt;
{&lt;br /&gt;
    $row =&amp;amp; $this-&amp;gt;getTable();&lt;br /&gt;
&lt;br /&gt;
    $data = JRequest::get( &#039;post&#039; );&lt;br /&gt;
    // Bind the form fields to the hello table&lt;br /&gt;
    if (!$row-&amp;gt;bind($data)) {&lt;br /&gt;
        $this-&amp;gt;setError($this-&amp;gt;_db-&amp;gt;getErrorMsg());&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // Make sure the hello record is valid&lt;br /&gt;
    if (!$row-&amp;gt;check()) {&lt;br /&gt;
        $this-&amp;gt;setError($this-&amp;gt;_db-&amp;gt;getErrorMsg());&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // Store the web link table to the database&lt;br /&gt;
    if (!$row-&amp;gt;store()) {&lt;br /&gt;
        $this-&amp;gt;setError($this-&amp;gt;_db-&amp;gt;getErrorMsg());&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return true;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This method gets added to the hello model.&lt;br /&gt;
&lt;br /&gt;
The method takes one parameter, which is an associative array of data that we want to store in the database. This can easily be retrieved from the request as will be seen later.&lt;br /&gt;
&lt;br /&gt;
You will see that the first line retrieves a reference to our JTable object. If we name our table properly, we don&#039;t have to specify its name - the JModel class knows where to find it. You may recall that we called our table class TableHello and put it in a file called hello.php in the tables directory. If you follow this convention, the JModel class can create your object automatically.&lt;br /&gt;
&lt;br /&gt;
The second line will retrieve the data from the form. The JRequest class makes this very easy. In this case, we are retrieving all of the variables that were submitted using the &#039;POST&#039; method. These will be returned as an associative array.&lt;br /&gt;
&lt;br /&gt;
The rest is easy - we bind, check and store. The bind() method will copy values from the array into the corresponding property of the table object. In this case, it will take the values of id and greeting and copy them to our TableHello object.&lt;br /&gt;
&lt;br /&gt;
The check() method will perform data verification. In the JTable() class, this method simply returns true. While this doesn&#039;t provide any value for us currently, by calling this method we make it possible to do data checking using our TableHello class in the future. This method can be overridden in our TableHello class with a method that performs the appropriate checks.&lt;br /&gt;
&lt;br /&gt;
The store() method will take the data that is in the object and store it in the database. If the id is 0, it will create a new record (INSERT), otherwise, it will update the existing record (UPDATE).&lt;br /&gt;
&lt;br /&gt;
== Adding the Task to the Controller ==&lt;br /&gt;
&lt;br /&gt;
We are now ready to add our task to the controller. Since the task that we are firing is called &#039;save&#039;, we must call our method &#039;save&#039;. This is simple:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * save a record (and redirect to main page)&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function save()&lt;br /&gt;
{&lt;br /&gt;
    $model = $this-&amp;gt;getModel(&#039;hello&#039;);&lt;br /&gt;
&lt;br /&gt;
    if ($model-&amp;gt;store()) {&lt;br /&gt;
        $msg = JText::_( &#039;Greeting Saved!&#039; );&lt;br /&gt;
    } else {&lt;br /&gt;
        $msg = JText::_( &#039;Error Saving Greeting&#039; );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // Check the table in so it can be edited.... we are done with it anyway&lt;br /&gt;
    $link = &#039;index.php?option=com_hello&#039;;&lt;br /&gt;
    $this-&amp;gt;setRedirect($link, $msg);&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All we do is get our model and invoke the store() method. Then we use the setRedirect() method to redirect back to our list of greetings. We also pass a message along, which will be displayed at the top of the page.&lt;br /&gt;
&lt;br /&gt;
=== Deleting a Record ===&lt;br /&gt;
&lt;br /&gt;
=== Implementing the Function in the Model ===&lt;br /&gt;
&lt;br /&gt;
In the model, we will retrieve the list of IDs to delete and call the JTable class to delete them. Here it is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Method to delete record(s)&lt;br /&gt;
 *&lt;br /&gt;
 * @access    public&lt;br /&gt;
 * @return    boolean    True on success&lt;br /&gt;
 */&lt;br /&gt;
function delete()&lt;br /&gt;
{&lt;br /&gt;
    $cids = JRequest::getVar( &#039;cid&#039;, array(0), &#039;post&#039;, &#039;array&#039; );&lt;br /&gt;
    $row =&amp;amp; $this-&amp;gt;getTable();&lt;br /&gt;
&lt;br /&gt;
    foreach($cids as $cid) {&lt;br /&gt;
        if (!$row-&amp;gt;delete( $cid )) {&lt;br /&gt;
            $this-&amp;gt;setError( $row-&amp;gt;getErrorMsg() );&lt;br /&gt;
            return false;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return true;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We invoke the JRequest::getVar() method to get the data from the request, then we invoke the $row-&amp;gt;delete() method to delete each row. By storing errors in the model we make it possible to retrieve them later if we so choose.&lt;br /&gt;
&lt;br /&gt;
=== Handling the Remove Task in the Controller ===&lt;br /&gt;
&lt;br /&gt;
This is similar to the save() method which handled the save task:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * remove record(s)&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function remove()&lt;br /&gt;
{&lt;br /&gt;
    $model = $this-&amp;gt;getModel(&#039;hello&#039;);&lt;br /&gt;
    if(!$model-&amp;gt;delete()) {&lt;br /&gt;
        $msg = JText::_( &#039;Error: One or More Greetings Could not be Deleted&#039; );&lt;br /&gt;
    } else {&lt;br /&gt;
        $msg = JText::_( &#039;Greeting(s) Deleted&#039; );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    $this-&amp;gt;setRedirect( &#039;index.php?option=com_hello&#039;, $msg );&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Cancelling the Edit Operation ====&lt;br /&gt;
&lt;br /&gt;
To cancel the edit operation, all we have to do is redirect back to the main view:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * cancel editing a record&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function cancel()&lt;br /&gt;
{&lt;br /&gt;
    $msg = JText::_( &#039;Operation Cancelled&#039; );&lt;br /&gt;
    $this-&amp;gt;setRedirect( &#039;index.php?option=com_hello&#039;, $msg );&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
We have now implemented a basic backend to our component. We are now able to edit the entries that are viewed in the frontend. We have demonstrated the interaction between models, views and controllers. We have shown how the JTable class can be extended to provide easy access to tables in the database. It can also be seen how the JToolBarHelper class can be used to create button bars in components to present a standardized look between components.&lt;br /&gt;
&lt;br /&gt;
== Contributors ==&lt;br /&gt;
* staalanden&lt;br /&gt;
&lt;br /&gt;
== Download ==&lt;br /&gt;
&lt;br /&gt;
The component can be downloaded at: [http://joomlacode.org/gf/download/frsrelease/8111/29436/com_hello4_01.zip com_hello4_01]&lt;/div&gt;</summary>
		<author><name>Tonie</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Using_the_Database&amp;diff=9525</id>
		<title>J1.5:Developing a MVC Component/Using the Database</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Using_the_Database&amp;diff=9525"/>
		<updated>2008-07-27T20:14:24Z</updated>

		<summary type="html">&lt;p&gt;Tonie: Transwiki:Developing a Model-View-Controller Component - Part 3 - Using the Database moved to Developing a Model-View-Controller Component - Part 3 - Using the Database: Finished with cleanups.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
In the first two tutorials, we showed you how to build a simple model-view-controller component. We had one view which retrieved data from a model (which was created in the 2nd tutorial). In this tutorial, we will be working with the model. Instead of the data being hard coded in the model, the model will retrieve the data from a table in the database.&lt;br /&gt;
&lt;br /&gt;
This tutorial will demonstrate how to use the JDatabase class to retrieve data from the database.&lt;br /&gt;
&lt;br /&gt;
== Retrieving the Data ==&lt;br /&gt;
&lt;br /&gt;
Our model currently has one method: getGreeting(). This method is very simple - all it does is return the hard-coded greeting.&lt;br /&gt;
&lt;br /&gt;
To make things more interesting, we will load the greeting from a database table. We will demonstrate later how to create an SQL file and add the appropriate code to the XML manifest file so that the table and some sample data will be created when the component is installed. For now, we will simply replace our return statement with some code that will retrieve the greeting from the database and return it.&lt;br /&gt;
&lt;br /&gt;
The first step is to obtain a reference to a database object. Since Joomla! uses the database for its normal operation, a database connection already exists; therefore, it is not necessary to create your own. A reference to the existing database can be obtained using:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$db =&amp;amp; JFactory::getDBO();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
JFactory is a static class that is used to retrieve references to many of the system objects. More information about this class can be found in the API documentation.&lt;br /&gt;
&lt;br /&gt;
The method name (getDBO) stands for get DataBase Object, and is easy and important to remember.&lt;br /&gt;
&lt;br /&gt;
Now that we have obtained a reference to the database object, we can retrieve our data. We do this in two steps:&lt;br /&gt;
&lt;br /&gt;
* store our query in the database object&lt;br /&gt;
* load the result&lt;br /&gt;
&lt;br /&gt;
Our new getGreeting() method will therefore look like:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;function getGreeting()&lt;br /&gt;
{&lt;br /&gt;
   $db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
&lt;br /&gt;
   $query = &#039;SELECT greeting FROM #__hello&#039;;&lt;br /&gt;
   $db-&amp;gt;setQuery( $query );&lt;br /&gt;
   $greeting = $db-&amp;gt;loadResult();&lt;br /&gt;
&lt;br /&gt;
   return $greeting;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
hello is the name of the table that we will create later, and greeting is the name of the field that stores the greetings. If you are not familiar with SQL, it would be helpful to take a tutorial or a lesson to get yourself up to speed. One such tutorial can be found at [http://www.w3schools.com/sql/default.asp w3schools].&lt;br /&gt;
&lt;br /&gt;
The $db-&amp;gt;loadResult() method will execute the stored database query and return the first field of the first row of the result. See [http://api.joomla.org/Joomla-Framework/Database/JDatabase.html JDatabase API reference] for more information about other load methods in the JDatabase class.&lt;br /&gt;
&lt;br /&gt;
== Creating the Installation SQL File ==&lt;br /&gt;
&lt;br /&gt;
The Joomla! installer has built-in support for executing queries during component installation. These queries are all stored in a standard text file.&lt;br /&gt;
&lt;br /&gt;
We will have three queries in our install file: the first will drop the table in case it already exists, the second will create the table with the appropriate fields, and the third will insert the data.&lt;br /&gt;
&lt;br /&gt;
Here are our queries:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;DROP TABLE IF EXISTS `#__hello`;&lt;br /&gt;
&lt;br /&gt;
CREATE TABLE `#__hello` (&lt;br /&gt;
  `id` int(11) NOT NULL auto_increment,&lt;br /&gt;
  `greeting` varchar(25) NOT NULL,&lt;br /&gt;
  PRIMARY KEY  (`id`)&lt;br /&gt;
) ENGINE=MyISAM AUTO_INCREMENT DEFAULT CHARSET=utf8;&lt;br /&gt;
&lt;br /&gt;
INSERT INTO `#__hello` (`greeting`) VALUES (&#039;Hello, World!&#039;),&lt;br /&gt;
(&#039;Bonjour, Monde!&#039;),&lt;br /&gt;
(&#039;Ciao, Mondo!&#039;);&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You might find the  prefix on the table names rather odd. Joomla! will replace this prefix with the prefix used by the current install. For most installs, this table will become jos_hello. This allows multiple installs of Joomla! to use the same database, and prevents collisions with other applications using the same table names (i.e. two applications might share a database, but might both require a &#039;users&#039; table. This convention avoids problems.)&lt;br /&gt;
&lt;br /&gt;
We have specified two fields in our database. The first field is id, and is called the &#039;primary key&#039;. The primary key of a database table is a field that is used to uniquely identify a record. This is often used to lookup rows in the database. The other field is greeting. This is the field that stores the greeting that is returned from the query that we used above.&lt;br /&gt;
&lt;br /&gt;
We will save our queries in a file called install.utf.sql.&lt;br /&gt;
&lt;br /&gt;
=== Creating the Uninstall SQL File ===&lt;br /&gt;
&lt;br /&gt;
Though we might hope that people will never want to uninstall our component, it is important that if they do, we don&#039;t leave anything behind. Joomla! will look after deleting the files and directories that were created during install, but you must manually include queries that will remove and tables that have been added to the database. Since we have only created one table, we only need one query:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;DROP TABLE IF EXISTS `#__hello`;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will save this query in a file called uninstall.utf.sql.&lt;br /&gt;
&lt;br /&gt;
== Updating our Install File ==&lt;br /&gt;
&lt;br /&gt;
We need to change a few things in our install file. First, we need to add our two new files to the list of files to install. SQL install file have to go in the admin directory. Second, we need to tell the installer to execute the queries in our files on install and uninstall.&lt;br /&gt;
&lt;br /&gt;
Our new file looks like this:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;&lt;br /&gt;
&amp;lt;install type=&amp;quot;component&amp;quot; version=&amp;quot;1.5.0&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;name&amp;gt;Hello&amp;lt;/name&amp;gt;&lt;br /&gt;
 &amp;lt;!-- The following elements are optional and free of formatting conttraints --&amp;gt;&lt;br /&gt;
 &amp;lt;creationDate&amp;gt;2007-02-22&amp;lt;/creationDate&amp;gt;&lt;br /&gt;
 &amp;lt;author&amp;gt;John Doe&amp;lt;/author&amp;gt;&lt;br /&gt;
 &amp;lt;authorEmail&amp;gt;john.doe@example.org&amp;lt;/authorEmail&amp;gt;&lt;br /&gt;
 &amp;lt;authorUrl&amp;gt;http://www.example.org&amp;lt;/authorUrl&amp;gt;&lt;br /&gt;
 &amp;lt;copyright&amp;gt;Copyright Info&amp;lt;/copyright&amp;gt;&lt;br /&gt;
 &amp;lt;license&amp;gt;License Info&amp;lt;/license&amp;gt;&lt;br /&gt;
 &amp;lt;!--  The version string is recorded in the components table --&amp;gt;&lt;br /&gt;
 &amp;lt;version&amp;gt;3.01&amp;lt;/version&amp;gt;&lt;br /&gt;
 &amp;lt;!-- The description is optional and defaults to the name --&amp;gt;&lt;br /&gt;
 &amp;lt;description&amp;gt;Description of the component ...&amp;lt;/description&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;!-- Site Main File Copy Section --&amp;gt;&lt;br /&gt;
 &amp;lt;!-- Note the folder attribute: This attribute describes the folder&lt;br /&gt;
      to copy FROM in the package to install therefore files copied&lt;br /&gt;
      in this section are copied from /site/ in the package --&amp;gt;&lt;br /&gt;
 &amp;lt;files folder=&amp;quot;site&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;controller.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;models/hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;models/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/view.html.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/tmpl/default.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/tmpl/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
 &amp;lt;/files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;install&amp;gt;&lt;br /&gt;
  &amp;lt;sql&amp;gt;&lt;br /&gt;
   &amp;lt;file charset=&amp;quot;utf8&amp;quot; driver=&amp;quot;mysql&amp;quot;&amp;gt;install.sql&amp;lt;/file&amp;gt;&lt;br /&gt;
  &amp;lt;/sql&amp;gt;&lt;br /&gt;
 &amp;lt;/install&amp;gt;&lt;br /&gt;
 &amp;lt;uninstall&amp;gt;&lt;br /&gt;
  &amp;lt;sql&amp;gt;&lt;br /&gt;
   &amp;lt;file charset=&amp;quot;utf8&amp;quot; driver=&amp;quot;mysql&amp;quot;&amp;gt;uninstall.sql&amp;lt;/file&amp;gt;&lt;br /&gt;
  &amp;lt;/sql&amp;gt;&lt;br /&gt;
 &amp;lt;/uninstall&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;administration&amp;gt;&lt;br /&gt;
  &amp;lt;!-- Administration Menu Section --&amp;gt;&lt;br /&gt;
  &amp;lt;menu&amp;gt;Hello World!&amp;lt;/menu&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;!-- Administration Main File Copy Section --&amp;gt;&lt;br /&gt;
  &amp;lt;files folder=&amp;quot;admin&amp;quot;&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;install.sql&amp;lt;/filename&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;uninstall.sql&amp;lt;/filename&amp;gt;&lt;br /&gt;
&amp;lt;/files&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
 &amp;lt;/administration&amp;gt;&lt;br /&gt;
&amp;lt;/install&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You will notice two attributes present on the &amp;lt;file&amp;gt; tags within the &amp;lt;install&amp;gt; and &amp;lt;uninstall&amp;gt; sections: charset and driver. The charset is the type of charset to use. The only valid charset is utf8. If you want to create install files for non-utf8 databases (for older version of MySQL), you should omit this attribute.&lt;br /&gt;
&lt;br /&gt;
The driver attribute specifies which database the queries were written for. Currently, this can only be mysql, but in future versions of Joomla! there may be more database drivers available.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
We now have a component that takes advantage of both the Joomla! MVC framework classes and the JDatabase classes. You are now able to write MVC components that interact with the database and can use the Joomla! installer to create and populate database tables.&lt;br /&gt;
&lt;br /&gt;
== Contributors ==&lt;br /&gt;
* staalanden&lt;br /&gt;
&lt;br /&gt;
== Download ==&lt;br /&gt;
&lt;br /&gt;
The component can be downloaded at: [http://joomlacode.org/gf/download/frsrelease/8110/29435/com_hello3_01.zip com_hello3_01]&lt;/div&gt;</summary>
		<author><name>Tonie</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Adding_a_Model&amp;diff=9523</id>
		<title>J1.5:Developing a MVC Component/Adding a Model</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Adding_a_Model&amp;diff=9523"/>
		<updated>2008-07-27T20:14:13Z</updated>

		<summary type="html">&lt;p&gt;Tonie: Transwiki:Developing a Model-View-Controller Component - Part 2 - Adding a Model moved to Developing a Model-View-Controller Component - Part 2 - Adding a Model: Finished with cleanups.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
In the first tutorial of this series, creating a simple view-controller component using the Joomla! 1.5 CMS framework was demonstrated.&lt;br /&gt;
&lt;br /&gt;
In the first tutorial, the greeting was hardcoded into the view. This doesn&#039;t follow the MVC pattern exactly because the view is intended to only display the data, and not contain it.&lt;br /&gt;
&lt;br /&gt;
In this second part of the tutorial we will demonstrate how to move this out of the view and into a model. In future tutorials we will demonstrate the power and flexibility that this design pattern provides.&lt;br /&gt;
&lt;br /&gt;
== Creating the Model ==&lt;br /&gt;
&lt;br /&gt;
The concept of model gets its name because this class is intended to represent (or &#039;model&#039;) some entity. In our case, our first model will represent a &#039;hello&#039;, or a greeting. This is in line with our design thus far, because we have one view (&#039;hello&#039;), which is a view of our greeting.&lt;br /&gt;
&lt;br /&gt;
The naming convention for models in the Joomla! framework is that the class name starts with the name of the component (in our case &#039;hello&#039;, followed by &#039;model&#039;, followed by the model name. Therefore, our model class is called HelloModelHello.&lt;br /&gt;
&lt;br /&gt;
At this point, we will only model one behaviour of our hello, and that is retrieving the greeting. We will thus have one method, called getGreeting(). It will simply return the string &#039;Hello, World!&#039;.&lt;br /&gt;
&lt;br /&gt;
Here is the code for our model class:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * Hello Model for Hello World Component&lt;br /&gt;
 * &lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_2&lt;br /&gt;
 * @license    GNU/GPL&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// No direct access&lt;br /&gt;
&lt;br /&gt;
defined( &#039;_JEXEC&#039; ) or die( &#039;Restricted access&#039; );&lt;br /&gt;
&lt;br /&gt;
jimport( &#039;joomla.application.component.model&#039; );&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Hello Model&lt;br /&gt;
 *&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 */&lt;br /&gt;
class HelloModelHello extends JModel&lt;br /&gt;
{&lt;br /&gt;
    /**&lt;br /&gt;
    * Gets the greeting&lt;br /&gt;
    * @return string The greeting to be displayed to the user&lt;br /&gt;
    */&lt;br /&gt;
    function getGreeting()&lt;br /&gt;
    {&lt;br /&gt;
        return &#039;Hello, World!&#039;;&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You will notice a line that starts with jimport. The jimport function is used to load files from the Joomla! framework that are required for our component. This particular statement will load the file /libraries/joomla/application/component/model.php. The &#039;.&#039;s are used as directory separators and the last part is the name of the file to load. All files are loaded relative to the libraries directory. This particular file contains the class definition for the JModel class, which is necessary because our model extends this class.&lt;br /&gt;
&lt;br /&gt;
Now that we have created our model, we must modify our view so that it uses it to obtain the greeting.&lt;br /&gt;
&lt;br /&gt;
== Using the Model ==&lt;br /&gt;
&lt;br /&gt;
The Joomla! framework is setup in such a way that the controller will automatically load the model that has the same name as the view and will push it into the view. Since our view is called &#039;Hello&#039;, our &#039;Hello&#039; model will automatically be loaded and pushed into the view. Therefore, we can easily retrieve a reference to our model using the JView::getModel() method.&lt;br /&gt;
&lt;br /&gt;
Our previous view code contained the lines:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$greeting = &amp;quot;Hello World!&amp;quot;;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To take advantage of our model, we change this line to:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$model =&amp;amp; $this-&amp;gt;getModel();&lt;br /&gt;
$greeting = $model-&amp;gt;getGreeting();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The complete view now looks like:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_2&lt;br /&gt;
 * @license    GNU/GPL&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
// No direct access&lt;br /&gt;
&lt;br /&gt;
defined( &#039;_JEXEC&#039; ) or die( &#039;Restricted access&#039; );&lt;br /&gt;
&lt;br /&gt;
jimport( &#039;joomla.application.component.view&#039;);&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * HTML View class for the HelloWorld Component&lt;br /&gt;
 *&lt;br /&gt;
 * @package    HelloWorld&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
class HelloViewHello extends JView&lt;br /&gt;
{&lt;br /&gt;
    function display($tpl = null)&lt;br /&gt;
    {&lt;br /&gt;
        $model =&amp;amp; $this-&amp;gt;getModel();&lt;br /&gt;
        $greeting = $model-&amp;gt;getGreeting();&lt;br /&gt;
        $this-&amp;gt;assignRef( &#039;greeting&#039;,	$greeting );&lt;br /&gt;
&lt;br /&gt;
        parent::display($tpl);&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Adding the File to the Package ===&lt;br /&gt;
&lt;br /&gt;
All that remains is to add an entry to the XML file so that our new model will be copied. The Joomla! framework will look for our model in the models directory, so the entry for this file will look like (it should be added to the site section):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;filename&amp;gt;models/hello.php&amp;lt;/filename&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Our new hello.xml file will look like:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;&lt;br /&gt;
&amp;lt;install type=&amp;quot;component&amp;quot; version=&amp;quot;1.5.0&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;name&amp;gt;Hello&amp;lt;/name&amp;gt;&lt;br /&gt;
 &amp;lt;!-- The following elements are optional and free of formatting conttraints --&amp;gt;&lt;br /&gt;
 &amp;lt;creationDate&amp;gt;2007-02-22&amp;lt;/creationDate&amp;gt;&lt;br /&gt;
 &amp;lt;author&amp;gt;John Doe&amp;lt;/author&amp;gt;&lt;br /&gt;
 &amp;lt;authorEmail&amp;gt;john.doe@example.org&amp;lt;/authorEmail&amp;gt;&lt;br /&gt;
 &amp;lt;authorUrl&amp;gt;http://www.example.org&amp;lt;/authorUrl&amp;gt;&lt;br /&gt;
 &amp;lt;copyright&amp;gt;Copyright Info&amp;lt;/copyright&amp;gt;&lt;br /&gt;
 &amp;lt;license&amp;gt;License Info&amp;lt;/license&amp;gt;&lt;br /&gt;
 &amp;lt;!--  The version string is recorded in the components table --&amp;gt;&lt;br /&gt;
 &amp;lt;version&amp;gt;1.01&amp;lt;/version&amp;gt;&lt;br /&gt;
 &amp;lt;!-- The description is optional and defaults to the name --&amp;gt;&lt;br /&gt;
 &amp;lt;description&amp;gt;Description of the component ...&amp;lt;/description&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;!-- Site Main File Copy Section --&amp;gt;&lt;br /&gt;
 &amp;lt;!-- Note the folder attribute: This attribute describes the folder&lt;br /&gt;
      to copy FROM in the package to install therefore files copied&lt;br /&gt;
      in this section are copied from /site/ in the package --&amp;gt;&lt;br /&gt;
 &amp;lt;files folder=&amp;quot;site&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;controller.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;models/hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;models/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/view.html.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/tmpl/default.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/tmpl/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
 &amp;lt;/files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;administration&amp;gt;&lt;br /&gt;
  &amp;lt;!-- Administration Menu Section --&amp;gt;&lt;br /&gt;
  &amp;lt;menu&amp;gt;Hello World!&amp;lt;/menu&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;!-- Administration Main File Copy Section --&amp;gt;&lt;br /&gt;
  &amp;lt;files folder=&amp;quot;admin&amp;quot;&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;/files&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
 &amp;lt;/administration&amp;gt;&lt;br /&gt;
&amp;lt;/install&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
We now have a simple MVC component. Each element is very simple at this point, but provides a great deal of flexibility and power.&lt;br /&gt;
&lt;br /&gt;
== Contributors ==&lt;br /&gt;
* staalanden&lt;br /&gt;
&lt;br /&gt;
== Download ==&lt;br /&gt;
&lt;br /&gt;
The component can be downloaded at: [http://joomlacode.org/gf/download/frsrelease/8109/29434/com_hello2_01.zip com_hello2_01]&lt;/div&gt;</summary>
		<author><name>Tonie</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Introduction&amp;diff=9521</id>
		<title>J1.5:Developing a MVC Component/Introduction</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Introduction&amp;diff=9521"/>
		<updated>2008-07-27T20:14:00Z</updated>

		<summary type="html">&lt;p&gt;Tonie: Transwiki:Developing a Model-View-Controller Component - Part 1 moved to Developing a Model-View-Controller Component - Part 1: Finished with cleanups.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
A software framework is the base of an application that can be used by a developer. The framework in Joomla! 1.5 unleashes a great deal of power for them. The Joomla! code has been completely overhauled and cleaned up. This tutorial will guide you through the process of developing a component using the framework.&lt;br /&gt;
&lt;br /&gt;
The scope of this project will be to develop a simple Hello World! component. In future tutorials, this simple framework will be built upon to show the full power and versatility of the MVC design pattern in Joomla!&lt;br /&gt;
&lt;br /&gt;
== Requirements ==&lt;br /&gt;
You need Joomla! 1.5 or greater for this tutorial.&lt;br /&gt;
&lt;br /&gt;
== Introduction to Model-View-Controller ==&lt;br /&gt;
While the idea behind a component may seem extremely simple, code can quickly become very complex as additional features are added or the interface is customized.&lt;br /&gt;
&lt;br /&gt;
Model-View-Controller (herein referred to as MVC) is a software design pattern that can be used to organize code in such a way that the business logic and data presentation are separate. The premise behind this approach is that if the business logic is grouped into one section, then the interface and user interaction that surrounds the data can revised and customized without having to reprogram the business logic.&lt;br /&gt;
&lt;br /&gt;
There are three main parts of an MVC component. They are described here in brief, but for a more thorough explanation, please refer to the links provided at the end of this tutorial.&lt;br /&gt;
&lt;br /&gt;
=== Model ===&lt;br /&gt;
The model is the part of the component that encapsulates the application&#039;s data. It will often provide routines to manage and manipulate this data in a meaningful way in addition to routines that retrieve the data from the model. In our case, the model will contain methods to add, remove and update information about the greetings in the database. It will also contain methods to retrieve the list of greetings from the database. In general, the underlying data access technique should be encapsulated in the model. In this way, if an application is to be moved from a system that utilizes a flat file to store its information to a system that uses a database, the model is the only element that needs to be changed, not the view or the controller.&lt;br /&gt;
&lt;br /&gt;
=== View ===&lt;br /&gt;
The view is the part of the component that is used to render the data from the model in a manner that is suitable for interaction. For a web-based application, the view would generally be an HTML page that is returned to the data. The view pulls data from the model (which is passed to it from the controller) and feeds the data into a template which is populated and presented to the user. The view does not cause the data to be modified in any way, it only displays data retrieved from the model.&lt;br /&gt;
&lt;br /&gt;
=== Controller ===&lt;br /&gt;
The controller is responsible for responding to user actions. In the case of a web application, a user action is (generally) a page request. The controller will determine what request is being made by the user and respond appropriately by triggering the model to manipulate the data appropriately and passing the model into the view. The controller does not display the data in the model, it only triggers methods in the model which modify the data, and then pass the model into the view which displays the data.&lt;br /&gt;
&lt;br /&gt;
== Joomla! MVC Implementation ==&lt;br /&gt;
In Joomla!, the MVC pattern is implemented using three classes: [http://api.joomla.org/Joomla-Framework/Application/JModel.html JModel], [http://api.joomla.org/Joomla-Framework/Application/JView.html JView] and [http://api.joomla.org/Joomla-Framework/Application/JController.html JController]. For more detailed information about these classes, please refer to the API reference documentation (WIP).&lt;br /&gt;
&lt;br /&gt;
== Creating a Component ==&lt;br /&gt;
For our basic component, we only require five files:&lt;br /&gt;
&lt;br /&gt;
* hello.php - this is the entry point to our component&lt;br /&gt;
* controller.php - this file contains our base controller&lt;br /&gt;
* views/hello/view.html.php - this file retrieves the necessary data and pushes it into the template&lt;br /&gt;
* views/hello/tmpl/default.php - this is the template for our output&lt;br /&gt;
* hello.xml - this is an XML file that tells Joomla! how to install our component.&lt;br /&gt;
&lt;br /&gt;
=== Creating the Entry Point ===&lt;br /&gt;
Joomla! is always accessed through a single point of entry: index.php for the Site Application or administrator/index.php for the Administrator Application. The application will then load the required component, based on the value of &#039;option&#039; in the URL or in the POST data. For our component, the URL would be:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;index.php?option=com_hello&amp;amp;view=hello&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will load our main file, which can be seen as the single point of entry for our component: components/com_hello/hello.php.&lt;br /&gt;
&lt;br /&gt;
The code for this file is fairly typical across components.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * components/com_hello/hello.php&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_1&lt;br /&gt;
 * @license    GNU/GPL&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
// No direct access&lt;br /&gt;
&lt;br /&gt;
defined( &#039;_JEXEC&#039; ) or die( &#039;Restricted access&#039; );&lt;br /&gt;
&lt;br /&gt;
// Require the base controller&lt;br /&gt;
&lt;br /&gt;
require_once( JPATH_COMPONENT.DS.&#039;controller.php&#039; );&lt;br /&gt;
&lt;br /&gt;
// Require specific controller if requested&lt;br /&gt;
if($controller = JRequest::getWord(&#039;controller&#039;)) {&lt;br /&gt;
    $path = JPATH_COMPONENT.DS.&#039;controllers&#039;.DS.$controller.&#039;.php&#039;;&lt;br /&gt;
    if (file_exists($path)) {&lt;br /&gt;
        require_once $path;&lt;br /&gt;
    } else {&lt;br /&gt;
        $controller = &#039;&#039;;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Create the controller&lt;br /&gt;
$classname    = &#039;HelloController&#039;.$controller;&lt;br /&gt;
$controller   = new $classname( );&lt;br /&gt;
&lt;br /&gt;
// Perform the Request task&lt;br /&gt;
$controller-&amp;gt;execute( JRequest::getVar( &#039;task&#039; ) );&lt;br /&gt;
&lt;br /&gt;
// Redirect if set by the controller&lt;br /&gt;
$controller-&amp;gt;redirect();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first statement is a security check.&lt;br /&gt;
&lt;br /&gt;
JPATH_COMPONENT is the absolute path to the current component, in our case components/com_hello. If you specifically need either the Site component or the Administrator component, you can use JPATH_COMPONENT_SITE or JPATH_COMPONENT_ADMINISTRATOR.&lt;br /&gt;
&lt;br /&gt;
DS is the directory separator of your system: either &#039;/&#039; or &#039;\&#039;. This is automatically set by the framework so the developer doesn&#039;t have to worry about developing different versions for different server OSs. DS should always be used when referring to files on the local server.&lt;br /&gt;
&lt;br /&gt;
After loading the base controller, we check if a specific controller is needed. In this component, the base controller is the only controller, but we will leave this here for future use.&lt;br /&gt;
&lt;br /&gt;
JRequest:getVar() finds a variable in the URL or the POST data. So if our URL is index.php?option=com_hello&amp;gt;controller=controller_name, then we can retrieve our controller name in our component using: echo JRequest::getVar(&#039;controller&#039;);&lt;br /&gt;
&lt;br /&gt;
Now we have our base controller &#039;HelloController&#039; in com_hello/controller.php, and, if needed, additional controllers like &#039;HelloControllerController1&#039; in com_hello/controllers/controller1.php. Using this standard naming scheme will make things easy later on: &#039;{Componentname}{Controller}{Controllername}&#039;&lt;br /&gt;
&lt;br /&gt;
After the controller is created, we instruct the controller to execute the task, as defined in the URL: index.php?option=com_hello&amp;amp;task=sometask. If no task is set, the default task &#039;display&#039; will be assumed. When display is used, the &#039;view&#039; variable will decide what will be displayed. Other common tasks are save, edit, new...&lt;br /&gt;
&lt;br /&gt;
The controller might decide to redirect the page, usually after a task like &#039;save&#039; has been completed. This last statement takes care of the actual redirection.&lt;br /&gt;
&lt;br /&gt;
The main entry point (hello.php) essentially passes control to the controller, which handles performing the task that was specified in the request.&lt;br /&gt;
&lt;br /&gt;
Note that we don&#039;t use a closing php tag in this file: ?&amp;gt;. The reason for this is that we will not have any unwanted whitespace in the output code. This is default practice since Joomla! 1.5, and will be used for all php-only files.&lt;br /&gt;
&lt;br /&gt;
=== Creating the Controller ===&lt;br /&gt;
Our component only has one task - greet the world. Therefore, the controller will be very simple. No data manipulation is required. All that needs to be done is the appropriate view loaded. We will have only one method in our controller: display(). Most of the required functionality is built into the JController class, so all that we need to do is invoke the JController::display() method.&lt;br /&gt;
&lt;br /&gt;
The code for the base controller is:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_1&lt;br /&gt;
 * @license    GNU/GPL&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// No direct access&lt;br /&gt;
&lt;br /&gt;
defined( &#039;_JEXEC&#039; ) or die( &#039;Restricted access&#039; );&lt;br /&gt;
&lt;br /&gt;
jimport(&#039;joomla.application.component.controller&#039;);&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Hello World Component Controller&lt;br /&gt;
 *&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 */&lt;br /&gt;
class HelloController extends JController&lt;br /&gt;
{&lt;br /&gt;
    /**&lt;br /&gt;
     * Method to display the view&lt;br /&gt;
     *&lt;br /&gt;
     * @access    public&lt;br /&gt;
     */&lt;br /&gt;
    function display()&lt;br /&gt;
    {&lt;br /&gt;
        parent::display();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The JController constructor will always register a display() task and unless otherwise specified (using the registerDefaultTask() method), it will set it as the default task.&lt;br /&gt;
&lt;br /&gt;
This barebones display() method isn&#039;t really even necessary since all it does is invoke the parent constructor. However, it is a good visual clue to indicate what is happening in the controller.&lt;br /&gt;
&lt;br /&gt;
The JController::display() method will determine the name of the view and layout from the request and load that view and set the layout. When you create a menu item for your component, the menu manager will allow the administrator to select the view that they would like the menu link to display and to specify the layout. A view usually refers to a view of a certain set of data (i.e. a list of cars, a list of events, a single car, a single event). A layout is a way that that view is organized.&lt;br /&gt;
&lt;br /&gt;
In our component, we will have a single view called hello, and a single layout (default).&lt;br /&gt;
&lt;br /&gt;
=== Creating the View ===&lt;br /&gt;
The task of the view is very simple: It retrieves the data to be displayed and pushes it into the template. Data is pushed into the template using the JView::assignRef method.&lt;br /&gt;
&lt;br /&gt;
The code for the view is:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_1&lt;br /&gt;
 * @license    GNU/GPL&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
// no direct access&lt;br /&gt;
&lt;br /&gt;
defined( &#039;_JEXEC&#039; ) or die( &#039;Restricted access&#039; );&lt;br /&gt;
&lt;br /&gt;
jimport( &#039;joomla.application.component.view&#039;);&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * HTML View class for the HelloWorld Component&lt;br /&gt;
 *&lt;br /&gt;
 * @package    HelloWorld&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
class HelloViewHello extends JView&lt;br /&gt;
{&lt;br /&gt;
    function display($tpl = null)&lt;br /&gt;
    {&lt;br /&gt;
        $greeting = &amp;quot;Hello World!&amp;quot;;&lt;br /&gt;
        $this-&amp;gt;assignRef( &#039;greeting&#039;, $greeting );&lt;br /&gt;
&lt;br /&gt;
        parent::display($tpl);&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Creating the Template ====&lt;br /&gt;
&lt;br /&gt;
Joomla! templates/layouts are regular PHP files that are used to layout the data from the view in a particular manner. The variables assigned by the JView::assignRef method can be accessed from the template using $this-&amp;gt;{propertyname} (see the template code below for an example).&lt;br /&gt;
&lt;br /&gt;
Our template is very simple: we only want to display the greeting that was passed in from the view:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
&lt;br /&gt;
// No direct access&lt;br /&gt;
&lt;br /&gt;
defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;); ?&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;&amp;lt;?php echo $this-&amp;gt;greeting; ?&amp;gt;&amp;lt;/h1&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Wrapping It All Up - Creating the hello.xml File ===&lt;br /&gt;
It is possible to install a component manually by copying the files using an FTP client and modifying the database tables. It is more efficient to create a package file that will allow the Joomla! Installer to do this for you. This package file contains a variety of information:&lt;br /&gt;
&lt;br /&gt;
* basic descriptive details about your component (i.e. name), and optionally, a description, copyright and license information.&lt;br /&gt;
* a list of files that need to be copied.&lt;br /&gt;
* optionally, a PHP file that performs additional install and uninstall operations.&lt;br /&gt;
* optionally, an SQL file which contains database queries that should be executed upon install/uninstall&lt;br /&gt;
&lt;br /&gt;
The format of the XML file is as follows:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;&lt;br /&gt;
&amp;lt;install type=&amp;quot;component&amp;quot; version=&amp;quot;1.5.0&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;name&amp;gt;Hello&amp;lt;/name&amp;gt;&lt;br /&gt;
 &amp;lt;!-- The following elements are optional and free of formatting conttraints --&amp;gt;&lt;br /&gt;
 &amp;lt;creationDate&amp;gt;2007-02-22&amp;lt;/creationDate&amp;gt;&lt;br /&gt;
 &amp;lt;author&amp;gt;John Doe&amp;lt;/author&amp;gt;&lt;br /&gt;
 &amp;lt;authorEmail&amp;gt;john.doe@example.org&amp;lt;/authorEmail&amp;gt;&lt;br /&gt;
 &amp;lt;authorUrl&amp;gt;http://www.example.org&amp;lt;/authorUrl&amp;gt;&lt;br /&gt;
 &amp;lt;copyright&amp;gt;Copyright Info&amp;lt;/copyright&amp;gt;&lt;br /&gt;
 &amp;lt;license&amp;gt;License Info&amp;lt;/license&amp;gt;&lt;br /&gt;
 &amp;lt;!--  The version string is recorded in the components table --&amp;gt;&lt;br /&gt;
 &amp;lt;version&amp;gt;1.01&amp;lt;/version&amp;gt;&lt;br /&gt;
 &amp;lt;!-- The description is optional and defaults to the name --&amp;gt;&lt;br /&gt;
 &amp;lt;description&amp;gt;Description of the component ...&amp;lt;/description&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;!-- Site Main File Copy Section --&amp;gt;&lt;br /&gt;
 &amp;lt;!-- Note the folder attribute: This attribute describes the folder&lt;br /&gt;
      to copy FROM in the package to install therefore files copied&lt;br /&gt;
      in this section are copied from /site/ in the package --&amp;gt;&lt;br /&gt;
 &amp;lt;files folder=&amp;quot;site&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;controller.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/view.html.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/tmpl/default.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/tmpl/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
 &amp;lt;/files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;administration&amp;gt;&lt;br /&gt;
  &amp;lt;!-- Administration Menu Section --&amp;gt;&lt;br /&gt;
  &amp;lt;menu&amp;gt;Hello World!&amp;lt;/menu&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;!-- Administration Main File Copy Section --&amp;gt;&lt;br /&gt;
  &amp;lt;files folder=&amp;quot;admin&amp;quot;&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;/files&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
 &amp;lt;/administration&amp;gt;&lt;br /&gt;
&amp;lt;/install&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you look closely you will notice that there are some files that will be copied that we have not discussed. These are the index.html files. An index.html file is placed in each directory to prevent prying users from getting a directory listing. If there is no index.html file, some web servers will list the directory contents. This is often undesirable. These files have the simple line:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;html&amp;gt;&amp;lt;body bgcolor=&amp;quot;#FFFFFF&amp;quot;&amp;gt;&amp;lt;/body&amp;gt;&amp;lt;/html&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It will simply display a blank page.&lt;br /&gt;
&lt;br /&gt;
The other file is the hello.php file. This is the entry point for the admin section of our component. Since we don&#039;t have an admin section of our component, it will have the same content as the index.html files at this point in time.&lt;br /&gt;
&lt;br /&gt;
== Contributors ==&lt;br /&gt;
* mjaz&lt;br /&gt;
* staalanden&lt;br /&gt;
&lt;br /&gt;
== Download ==&lt;br /&gt;
The component can be downloaded at: [http://joomlacode.org/gf/download/frsrelease/8108/29433/com_hello1_01.zip com_hello1_01]&lt;/div&gt;</summary>
		<author><name>Tonie</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Adding_a_Model&amp;diff=9520</id>
		<title>J1.5:Developing a MVC Component/Adding a Model</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Adding_a_Model&amp;diff=9520"/>
		<updated>2008-07-27T20:12:47Z</updated>

		<summary type="html">&lt;p&gt;Tonie: /* Download */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
In the first tutorial of this series, creating a simple view-controller component using the Joomla! 1.5 CMS framework was demonstrated.&lt;br /&gt;
&lt;br /&gt;
In the first tutorial, the greeting was hardcoded into the view. This doesn&#039;t follow the MVC pattern exactly because the view is intended to only display the data, and not contain it.&lt;br /&gt;
&lt;br /&gt;
In this second part of the tutorial we will demonstrate how to move this out of the view and into a model. In future tutorials we will demonstrate the power and flexibility that this design pattern provides.&lt;br /&gt;
&lt;br /&gt;
== Creating the Model ==&lt;br /&gt;
&lt;br /&gt;
The concept of model gets its name because this class is intended to represent (or &#039;model&#039;) some entity. In our case, our first model will represent a &#039;hello&#039;, or a greeting. This is in line with our design thus far, because we have one view (&#039;hello&#039;), which is a view of our greeting.&lt;br /&gt;
&lt;br /&gt;
The naming convention for models in the Joomla! framework is that the class name starts with the name of the component (in our case &#039;hello&#039;, followed by &#039;model&#039;, followed by the model name. Therefore, our model class is called HelloModelHello.&lt;br /&gt;
&lt;br /&gt;
At this point, we will only model one behaviour of our hello, and that is retrieving the greeting. We will thus have one method, called getGreeting(). It will simply return the string &#039;Hello, World!&#039;.&lt;br /&gt;
&lt;br /&gt;
Here is the code for our model class:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * Hello Model for Hello World Component&lt;br /&gt;
 * &lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_2&lt;br /&gt;
 * @license    GNU/GPL&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// No direct access&lt;br /&gt;
&lt;br /&gt;
defined( &#039;_JEXEC&#039; ) or die( &#039;Restricted access&#039; );&lt;br /&gt;
&lt;br /&gt;
jimport( &#039;joomla.application.component.model&#039; );&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Hello Model&lt;br /&gt;
 *&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 */&lt;br /&gt;
class HelloModelHello extends JModel&lt;br /&gt;
{&lt;br /&gt;
    /**&lt;br /&gt;
    * Gets the greeting&lt;br /&gt;
    * @return string The greeting to be displayed to the user&lt;br /&gt;
    */&lt;br /&gt;
    function getGreeting()&lt;br /&gt;
    {&lt;br /&gt;
        return &#039;Hello, World!&#039;;&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You will notice a line that starts with jimport. The jimport function is used to load files from the Joomla! framework that are required for our component. This particular statement will load the file /libraries/joomla/application/component/model.php. The &#039;.&#039;s are used as directory separators and the last part is the name of the file to load. All files are loaded relative to the libraries directory. This particular file contains the class definition for the JModel class, which is necessary because our model extends this class.&lt;br /&gt;
&lt;br /&gt;
Now that we have created our model, we must modify our view so that it uses it to obtain the greeting.&lt;br /&gt;
&lt;br /&gt;
== Using the Model ==&lt;br /&gt;
&lt;br /&gt;
The Joomla! framework is setup in such a way that the controller will automatically load the model that has the same name as the view and will push it into the view. Since our view is called &#039;Hello&#039;, our &#039;Hello&#039; model will automatically be loaded and pushed into the view. Therefore, we can easily retrieve a reference to our model using the JView::getModel() method.&lt;br /&gt;
&lt;br /&gt;
Our previous view code contained the lines:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$greeting = &amp;quot;Hello World!&amp;quot;;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To take advantage of our model, we change this line to:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$model =&amp;amp; $this-&amp;gt;getModel();&lt;br /&gt;
$greeting = $model-&amp;gt;getGreeting();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The complete view now looks like:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_2&lt;br /&gt;
 * @license    GNU/GPL&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
// No direct access&lt;br /&gt;
&lt;br /&gt;
defined( &#039;_JEXEC&#039; ) or die( &#039;Restricted access&#039; );&lt;br /&gt;
&lt;br /&gt;
jimport( &#039;joomla.application.component.view&#039;);&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * HTML View class for the HelloWorld Component&lt;br /&gt;
 *&lt;br /&gt;
 * @package    HelloWorld&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
class HelloViewHello extends JView&lt;br /&gt;
{&lt;br /&gt;
    function display($tpl = null)&lt;br /&gt;
    {&lt;br /&gt;
        $model =&amp;amp; $this-&amp;gt;getModel();&lt;br /&gt;
        $greeting = $model-&amp;gt;getGreeting();&lt;br /&gt;
        $this-&amp;gt;assignRef( &#039;greeting&#039;,	$greeting );&lt;br /&gt;
&lt;br /&gt;
        parent::display($tpl);&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Adding the File to the Package ===&lt;br /&gt;
&lt;br /&gt;
All that remains is to add an entry to the XML file so that our new model will be copied. The Joomla! framework will look for our model in the models directory, so the entry for this file will look like (it should be added to the site section):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;filename&amp;gt;models/hello.php&amp;lt;/filename&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Our new hello.xml file will look like:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;&lt;br /&gt;
&amp;lt;install type=&amp;quot;component&amp;quot; version=&amp;quot;1.5.0&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;name&amp;gt;Hello&amp;lt;/name&amp;gt;&lt;br /&gt;
 &amp;lt;!-- The following elements are optional and free of formatting conttraints --&amp;gt;&lt;br /&gt;
 &amp;lt;creationDate&amp;gt;2007-02-22&amp;lt;/creationDate&amp;gt;&lt;br /&gt;
 &amp;lt;author&amp;gt;John Doe&amp;lt;/author&amp;gt;&lt;br /&gt;
 &amp;lt;authorEmail&amp;gt;john.doe@example.org&amp;lt;/authorEmail&amp;gt;&lt;br /&gt;
 &amp;lt;authorUrl&amp;gt;http://www.example.org&amp;lt;/authorUrl&amp;gt;&lt;br /&gt;
 &amp;lt;copyright&amp;gt;Copyright Info&amp;lt;/copyright&amp;gt;&lt;br /&gt;
 &amp;lt;license&amp;gt;License Info&amp;lt;/license&amp;gt;&lt;br /&gt;
 &amp;lt;!--  The version string is recorded in the components table --&amp;gt;&lt;br /&gt;
 &amp;lt;version&amp;gt;1.01&amp;lt;/version&amp;gt;&lt;br /&gt;
 &amp;lt;!-- The description is optional and defaults to the name --&amp;gt;&lt;br /&gt;
 &amp;lt;description&amp;gt;Description of the component ...&amp;lt;/description&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;!-- Site Main File Copy Section --&amp;gt;&lt;br /&gt;
 &amp;lt;!-- Note the folder attribute: This attribute describes the folder&lt;br /&gt;
      to copy FROM in the package to install therefore files copied&lt;br /&gt;
      in this section are copied from /site/ in the package --&amp;gt;&lt;br /&gt;
 &amp;lt;files folder=&amp;quot;site&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;controller.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;models/hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;models/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/view.html.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/tmpl/default.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/tmpl/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
 &amp;lt;/files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;administration&amp;gt;&lt;br /&gt;
  &amp;lt;!-- Administration Menu Section --&amp;gt;&lt;br /&gt;
  &amp;lt;menu&amp;gt;Hello World!&amp;lt;/menu&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;!-- Administration Main File Copy Section --&amp;gt;&lt;br /&gt;
  &amp;lt;files folder=&amp;quot;admin&amp;quot;&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;/files&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
 &amp;lt;/administration&amp;gt;&lt;br /&gt;
&amp;lt;/install&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
We now have a simple MVC component. Each element is very simple at this point, but provides a great deal of flexibility and power.&lt;br /&gt;
&lt;br /&gt;
== Contributors ==&lt;br /&gt;
* staalanden&lt;br /&gt;
&lt;br /&gt;
== Download ==&lt;br /&gt;
&lt;br /&gt;
The component can be downloaded at: [http://joomlacode.org/gf/download/frsrelease/8109/29434/com_hello2_01.zip com_hello2_01]&lt;/div&gt;</summary>
		<author><name>Tonie</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Introduction&amp;diff=9519</id>
		<title>J1.5:Developing a MVC Component/Introduction</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Introduction&amp;diff=9519"/>
		<updated>2008-07-27T20:12:17Z</updated>

		<summary type="html">&lt;p&gt;Tonie: /* Download */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
A software framework is the base of an application that can be used by a developer. The framework in Joomla! 1.5 unleashes a great deal of power for them. The Joomla! code has been completely overhauled and cleaned up. This tutorial will guide you through the process of developing a component using the framework.&lt;br /&gt;
&lt;br /&gt;
The scope of this project will be to develop a simple Hello World! component. In future tutorials, this simple framework will be built upon to show the full power and versatility of the MVC design pattern in Joomla!&lt;br /&gt;
&lt;br /&gt;
== Requirements ==&lt;br /&gt;
You need Joomla! 1.5 or greater for this tutorial.&lt;br /&gt;
&lt;br /&gt;
== Introduction to Model-View-Controller ==&lt;br /&gt;
While the idea behind a component may seem extremely simple, code can quickly become very complex as additional features are added or the interface is customized.&lt;br /&gt;
&lt;br /&gt;
Model-View-Controller (herein referred to as MVC) is a software design pattern that can be used to organize code in such a way that the business logic and data presentation are separate. The premise behind this approach is that if the business logic is grouped into one section, then the interface and user interaction that surrounds the data can revised and customized without having to reprogram the business logic.&lt;br /&gt;
&lt;br /&gt;
There are three main parts of an MVC component. They are described here in brief, but for a more thorough explanation, please refer to the links provided at the end of this tutorial.&lt;br /&gt;
&lt;br /&gt;
=== Model ===&lt;br /&gt;
The model is the part of the component that encapsulates the application&#039;s data. It will often provide routines to manage and manipulate this data in a meaningful way in addition to routines that retrieve the data from the model. In our case, the model will contain methods to add, remove and update information about the greetings in the database. It will also contain methods to retrieve the list of greetings from the database. In general, the underlying data access technique should be encapsulated in the model. In this way, if an application is to be moved from a system that utilizes a flat file to store its information to a system that uses a database, the model is the only element that needs to be changed, not the view or the controller.&lt;br /&gt;
&lt;br /&gt;
=== View ===&lt;br /&gt;
The view is the part of the component that is used to render the data from the model in a manner that is suitable for interaction. For a web-based application, the view would generally be an HTML page that is returned to the data. The view pulls data from the model (which is passed to it from the controller) and feeds the data into a template which is populated and presented to the user. The view does not cause the data to be modified in any way, it only displays data retrieved from the model.&lt;br /&gt;
&lt;br /&gt;
=== Controller ===&lt;br /&gt;
The controller is responsible for responding to user actions. In the case of a web application, a user action is (generally) a page request. The controller will determine what request is being made by the user and respond appropriately by triggering the model to manipulate the data appropriately and passing the model into the view. The controller does not display the data in the model, it only triggers methods in the model which modify the data, and then pass the model into the view which displays the data.&lt;br /&gt;
&lt;br /&gt;
== Joomla! MVC Implementation ==&lt;br /&gt;
In Joomla!, the MVC pattern is implemented using three classes: [http://api.joomla.org/Joomla-Framework/Application/JModel.html JModel], [http://api.joomla.org/Joomla-Framework/Application/JView.html JView] and [http://api.joomla.org/Joomla-Framework/Application/JController.html JController]. For more detailed information about these classes, please refer to the API reference documentation (WIP).&lt;br /&gt;
&lt;br /&gt;
== Creating a Component ==&lt;br /&gt;
For our basic component, we only require five files:&lt;br /&gt;
&lt;br /&gt;
* hello.php - this is the entry point to our component&lt;br /&gt;
* controller.php - this file contains our base controller&lt;br /&gt;
* views/hello/view.html.php - this file retrieves the necessary data and pushes it into the template&lt;br /&gt;
* views/hello/tmpl/default.php - this is the template for our output&lt;br /&gt;
* hello.xml - this is an XML file that tells Joomla! how to install our component.&lt;br /&gt;
&lt;br /&gt;
=== Creating the Entry Point ===&lt;br /&gt;
Joomla! is always accessed through a single point of entry: index.php for the Site Application or administrator/index.php for the Administrator Application. The application will then load the required component, based on the value of &#039;option&#039; in the URL or in the POST data. For our component, the URL would be:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;index.php?option=com_hello&amp;amp;view=hello&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will load our main file, which can be seen as the single point of entry for our component: components/com_hello/hello.php.&lt;br /&gt;
&lt;br /&gt;
The code for this file is fairly typical across components.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * components/com_hello/hello.php&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_1&lt;br /&gt;
 * @license    GNU/GPL&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
// No direct access&lt;br /&gt;
&lt;br /&gt;
defined( &#039;_JEXEC&#039; ) or die( &#039;Restricted access&#039; );&lt;br /&gt;
&lt;br /&gt;
// Require the base controller&lt;br /&gt;
&lt;br /&gt;
require_once( JPATH_COMPONENT.DS.&#039;controller.php&#039; );&lt;br /&gt;
&lt;br /&gt;
// Require specific controller if requested&lt;br /&gt;
if($controller = JRequest::getWord(&#039;controller&#039;)) {&lt;br /&gt;
    $path = JPATH_COMPONENT.DS.&#039;controllers&#039;.DS.$controller.&#039;.php&#039;;&lt;br /&gt;
    if (file_exists($path)) {&lt;br /&gt;
        require_once $path;&lt;br /&gt;
    } else {&lt;br /&gt;
        $controller = &#039;&#039;;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Create the controller&lt;br /&gt;
$classname    = &#039;HelloController&#039;.$controller;&lt;br /&gt;
$controller   = new $classname( );&lt;br /&gt;
&lt;br /&gt;
// Perform the Request task&lt;br /&gt;
$controller-&amp;gt;execute( JRequest::getVar( &#039;task&#039; ) );&lt;br /&gt;
&lt;br /&gt;
// Redirect if set by the controller&lt;br /&gt;
$controller-&amp;gt;redirect();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first statement is a security check.&lt;br /&gt;
&lt;br /&gt;
JPATH_COMPONENT is the absolute path to the current component, in our case components/com_hello. If you specifically need either the Site component or the Administrator component, you can use JPATH_COMPONENT_SITE or JPATH_COMPONENT_ADMINISTRATOR.&lt;br /&gt;
&lt;br /&gt;
DS is the directory separator of your system: either &#039;/&#039; or &#039;\&#039;. This is automatically set by the framework so the developer doesn&#039;t have to worry about developing different versions for different server OSs. DS should always be used when referring to files on the local server.&lt;br /&gt;
&lt;br /&gt;
After loading the base controller, we check if a specific controller is needed. In this component, the base controller is the only controller, but we will leave this here for future use.&lt;br /&gt;
&lt;br /&gt;
JRequest:getVar() finds a variable in the URL or the POST data. So if our URL is index.php?option=com_hello&amp;gt;controller=controller_name, then we can retrieve our controller name in our component using: echo JRequest::getVar(&#039;controller&#039;);&lt;br /&gt;
&lt;br /&gt;
Now we have our base controller &#039;HelloController&#039; in com_hello/controller.php, and, if needed, additional controllers like &#039;HelloControllerController1&#039; in com_hello/controllers/controller1.php. Using this standard naming scheme will make things easy later on: &#039;{Componentname}{Controller}{Controllername}&#039;&lt;br /&gt;
&lt;br /&gt;
After the controller is created, we instruct the controller to execute the task, as defined in the URL: index.php?option=com_hello&amp;amp;task=sometask. If no task is set, the default task &#039;display&#039; will be assumed. When display is used, the &#039;view&#039; variable will decide what will be displayed. Other common tasks are save, edit, new...&lt;br /&gt;
&lt;br /&gt;
The controller might decide to redirect the page, usually after a task like &#039;save&#039; has been completed. This last statement takes care of the actual redirection.&lt;br /&gt;
&lt;br /&gt;
The main entry point (hello.php) essentially passes control to the controller, which handles performing the task that was specified in the request.&lt;br /&gt;
&lt;br /&gt;
Note that we don&#039;t use a closing php tag in this file: ?&amp;gt;. The reason for this is that we will not have any unwanted whitespace in the output code. This is default practice since Joomla! 1.5, and will be used for all php-only files.&lt;br /&gt;
&lt;br /&gt;
=== Creating the Controller ===&lt;br /&gt;
Our component only has one task - greet the world. Therefore, the controller will be very simple. No data manipulation is required. All that needs to be done is the appropriate view loaded. We will have only one method in our controller: display(). Most of the required functionality is built into the JController class, so all that we need to do is invoke the JController::display() method.&lt;br /&gt;
&lt;br /&gt;
The code for the base controller is:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_1&lt;br /&gt;
 * @license    GNU/GPL&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// No direct access&lt;br /&gt;
&lt;br /&gt;
defined( &#039;_JEXEC&#039; ) or die( &#039;Restricted access&#039; );&lt;br /&gt;
&lt;br /&gt;
jimport(&#039;joomla.application.component.controller&#039;);&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Hello World Component Controller&lt;br /&gt;
 *&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 */&lt;br /&gt;
class HelloController extends JController&lt;br /&gt;
{&lt;br /&gt;
    /**&lt;br /&gt;
     * Method to display the view&lt;br /&gt;
     *&lt;br /&gt;
     * @access    public&lt;br /&gt;
     */&lt;br /&gt;
    function display()&lt;br /&gt;
    {&lt;br /&gt;
        parent::display();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The JController constructor will always register a display() task and unless otherwise specified (using the registerDefaultTask() method), it will set it as the default task.&lt;br /&gt;
&lt;br /&gt;
This barebones display() method isn&#039;t really even necessary since all it does is invoke the parent constructor. However, it is a good visual clue to indicate what is happening in the controller.&lt;br /&gt;
&lt;br /&gt;
The JController::display() method will determine the name of the view and layout from the request and load that view and set the layout. When you create a menu item for your component, the menu manager will allow the administrator to select the view that they would like the menu link to display and to specify the layout. A view usually refers to a view of a certain set of data (i.e. a list of cars, a list of events, a single car, a single event). A layout is a way that that view is organized.&lt;br /&gt;
&lt;br /&gt;
In our component, we will have a single view called hello, and a single layout (default).&lt;br /&gt;
&lt;br /&gt;
=== Creating the View ===&lt;br /&gt;
The task of the view is very simple: It retrieves the data to be displayed and pushes it into the template. Data is pushed into the template using the JView::assignRef method.&lt;br /&gt;
&lt;br /&gt;
The code for the view is:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_1&lt;br /&gt;
 * @license    GNU/GPL&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
// no direct access&lt;br /&gt;
&lt;br /&gt;
defined( &#039;_JEXEC&#039; ) or die( &#039;Restricted access&#039; );&lt;br /&gt;
&lt;br /&gt;
jimport( &#039;joomla.application.component.view&#039;);&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * HTML View class for the HelloWorld Component&lt;br /&gt;
 *&lt;br /&gt;
 * @package    HelloWorld&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
class HelloViewHello extends JView&lt;br /&gt;
{&lt;br /&gt;
    function display($tpl = null)&lt;br /&gt;
    {&lt;br /&gt;
        $greeting = &amp;quot;Hello World!&amp;quot;;&lt;br /&gt;
        $this-&amp;gt;assignRef( &#039;greeting&#039;, $greeting );&lt;br /&gt;
&lt;br /&gt;
        parent::display($tpl);&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Creating the Template ====&lt;br /&gt;
&lt;br /&gt;
Joomla! templates/layouts are regular PHP files that are used to layout the data from the view in a particular manner. The variables assigned by the JView::assignRef method can be accessed from the template using $this-&amp;gt;{propertyname} (see the template code below for an example).&lt;br /&gt;
&lt;br /&gt;
Our template is very simple: we only want to display the greeting that was passed in from the view:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
&lt;br /&gt;
// No direct access&lt;br /&gt;
&lt;br /&gt;
defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;); ?&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;&amp;lt;?php echo $this-&amp;gt;greeting; ?&amp;gt;&amp;lt;/h1&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Wrapping It All Up - Creating the hello.xml File ===&lt;br /&gt;
It is possible to install a component manually by copying the files using an FTP client and modifying the database tables. It is more efficient to create a package file that will allow the Joomla! Installer to do this for you. This package file contains a variety of information:&lt;br /&gt;
&lt;br /&gt;
* basic descriptive details about your component (i.e. name), and optionally, a description, copyright and license information.&lt;br /&gt;
* a list of files that need to be copied.&lt;br /&gt;
* optionally, a PHP file that performs additional install and uninstall operations.&lt;br /&gt;
* optionally, an SQL file which contains database queries that should be executed upon install/uninstall&lt;br /&gt;
&lt;br /&gt;
The format of the XML file is as follows:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;&lt;br /&gt;
&amp;lt;install type=&amp;quot;component&amp;quot; version=&amp;quot;1.5.0&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;name&amp;gt;Hello&amp;lt;/name&amp;gt;&lt;br /&gt;
 &amp;lt;!-- The following elements are optional and free of formatting conttraints --&amp;gt;&lt;br /&gt;
 &amp;lt;creationDate&amp;gt;2007-02-22&amp;lt;/creationDate&amp;gt;&lt;br /&gt;
 &amp;lt;author&amp;gt;John Doe&amp;lt;/author&amp;gt;&lt;br /&gt;
 &amp;lt;authorEmail&amp;gt;john.doe@example.org&amp;lt;/authorEmail&amp;gt;&lt;br /&gt;
 &amp;lt;authorUrl&amp;gt;http://www.example.org&amp;lt;/authorUrl&amp;gt;&lt;br /&gt;
 &amp;lt;copyright&amp;gt;Copyright Info&amp;lt;/copyright&amp;gt;&lt;br /&gt;
 &amp;lt;license&amp;gt;License Info&amp;lt;/license&amp;gt;&lt;br /&gt;
 &amp;lt;!--  The version string is recorded in the components table --&amp;gt;&lt;br /&gt;
 &amp;lt;version&amp;gt;1.01&amp;lt;/version&amp;gt;&lt;br /&gt;
 &amp;lt;!-- The description is optional and defaults to the name --&amp;gt;&lt;br /&gt;
 &amp;lt;description&amp;gt;Description of the component ...&amp;lt;/description&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;!-- Site Main File Copy Section --&amp;gt;&lt;br /&gt;
 &amp;lt;!-- Note the folder attribute: This attribute describes the folder&lt;br /&gt;
      to copy FROM in the package to install therefore files copied&lt;br /&gt;
      in this section are copied from /site/ in the package --&amp;gt;&lt;br /&gt;
 &amp;lt;files folder=&amp;quot;site&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;controller.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/view.html.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/tmpl/default.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/tmpl/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
 &amp;lt;/files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;administration&amp;gt;&lt;br /&gt;
  &amp;lt;!-- Administration Menu Section --&amp;gt;&lt;br /&gt;
  &amp;lt;menu&amp;gt;Hello World!&amp;lt;/menu&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;!-- Administration Main File Copy Section --&amp;gt;&lt;br /&gt;
  &amp;lt;files folder=&amp;quot;admin&amp;quot;&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;/files&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
 &amp;lt;/administration&amp;gt;&lt;br /&gt;
&amp;lt;/install&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you look closely you will notice that there are some files that will be copied that we have not discussed. These are the index.html files. An index.html file is placed in each directory to prevent prying users from getting a directory listing. If there is no index.html file, some web servers will list the directory contents. This is often undesirable. These files have the simple line:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;html&amp;gt;&amp;lt;body bgcolor=&amp;quot;#FFFFFF&amp;quot;&amp;gt;&amp;lt;/body&amp;gt;&amp;lt;/html&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It will simply display a blank page.&lt;br /&gt;
&lt;br /&gt;
The other file is the hello.php file. This is the entry point for the admin section of our component. Since we don&#039;t have an admin section of our component, it will have the same content as the index.html files at this point in time.&lt;br /&gt;
&lt;br /&gt;
== Contributors ==&lt;br /&gt;
* mjaz&lt;br /&gt;
* staalanden&lt;br /&gt;
&lt;br /&gt;
== Download ==&lt;br /&gt;
The component can be downloaded at: [http://joomlacode.org/gf/download/frsrelease/8108/29433/com_hello1_01.zip com_hello1_01]&lt;/div&gt;</summary>
		<author><name>Tonie</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Creating_an_Administrator_Interface&amp;diff=9518</id>
		<title>J1.5:Developing a MVC Component/Creating an Administrator Interface</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Creating_an_Administrator_Interface&amp;diff=9518"/>
		<updated>2008-07-27T20:11:55Z</updated>

		<summary type="html">&lt;p&gt;Tonie: /* Download */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
In the first three tutorials, we have developed a MVC component that retrieves its data from a table in the database. Currently, there is no way to add data to the database except to do it manually using another tool. In this tutorial, we will develop an administrator section for our component which will make it possible to manage the entries in the database.&lt;br /&gt;
&lt;br /&gt;
== Creating the Basic Framework ==&lt;br /&gt;
&lt;br /&gt;
The basic framework of the administrator panel is very similar to the site portion. The main entry point for the administrator section of the component is hello.php. This file is identical to the hello.php file that was used in the site portion except the name of the controller it loads will be changed to HellosController. The default controller is also called controller.php and this file is identical to the default controller in the site portion, with the exception that the controller is named HellosController instead of HelloController. This difference is so that JController will by default load the hellos view, which will display a list of our greetings.&lt;br /&gt;
&lt;br /&gt;
Here is the listing for hello.php:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_4&lt;br /&gt;
 * @license    GNU/GPL&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
// No direct access&lt;br /&gt;
&lt;br /&gt;
defined( &#039;_JEXEC&#039; ) or die( &#039;Restricted access&#039; );&lt;br /&gt;
&lt;br /&gt;
// Require the base controller&lt;br /&gt;
&lt;br /&gt;
require_once( JPATH_COMPONENT.DS.&#039;controller.php&#039; );&lt;br /&gt;
&lt;br /&gt;
// Require specific controller if requested&lt;br /&gt;
if($controller = JRequest::getWord(&#039;controller&#039;)) {&lt;br /&gt;
    $path = JPATH_COMPONENT.DS.&#039;controllers&#039;.DS.$controller.&#039;.php&#039;;&lt;br /&gt;
    if (file_exists($path)) {&lt;br /&gt;
        require_once $path;&lt;br /&gt;
    } else {&lt;br /&gt;
        $controller = &#039;&#039;;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Create the controller&lt;br /&gt;
$classname    = &#039;HellosController&#039;.$controller;&lt;br /&gt;
$controller   = new $classname( );&lt;br /&gt;
&lt;br /&gt;
// Perform the Request task&lt;br /&gt;
$controller-&amp;gt;execute( JRequest::getVar( &#039;task&#039; ) );&lt;br /&gt;
&lt;br /&gt;
// Redirect if set by the controller&lt;br /&gt;
$controller-&amp;gt;redirect();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The view and model that we will start with is the hellos view and the hellos model. We will start with the model.&lt;br /&gt;
&lt;br /&gt;
==== The Hellos Model ====&lt;br /&gt;
&lt;br /&gt;
The Hellos Model will be very simple. The only operation that we currently need is the ability to retrieve the list of hellos from the database. This operation will be implemented in a method called getData().&lt;br /&gt;
&lt;br /&gt;
The JModel class has a built in protected method called _getList(). This method can be used to simplify the task of retrieving a list of records from the database. We simply need to pass it the query and it will return the list of records.&lt;br /&gt;
&lt;br /&gt;
At a later point in time, we might want to use our query from within another method. Therefore, we will create a private method called _buildQuery() which will return the query that will be passed to _getList(). This makes it easier to change the query as well since it is localized in one place.&lt;br /&gt;
&lt;br /&gt;
Therefore we need two methods in our class: getData() and _buildQuery().&lt;br /&gt;
&lt;br /&gt;
_buildQuery() simply returns the query. It looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source  lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Returns the query&lt;br /&gt;
 * @return string The query to be used to retrieve the rows from the database&lt;br /&gt;
 */&lt;br /&gt;
function _buildQuery()&lt;br /&gt;
{&lt;br /&gt;
    $query = &#039; SELECT * &#039;&lt;br /&gt;
           . &#039; FROM #__hello &#039;&lt;br /&gt;
    ;&lt;br /&gt;
&lt;br /&gt;
    return $query;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
getData() will obtain the query and retrieve the records from the database. Now it might happen that we need to retrieve this list of data twice in one page load. It would be a waste to have to query the database twice. Therefore, we will have this method store the data in a protected property so that on subsequent requests it can simply return the data it has already retrieved. This property will be called _data.&lt;br /&gt;
&lt;br /&gt;
Here is the getData() method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source  lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Retrieves the hello data&lt;br /&gt;
 * @return array Array of objects containing the data from the database&lt;br /&gt;
 */&lt;br /&gt;
function getData()&lt;br /&gt;
{&lt;br /&gt;
    // Lets load the data if it doesn&#039;t already exist&lt;br /&gt;
    if (empty( $this-&amp;gt;_data ))&lt;br /&gt;
    {&lt;br /&gt;
        $query = $this-&amp;gt;_buildQuery();&lt;br /&gt;
        $this-&amp;gt;_data = $this-&amp;gt;_getList( $query );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return $this-&amp;gt;_data;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The completed model looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * Hellos Model for Hello World Component&lt;br /&gt;
 * &lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_4&lt;br /&gt;
 * @license        GNU/GPL&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// Check to ensure this file is included in Joomla!&lt;br /&gt;
defined(&#039;_JEXEC&#039;) or die();&lt;br /&gt;
&lt;br /&gt;
jimport( &#039;joomla.application.component.model&#039; );&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Hello Model&lt;br /&gt;
 *&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 */&lt;br /&gt;
class HellosModelHellos extends JModel&lt;br /&gt;
{&lt;br /&gt;
    /**&lt;br /&gt;
     * Hellos data array&lt;br /&gt;
     *&lt;br /&gt;
     * @var array&lt;br /&gt;
     */&lt;br /&gt;
    var $_data;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Returns the query&lt;br /&gt;
     * @return string The query to be used to retrieve the rows from the database&lt;br /&gt;
     */&lt;br /&gt;
    function _buildQuery()&lt;br /&gt;
    {&lt;br /&gt;
        $query = &#039; SELECT * &#039;&lt;br /&gt;
            . &#039; FROM #__hello &#039;&lt;br /&gt;
        ;&lt;br /&gt;
        return $query;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Retrieves the hello data&lt;br /&gt;
     * @return array Array of objects containing the data from the database&lt;br /&gt;
     */&lt;br /&gt;
    function getData()&lt;br /&gt;
    {&lt;br /&gt;
        // Lets load the data if it doesn&#039;t already exist&lt;br /&gt;
        if (empty( $this-&amp;gt;_data ))&lt;br /&gt;
        {&lt;br /&gt;
            $query = $this-&amp;gt;_buildQuery();&lt;br /&gt;
            $this-&amp;gt;_data = $this-&amp;gt;_getList( $query );&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        return $this-&amp;gt;_data;&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This file is saved as models/hellos.php.&lt;br /&gt;
&lt;br /&gt;
==== The Hellos View ====&lt;br /&gt;
&lt;br /&gt;
Now that we have a model to retrieve our data, we need to display it. This view will be fairly similar to the view from the site section as well.&lt;br /&gt;
&lt;br /&gt;
Just as our model was automatically instantiated in the site, so it is in the administrator. Methods that start with get in the model can be accessed using the get() method of the JView class. So our view has three lines: one to retrieve the data from the model, one to push the data into the template, and one to invoke the display method to display the output. Thus we have:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * Hellos View for Hello World Component&lt;br /&gt;
 * &lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_4&lt;br /&gt;
 * @license        GNU/GPL&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// Check to ensure this file is included in Joomla!&lt;br /&gt;
defined(&#039;_JEXEC&#039;) or die();&lt;br /&gt;
&lt;br /&gt;
jimport( &#039;joomla.application.component.view&#039; );&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Hellos View&lt;br /&gt;
 *&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 */&lt;br /&gt;
class HellosViewHellos extends JView&lt;br /&gt;
{&lt;br /&gt;
    /**&lt;br /&gt;
     * Hellos view display method&lt;br /&gt;
     * @return void&lt;br /&gt;
     **/&lt;br /&gt;
    function display($tpl = null)&lt;br /&gt;
    {&lt;br /&gt;
        JToolBarHelper::title( JText::_( &#039;Hello Manager&#039; ), &#039;generic.png&#039; );&lt;br /&gt;
        JToolBarHelper::deleteList();&lt;br /&gt;
        JToolBarHelper::editListX();&lt;br /&gt;
        JToolBarHelper::addNewX();&lt;br /&gt;
&lt;br /&gt;
        // Get data from the model&lt;br /&gt;
        $items =&amp;amp; $this-&amp;gt;get( &#039;Data&#039;);&lt;br /&gt;
&lt;br /&gt;
        $this-&amp;gt;assignRef( &#039;items&#039;, $items );&lt;br /&gt;
&lt;br /&gt;
        parent::display($tpl);&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This file is saved as views/hellos/view.html.php.&lt;br /&gt;
&lt;br /&gt;
==== The Hellos Template ====&lt;br /&gt;
&lt;br /&gt;
The template will take the data pushed into it from the view and produce the output. We will display our output in a simple table. While the frontend template was very simple, in the administrator we will need a minimal amount of extra logic to handle looping through the data.&lt;br /&gt;
&lt;br /&gt;
Here is our template:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;); ?&amp;gt;&lt;br /&gt;
&amp;lt;form action=&amp;quot;index.php&amp;quot; method=&amp;quot;post&amp;quot; name=&amp;quot;adminForm&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;div id=&amp;quot;editcell&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;table class=&amp;quot;adminlist&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;thead&amp;gt;&lt;br /&gt;
        &amp;lt;tr&amp;gt;&lt;br /&gt;
            &amp;lt;th width=&amp;quot;5&amp;quot;&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo JText::_( &#039;ID&#039; ); ?&amp;gt;&lt;br /&gt;
            &amp;lt;/th&amp;gt;&lt;br /&gt;
            &amp;lt;th&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo JText::_( &#039;Greeting&#039; ); ?&amp;gt;&lt;br /&gt;
            &amp;lt;/th&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;            &lt;br /&gt;
    &amp;lt;/thead&amp;gt;&lt;br /&gt;
    &amp;lt;?php&lt;br /&gt;
    $k = 0;&lt;br /&gt;
    for ($i=0, $n=count( $this-&amp;gt;items ); $i &amp;lt; $n; $i++)&lt;br /&gt;
    {&lt;br /&gt;
        $row =&amp;amp; $this-&amp;gt;items[$i];&lt;br /&gt;
        ?&amp;gt;&lt;br /&gt;
        &amp;lt;tr class=&amp;quot;&amp;lt;?php echo &amp;quot;row$k&amp;quot;; ?&amp;gt;&amp;quot;&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo $row-&amp;gt;id; ?&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo $row-&amp;gt;greeting; ?&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;&lt;br /&gt;
        &amp;lt;?php&lt;br /&gt;
        $k = 1 - $k;&lt;br /&gt;
    }&lt;br /&gt;
    ?&amp;gt;&lt;br /&gt;
    &amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;option&amp;quot; value=&amp;quot;com_hello&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;task&amp;quot; value=&amp;quot;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;boxchecked&amp;quot; value=&amp;quot;0&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;controller&amp;quot; value=&amp;quot;hello&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/form&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This template is saved as views/hellos/tmpl/default.php.&lt;br /&gt;
&lt;br /&gt;
You will notice that our output is enclosed in a form. Though this is not necessary now, it will be soon.&lt;br /&gt;
&lt;br /&gt;
We have now completed the basic part of the first view. We have added five files to the admin section of our component:&lt;br /&gt;
&lt;br /&gt;
* hello.php&lt;br /&gt;
* controller.php&lt;br /&gt;
* models/hellos.php&lt;br /&gt;
* views/hellos/view.html.php&lt;br /&gt;
* views/hellos/tmpl/default.php&lt;br /&gt;
&lt;br /&gt;
You can now add these files to the XML install file and give it a try!&lt;br /&gt;
&lt;br /&gt;
== Adding Functionality ==&lt;br /&gt;
&lt;br /&gt;
So far our administrator section is pretty useless. It doesn&#039;t really do anything - all it does is display the entries that we have in our database.&lt;br /&gt;
&lt;br /&gt;
In order to make it useful, we need to add some buttons and links.&lt;br /&gt;
&lt;br /&gt;
==== The Toolbar ====&lt;br /&gt;
&lt;br /&gt;
You may have noticed the toolbar that appears at the top of other Joomla! component administrator panels. Our component needs one as well. Joomla! makes this very easy to do. We will add buttons Delete records, Edit records, and create New records. We will also add a title that will be displayed on our toolbar.&lt;br /&gt;
&lt;br /&gt;
This is done by adding code to the view. To add the buttons, we use static methods from the Joomla! JToolBarHelper class. The code looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;JToolBarHelper::title(   JText::_( &#039;Hello Manager&#039; ), &#039;generic.png&#039; );&lt;br /&gt;
JToolBarHelper::deleteList();&lt;br /&gt;
JToolBarHelper::editListX();&lt;br /&gt;
JToolBarHelper::addNewX();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
These three methods will create the appropriate buttons. The deleteList() method can optionally take up to three parameters - the first parameter is a string to display to the user to confirm that they want to delete the records. The second is the task that should be sent with the query (the default is &#039;remove&#039;), and the third is the text that should be displayed below the button.&lt;br /&gt;
&lt;br /&gt;
The editListX() and addNewX() methods can each take two optional parameters. The first is the task (which are by default edit and add, respectively), and the second is the text that should be displayed below the button.&lt;br /&gt;
&lt;br /&gt;
*You may have noticed the use of the JText::_ method in the template before and here as well. This is a handy function that makes component translation much easier. The JText::_ method will look up the string in your component language file and return the translated string. If not translation is found, it will return the string that you passed it. If you want to translate your component into another language, all you have to do is create a language file that will map the strings within the quotes to the translated version of the string.&lt;br /&gt;
&lt;br /&gt;
==== Checkboxes and Links ====&lt;br /&gt;
&lt;br /&gt;
We now have buttons. Two of those buttons operate on existing records. But how do we know which records to operate on? We have to let the user tell us. To do this, we need to add checkboxes to our table so that the user can select certain records. This is done in our template.&lt;br /&gt;
&lt;br /&gt;
In order to the add the checkboxes, we need to add an extra column into our table. We will add the column in between the two that we already have.&lt;br /&gt;
&lt;br /&gt;
In the header of the column, we will add a checkbox which can be used to toggle all the boxes below it on or off:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;th width=&amp;quot;20&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;input type=&amp;quot;checkbox&amp;quot; name=&amp;quot;toggle&amp;quot; value=&amp;quot;&amp;quot; onclick=&amp;quot;checkAll(&amp;lt;?php echo count( $this-&amp;gt;items ); ?&amp;gt;);&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/th&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Javascript checkAll function is a function that is built into the Joomla! base Javascript package that provides the functionality that we want here.&lt;br /&gt;
&lt;br /&gt;
Now we need to add the checkboxes into the individual rows. Joomla!&#039;s JHTML class has a method, JHTML::_(), which will generate our checkbox for us. We will add the following line to our loop:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$checked    = JHTML::_( &#039;grid.id&#039;, $i, $row-&amp;gt;id );&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
after the line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$row =&amp;amp; $this-&amp;gt;items[$i];&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then we will add a cell in between the two that we already have:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;td&amp;gt;&lt;br /&gt;
    &amp;lt;?php echo $checked; ?&amp;gt;&lt;br /&gt;
&amp;lt;/td&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It can be cumbersome to have to check the box that we want to edit and then move up and click the edit button. Therefore, we will add a link that it will go straight to the greeting&#039;s edit form. We will add the following line after the call to the JHTML::_() method to generate the link HTML:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$link = JRoute::_( &#039;index.php?option=com_hello&amp;gt;controller=hello&amp;gt;task=edit&amp;gt;cid[]=&#039;. $row-&amp;gt;id );&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And we include the link in the cell showing the greeting text:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;td&amp;gt;&lt;br /&gt;
    &amp;lt;a href=&amp;quot;&amp;lt;?php echo $link; ?&amp;gt;&amp;quot;&amp;gt;&amp;lt;?php echo $row-&amp;gt;greeting; ?&amp;gt;&amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;lt;/td&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You will notice that this link points to the hello controller. This controller will handle the data manipulation of our greetings.&lt;br /&gt;
&lt;br /&gt;
If you recall from above, we had four hidden input fields at the bottom of our form. The first input field was named &#039;option&#039;. This field is necessary so that we stay in our component. The second input field was task. This form property gets set when one of the buttons in the toolbar is clicked. A Javascript error will result and the buttons will not work if this input field is omitted. The third input field is the boxchecked field. This field keeps track of the number of boxes that are checked. The edit and delete buttons will check to ensure that this is greater than zero and will not allow the form to be submitted if it is not. The fourth input field is the controller field. This is used to specify that tasks fired from this form will be handled by the hello controller.&lt;br /&gt;
&lt;br /&gt;
Here is the code for the completed default.php file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;); ?&amp;gt;&lt;br /&gt;
&amp;lt;form action=&amp;quot;index.php&amp;quot; method=&amp;quot;post&amp;quot; name=&amp;quot;adminForm&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;div id=&amp;quot;editcell&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;table class=&amp;quot;adminlist&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;thead&amp;gt;&lt;br /&gt;
        &amp;lt;tr&amp;gt;&lt;br /&gt;
            &amp;lt;th width=&amp;quot;5&amp;quot;&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo JText::_( &#039;ID&#039; ); ?&amp;gt;&lt;br /&gt;
            &amp;lt;/th&amp;gt;&lt;br /&gt;
            &amp;lt;th&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo JText::_( &#039;Greeting&#039; ); ?&amp;gt;&lt;br /&gt;
            &amp;lt;/th&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;            &lt;br /&gt;
    &amp;lt;/thead&amp;gt;&lt;br /&gt;
    &amp;lt;?php&lt;br /&gt;
    $k = 0;&lt;br /&gt;
    for ($i=0, $n=count( $this-&amp;gt;items ); $i &amp;lt; $n; $i++)&lt;br /&gt;
    {&lt;br /&gt;
        $row =&amp;amp; $this-&amp;gt;items[$i];&lt;br /&gt;
        ?&amp;gt;&lt;br /&gt;
        &amp;lt;tr class=&amp;quot;&amp;lt;?php echo &amp;quot;row$k&amp;quot;; ?&amp;gt;&amp;quot;&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo $row-&amp;gt;id; ?&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo $row-&amp;gt;greeting; ?&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;&lt;br /&gt;
        &amp;lt;?php&lt;br /&gt;
        $k = 1 - $k;&lt;br /&gt;
    }&lt;br /&gt;
    ?&amp;gt;&lt;br /&gt;
    &amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;option&amp;quot; value=&amp;quot;com_hello&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;task&amp;quot; value=&amp;quot;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;boxchecked&amp;quot; value=&amp;quot;0&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;controller&amp;quot; value=&amp;quot;hello&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/form&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Our hellos view is now complete.&lt;br /&gt;
&lt;br /&gt;
== Getting Down and Dirty: Doing the Real Work ==&lt;br /&gt;
&lt;br /&gt;
Now that the Hellos view is done, it is time to move to the Hello view and model. This is where the real work will get done.&lt;br /&gt;
&lt;br /&gt;
==== The Hello Controller ====&lt;br /&gt;
&lt;br /&gt;
Our default controller just isn&#039;t going to cut it when it comes to doing work - all it is capable of doing is displaying views.&lt;br /&gt;
&lt;br /&gt;
We need to be able to handle the tasks that we are launching from the Hellos view: add, edit and remove.&lt;br /&gt;
&lt;br /&gt;
Add and edit are essentially the same task: they both display a form to the user that allows a greeting to be edited. The only difference is that new displays a blank form, and edit displays a form with data already in it. Since they are similar, we will map the add task onto the edit task handler. This is specified in our constructor:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * constructor (registers additional tasks to methods)&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function __construct()&lt;br /&gt;
{&lt;br /&gt;
    parent::__construct();&lt;br /&gt;
&lt;br /&gt;
    // Register Extra tasks&lt;br /&gt;
    $this-&amp;gt;registerTask( &#039;add&#039;  ,     &#039;edit&#039; );&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first parameter of JController::registerTask is the task to map, and the second is the method to map it to.&lt;br /&gt;
&lt;br /&gt;
We will start with handling the edit task. The controller&#039;s job is fairly simple for the edit task. All it has to do is specify the view and layout to load (the hello view and the form layout). We will also tell Joomla! to disable the mainmenu while we are editing our greeting. This prevents users from leaving unsaved records open.&lt;br /&gt;
&lt;br /&gt;
Our edit task handler looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * display the edit form&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function edit()&lt;br /&gt;
{&lt;br /&gt;
    JRequest::setVar( &#039;view&#039;, &#039;hello&#039; );&lt;br /&gt;
    JRequest::setVar( &#039;layout&#039;, &#039;form&#039;  );&lt;br /&gt;
    JRequest::setVar(&#039;hidemainmenu&#039;, 1);&lt;br /&gt;
&lt;br /&gt;
    parent::display();&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== The Hello View ====&lt;br /&gt;
&lt;br /&gt;
The Hello view will display a form which will allow the user to edit a greeting. The display method if the hello view has to do a few simple tasks:&lt;br /&gt;
&lt;br /&gt;
* retrieve the data from the model&lt;br /&gt;
* create the toolbar&lt;br /&gt;
* pass the data into the template&lt;br /&gt;
* invoke the display() method to render the template&lt;br /&gt;
&lt;br /&gt;
This becomes a bit more complicated because the one view handles both the edit and add tasks. In our toolbar we want the user to know what whether they are adding or editing, so we have to determine which task was fired.&lt;br /&gt;
&lt;br /&gt;
Since we are already retrieving the record that we want to display from the model, we can use this data to determine what task was fired. If the task was edit, then the id field of our record will have been set. If the task was new, then it will not have been set. This can be used to determine if we have a new record or an existing record.&lt;br /&gt;
&lt;br /&gt;
We will add two buttons to the toolbar: save and cancel. Though the functionality will be the same, we want to display different buttons depending on whether it is a new or existing record. If it is a new record, we will display cancel. If it already exists, we will display close.&lt;br /&gt;
&lt;br /&gt;
Thus our display method looks like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * display method of Hello view&lt;br /&gt;
 * @return void&lt;br /&gt;
 **/&lt;br /&gt;
function display($tpl = null)&lt;br /&gt;
{&lt;br /&gt;
    //get the hello&lt;br /&gt;
    $hello        =&amp;amp; $this-&amp;gt;get(&#039;Data&#039;);&lt;br /&gt;
    $isNew        = ($hello-&amp;gt;id &amp;lt; 1);&lt;br /&gt;
&lt;br /&gt;
    $text = $isNew ? JText::_( &#039;New&#039; ) : JText::_( &#039;Edit&#039; );&lt;br /&gt;
    JToolBarHelper::title(   JText::_( &#039;Hello&#039; ).&#039;: &amp;lt;small&amp;gt;&amp;lt;small&amp;gt;[ &#039; . $text.&#039; ]&amp;lt;/small&amp;gt;&amp;lt;/small&amp;gt;&#039; );&lt;br /&gt;
    JToolBarHelper::save();&lt;br /&gt;
    if ($isNew)  {&lt;br /&gt;
        JToolBarHelper::cancel();&lt;br /&gt;
    } else {&lt;br /&gt;
        // for existing items the button is renamed `close`&lt;br /&gt;
        JToolBarHelper::cancel( &#039;cancel&#039;, &#039;Close&#039; );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    $this-&amp;gt;assignRef(&#039;hello&#039;, $hello);&lt;br /&gt;
    parent::display($tpl);&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== The Hello Model ====&lt;br /&gt;
&lt;br /&gt;
Our view needs data. Therefore, we need to create a model to model a hello.&lt;br /&gt;
&lt;br /&gt;
Our model will have two properties: _id and _data. _id will hold the id of the greeting and data will hold the data.&lt;br /&gt;
&lt;br /&gt;
We will start with a constructor, which will attempt to retrieve the id from the query:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Constructor that retrieves the ID from the request&lt;br /&gt;
 *&lt;br /&gt;
 * @access    public&lt;br /&gt;
 * @return    void&lt;br /&gt;
 */&lt;br /&gt;
function __construct()&lt;br /&gt;
{&lt;br /&gt;
    parent::__construct();&lt;br /&gt;
&lt;br /&gt;
    $array = JRequest::getVar(&#039;cid&#039;,  0, &#039;&#039;, &#039;array&#039;);&lt;br /&gt;
    $this-&amp;gt;setId((int)$array[0]);&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The JRequest::getVar() method is used to retrieve data from the request. The first parameter is the name of the form variable. The second parameter is the default value to assign if there is no value found. The third parameter is the name of the hash to retrieve the value from (get, post, etc), and the last value is the data type that should be forced on the value.&lt;br /&gt;
&lt;br /&gt;
Our constructor will take the first value from the cid array and assign it to the id.&lt;br /&gt;
&lt;br /&gt;
Our setId() method can be used to set our id. Changing the id that our model points to will mean the id points to the wrong data. Therefore, when we set the id, we will clear the data property:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Method to set the hello identifier&lt;br /&gt;
 *&lt;br /&gt;
 * @access    public&lt;br /&gt;
 * @param    int Hello identifier&lt;br /&gt;
 * @return    void&lt;br /&gt;
 */&lt;br /&gt;
function setId($id)&lt;br /&gt;
{&lt;br /&gt;
    // Set id and wipe data&lt;br /&gt;
    $this-&amp;gt;_id        = $id;&lt;br /&gt;
    $this-&amp;gt;_data    = null;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finally, we need a method to retrieve our data: getData()&lt;br /&gt;
&lt;br /&gt;
getData will check if the _data property has already been set. If it has, it will simply return it. Otherwise, it will load the data from the database.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Method to get a hello&lt;br /&gt;
 * @return object with data&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
function &amp;amp;getData()&lt;br /&gt;
{&lt;br /&gt;
    // Load the data&lt;br /&gt;
    if (empty( $this-&amp;gt;_data )) {&lt;br /&gt;
        $query = &#039; SELECT * FROM #__hello &#039;.&lt;br /&gt;
                &#039;  WHERE id = &#039;.$this-&amp;gt;_id;&lt;br /&gt;
        $this-&amp;gt;_db-&amp;gt;setQuery( $query );&lt;br /&gt;
        $this-&amp;gt;_data = $this-&amp;gt;_db-&amp;gt;loadObject();&lt;br /&gt;
    }&lt;br /&gt;
    if (!$this-&amp;gt;_data) {&lt;br /&gt;
        $this-&amp;gt;_data = new stdClass();&lt;br /&gt;
        $this-&amp;gt;_data-&amp;gt;id = 0;&lt;br /&gt;
        $this-&amp;gt;_data-&amp;gt;greeting = null;&lt;br /&gt;
    }&lt;br /&gt;
    return $this-&amp;gt;_data;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== The Form ====&lt;br /&gt;
&lt;br /&gt;
Now all that is left is to create the form that the data will go into. Since we specified our layout as form, the form will go in a file in the tmpl directory of the hello view called form.php:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;); ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;form action=&amp;quot;index.php&amp;quot; method=&amp;quot;post&amp;quot; name=&amp;quot;adminForm&amp;quot; id=&amp;quot;adminForm&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;div class=&amp;quot;col100&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;fieldset class=&amp;quot;adminform&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;legend&amp;gt;&amp;lt;?php echo JText::_( &#039;Details&#039; ); ?&amp;gt;&amp;lt;/legend&amp;gt;&lt;br /&gt;
        &amp;lt;table class=&amp;quot;admintable&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;tr&amp;gt;&lt;br /&gt;
            &amp;lt;td width=&amp;quot;100&amp;quot; align=&amp;quot;right&amp;quot; class=&amp;quot;key&amp;quot;&amp;gt;&lt;br /&gt;
                &amp;lt;label for=&amp;quot;greeting&amp;quot;&amp;gt;&lt;br /&gt;
                    &amp;lt;?php echo JText::_( &#039;Greeting&#039; ); ?&amp;gt;:&lt;br /&gt;
                &amp;lt;/label&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;input class=&amp;quot;text_area&amp;quot; type=&amp;quot;text&amp;quot; name=&amp;quot;greeting&amp;quot; id=&amp;quot;greeting&amp;quot; size=&amp;quot;32&amp;quot; maxlength=&amp;quot;250&amp;quot; value=&amp;quot;&amp;lt;?php echo $this-&amp;gt;hello-&amp;gt;greeting;?&amp;gt;&amp;quot; /&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;&lt;br /&gt;
    &amp;lt;/table&amp;gt;&lt;br /&gt;
    &amp;lt;/fieldset&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;clr&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;option&amp;quot; value=&amp;quot;com_hello&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;id&amp;quot; value=&amp;quot;&amp;lt;?php echo $this-&amp;gt;hello-&amp;gt;id; ?&amp;gt;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;task&amp;quot; value=&amp;quot;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;controller&amp;quot; value=&amp;quot;hello&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/form&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in addition to the input field, there is a hidden field for the id. The user doesn&#039;t need to edit the id (and shouldn&#039;t), so we silently pass it along in the form.&lt;br /&gt;
&lt;br /&gt;
==== Implementing the Functionality ====&lt;br /&gt;
&lt;br /&gt;
So far, our controller only handles two tasks: edit and new. However, we also have buttons to save, delete and cancel records. We need to write code to handle and perform these tasks.&lt;br /&gt;
&lt;br /&gt;
=== Saving a Record ===&lt;br /&gt;
&lt;br /&gt;
The logical next step is to implement the functionality to save a record. Normally, this would require some switches and logic to handle various cases, such as the difference between creating a new record (an INSERT query), and updating an existing query (an UPDATE query). Also, there are complexities involved in getting the data from the form and putting it into the query.&lt;br /&gt;
&lt;br /&gt;
The Joomla! framework takes care of a lot of this for you. The JTable class makes it easy to manipulate records in the database without having to worry about writing the SQL code that lies behind these updates. It also makes it easy to transfer data from an HTML form into the database.&lt;br /&gt;
&lt;br /&gt;
== Creating the Table Class ==&lt;br /&gt;
&lt;br /&gt;
The JTable class is an abstract class from which you can derive child classes to work with specific tables. To use it, you simply create a class that extends the JTable class, add your database fields as properties, and override the constructor to specify the name of the table and the primary key.&lt;br /&gt;
&lt;br /&gt;
Here is our JTable class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * Hello World table class&lt;br /&gt;
 * &lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_4&lt;br /&gt;
 * @license        GNU/GPL&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// No direct access&lt;br /&gt;
defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;);&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Hello Table class&lt;br /&gt;
 *&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 */&lt;br /&gt;
class TableHello extends JTable&lt;br /&gt;
{&lt;br /&gt;
    /**&lt;br /&gt;
     * Primary Key&lt;br /&gt;
     *&lt;br /&gt;
     * @var int&lt;br /&gt;
     */&lt;br /&gt;
    var $id = null;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * @var string&lt;br /&gt;
     */&lt;br /&gt;
    var $greeting = null;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Constructor&lt;br /&gt;
     *&lt;br /&gt;
     * @param object Database connector object&lt;br /&gt;
     */&lt;br /&gt;
    function TableHello( &amp;amp;$db ) {&lt;br /&gt;
        parent::__construct(&#039;#__hello&#039;, &#039;id&#039;, $db);&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You will see here that we have defined our two fields: the id field and the greeting field. Then we have defined a constructor, which will call the constructor of the parent class and pass it the name of the table (hello), the name of the field which is the primary key (id), and the database connector object.&lt;br /&gt;
&lt;br /&gt;
This file should be called hello.php and it will go in a directory called tables in the administrator section of our component.&lt;br /&gt;
&lt;br /&gt;
== Implementing the Function in our Model ==&lt;br /&gt;
&lt;br /&gt;
We are now ready to add the method to the model which will save our record. We will call this method store. Our store() method will do three things: bind the data from the form to the TableHello object, check to ensure that the record is properly formed, and store the record in the database.&lt;br /&gt;
&lt;br /&gt;
Our method looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Method to store a record&lt;br /&gt;
 *&lt;br /&gt;
 * @access    public&lt;br /&gt;
 * @return    boolean    True on success&lt;br /&gt;
 */&lt;br /&gt;
function store()&lt;br /&gt;
{&lt;br /&gt;
    $row =&amp;amp; $this-&amp;gt;getTable();&lt;br /&gt;
&lt;br /&gt;
    $data = JRequest::get( &#039;post&#039; );&lt;br /&gt;
    // Bind the form fields to the hello table&lt;br /&gt;
    if (!$row-&amp;gt;bind($data)) {&lt;br /&gt;
        $this-&amp;gt;setError($this-&amp;gt;_db-&amp;gt;getErrorMsg());&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // Make sure the hello record is valid&lt;br /&gt;
    if (!$row-&amp;gt;check()) {&lt;br /&gt;
        $this-&amp;gt;setError($this-&amp;gt;_db-&amp;gt;getErrorMsg());&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // Store the web link table to the database&lt;br /&gt;
    if (!$row-&amp;gt;store()) {&lt;br /&gt;
        $this-&amp;gt;setError($this-&amp;gt;_db-&amp;gt;getErrorMsg());&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return true;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This method gets added to the hello model.&lt;br /&gt;
&lt;br /&gt;
The method takes one parameter, which is an associative array of data that we want to store in the database. This can easily be retrieved from the request as will be seen later.&lt;br /&gt;
&lt;br /&gt;
You will see that the first line retrieves a reference to our JTable object. If we name our table properly, we don&#039;t have to specify its name - the JModel class knows where to find it. You may recall that we called our table class TableHello and put it in a file called hello.php in the tables directory. If you follow this convention, the JModel class can create your object automatically.&lt;br /&gt;
&lt;br /&gt;
The second line will retrieve the data from the form. The JRequest class makes this very easy. In this case, we are retrieving all of the variables that were submitted using the &#039;POST&#039; method. These will be returned as an associative array.&lt;br /&gt;
&lt;br /&gt;
The rest is easy - we bind, check and store. The bind() method will copy values from the array into the corresponding property of the table object. In this case, it will take the values of id and greeting and copy them to our TableHello object.&lt;br /&gt;
&lt;br /&gt;
The check() method will perform data verification. In the JTable() class, this method simply returns true. While this doesn&#039;t provide any value for us currently, by calling this method we make it possible to do data checking using our TableHello class in the future. This method can be overridden in our TableHello class with a method that performs the appropriate checks.&lt;br /&gt;
&lt;br /&gt;
The store() method will take the data that is in the object and store it in the database. If the id is 0, it will create a new record (INSERT), otherwise, it will update the existing record (UPDATE).&lt;br /&gt;
&lt;br /&gt;
== Adding the Task to the Controller ==&lt;br /&gt;
&lt;br /&gt;
We are now ready to add our task to the controller. Since the task that we are firing is called &#039;save&#039;, we must call our method &#039;save&#039;. This is simple:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * save a record (and redirect to main page)&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function save()&lt;br /&gt;
{&lt;br /&gt;
    $model = $this-&amp;gt;getModel(&#039;hello&#039;);&lt;br /&gt;
&lt;br /&gt;
    if ($model-&amp;gt;store()) {&lt;br /&gt;
        $msg = JText::_( &#039;Greeting Saved!&#039; );&lt;br /&gt;
    } else {&lt;br /&gt;
        $msg = JText::_( &#039;Error Saving Greeting&#039; );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // Check the table in so it can be edited.... we are done with it anyway&lt;br /&gt;
    $link = &#039;index.php?option=com_hello&#039;;&lt;br /&gt;
    $this-&amp;gt;setRedirect($link, $msg);&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All we do is get our model and invoke the store() method. Then we use the setRedirect() method to redirect back to our list of greetings. We also pass a message along, which will be displayed at the top of the page.&lt;br /&gt;
&lt;br /&gt;
=== Deleting a Record ===&lt;br /&gt;
&lt;br /&gt;
=== Implementing the Function in the Model ===&lt;br /&gt;
&lt;br /&gt;
In the model, we will retrieve the list of IDs to delete and call the JTable class to delete them. Here it is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Method to delete record(s)&lt;br /&gt;
 *&lt;br /&gt;
 * @access    public&lt;br /&gt;
 * @return    boolean    True on success&lt;br /&gt;
 */&lt;br /&gt;
function delete()&lt;br /&gt;
{&lt;br /&gt;
    $cids = JRequest::getVar( &#039;cid&#039;, array(0), &#039;post&#039;, &#039;array&#039; );&lt;br /&gt;
    $row =&amp;amp; $this-&amp;gt;getTable();&lt;br /&gt;
&lt;br /&gt;
    foreach($cids as $cid) {&lt;br /&gt;
        if (!$row-&amp;gt;delete( $cid )) {&lt;br /&gt;
            $this-&amp;gt;setError( $row-&amp;gt;getErrorMsg() );&lt;br /&gt;
            return false;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return true;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We invoke the JRequest::getVar() method to get the data from the request, then we invoke the $row-&amp;gt;delete() method to delete each row. By storing errors in the model we make it possible to retrieve them later if we so choose.&lt;br /&gt;
&lt;br /&gt;
=== Handling the Remove Task in the Controller ===&lt;br /&gt;
&lt;br /&gt;
This is similar to the save() method which handled the save task:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * remove record(s)&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function remove()&lt;br /&gt;
{&lt;br /&gt;
    $model = $this-&amp;gt;getModel(&#039;hello&#039;);&lt;br /&gt;
    if(!$model-&amp;gt;delete()) {&lt;br /&gt;
        $msg = JText::_( &#039;Error: One or More Greetings Could not be Deleted&#039; );&lt;br /&gt;
    } else {&lt;br /&gt;
        $msg = JText::_( &#039;Greeting(s) Deleted&#039; );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    $this-&amp;gt;setRedirect( &#039;index.php?option=com_hello&#039;, $msg );&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Cancelling the Edit Operation ====&lt;br /&gt;
&lt;br /&gt;
To cancel the edit operation, all we have to do is redirect back to the main view:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * cancel editing a record&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function cancel()&lt;br /&gt;
{&lt;br /&gt;
    $msg = JText::_( &#039;Operation Cancelled&#039; );&lt;br /&gt;
    $this-&amp;gt;setRedirect( &#039;index.php?option=com_hello&#039;, $msg );&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
We have now implemented a basic backend to our component. We are now able to edit the entries that are viewed in the frontend. We have demonstrated the interaction between models, views and controllers. We have shown how the JTable class can be extended to provide easy access to tables in the database. It can also be seen how the JToolBarHelper class can be used to create button bars in components to present a standardized look between components.&lt;br /&gt;
&lt;br /&gt;
== Contributors ==&lt;br /&gt;
* staalanden&lt;br /&gt;
&lt;br /&gt;
== Download ==&lt;br /&gt;
&lt;br /&gt;
The component can be downloaded at: [http://joomlacode.org/gf/download/frsrelease/8111/29436/com_hello4_01.zip com_hello4_01]&lt;/div&gt;</summary>
		<author><name>Tonie</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Using_the_Database&amp;diff=9517</id>
		<title>J1.5:Developing a MVC Component/Using the Database</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Using_the_Database&amp;diff=9517"/>
		<updated>2008-07-27T20:11:11Z</updated>

		<summary type="html">&lt;p&gt;Tonie: /* Download */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
In the first two tutorials, we showed you how to build a simple model-view-controller component. We had one view which retrieved data from a model (which was created in the 2nd tutorial). In this tutorial, we will be working with the model. Instead of the data being hard coded in the model, the model will retrieve the data from a table in the database.&lt;br /&gt;
&lt;br /&gt;
This tutorial will demonstrate how to use the JDatabase class to retrieve data from the database.&lt;br /&gt;
&lt;br /&gt;
== Retrieving the Data ==&lt;br /&gt;
&lt;br /&gt;
Our model currently has one method: getGreeting(). This method is very simple - all it does is return the hard-coded greeting.&lt;br /&gt;
&lt;br /&gt;
To make things more interesting, we will load the greeting from a database table. We will demonstrate later how to create an SQL file and add the appropriate code to the XML manifest file so that the table and some sample data will be created when the component is installed. For now, we will simply replace our return statement with some code that will retrieve the greeting from the database and return it.&lt;br /&gt;
&lt;br /&gt;
The first step is to obtain a reference to a database object. Since Joomla! uses the database for its normal operation, a database connection already exists; therefore, it is not necessary to create your own. A reference to the existing database can be obtained using:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$db =&amp;amp; JFactory::getDBO();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
JFactory is a static class that is used to retrieve references to many of the system objects. More information about this class can be found in the API documentation.&lt;br /&gt;
&lt;br /&gt;
The method name (getDBO) stands for get DataBase Object, and is easy and important to remember.&lt;br /&gt;
&lt;br /&gt;
Now that we have obtained a reference to the database object, we can retrieve our data. We do this in two steps:&lt;br /&gt;
&lt;br /&gt;
* store our query in the database object&lt;br /&gt;
* load the result&lt;br /&gt;
&lt;br /&gt;
Our new getGreeting() method will therefore look like:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;function getGreeting()&lt;br /&gt;
{&lt;br /&gt;
   $db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
&lt;br /&gt;
   $query = &#039;SELECT greeting FROM #__hello&#039;;&lt;br /&gt;
   $db-&amp;gt;setQuery( $query );&lt;br /&gt;
   $greeting = $db-&amp;gt;loadResult();&lt;br /&gt;
&lt;br /&gt;
   return $greeting;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
hello is the name of the table that we will create later, and greeting is the name of the field that stores the greetings. If you are not familiar with SQL, it would be helpful to take a tutorial or a lesson to get yourself up to speed. One such tutorial can be found at [http://www.w3schools.com/sql/default.asp w3schools].&lt;br /&gt;
&lt;br /&gt;
The $db-&amp;gt;loadResult() method will execute the stored database query and return the first field of the first row of the result. See [http://api.joomla.org/Joomla-Framework/Database/JDatabase.html JDatabase API reference] for more information about other load methods in the JDatabase class.&lt;br /&gt;
&lt;br /&gt;
== Creating the Installation SQL File ==&lt;br /&gt;
&lt;br /&gt;
The Joomla! installer has built-in support for executing queries during component installation. These queries are all stored in a standard text file.&lt;br /&gt;
&lt;br /&gt;
We will have three queries in our install file: the first will drop the table in case it already exists, the second will create the table with the appropriate fields, and the third will insert the data.&lt;br /&gt;
&lt;br /&gt;
Here are our queries:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;DROP TABLE IF EXISTS `#__hello`;&lt;br /&gt;
&lt;br /&gt;
CREATE TABLE `#__hello` (&lt;br /&gt;
  `id` int(11) NOT NULL auto_increment,&lt;br /&gt;
  `greeting` varchar(25) NOT NULL,&lt;br /&gt;
  PRIMARY KEY  (`id`)&lt;br /&gt;
) ENGINE=MyISAM AUTO_INCREMENT DEFAULT CHARSET=utf8;&lt;br /&gt;
&lt;br /&gt;
INSERT INTO `#__hello` (`greeting`) VALUES (&#039;Hello, World!&#039;),&lt;br /&gt;
(&#039;Bonjour, Monde!&#039;),&lt;br /&gt;
(&#039;Ciao, Mondo!&#039;);&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You might find the  prefix on the table names rather odd. Joomla! will replace this prefix with the prefix used by the current install. For most installs, this table will become jos_hello. This allows multiple installs of Joomla! to use the same database, and prevents collisions with other applications using the same table names (i.e. two applications might share a database, but might both require a &#039;users&#039; table. This convention avoids problems.)&lt;br /&gt;
&lt;br /&gt;
We have specified two fields in our database. The first field is id, and is called the &#039;primary key&#039;. The primary key of a database table is a field that is used to uniquely identify a record. This is often used to lookup rows in the database. The other field is greeting. This is the field that stores the greeting that is returned from the query that we used above.&lt;br /&gt;
&lt;br /&gt;
We will save our queries in a file called install.utf.sql.&lt;br /&gt;
&lt;br /&gt;
=== Creating the Uninstall SQL File ===&lt;br /&gt;
&lt;br /&gt;
Though we might hope that people will never want to uninstall our component, it is important that if they do, we don&#039;t leave anything behind. Joomla! will look after deleting the files and directories that were created during install, but you must manually include queries that will remove and tables that have been added to the database. Since we have only created one table, we only need one query:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;DROP TABLE IF EXISTS `#__hello`;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will save this query in a file called uninstall.utf.sql.&lt;br /&gt;
&lt;br /&gt;
== Updating our Install File ==&lt;br /&gt;
&lt;br /&gt;
We need to change a few things in our install file. First, we need to add our two new files to the list of files to install. SQL install file have to go in the admin directory. Second, we need to tell the installer to execute the queries in our files on install and uninstall.&lt;br /&gt;
&lt;br /&gt;
Our new file looks like this:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;&lt;br /&gt;
&amp;lt;install type=&amp;quot;component&amp;quot; version=&amp;quot;1.5.0&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;name&amp;gt;Hello&amp;lt;/name&amp;gt;&lt;br /&gt;
 &amp;lt;!-- The following elements are optional and free of formatting conttraints --&amp;gt;&lt;br /&gt;
 &amp;lt;creationDate&amp;gt;2007-02-22&amp;lt;/creationDate&amp;gt;&lt;br /&gt;
 &amp;lt;author&amp;gt;John Doe&amp;lt;/author&amp;gt;&lt;br /&gt;
 &amp;lt;authorEmail&amp;gt;john.doe@example.org&amp;lt;/authorEmail&amp;gt;&lt;br /&gt;
 &amp;lt;authorUrl&amp;gt;http://www.example.org&amp;lt;/authorUrl&amp;gt;&lt;br /&gt;
 &amp;lt;copyright&amp;gt;Copyright Info&amp;lt;/copyright&amp;gt;&lt;br /&gt;
 &amp;lt;license&amp;gt;License Info&amp;lt;/license&amp;gt;&lt;br /&gt;
 &amp;lt;!--  The version string is recorded in the components table --&amp;gt;&lt;br /&gt;
 &amp;lt;version&amp;gt;3.01&amp;lt;/version&amp;gt;&lt;br /&gt;
 &amp;lt;!-- The description is optional and defaults to the name --&amp;gt;&lt;br /&gt;
 &amp;lt;description&amp;gt;Description of the component ...&amp;lt;/description&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;!-- Site Main File Copy Section --&amp;gt;&lt;br /&gt;
 &amp;lt;!-- Note the folder attribute: This attribute describes the folder&lt;br /&gt;
      to copy FROM in the package to install therefore files copied&lt;br /&gt;
      in this section are copied from /site/ in the package --&amp;gt;&lt;br /&gt;
 &amp;lt;files folder=&amp;quot;site&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;controller.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;models/hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;models/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/view.html.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/tmpl/default.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/tmpl/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
 &amp;lt;/files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;install&amp;gt;&lt;br /&gt;
  &amp;lt;sql&amp;gt;&lt;br /&gt;
   &amp;lt;file charset=&amp;quot;utf8&amp;quot; driver=&amp;quot;mysql&amp;quot;&amp;gt;install.sql&amp;lt;/file&amp;gt;&lt;br /&gt;
  &amp;lt;/sql&amp;gt;&lt;br /&gt;
 &amp;lt;/install&amp;gt;&lt;br /&gt;
 &amp;lt;uninstall&amp;gt;&lt;br /&gt;
  &amp;lt;sql&amp;gt;&lt;br /&gt;
   &amp;lt;file charset=&amp;quot;utf8&amp;quot; driver=&amp;quot;mysql&amp;quot;&amp;gt;uninstall.sql&amp;lt;/file&amp;gt;&lt;br /&gt;
  &amp;lt;/sql&amp;gt;&lt;br /&gt;
 &amp;lt;/uninstall&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;administration&amp;gt;&lt;br /&gt;
  &amp;lt;!-- Administration Menu Section --&amp;gt;&lt;br /&gt;
  &amp;lt;menu&amp;gt;Hello World!&amp;lt;/menu&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;!-- Administration Main File Copy Section --&amp;gt;&lt;br /&gt;
  &amp;lt;files folder=&amp;quot;admin&amp;quot;&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;install.sql&amp;lt;/filename&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;uninstall.sql&amp;lt;/filename&amp;gt;&lt;br /&gt;
&amp;lt;/files&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
 &amp;lt;/administration&amp;gt;&lt;br /&gt;
&amp;lt;/install&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You will notice two attributes present on the &amp;lt;file&amp;gt; tags within the &amp;lt;install&amp;gt; and &amp;lt;uninstall&amp;gt; sections: charset and driver. The charset is the type of charset to use. The only valid charset is utf8. If you want to create install files for non-utf8 databases (for older version of MySQL), you should omit this attribute.&lt;br /&gt;
&lt;br /&gt;
The driver attribute specifies which database the queries were written for. Currently, this can only be mysql, but in future versions of Joomla! there may be more database drivers available.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
We now have a component that takes advantage of both the Joomla! MVC framework classes and the JDatabase classes. You are now able to write MVC components that interact with the database and can use the Joomla! installer to create and populate database tables.&lt;br /&gt;
&lt;br /&gt;
== Contributors ==&lt;br /&gt;
* staalanden&lt;br /&gt;
&lt;br /&gt;
== Download ==&lt;br /&gt;
&lt;br /&gt;
The component can be downloaded at: [http://joomlacode.org/gf/download/frsrelease/8110/29435/com_hello3_01.zip com_hello3_01]&lt;/div&gt;</summary>
		<author><name>Tonie</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Using_the_Database&amp;diff=9516</id>
		<title>J1.5:Developing a MVC Component/Using the Database</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Using_the_Database&amp;diff=9516"/>
		<updated>2008-07-27T20:10:53Z</updated>

		<summary type="html">&lt;p&gt;Tonie: /* Download */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
In the first two tutorials, we showed you how to build a simple model-view-controller component. We had one view which retrieved data from a model (which was created in the 2nd tutorial). In this tutorial, we will be working with the model. Instead of the data being hard coded in the model, the model will retrieve the data from a table in the database.&lt;br /&gt;
&lt;br /&gt;
This tutorial will demonstrate how to use the JDatabase class to retrieve data from the database.&lt;br /&gt;
&lt;br /&gt;
== Retrieving the Data ==&lt;br /&gt;
&lt;br /&gt;
Our model currently has one method: getGreeting(). This method is very simple - all it does is return the hard-coded greeting.&lt;br /&gt;
&lt;br /&gt;
To make things more interesting, we will load the greeting from a database table. We will demonstrate later how to create an SQL file and add the appropriate code to the XML manifest file so that the table and some sample data will be created when the component is installed. For now, we will simply replace our return statement with some code that will retrieve the greeting from the database and return it.&lt;br /&gt;
&lt;br /&gt;
The first step is to obtain a reference to a database object. Since Joomla! uses the database for its normal operation, a database connection already exists; therefore, it is not necessary to create your own. A reference to the existing database can be obtained using:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$db =&amp;amp; JFactory::getDBO();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
JFactory is a static class that is used to retrieve references to many of the system objects. More information about this class can be found in the API documentation.&lt;br /&gt;
&lt;br /&gt;
The method name (getDBO) stands for get DataBase Object, and is easy and important to remember.&lt;br /&gt;
&lt;br /&gt;
Now that we have obtained a reference to the database object, we can retrieve our data. We do this in two steps:&lt;br /&gt;
&lt;br /&gt;
* store our query in the database object&lt;br /&gt;
* load the result&lt;br /&gt;
&lt;br /&gt;
Our new getGreeting() method will therefore look like:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;function getGreeting()&lt;br /&gt;
{&lt;br /&gt;
   $db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
&lt;br /&gt;
   $query = &#039;SELECT greeting FROM #__hello&#039;;&lt;br /&gt;
   $db-&amp;gt;setQuery( $query );&lt;br /&gt;
   $greeting = $db-&amp;gt;loadResult();&lt;br /&gt;
&lt;br /&gt;
   return $greeting;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
hello is the name of the table that we will create later, and greeting is the name of the field that stores the greetings. If you are not familiar with SQL, it would be helpful to take a tutorial or a lesson to get yourself up to speed. One such tutorial can be found at [http://www.w3schools.com/sql/default.asp w3schools].&lt;br /&gt;
&lt;br /&gt;
The $db-&amp;gt;loadResult() method will execute the stored database query and return the first field of the first row of the result. See [http://api.joomla.org/Joomla-Framework/Database/JDatabase.html JDatabase API reference] for more information about other load methods in the JDatabase class.&lt;br /&gt;
&lt;br /&gt;
== Creating the Installation SQL File ==&lt;br /&gt;
&lt;br /&gt;
The Joomla! installer has built-in support for executing queries during component installation. These queries are all stored in a standard text file.&lt;br /&gt;
&lt;br /&gt;
We will have three queries in our install file: the first will drop the table in case it already exists, the second will create the table with the appropriate fields, and the third will insert the data.&lt;br /&gt;
&lt;br /&gt;
Here are our queries:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;DROP TABLE IF EXISTS `#__hello`;&lt;br /&gt;
&lt;br /&gt;
CREATE TABLE `#__hello` (&lt;br /&gt;
  `id` int(11) NOT NULL auto_increment,&lt;br /&gt;
  `greeting` varchar(25) NOT NULL,&lt;br /&gt;
  PRIMARY KEY  (`id`)&lt;br /&gt;
) ENGINE=MyISAM AUTO_INCREMENT DEFAULT CHARSET=utf8;&lt;br /&gt;
&lt;br /&gt;
INSERT INTO `#__hello` (`greeting`) VALUES (&#039;Hello, World!&#039;),&lt;br /&gt;
(&#039;Bonjour, Monde!&#039;),&lt;br /&gt;
(&#039;Ciao, Mondo!&#039;);&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You might find the  prefix on the table names rather odd. Joomla! will replace this prefix with the prefix used by the current install. For most installs, this table will become jos_hello. This allows multiple installs of Joomla! to use the same database, and prevents collisions with other applications using the same table names (i.e. two applications might share a database, but might both require a &#039;users&#039; table. This convention avoids problems.)&lt;br /&gt;
&lt;br /&gt;
We have specified two fields in our database. The first field is id, and is called the &#039;primary key&#039;. The primary key of a database table is a field that is used to uniquely identify a record. This is often used to lookup rows in the database. The other field is greeting. This is the field that stores the greeting that is returned from the query that we used above.&lt;br /&gt;
&lt;br /&gt;
We will save our queries in a file called install.utf.sql.&lt;br /&gt;
&lt;br /&gt;
=== Creating the Uninstall SQL File ===&lt;br /&gt;
&lt;br /&gt;
Though we might hope that people will never want to uninstall our component, it is important that if they do, we don&#039;t leave anything behind. Joomla! will look after deleting the files and directories that were created during install, but you must manually include queries that will remove and tables that have been added to the database. Since we have only created one table, we only need one query:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;DROP TABLE IF EXISTS `#__hello`;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will save this query in a file called uninstall.utf.sql.&lt;br /&gt;
&lt;br /&gt;
== Updating our Install File ==&lt;br /&gt;
&lt;br /&gt;
We need to change a few things in our install file. First, we need to add our two new files to the list of files to install. SQL install file have to go in the admin directory. Second, we need to tell the installer to execute the queries in our files on install and uninstall.&lt;br /&gt;
&lt;br /&gt;
Our new file looks like this:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;&lt;br /&gt;
&amp;lt;install type=&amp;quot;component&amp;quot; version=&amp;quot;1.5.0&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;name&amp;gt;Hello&amp;lt;/name&amp;gt;&lt;br /&gt;
 &amp;lt;!-- The following elements are optional and free of formatting conttraints --&amp;gt;&lt;br /&gt;
 &amp;lt;creationDate&amp;gt;2007-02-22&amp;lt;/creationDate&amp;gt;&lt;br /&gt;
 &amp;lt;author&amp;gt;John Doe&amp;lt;/author&amp;gt;&lt;br /&gt;
 &amp;lt;authorEmail&amp;gt;john.doe@example.org&amp;lt;/authorEmail&amp;gt;&lt;br /&gt;
 &amp;lt;authorUrl&amp;gt;http://www.example.org&amp;lt;/authorUrl&amp;gt;&lt;br /&gt;
 &amp;lt;copyright&amp;gt;Copyright Info&amp;lt;/copyright&amp;gt;&lt;br /&gt;
 &amp;lt;license&amp;gt;License Info&amp;lt;/license&amp;gt;&lt;br /&gt;
 &amp;lt;!--  The version string is recorded in the components table --&amp;gt;&lt;br /&gt;
 &amp;lt;version&amp;gt;3.01&amp;lt;/version&amp;gt;&lt;br /&gt;
 &amp;lt;!-- The description is optional and defaults to the name --&amp;gt;&lt;br /&gt;
 &amp;lt;description&amp;gt;Description of the component ...&amp;lt;/description&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;!-- Site Main File Copy Section --&amp;gt;&lt;br /&gt;
 &amp;lt;!-- Note the folder attribute: This attribute describes the folder&lt;br /&gt;
      to copy FROM in the package to install therefore files copied&lt;br /&gt;
      in this section are copied from /site/ in the package --&amp;gt;&lt;br /&gt;
 &amp;lt;files folder=&amp;quot;site&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;controller.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;models/hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;models/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/view.html.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/tmpl/default.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/tmpl/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
 &amp;lt;/files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;install&amp;gt;&lt;br /&gt;
  &amp;lt;sql&amp;gt;&lt;br /&gt;
   &amp;lt;file charset=&amp;quot;utf8&amp;quot; driver=&amp;quot;mysql&amp;quot;&amp;gt;install.sql&amp;lt;/file&amp;gt;&lt;br /&gt;
  &amp;lt;/sql&amp;gt;&lt;br /&gt;
 &amp;lt;/install&amp;gt;&lt;br /&gt;
 &amp;lt;uninstall&amp;gt;&lt;br /&gt;
  &amp;lt;sql&amp;gt;&lt;br /&gt;
   &amp;lt;file charset=&amp;quot;utf8&amp;quot; driver=&amp;quot;mysql&amp;quot;&amp;gt;uninstall.sql&amp;lt;/file&amp;gt;&lt;br /&gt;
  &amp;lt;/sql&amp;gt;&lt;br /&gt;
 &amp;lt;/uninstall&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;administration&amp;gt;&lt;br /&gt;
  &amp;lt;!-- Administration Menu Section --&amp;gt;&lt;br /&gt;
  &amp;lt;menu&amp;gt;Hello World!&amp;lt;/menu&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;!-- Administration Main File Copy Section --&amp;gt;&lt;br /&gt;
  &amp;lt;files folder=&amp;quot;admin&amp;quot;&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;install.sql&amp;lt;/filename&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;uninstall.sql&amp;lt;/filename&amp;gt;&lt;br /&gt;
&amp;lt;/files&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
 &amp;lt;/administration&amp;gt;&lt;br /&gt;
&amp;lt;/install&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You will notice two attributes present on the &amp;lt;file&amp;gt; tags within the &amp;lt;install&amp;gt; and &amp;lt;uninstall&amp;gt; sections: charset and driver. The charset is the type of charset to use. The only valid charset is utf8. If you want to create install files for non-utf8 databases (for older version of MySQL), you should omit this attribute.&lt;br /&gt;
&lt;br /&gt;
The driver attribute specifies which database the queries were written for. Currently, this can only be mysql, but in future versions of Joomla! there may be more database drivers available.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
We now have a component that takes advantage of both the Joomla! MVC framework classes and the JDatabase classes. You are now able to write MVC components that interact with the database and can use the Joomla! installer to create and populate database tables.&lt;br /&gt;
&lt;br /&gt;
== Contributors ==&lt;br /&gt;
* staalanden&lt;br /&gt;
&lt;br /&gt;
== Download ==&lt;br /&gt;
&lt;br /&gt;
The component can be downloaded at: [http://joomlacode.org/gf/download/frsrelease/8110/29435/com_hello3_01.zip]&lt;/div&gt;</summary>
		<author><name>Tonie</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Adding_a_Model&amp;diff=9515</id>
		<title>J1.5:Developing a MVC Component/Adding a Model</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Adding_a_Model&amp;diff=9515"/>
		<updated>2008-07-27T20:10:35Z</updated>

		<summary type="html">&lt;p&gt;Tonie: /* Download */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
In the first tutorial of this series, creating a simple view-controller component using the Joomla! 1.5 CMS framework was demonstrated.&lt;br /&gt;
&lt;br /&gt;
In the first tutorial, the greeting was hardcoded into the view. This doesn&#039;t follow the MVC pattern exactly because the view is intended to only display the data, and not contain it.&lt;br /&gt;
&lt;br /&gt;
In this second part of the tutorial we will demonstrate how to move this out of the view and into a model. In future tutorials we will demonstrate the power and flexibility that this design pattern provides.&lt;br /&gt;
&lt;br /&gt;
== Creating the Model ==&lt;br /&gt;
&lt;br /&gt;
The concept of model gets its name because this class is intended to represent (or &#039;model&#039;) some entity. In our case, our first model will represent a &#039;hello&#039;, or a greeting. This is in line with our design thus far, because we have one view (&#039;hello&#039;), which is a view of our greeting.&lt;br /&gt;
&lt;br /&gt;
The naming convention for models in the Joomla! framework is that the class name starts with the name of the component (in our case &#039;hello&#039;, followed by &#039;model&#039;, followed by the model name. Therefore, our model class is called HelloModelHello.&lt;br /&gt;
&lt;br /&gt;
At this point, we will only model one behaviour of our hello, and that is retrieving the greeting. We will thus have one method, called getGreeting(). It will simply return the string &#039;Hello, World!&#039;.&lt;br /&gt;
&lt;br /&gt;
Here is the code for our model class:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * Hello Model for Hello World Component&lt;br /&gt;
 * &lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_2&lt;br /&gt;
 * @license    GNU/GPL&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// No direct access&lt;br /&gt;
&lt;br /&gt;
defined( &#039;_JEXEC&#039; ) or die( &#039;Restricted access&#039; );&lt;br /&gt;
&lt;br /&gt;
jimport( &#039;joomla.application.component.model&#039; );&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Hello Model&lt;br /&gt;
 *&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 */&lt;br /&gt;
class HelloModelHello extends JModel&lt;br /&gt;
{&lt;br /&gt;
    /**&lt;br /&gt;
    * Gets the greeting&lt;br /&gt;
    * @return string The greeting to be displayed to the user&lt;br /&gt;
    */&lt;br /&gt;
    function getGreeting()&lt;br /&gt;
    {&lt;br /&gt;
        return &#039;Hello, World!&#039;;&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You will notice a line that starts with jimport. The jimport function is used to load files from the Joomla! framework that are required for our component. This particular statement will load the file /libraries/joomla/application/component/model.php. The &#039;.&#039;s are used as directory separators and the last part is the name of the file to load. All files are loaded relative to the libraries directory. This particular file contains the class definition for the JModel class, which is necessary because our model extends this class.&lt;br /&gt;
&lt;br /&gt;
Now that we have created our model, we must modify our view so that it uses it to obtain the greeting.&lt;br /&gt;
&lt;br /&gt;
== Using the Model ==&lt;br /&gt;
&lt;br /&gt;
The Joomla! framework is setup in such a way that the controller will automatically load the model that has the same name as the view and will push it into the view. Since our view is called &#039;Hello&#039;, our &#039;Hello&#039; model will automatically be loaded and pushed into the view. Therefore, we can easily retrieve a reference to our model using the JView::getModel() method.&lt;br /&gt;
&lt;br /&gt;
Our previous view code contained the lines:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$greeting = &amp;quot;Hello World!&amp;quot;;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To take advantage of our model, we change this line to:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$model =&amp;amp; $this-&amp;gt;getModel();&lt;br /&gt;
$greeting = $model-&amp;gt;getGreeting();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The complete view now looks like:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_2&lt;br /&gt;
 * @license    GNU/GPL&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
// No direct access&lt;br /&gt;
&lt;br /&gt;
defined( &#039;_JEXEC&#039; ) or die( &#039;Restricted access&#039; );&lt;br /&gt;
&lt;br /&gt;
jimport( &#039;joomla.application.component.view&#039;);&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * HTML View class for the HelloWorld Component&lt;br /&gt;
 *&lt;br /&gt;
 * @package    HelloWorld&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
class HelloViewHello extends JView&lt;br /&gt;
{&lt;br /&gt;
    function display($tpl = null)&lt;br /&gt;
    {&lt;br /&gt;
        $model =&amp;amp; $this-&amp;gt;getModel();&lt;br /&gt;
        $greeting = $model-&amp;gt;getGreeting();&lt;br /&gt;
        $this-&amp;gt;assignRef( &#039;greeting&#039;,	$greeting );&lt;br /&gt;
&lt;br /&gt;
        parent::display($tpl);&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Adding the File to the Package ===&lt;br /&gt;
&lt;br /&gt;
All that remains is to add an entry to the XML file so that our new model will be copied. The Joomla! framework will look for our model in the models directory, so the entry for this file will look like (it should be added to the site section):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;filename&amp;gt;models/hello.php&amp;lt;/filename&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Our new hello.xml file will look like:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;&lt;br /&gt;
&amp;lt;install type=&amp;quot;component&amp;quot; version=&amp;quot;1.5.0&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;name&amp;gt;Hello&amp;lt;/name&amp;gt;&lt;br /&gt;
 &amp;lt;!-- The following elements are optional and free of formatting conttraints --&amp;gt;&lt;br /&gt;
 &amp;lt;creationDate&amp;gt;2007-02-22&amp;lt;/creationDate&amp;gt;&lt;br /&gt;
 &amp;lt;author&amp;gt;John Doe&amp;lt;/author&amp;gt;&lt;br /&gt;
 &amp;lt;authorEmail&amp;gt;john.doe@example.org&amp;lt;/authorEmail&amp;gt;&lt;br /&gt;
 &amp;lt;authorUrl&amp;gt;http://www.example.org&amp;lt;/authorUrl&amp;gt;&lt;br /&gt;
 &amp;lt;copyright&amp;gt;Copyright Info&amp;lt;/copyright&amp;gt;&lt;br /&gt;
 &amp;lt;license&amp;gt;License Info&amp;lt;/license&amp;gt;&lt;br /&gt;
 &amp;lt;!--  The version string is recorded in the components table --&amp;gt;&lt;br /&gt;
 &amp;lt;version&amp;gt;1.01&amp;lt;/version&amp;gt;&lt;br /&gt;
 &amp;lt;!-- The description is optional and defaults to the name --&amp;gt;&lt;br /&gt;
 &amp;lt;description&amp;gt;Description of the component ...&amp;lt;/description&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;!-- Site Main File Copy Section --&amp;gt;&lt;br /&gt;
 &amp;lt;!-- Note the folder attribute: This attribute describes the folder&lt;br /&gt;
      to copy FROM in the package to install therefore files copied&lt;br /&gt;
      in this section are copied from /site/ in the package --&amp;gt;&lt;br /&gt;
 &amp;lt;files folder=&amp;quot;site&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;controller.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;models/hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;models/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/view.html.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/tmpl/default.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/tmpl/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
 &amp;lt;/files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;administration&amp;gt;&lt;br /&gt;
  &amp;lt;!-- Administration Menu Section --&amp;gt;&lt;br /&gt;
  &amp;lt;menu&amp;gt;Hello World!&amp;lt;/menu&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;!-- Administration Main File Copy Section --&amp;gt;&lt;br /&gt;
  &amp;lt;files folder=&amp;quot;admin&amp;quot;&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;/files&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
 &amp;lt;/administration&amp;gt;&lt;br /&gt;
&amp;lt;/install&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
We now have a simple MVC component. Each element is very simple at this point, but provides a great deal of flexibility and power.&lt;br /&gt;
&lt;br /&gt;
== Contributors ==&lt;br /&gt;
* staalanden&lt;br /&gt;
&lt;br /&gt;
== Download ==&lt;br /&gt;
&lt;br /&gt;
The component can be downloaded at: [http://joomlacode.org/gf/download/frsrelease/8109/29434/com_hello2_01.zip]&lt;/div&gt;</summary>
		<author><name>Tonie</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Introduction&amp;diff=9514</id>
		<title>J1.5:Developing a MVC Component/Introduction</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Introduction&amp;diff=9514"/>
		<updated>2008-07-27T20:10:06Z</updated>

		<summary type="html">&lt;p&gt;Tonie: /* Download */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
A software framework is the base of an application that can be used by a developer. The framework in Joomla! 1.5 unleashes a great deal of power for them. The Joomla! code has been completely overhauled and cleaned up. This tutorial will guide you through the process of developing a component using the framework.&lt;br /&gt;
&lt;br /&gt;
The scope of this project will be to develop a simple Hello World! component. In future tutorials, this simple framework will be built upon to show the full power and versatility of the MVC design pattern in Joomla!&lt;br /&gt;
&lt;br /&gt;
== Requirements ==&lt;br /&gt;
You need Joomla! 1.5 or greater for this tutorial.&lt;br /&gt;
&lt;br /&gt;
== Introduction to Model-View-Controller ==&lt;br /&gt;
While the idea behind a component may seem extremely simple, code can quickly become very complex as additional features are added or the interface is customized.&lt;br /&gt;
&lt;br /&gt;
Model-View-Controller (herein referred to as MVC) is a software design pattern that can be used to organize code in such a way that the business logic and data presentation are separate. The premise behind this approach is that if the business logic is grouped into one section, then the interface and user interaction that surrounds the data can revised and customized without having to reprogram the business logic.&lt;br /&gt;
&lt;br /&gt;
There are three main parts of an MVC component. They are described here in brief, but for a more thorough explanation, please refer to the links provided at the end of this tutorial.&lt;br /&gt;
&lt;br /&gt;
=== Model ===&lt;br /&gt;
The model is the part of the component that encapsulates the application&#039;s data. It will often provide routines to manage and manipulate this data in a meaningful way in addition to routines that retrieve the data from the model. In our case, the model will contain methods to add, remove and update information about the greetings in the database. It will also contain methods to retrieve the list of greetings from the database. In general, the underlying data access technique should be encapsulated in the model. In this way, if an application is to be moved from a system that utilizes a flat file to store its information to a system that uses a database, the model is the only element that needs to be changed, not the view or the controller.&lt;br /&gt;
&lt;br /&gt;
=== View ===&lt;br /&gt;
The view is the part of the component that is used to render the data from the model in a manner that is suitable for interaction. For a web-based application, the view would generally be an HTML page that is returned to the data. The view pulls data from the model (which is passed to it from the controller) and feeds the data into a template which is populated and presented to the user. The view does not cause the data to be modified in any way, it only displays data retrieved from the model.&lt;br /&gt;
&lt;br /&gt;
=== Controller ===&lt;br /&gt;
The controller is responsible for responding to user actions. In the case of a web application, a user action is (generally) a page request. The controller will determine what request is being made by the user and respond appropriately by triggering the model to manipulate the data appropriately and passing the model into the view. The controller does not display the data in the model, it only triggers methods in the model which modify the data, and then pass the model into the view which displays the data.&lt;br /&gt;
&lt;br /&gt;
== Joomla! MVC Implementation ==&lt;br /&gt;
In Joomla!, the MVC pattern is implemented using three classes: [http://api.joomla.org/Joomla-Framework/Application/JModel.html JModel], [http://api.joomla.org/Joomla-Framework/Application/JView.html JView] and [http://api.joomla.org/Joomla-Framework/Application/JController.html JController]. For more detailed information about these classes, please refer to the API reference documentation (WIP).&lt;br /&gt;
&lt;br /&gt;
== Creating a Component ==&lt;br /&gt;
For our basic component, we only require five files:&lt;br /&gt;
&lt;br /&gt;
* hello.php - this is the entry point to our component&lt;br /&gt;
* controller.php - this file contains our base controller&lt;br /&gt;
* views/hello/view.html.php - this file retrieves the necessary data and pushes it into the template&lt;br /&gt;
* views/hello/tmpl/default.php - this is the template for our output&lt;br /&gt;
* hello.xml - this is an XML file that tells Joomla! how to install our component.&lt;br /&gt;
&lt;br /&gt;
=== Creating the Entry Point ===&lt;br /&gt;
Joomla! is always accessed through a single point of entry: index.php for the Site Application or administrator/index.php for the Administrator Application. The application will then load the required component, based on the value of &#039;option&#039; in the URL or in the POST data. For our component, the URL would be:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;index.php?option=com_hello&amp;amp;view=hello&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will load our main file, which can be seen as the single point of entry for our component: components/com_hello/hello.php.&lt;br /&gt;
&lt;br /&gt;
The code for this file is fairly typical across components.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * components/com_hello/hello.php&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_1&lt;br /&gt;
 * @license    GNU/GPL&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
// No direct access&lt;br /&gt;
&lt;br /&gt;
defined( &#039;_JEXEC&#039; ) or die( &#039;Restricted access&#039; );&lt;br /&gt;
&lt;br /&gt;
// Require the base controller&lt;br /&gt;
&lt;br /&gt;
require_once( JPATH_COMPONENT.DS.&#039;controller.php&#039; );&lt;br /&gt;
&lt;br /&gt;
// Require specific controller if requested&lt;br /&gt;
if($controller = JRequest::getWord(&#039;controller&#039;)) {&lt;br /&gt;
    $path = JPATH_COMPONENT.DS.&#039;controllers&#039;.DS.$controller.&#039;.php&#039;;&lt;br /&gt;
    if (file_exists($path)) {&lt;br /&gt;
        require_once $path;&lt;br /&gt;
    } else {&lt;br /&gt;
        $controller = &#039;&#039;;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Create the controller&lt;br /&gt;
$classname    = &#039;HelloController&#039;.$controller;&lt;br /&gt;
$controller   = new $classname( );&lt;br /&gt;
&lt;br /&gt;
// Perform the Request task&lt;br /&gt;
$controller-&amp;gt;execute( JRequest::getVar( &#039;task&#039; ) );&lt;br /&gt;
&lt;br /&gt;
// Redirect if set by the controller&lt;br /&gt;
$controller-&amp;gt;redirect();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first statement is a security check.&lt;br /&gt;
&lt;br /&gt;
JPATH_COMPONENT is the absolute path to the current component, in our case components/com_hello. If you specifically need either the Site component or the Administrator component, you can use JPATH_COMPONENT_SITE or JPATH_COMPONENT_ADMINISTRATOR.&lt;br /&gt;
&lt;br /&gt;
DS is the directory separator of your system: either &#039;/&#039; or &#039;\&#039;. This is automatically set by the framework so the developer doesn&#039;t have to worry about developing different versions for different server OSs. DS should always be used when referring to files on the local server.&lt;br /&gt;
&lt;br /&gt;
After loading the base controller, we check if a specific controller is needed. In this component, the base controller is the only controller, but we will leave this here for future use.&lt;br /&gt;
&lt;br /&gt;
JRequest:getVar() finds a variable in the URL or the POST data. So if our URL is index.php?option=com_hello&amp;gt;controller=controller_name, then we can retrieve our controller name in our component using: echo JRequest::getVar(&#039;controller&#039;);&lt;br /&gt;
&lt;br /&gt;
Now we have our base controller &#039;HelloController&#039; in com_hello/controller.php, and, if needed, additional controllers like &#039;HelloControllerController1&#039; in com_hello/controllers/controller1.php. Using this standard naming scheme will make things easy later on: &#039;{Componentname}{Controller}{Controllername}&#039;&lt;br /&gt;
&lt;br /&gt;
After the controller is created, we instruct the controller to execute the task, as defined in the URL: index.php?option=com_hello&amp;amp;task=sometask. If no task is set, the default task &#039;display&#039; will be assumed. When display is used, the &#039;view&#039; variable will decide what will be displayed. Other common tasks are save, edit, new...&lt;br /&gt;
&lt;br /&gt;
The controller might decide to redirect the page, usually after a task like &#039;save&#039; has been completed. This last statement takes care of the actual redirection.&lt;br /&gt;
&lt;br /&gt;
The main entry point (hello.php) essentially passes control to the controller, which handles performing the task that was specified in the request.&lt;br /&gt;
&lt;br /&gt;
Note that we don&#039;t use a closing php tag in this file: ?&amp;gt;. The reason for this is that we will not have any unwanted whitespace in the output code. This is default practice since Joomla! 1.5, and will be used for all php-only files.&lt;br /&gt;
&lt;br /&gt;
=== Creating the Controller ===&lt;br /&gt;
Our component only has one task - greet the world. Therefore, the controller will be very simple. No data manipulation is required. All that needs to be done is the appropriate view loaded. We will have only one method in our controller: display(). Most of the required functionality is built into the JController class, so all that we need to do is invoke the JController::display() method.&lt;br /&gt;
&lt;br /&gt;
The code for the base controller is:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_1&lt;br /&gt;
 * @license    GNU/GPL&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// No direct access&lt;br /&gt;
&lt;br /&gt;
defined( &#039;_JEXEC&#039; ) or die( &#039;Restricted access&#039; );&lt;br /&gt;
&lt;br /&gt;
jimport(&#039;joomla.application.component.controller&#039;);&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Hello World Component Controller&lt;br /&gt;
 *&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 */&lt;br /&gt;
class HelloController extends JController&lt;br /&gt;
{&lt;br /&gt;
    /**&lt;br /&gt;
     * Method to display the view&lt;br /&gt;
     *&lt;br /&gt;
     * @access    public&lt;br /&gt;
     */&lt;br /&gt;
    function display()&lt;br /&gt;
    {&lt;br /&gt;
        parent::display();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The JController constructor will always register a display() task and unless otherwise specified (using the registerDefaultTask() method), it will set it as the default task.&lt;br /&gt;
&lt;br /&gt;
This barebones display() method isn&#039;t really even necessary since all it does is invoke the parent constructor. However, it is a good visual clue to indicate what is happening in the controller.&lt;br /&gt;
&lt;br /&gt;
The JController::display() method will determine the name of the view and layout from the request and load that view and set the layout. When you create a menu item for your component, the menu manager will allow the administrator to select the view that they would like the menu link to display and to specify the layout. A view usually refers to a view of a certain set of data (i.e. a list of cars, a list of events, a single car, a single event). A layout is a way that that view is organized.&lt;br /&gt;
&lt;br /&gt;
In our component, we will have a single view called hello, and a single layout (default).&lt;br /&gt;
&lt;br /&gt;
=== Creating the View ===&lt;br /&gt;
The task of the view is very simple: It retrieves the data to be displayed and pushes it into the template. Data is pushed into the template using the JView::assignRef method.&lt;br /&gt;
&lt;br /&gt;
The code for the view is:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_1&lt;br /&gt;
 * @license    GNU/GPL&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
// no direct access&lt;br /&gt;
&lt;br /&gt;
defined( &#039;_JEXEC&#039; ) or die( &#039;Restricted access&#039; );&lt;br /&gt;
&lt;br /&gt;
jimport( &#039;joomla.application.component.view&#039;);&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * HTML View class for the HelloWorld Component&lt;br /&gt;
 *&lt;br /&gt;
 * @package    HelloWorld&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
class HelloViewHello extends JView&lt;br /&gt;
{&lt;br /&gt;
    function display($tpl = null)&lt;br /&gt;
    {&lt;br /&gt;
        $greeting = &amp;quot;Hello World!&amp;quot;;&lt;br /&gt;
        $this-&amp;gt;assignRef( &#039;greeting&#039;, $greeting );&lt;br /&gt;
&lt;br /&gt;
        parent::display($tpl);&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Creating the Template ====&lt;br /&gt;
&lt;br /&gt;
Joomla! templates/layouts are regular PHP files that are used to layout the data from the view in a particular manner. The variables assigned by the JView::assignRef method can be accessed from the template using $this-&amp;gt;{propertyname} (see the template code below for an example).&lt;br /&gt;
&lt;br /&gt;
Our template is very simple: we only want to display the greeting that was passed in from the view:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
&lt;br /&gt;
// No direct access&lt;br /&gt;
&lt;br /&gt;
defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;); ?&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;&amp;lt;?php echo $this-&amp;gt;greeting; ?&amp;gt;&amp;lt;/h1&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Wrapping It All Up - Creating the hello.xml File ===&lt;br /&gt;
It is possible to install a component manually by copying the files using an FTP client and modifying the database tables. It is more efficient to create a package file that will allow the Joomla! Installer to do this for you. This package file contains a variety of information:&lt;br /&gt;
&lt;br /&gt;
* basic descriptive details about your component (i.e. name), and optionally, a description, copyright and license information.&lt;br /&gt;
* a list of files that need to be copied.&lt;br /&gt;
* optionally, a PHP file that performs additional install and uninstall operations.&lt;br /&gt;
* optionally, an SQL file which contains database queries that should be executed upon install/uninstall&lt;br /&gt;
&lt;br /&gt;
The format of the XML file is as follows:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;&lt;br /&gt;
&amp;lt;install type=&amp;quot;component&amp;quot; version=&amp;quot;1.5.0&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;name&amp;gt;Hello&amp;lt;/name&amp;gt;&lt;br /&gt;
 &amp;lt;!-- The following elements are optional and free of formatting conttraints --&amp;gt;&lt;br /&gt;
 &amp;lt;creationDate&amp;gt;2007-02-22&amp;lt;/creationDate&amp;gt;&lt;br /&gt;
 &amp;lt;author&amp;gt;John Doe&amp;lt;/author&amp;gt;&lt;br /&gt;
 &amp;lt;authorEmail&amp;gt;john.doe@example.org&amp;lt;/authorEmail&amp;gt;&lt;br /&gt;
 &amp;lt;authorUrl&amp;gt;http://www.example.org&amp;lt;/authorUrl&amp;gt;&lt;br /&gt;
 &amp;lt;copyright&amp;gt;Copyright Info&amp;lt;/copyright&amp;gt;&lt;br /&gt;
 &amp;lt;license&amp;gt;License Info&amp;lt;/license&amp;gt;&lt;br /&gt;
 &amp;lt;!--  The version string is recorded in the components table --&amp;gt;&lt;br /&gt;
 &amp;lt;version&amp;gt;1.01&amp;lt;/version&amp;gt;&lt;br /&gt;
 &amp;lt;!-- The description is optional and defaults to the name --&amp;gt;&lt;br /&gt;
 &amp;lt;description&amp;gt;Description of the component ...&amp;lt;/description&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;!-- Site Main File Copy Section --&amp;gt;&lt;br /&gt;
 &amp;lt;!-- Note the folder attribute: This attribute describes the folder&lt;br /&gt;
      to copy FROM in the package to install therefore files copied&lt;br /&gt;
      in this section are copied from /site/ in the package --&amp;gt;&lt;br /&gt;
 &amp;lt;files folder=&amp;quot;site&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;controller.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/view.html.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/tmpl/default.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/tmpl/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
 &amp;lt;/files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;administration&amp;gt;&lt;br /&gt;
  &amp;lt;!-- Administration Menu Section --&amp;gt;&lt;br /&gt;
  &amp;lt;menu&amp;gt;Hello World!&amp;lt;/menu&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;!-- Administration Main File Copy Section --&amp;gt;&lt;br /&gt;
  &amp;lt;files folder=&amp;quot;admin&amp;quot;&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;/files&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
 &amp;lt;/administration&amp;gt;&lt;br /&gt;
&amp;lt;/install&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you look closely you will notice that there are some files that will be copied that we have not discussed. These are the index.html files. An index.html file is placed in each directory to prevent prying users from getting a directory listing. If there is no index.html file, some web servers will list the directory contents. This is often undesirable. These files have the simple line:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;html&amp;gt;&amp;lt;body bgcolor=&amp;quot;#FFFFFF&amp;quot;&amp;gt;&amp;lt;/body&amp;gt;&amp;lt;/html&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It will simply display a blank page.&lt;br /&gt;
&lt;br /&gt;
The other file is the hello.php file. This is the entry point for the admin section of our component. Since we don&#039;t have an admin section of our component, it will have the same content as the index.html files at this point in time.&lt;br /&gt;
&lt;br /&gt;
== Contributors ==&lt;br /&gt;
* mjaz&lt;br /&gt;
* staalanden&lt;br /&gt;
&lt;br /&gt;
== Download ==&lt;br /&gt;
The component can be downloaded at: [http://joomlacode.org/gf/download/frsrelease/8108/29433/com_hello1_01.zip]&lt;/div&gt;</summary>
		<author><name>Tonie</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Creating_an_Administrator_Interface&amp;diff=9513</id>
		<title>J1.5:Developing a MVC Component/Creating an Administrator Interface</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Creating_an_Administrator_Interface&amp;diff=9513"/>
		<updated>2008-07-27T19:50:44Z</updated>

		<summary type="html">&lt;p&gt;Tonie: /* Download */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
In the first three tutorials, we have developed a MVC component that retrieves its data from a table in the database. Currently, there is no way to add data to the database except to do it manually using another tool. In this tutorial, we will develop an administrator section for our component which will make it possible to manage the entries in the database.&lt;br /&gt;
&lt;br /&gt;
== Creating the Basic Framework ==&lt;br /&gt;
&lt;br /&gt;
The basic framework of the administrator panel is very similar to the site portion. The main entry point for the administrator section of the component is hello.php. This file is identical to the hello.php file that was used in the site portion except the name of the controller it loads will be changed to HellosController. The default controller is also called controller.php and this file is identical to the default controller in the site portion, with the exception that the controller is named HellosController instead of HelloController. This difference is so that JController will by default load the hellos view, which will display a list of our greetings.&lt;br /&gt;
&lt;br /&gt;
Here is the listing for hello.php:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_4&lt;br /&gt;
 * @license    GNU/GPL&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
// No direct access&lt;br /&gt;
&lt;br /&gt;
defined( &#039;_JEXEC&#039; ) or die( &#039;Restricted access&#039; );&lt;br /&gt;
&lt;br /&gt;
// Require the base controller&lt;br /&gt;
&lt;br /&gt;
require_once( JPATH_COMPONENT.DS.&#039;controller.php&#039; );&lt;br /&gt;
&lt;br /&gt;
// Require specific controller if requested&lt;br /&gt;
if($controller = JRequest::getWord(&#039;controller&#039;)) {&lt;br /&gt;
    $path = JPATH_COMPONENT.DS.&#039;controllers&#039;.DS.$controller.&#039;.php&#039;;&lt;br /&gt;
    if (file_exists($path)) {&lt;br /&gt;
        require_once $path;&lt;br /&gt;
    } else {&lt;br /&gt;
        $controller = &#039;&#039;;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Create the controller&lt;br /&gt;
$classname    = &#039;HellosController&#039;.$controller;&lt;br /&gt;
$controller   = new $classname( );&lt;br /&gt;
&lt;br /&gt;
// Perform the Request task&lt;br /&gt;
$controller-&amp;gt;execute( JRequest::getVar( &#039;task&#039; ) );&lt;br /&gt;
&lt;br /&gt;
// Redirect if set by the controller&lt;br /&gt;
$controller-&amp;gt;redirect();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The view and model that we will start with is the hellos view and the hellos model. We will start with the model.&lt;br /&gt;
&lt;br /&gt;
==== The Hellos Model ====&lt;br /&gt;
&lt;br /&gt;
The Hellos Model will be very simple. The only operation that we currently need is the ability to retrieve the list of hellos from the database. This operation will be implemented in a method called getData().&lt;br /&gt;
&lt;br /&gt;
The JModel class has a built in protected method called _getList(). This method can be used to simplify the task of retrieving a list of records from the database. We simply need to pass it the query and it will return the list of records.&lt;br /&gt;
&lt;br /&gt;
At a later point in time, we might want to use our query from within another method. Therefore, we will create a private method called _buildQuery() which will return the query that will be passed to _getList(). This makes it easier to change the query as well since it is localized in one place.&lt;br /&gt;
&lt;br /&gt;
Therefore we need two methods in our class: getData() and _buildQuery().&lt;br /&gt;
&lt;br /&gt;
_buildQuery() simply returns the query. It looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source  lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Returns the query&lt;br /&gt;
 * @return string The query to be used to retrieve the rows from the database&lt;br /&gt;
 */&lt;br /&gt;
function _buildQuery()&lt;br /&gt;
{&lt;br /&gt;
    $query = &#039; SELECT * &#039;&lt;br /&gt;
           . &#039; FROM #__hello &#039;&lt;br /&gt;
    ;&lt;br /&gt;
&lt;br /&gt;
    return $query;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
getData() will obtain the query and retrieve the records from the database. Now it might happen that we need to retrieve this list of data twice in one page load. It would be a waste to have to query the database twice. Therefore, we will have this method store the data in a protected property so that on subsequent requests it can simply return the data it has already retrieved. This property will be called _data.&lt;br /&gt;
&lt;br /&gt;
Here is the getData() method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source  lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Retrieves the hello data&lt;br /&gt;
 * @return array Array of objects containing the data from the database&lt;br /&gt;
 */&lt;br /&gt;
function getData()&lt;br /&gt;
{&lt;br /&gt;
    // Lets load the data if it doesn&#039;t already exist&lt;br /&gt;
    if (empty( $this-&amp;gt;_data ))&lt;br /&gt;
    {&lt;br /&gt;
        $query = $this-&amp;gt;_buildQuery();&lt;br /&gt;
        $this-&amp;gt;_data = $this-&amp;gt;_getList( $query );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return $this-&amp;gt;_data;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The completed model looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * Hellos Model for Hello World Component&lt;br /&gt;
 * &lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_4&lt;br /&gt;
 * @license        GNU/GPL&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// Check to ensure this file is included in Joomla!&lt;br /&gt;
defined(&#039;_JEXEC&#039;) or die();&lt;br /&gt;
&lt;br /&gt;
jimport( &#039;joomla.application.component.model&#039; );&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Hello Model&lt;br /&gt;
 *&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 */&lt;br /&gt;
class HellosModelHellos extends JModel&lt;br /&gt;
{&lt;br /&gt;
    /**&lt;br /&gt;
     * Hellos data array&lt;br /&gt;
     *&lt;br /&gt;
     * @var array&lt;br /&gt;
     */&lt;br /&gt;
    var $_data;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Returns the query&lt;br /&gt;
     * @return string The query to be used to retrieve the rows from the database&lt;br /&gt;
     */&lt;br /&gt;
    function _buildQuery()&lt;br /&gt;
    {&lt;br /&gt;
        $query = &#039; SELECT * &#039;&lt;br /&gt;
            . &#039; FROM #__hello &#039;&lt;br /&gt;
        ;&lt;br /&gt;
        return $query;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Retrieves the hello data&lt;br /&gt;
     * @return array Array of objects containing the data from the database&lt;br /&gt;
     */&lt;br /&gt;
    function getData()&lt;br /&gt;
    {&lt;br /&gt;
        // Lets load the data if it doesn&#039;t already exist&lt;br /&gt;
        if (empty( $this-&amp;gt;_data ))&lt;br /&gt;
        {&lt;br /&gt;
            $query = $this-&amp;gt;_buildQuery();&lt;br /&gt;
            $this-&amp;gt;_data = $this-&amp;gt;_getList( $query );&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        return $this-&amp;gt;_data;&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This file is saved as models/hellos.php.&lt;br /&gt;
&lt;br /&gt;
==== The Hellos View ====&lt;br /&gt;
&lt;br /&gt;
Now that we have a model to retrieve our data, we need to display it. This view will be fairly similar to the view from the site section as well.&lt;br /&gt;
&lt;br /&gt;
Just as our model was automatically instantiated in the site, so it is in the administrator. Methods that start with get in the model can be accessed using the get() method of the JView class. So our view has three lines: one to retrieve the data from the model, one to push the data into the template, and one to invoke the display method to display the output. Thus we have:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * Hellos View for Hello World Component&lt;br /&gt;
 * &lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_4&lt;br /&gt;
 * @license        GNU/GPL&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// Check to ensure this file is included in Joomla!&lt;br /&gt;
defined(&#039;_JEXEC&#039;) or die();&lt;br /&gt;
&lt;br /&gt;
jimport( &#039;joomla.application.component.view&#039; );&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Hellos View&lt;br /&gt;
 *&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 */&lt;br /&gt;
class HellosViewHellos extends JView&lt;br /&gt;
{&lt;br /&gt;
    /**&lt;br /&gt;
     * Hellos view display method&lt;br /&gt;
     * @return void&lt;br /&gt;
     **/&lt;br /&gt;
    function display($tpl = null)&lt;br /&gt;
    {&lt;br /&gt;
        JToolBarHelper::title( JText::_( &#039;Hello Manager&#039; ), &#039;generic.png&#039; );&lt;br /&gt;
        JToolBarHelper::deleteList();&lt;br /&gt;
        JToolBarHelper::editListX();&lt;br /&gt;
        JToolBarHelper::addNewX();&lt;br /&gt;
&lt;br /&gt;
        // Get data from the model&lt;br /&gt;
        $items =&amp;amp; $this-&amp;gt;get( &#039;Data&#039;);&lt;br /&gt;
&lt;br /&gt;
        $this-&amp;gt;assignRef( &#039;items&#039;, $items );&lt;br /&gt;
&lt;br /&gt;
        parent::display($tpl);&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This file is saved as views/hellos/view.html.php.&lt;br /&gt;
&lt;br /&gt;
==== The Hellos Template ====&lt;br /&gt;
&lt;br /&gt;
The template will take the data pushed into it from the view and produce the output. We will display our output in a simple table. While the frontend template was very simple, in the administrator we will need a minimal amount of extra logic to handle looping through the data.&lt;br /&gt;
&lt;br /&gt;
Here is our template:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;); ?&amp;gt;&lt;br /&gt;
&amp;lt;form action=&amp;quot;index.php&amp;quot; method=&amp;quot;post&amp;quot; name=&amp;quot;adminForm&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;div id=&amp;quot;editcell&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;table class=&amp;quot;adminlist&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;thead&amp;gt;&lt;br /&gt;
        &amp;lt;tr&amp;gt;&lt;br /&gt;
            &amp;lt;th width=&amp;quot;5&amp;quot;&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo JText::_( &#039;ID&#039; ); ?&amp;gt;&lt;br /&gt;
            &amp;lt;/th&amp;gt;&lt;br /&gt;
            &amp;lt;th&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo JText::_( &#039;Greeting&#039; ); ?&amp;gt;&lt;br /&gt;
            &amp;lt;/th&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;            &lt;br /&gt;
    &amp;lt;/thead&amp;gt;&lt;br /&gt;
    &amp;lt;?php&lt;br /&gt;
    $k = 0;&lt;br /&gt;
    for ($i=0, $n=count( $this-&amp;gt;items ); $i &amp;lt; $n; $i++)&lt;br /&gt;
    {&lt;br /&gt;
        $row =&amp;amp; $this-&amp;gt;items[$i];&lt;br /&gt;
        ?&amp;gt;&lt;br /&gt;
        &amp;lt;tr class=&amp;quot;&amp;lt;?php echo &amp;quot;row$k&amp;quot;; ?&amp;gt;&amp;quot;&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo $row-&amp;gt;id; ?&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo $row-&amp;gt;greeting; ?&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;&lt;br /&gt;
        &amp;lt;?php&lt;br /&gt;
        $k = 1 - $k;&lt;br /&gt;
    }&lt;br /&gt;
    ?&amp;gt;&lt;br /&gt;
    &amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;option&amp;quot; value=&amp;quot;com_hello&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;task&amp;quot; value=&amp;quot;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;boxchecked&amp;quot; value=&amp;quot;0&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;controller&amp;quot; value=&amp;quot;hello&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/form&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This template is saved as views/hellos/tmpl/default.php.&lt;br /&gt;
&lt;br /&gt;
You will notice that our output is enclosed in a form. Though this is not necessary now, it will be soon.&lt;br /&gt;
&lt;br /&gt;
We have now completed the basic part of the first view. We have added five files to the admin section of our component:&lt;br /&gt;
&lt;br /&gt;
* hello.php&lt;br /&gt;
* controller.php&lt;br /&gt;
* models/hellos.php&lt;br /&gt;
* views/hellos/view.html.php&lt;br /&gt;
* views/hellos/tmpl/default.php&lt;br /&gt;
&lt;br /&gt;
You can now add these files to the XML install file and give it a try!&lt;br /&gt;
&lt;br /&gt;
== Adding Functionality ==&lt;br /&gt;
&lt;br /&gt;
So far our administrator section is pretty useless. It doesn&#039;t really do anything - all it does is display the entries that we have in our database.&lt;br /&gt;
&lt;br /&gt;
In order to make it useful, we need to add some buttons and links.&lt;br /&gt;
&lt;br /&gt;
==== The Toolbar ====&lt;br /&gt;
&lt;br /&gt;
You may have noticed the toolbar that appears at the top of other Joomla! component administrator panels. Our component needs one as well. Joomla! makes this very easy to do. We will add buttons Delete records, Edit records, and create New records. We will also add a title that will be displayed on our toolbar.&lt;br /&gt;
&lt;br /&gt;
This is done by adding code to the view. To add the buttons, we use static methods from the Joomla! JToolBarHelper class. The code looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;JToolBarHelper::title(   JText::_( &#039;Hello Manager&#039; ), &#039;generic.png&#039; );&lt;br /&gt;
JToolBarHelper::deleteList();&lt;br /&gt;
JToolBarHelper::editListX();&lt;br /&gt;
JToolBarHelper::addNewX();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
These three methods will create the appropriate buttons. The deleteList() method can optionally take up to three parameters - the first parameter is a string to display to the user to confirm that they want to delete the records. The second is the task that should be sent with the query (the default is &#039;remove&#039;), and the third is the text that should be displayed below the button.&lt;br /&gt;
&lt;br /&gt;
The editListX() and addNewX() methods can each take two optional parameters. The first is the task (which are by default edit and add, respectively), and the second is the text that should be displayed below the button.&lt;br /&gt;
&lt;br /&gt;
*You may have noticed the use of the JText::_ method in the template before and here as well. This is a handy function that makes component translation much easier. The JText::_ method will look up the string in your component language file and return the translated string. If not translation is found, it will return the string that you passed it. If you want to translate your component into another language, all you have to do is create a language file that will map the strings within the quotes to the translated version of the string.&lt;br /&gt;
&lt;br /&gt;
==== Checkboxes and Links ====&lt;br /&gt;
&lt;br /&gt;
We now have buttons. Two of those buttons operate on existing records. But how do we know which records to operate on? We have to let the user tell us. To do this, we need to add checkboxes to our table so that the user can select certain records. This is done in our template.&lt;br /&gt;
&lt;br /&gt;
In order to the add the checkboxes, we need to add an extra column into our table. We will add the column in between the two that we already have.&lt;br /&gt;
&lt;br /&gt;
In the header of the column, we will add a checkbox which can be used to toggle all the boxes below it on or off:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;th width=&amp;quot;20&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;input type=&amp;quot;checkbox&amp;quot; name=&amp;quot;toggle&amp;quot; value=&amp;quot;&amp;quot; onclick=&amp;quot;checkAll(&amp;lt;?php echo count( $this-&amp;gt;items ); ?&amp;gt;);&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/th&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Javascript checkAll function is a function that is built into the Joomla! base Javascript package that provides the functionality that we want here.&lt;br /&gt;
&lt;br /&gt;
Now we need to add the checkboxes into the individual rows. Joomla!&#039;s JHTML class has a method, JHTML::_(), which will generate our checkbox for us. We will add the following line to our loop:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$checked    = JHTML::_( &#039;grid.id&#039;, $i, $row-&amp;gt;id );&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
after the line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$row =&amp;amp; $this-&amp;gt;items[$i];&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then we will add a cell in between the two that we already have:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;td&amp;gt;&lt;br /&gt;
    &amp;lt;?php echo $checked; ?&amp;gt;&lt;br /&gt;
&amp;lt;/td&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It can be cumbersome to have to check the box that we want to edit and then move up and click the edit button. Therefore, we will add a link that it will go straight to the greeting&#039;s edit form. We will add the following line after the call to the JHTML::_() method to generate the link HTML:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$link = JRoute::_( &#039;index.php?option=com_hello&amp;gt;controller=hello&amp;gt;task=edit&amp;gt;cid[]=&#039;. $row-&amp;gt;id );&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And we include the link in the cell showing the greeting text:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;td&amp;gt;&lt;br /&gt;
    &amp;lt;a href=&amp;quot;&amp;lt;?php echo $link; ?&amp;gt;&amp;quot;&amp;gt;&amp;lt;?php echo $row-&amp;gt;greeting; ?&amp;gt;&amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;lt;/td&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You will notice that this link points to the hello controller. This controller will handle the data manipulation of our greetings.&lt;br /&gt;
&lt;br /&gt;
If you recall from above, we had four hidden input fields at the bottom of our form. The first input field was named &#039;option&#039;. This field is necessary so that we stay in our component. The second input field was task. This form property gets set when one of the buttons in the toolbar is clicked. A Javascript error will result and the buttons will not work if this input field is omitted. The third input field is the boxchecked field. This field keeps track of the number of boxes that are checked. The edit and delete buttons will check to ensure that this is greater than zero and will not allow the form to be submitted if it is not. The fourth input field is the controller field. This is used to specify that tasks fired from this form will be handled by the hello controller.&lt;br /&gt;
&lt;br /&gt;
Here is the code for the completed default.php file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;); ?&amp;gt;&lt;br /&gt;
&amp;lt;form action=&amp;quot;index.php&amp;quot; method=&amp;quot;post&amp;quot; name=&amp;quot;adminForm&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;div id=&amp;quot;editcell&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;table class=&amp;quot;adminlist&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;thead&amp;gt;&lt;br /&gt;
        &amp;lt;tr&amp;gt;&lt;br /&gt;
            &amp;lt;th width=&amp;quot;5&amp;quot;&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo JText::_( &#039;ID&#039; ); ?&amp;gt;&lt;br /&gt;
            &amp;lt;/th&amp;gt;&lt;br /&gt;
            &amp;lt;th&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo JText::_( &#039;Greeting&#039; ); ?&amp;gt;&lt;br /&gt;
            &amp;lt;/th&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;            &lt;br /&gt;
    &amp;lt;/thead&amp;gt;&lt;br /&gt;
    &amp;lt;?php&lt;br /&gt;
    $k = 0;&lt;br /&gt;
    for ($i=0, $n=count( $this-&amp;gt;items ); $i &amp;lt; $n; $i++)&lt;br /&gt;
    {&lt;br /&gt;
        $row =&amp;amp; $this-&amp;gt;items[$i];&lt;br /&gt;
        ?&amp;gt;&lt;br /&gt;
        &amp;lt;tr class=&amp;quot;&amp;lt;?php echo &amp;quot;row$k&amp;quot;; ?&amp;gt;&amp;quot;&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo $row-&amp;gt;id; ?&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo $row-&amp;gt;greeting; ?&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;&lt;br /&gt;
        &amp;lt;?php&lt;br /&gt;
        $k = 1 - $k;&lt;br /&gt;
    }&lt;br /&gt;
    ?&amp;gt;&lt;br /&gt;
    &amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;option&amp;quot; value=&amp;quot;com_hello&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;task&amp;quot; value=&amp;quot;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;boxchecked&amp;quot; value=&amp;quot;0&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;controller&amp;quot; value=&amp;quot;hello&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/form&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Our hellos view is now complete.&lt;br /&gt;
&lt;br /&gt;
== Getting Down and Dirty: Doing the Real Work ==&lt;br /&gt;
&lt;br /&gt;
Now that the Hellos view is done, it is time to move to the Hello view and model. This is where the real work will get done.&lt;br /&gt;
&lt;br /&gt;
==== The Hello Controller ====&lt;br /&gt;
&lt;br /&gt;
Our default controller just isn&#039;t going to cut it when it comes to doing work - all it is capable of doing is displaying views.&lt;br /&gt;
&lt;br /&gt;
We need to be able to handle the tasks that we are launching from the Hellos view: add, edit and remove.&lt;br /&gt;
&lt;br /&gt;
Add and edit are essentially the same task: they both display a form to the user that allows a greeting to be edited. The only difference is that new displays a blank form, and edit displays a form with data already in it. Since they are similar, we will map the add task onto the edit task handler. This is specified in our constructor:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * constructor (registers additional tasks to methods)&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function __construct()&lt;br /&gt;
{&lt;br /&gt;
    parent::__construct();&lt;br /&gt;
&lt;br /&gt;
    // Register Extra tasks&lt;br /&gt;
    $this-&amp;gt;registerTask( &#039;add&#039;  ,     &#039;edit&#039; );&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first parameter of JController::registerTask is the task to map, and the second is the method to map it to.&lt;br /&gt;
&lt;br /&gt;
We will start with handling the edit task. The controller&#039;s job is fairly simple for the edit task. All it has to do is specify the view and layout to load (the hello view and the form layout). We will also tell Joomla! to disable the mainmenu while we are editing our greeting. This prevents users from leaving unsaved records open.&lt;br /&gt;
&lt;br /&gt;
Our edit task handler looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * display the edit form&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function edit()&lt;br /&gt;
{&lt;br /&gt;
    JRequest::setVar( &#039;view&#039;, &#039;hello&#039; );&lt;br /&gt;
    JRequest::setVar( &#039;layout&#039;, &#039;form&#039;  );&lt;br /&gt;
    JRequest::setVar(&#039;hidemainmenu&#039;, 1);&lt;br /&gt;
&lt;br /&gt;
    parent::display();&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== The Hello View ====&lt;br /&gt;
&lt;br /&gt;
The Hello view will display a form which will allow the user to edit a greeting. The display method if the hello view has to do a few simple tasks:&lt;br /&gt;
&lt;br /&gt;
* retrieve the data from the model&lt;br /&gt;
* create the toolbar&lt;br /&gt;
* pass the data into the template&lt;br /&gt;
* invoke the display() method to render the template&lt;br /&gt;
&lt;br /&gt;
This becomes a bit more complicated because the one view handles both the edit and add tasks. In our toolbar we want the user to know what whether they are adding or editing, so we have to determine which task was fired.&lt;br /&gt;
&lt;br /&gt;
Since we are already retrieving the record that we want to display from the model, we can use this data to determine what task was fired. If the task was edit, then the id field of our record will have been set. If the task was new, then it will not have been set. This can be used to determine if we have a new record or an existing record.&lt;br /&gt;
&lt;br /&gt;
We will add two buttons to the toolbar: save and cancel. Though the functionality will be the same, we want to display different buttons depending on whether it is a new or existing record. If it is a new record, we will display cancel. If it already exists, we will display close.&lt;br /&gt;
&lt;br /&gt;
Thus our display method looks like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * display method of Hello view&lt;br /&gt;
 * @return void&lt;br /&gt;
 **/&lt;br /&gt;
function display($tpl = null)&lt;br /&gt;
{&lt;br /&gt;
    //get the hello&lt;br /&gt;
    $hello        =&amp;amp; $this-&amp;gt;get(&#039;Data&#039;);&lt;br /&gt;
    $isNew        = ($hello-&amp;gt;id &amp;lt; 1);&lt;br /&gt;
&lt;br /&gt;
    $text = $isNew ? JText::_( &#039;New&#039; ) : JText::_( &#039;Edit&#039; );&lt;br /&gt;
    JToolBarHelper::title(   JText::_( &#039;Hello&#039; ).&#039;: &amp;lt;small&amp;gt;&amp;lt;small&amp;gt;[ &#039; . $text.&#039; ]&amp;lt;/small&amp;gt;&amp;lt;/small&amp;gt;&#039; );&lt;br /&gt;
    JToolBarHelper::save();&lt;br /&gt;
    if ($isNew)  {&lt;br /&gt;
        JToolBarHelper::cancel();&lt;br /&gt;
    } else {&lt;br /&gt;
        // for existing items the button is renamed `close`&lt;br /&gt;
        JToolBarHelper::cancel( &#039;cancel&#039;, &#039;Close&#039; );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    $this-&amp;gt;assignRef(&#039;hello&#039;, $hello);&lt;br /&gt;
    parent::display($tpl);&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== The Hello Model ====&lt;br /&gt;
&lt;br /&gt;
Our view needs data. Therefore, we need to create a model to model a hello.&lt;br /&gt;
&lt;br /&gt;
Our model will have two properties: _id and _data. _id will hold the id of the greeting and data will hold the data.&lt;br /&gt;
&lt;br /&gt;
We will start with a constructor, which will attempt to retrieve the id from the query:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Constructor that retrieves the ID from the request&lt;br /&gt;
 *&lt;br /&gt;
 * @access    public&lt;br /&gt;
 * @return    void&lt;br /&gt;
 */&lt;br /&gt;
function __construct()&lt;br /&gt;
{&lt;br /&gt;
    parent::__construct();&lt;br /&gt;
&lt;br /&gt;
    $array = JRequest::getVar(&#039;cid&#039;,  0, &#039;&#039;, &#039;array&#039;);&lt;br /&gt;
    $this-&amp;gt;setId((int)$array[0]);&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The JRequest::getVar() method is used to retrieve data from the request. The first parameter is the name of the form variable. The second parameter is the default value to assign if there is no value found. The third parameter is the name of the hash to retrieve the value from (get, post, etc), and the last value is the data type that should be forced on the value.&lt;br /&gt;
&lt;br /&gt;
Our constructor will take the first value from the cid array and assign it to the id.&lt;br /&gt;
&lt;br /&gt;
Our setId() method can be used to set our id. Changing the id that our model points to will mean the id points to the wrong data. Therefore, when we set the id, we will clear the data property:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Method to set the hello identifier&lt;br /&gt;
 *&lt;br /&gt;
 * @access    public&lt;br /&gt;
 * @param    int Hello identifier&lt;br /&gt;
 * @return    void&lt;br /&gt;
 */&lt;br /&gt;
function setId($id)&lt;br /&gt;
{&lt;br /&gt;
    // Set id and wipe data&lt;br /&gt;
    $this-&amp;gt;_id        = $id;&lt;br /&gt;
    $this-&amp;gt;_data    = null;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finally, we need a method to retrieve our data: getData()&lt;br /&gt;
&lt;br /&gt;
getData will check if the _data property has already been set. If it has, it will simply return it. Otherwise, it will load the data from the database.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Method to get a hello&lt;br /&gt;
 * @return object with data&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
function &amp;amp;getData()&lt;br /&gt;
{&lt;br /&gt;
    // Load the data&lt;br /&gt;
    if (empty( $this-&amp;gt;_data )) {&lt;br /&gt;
        $query = &#039; SELECT * FROM #__hello &#039;.&lt;br /&gt;
                &#039;  WHERE id = &#039;.$this-&amp;gt;_id;&lt;br /&gt;
        $this-&amp;gt;_db-&amp;gt;setQuery( $query );&lt;br /&gt;
        $this-&amp;gt;_data = $this-&amp;gt;_db-&amp;gt;loadObject();&lt;br /&gt;
    }&lt;br /&gt;
    if (!$this-&amp;gt;_data) {&lt;br /&gt;
        $this-&amp;gt;_data = new stdClass();&lt;br /&gt;
        $this-&amp;gt;_data-&amp;gt;id = 0;&lt;br /&gt;
        $this-&amp;gt;_data-&amp;gt;greeting = null;&lt;br /&gt;
    }&lt;br /&gt;
    return $this-&amp;gt;_data;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== The Form ====&lt;br /&gt;
&lt;br /&gt;
Now all that is left is to create the form that the data will go into. Since we specified our layout as form, the form will go in a file in the tmpl directory of the hello view called form.php:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;); ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;form action=&amp;quot;index.php&amp;quot; method=&amp;quot;post&amp;quot; name=&amp;quot;adminForm&amp;quot; id=&amp;quot;adminForm&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;div class=&amp;quot;col100&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;fieldset class=&amp;quot;adminform&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;legend&amp;gt;&amp;lt;?php echo JText::_( &#039;Details&#039; ); ?&amp;gt;&amp;lt;/legend&amp;gt;&lt;br /&gt;
        &amp;lt;table class=&amp;quot;admintable&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;tr&amp;gt;&lt;br /&gt;
            &amp;lt;td width=&amp;quot;100&amp;quot; align=&amp;quot;right&amp;quot; class=&amp;quot;key&amp;quot;&amp;gt;&lt;br /&gt;
                &amp;lt;label for=&amp;quot;greeting&amp;quot;&amp;gt;&lt;br /&gt;
                    &amp;lt;?php echo JText::_( &#039;Greeting&#039; ); ?&amp;gt;:&lt;br /&gt;
                &amp;lt;/label&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;input class=&amp;quot;text_area&amp;quot; type=&amp;quot;text&amp;quot; name=&amp;quot;greeting&amp;quot; id=&amp;quot;greeting&amp;quot; size=&amp;quot;32&amp;quot; maxlength=&amp;quot;250&amp;quot; value=&amp;quot;&amp;lt;?php echo $this-&amp;gt;hello-&amp;gt;greeting;?&amp;gt;&amp;quot; /&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;&lt;br /&gt;
    &amp;lt;/table&amp;gt;&lt;br /&gt;
    &amp;lt;/fieldset&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;clr&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;option&amp;quot; value=&amp;quot;com_hello&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;id&amp;quot; value=&amp;quot;&amp;lt;?php echo $this-&amp;gt;hello-&amp;gt;id; ?&amp;gt;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;task&amp;quot; value=&amp;quot;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;controller&amp;quot; value=&amp;quot;hello&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/form&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in addition to the input field, there is a hidden field for the id. The user doesn&#039;t need to edit the id (and shouldn&#039;t), so we silently pass it along in the form.&lt;br /&gt;
&lt;br /&gt;
==== Implementing the Functionality ====&lt;br /&gt;
&lt;br /&gt;
So far, our controller only handles two tasks: edit and new. However, we also have buttons to save, delete and cancel records. We need to write code to handle and perform these tasks.&lt;br /&gt;
&lt;br /&gt;
=== Saving a Record ===&lt;br /&gt;
&lt;br /&gt;
The logical next step is to implement the functionality to save a record. Normally, this would require some switches and logic to handle various cases, such as the difference between creating a new record (an INSERT query), and updating an existing query (an UPDATE query). Also, there are complexities involved in getting the data from the form and putting it into the query.&lt;br /&gt;
&lt;br /&gt;
The Joomla! framework takes care of a lot of this for you. The JTable class makes it easy to manipulate records in the database without having to worry about writing the SQL code that lies behind these updates. It also makes it easy to transfer data from an HTML form into the database.&lt;br /&gt;
&lt;br /&gt;
== Creating the Table Class ==&lt;br /&gt;
&lt;br /&gt;
The JTable class is an abstract class from which you can derive child classes to work with specific tables. To use it, you simply create a class that extends the JTable class, add your database fields as properties, and override the constructor to specify the name of the table and the primary key.&lt;br /&gt;
&lt;br /&gt;
Here is our JTable class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * Hello World table class&lt;br /&gt;
 * &lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_4&lt;br /&gt;
 * @license        GNU/GPL&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// No direct access&lt;br /&gt;
defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;);&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Hello Table class&lt;br /&gt;
 *&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 */&lt;br /&gt;
class TableHello extends JTable&lt;br /&gt;
{&lt;br /&gt;
    /**&lt;br /&gt;
     * Primary Key&lt;br /&gt;
     *&lt;br /&gt;
     * @var int&lt;br /&gt;
     */&lt;br /&gt;
    var $id = null;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * @var string&lt;br /&gt;
     */&lt;br /&gt;
    var $greeting = null;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Constructor&lt;br /&gt;
     *&lt;br /&gt;
     * @param object Database connector object&lt;br /&gt;
     */&lt;br /&gt;
    function TableHello( &amp;amp;$db ) {&lt;br /&gt;
        parent::__construct(&#039;#__hello&#039;, &#039;id&#039;, $db);&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You will see here that we have defined our two fields: the id field and the greeting field. Then we have defined a constructor, which will call the constructor of the parent class and pass it the name of the table (hello), the name of the field which is the primary key (id), and the database connector object.&lt;br /&gt;
&lt;br /&gt;
This file should be called hello.php and it will go in a directory called tables in the administrator section of our component.&lt;br /&gt;
&lt;br /&gt;
== Implementing the Function in our Model ==&lt;br /&gt;
&lt;br /&gt;
We are now ready to add the method to the model which will save our record. We will call this method store. Our store() method will do three things: bind the data from the form to the TableHello object, check to ensure that the record is properly formed, and store the record in the database.&lt;br /&gt;
&lt;br /&gt;
Our method looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Method to store a record&lt;br /&gt;
 *&lt;br /&gt;
 * @access    public&lt;br /&gt;
 * @return    boolean    True on success&lt;br /&gt;
 */&lt;br /&gt;
function store()&lt;br /&gt;
{&lt;br /&gt;
    $row =&amp;amp; $this-&amp;gt;getTable();&lt;br /&gt;
&lt;br /&gt;
    $data = JRequest::get( &#039;post&#039; );&lt;br /&gt;
    // Bind the form fields to the hello table&lt;br /&gt;
    if (!$row-&amp;gt;bind($data)) {&lt;br /&gt;
        $this-&amp;gt;setError($this-&amp;gt;_db-&amp;gt;getErrorMsg());&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // Make sure the hello record is valid&lt;br /&gt;
    if (!$row-&amp;gt;check()) {&lt;br /&gt;
        $this-&amp;gt;setError($this-&amp;gt;_db-&amp;gt;getErrorMsg());&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // Store the web link table to the database&lt;br /&gt;
    if (!$row-&amp;gt;store()) {&lt;br /&gt;
        $this-&amp;gt;setError($this-&amp;gt;_db-&amp;gt;getErrorMsg());&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return true;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This method gets added to the hello model.&lt;br /&gt;
&lt;br /&gt;
The method takes one parameter, which is an associative array of data that we want to store in the database. This can easily be retrieved from the request as will be seen later.&lt;br /&gt;
&lt;br /&gt;
You will see that the first line retrieves a reference to our JTable object. If we name our table properly, we don&#039;t have to specify its name - the JModel class knows where to find it. You may recall that we called our table class TableHello and put it in a file called hello.php in the tables directory. If you follow this convention, the JModel class can create your object automatically.&lt;br /&gt;
&lt;br /&gt;
The second line will retrieve the data from the form. The JRequest class makes this very easy. In this case, we are retrieving all of the variables that were submitted using the &#039;POST&#039; method. These will be returned as an associative array.&lt;br /&gt;
&lt;br /&gt;
The rest is easy - we bind, check and store. The bind() method will copy values from the array into the corresponding property of the table object. In this case, it will take the values of id and greeting and copy them to our TableHello object.&lt;br /&gt;
&lt;br /&gt;
The check() method will perform data verification. In the JTable() class, this method simply returns true. While this doesn&#039;t provide any value for us currently, by calling this method we make it possible to do data checking using our TableHello class in the future. This method can be overridden in our TableHello class with a method that performs the appropriate checks.&lt;br /&gt;
&lt;br /&gt;
The store() method will take the data that is in the object and store it in the database. If the id is 0, it will create a new record (INSERT), otherwise, it will update the existing record (UPDATE).&lt;br /&gt;
&lt;br /&gt;
== Adding the Task to the Controller ==&lt;br /&gt;
&lt;br /&gt;
We are now ready to add our task to the controller. Since the task that we are firing is called &#039;save&#039;, we must call our method &#039;save&#039;. This is simple:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * save a record (and redirect to main page)&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function save()&lt;br /&gt;
{&lt;br /&gt;
    $model = $this-&amp;gt;getModel(&#039;hello&#039;);&lt;br /&gt;
&lt;br /&gt;
    if ($model-&amp;gt;store()) {&lt;br /&gt;
        $msg = JText::_( &#039;Greeting Saved!&#039; );&lt;br /&gt;
    } else {&lt;br /&gt;
        $msg = JText::_( &#039;Error Saving Greeting&#039; );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // Check the table in so it can be edited.... we are done with it anyway&lt;br /&gt;
    $link = &#039;index.php?option=com_hello&#039;;&lt;br /&gt;
    $this-&amp;gt;setRedirect($link, $msg);&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All we do is get our model and invoke the store() method. Then we use the setRedirect() method to redirect back to our list of greetings. We also pass a message along, which will be displayed at the top of the page.&lt;br /&gt;
&lt;br /&gt;
=== Deleting a Record ===&lt;br /&gt;
&lt;br /&gt;
=== Implementing the Function in the Model ===&lt;br /&gt;
&lt;br /&gt;
In the model, we will retrieve the list of IDs to delete and call the JTable class to delete them. Here it is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Method to delete record(s)&lt;br /&gt;
 *&lt;br /&gt;
 * @access    public&lt;br /&gt;
 * @return    boolean    True on success&lt;br /&gt;
 */&lt;br /&gt;
function delete()&lt;br /&gt;
{&lt;br /&gt;
    $cids = JRequest::getVar( &#039;cid&#039;, array(0), &#039;post&#039;, &#039;array&#039; );&lt;br /&gt;
    $row =&amp;amp; $this-&amp;gt;getTable();&lt;br /&gt;
&lt;br /&gt;
    foreach($cids as $cid) {&lt;br /&gt;
        if (!$row-&amp;gt;delete( $cid )) {&lt;br /&gt;
            $this-&amp;gt;setError( $row-&amp;gt;getErrorMsg() );&lt;br /&gt;
            return false;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return true;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We invoke the JRequest::getVar() method to get the data from the request, then we invoke the $row-&amp;gt;delete() method to delete each row. By storing errors in the model we make it possible to retrieve them later if we so choose.&lt;br /&gt;
&lt;br /&gt;
=== Handling the Remove Task in the Controller ===&lt;br /&gt;
&lt;br /&gt;
This is similar to the save() method which handled the save task:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * remove record(s)&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function remove()&lt;br /&gt;
{&lt;br /&gt;
    $model = $this-&amp;gt;getModel(&#039;hello&#039;);&lt;br /&gt;
    if(!$model-&amp;gt;delete()) {&lt;br /&gt;
        $msg = JText::_( &#039;Error: One or More Greetings Could not be Deleted&#039; );&lt;br /&gt;
    } else {&lt;br /&gt;
        $msg = JText::_( &#039;Greeting(s) Deleted&#039; );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    $this-&amp;gt;setRedirect( &#039;index.php?option=com_hello&#039;, $msg );&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Cancelling the Edit Operation ====&lt;br /&gt;
&lt;br /&gt;
To cancel the edit operation, all we have to do is redirect back to the main view:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * cancel editing a record&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function cancel()&lt;br /&gt;
{&lt;br /&gt;
    $msg = JText::_( &#039;Operation Cancelled&#039; );&lt;br /&gt;
    $this-&amp;gt;setRedirect( &#039;index.php?option=com_hello&#039;, $msg );&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
We have now implemented a basic backend to our component. We are now able to edit the entries that are viewed in the frontend. We have demonstrated the interaction between models, views and controllers. We have shown how the JTable class can be extended to provide easy access to tables in the database. It can also be seen how the JToolBarHelper class can be used to create button bars in components to present a standardized look between components.&lt;br /&gt;
&lt;br /&gt;
== Contributors ==&lt;br /&gt;
* staalanden&lt;br /&gt;
&lt;br /&gt;
== Download ==&lt;br /&gt;
&lt;br /&gt;
The component can be downloaded at: [http://joomlacode.org/gf/download/frsrelease/8104/29402/com_hello4_01.zip com_hello4a_01]&lt;/div&gt;</summary>
		<author><name>Tonie</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Creating_an_Administrator_Interface&amp;diff=9512</id>
		<title>J1.5:Developing a MVC Component/Creating an Administrator Interface</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Creating_an_Administrator_Interface&amp;diff=9512"/>
		<updated>2008-07-27T19:50:10Z</updated>

		<summary type="html">&lt;p&gt;Tonie: /* Creating the Table Class */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
In the first three tutorials, we have developed a MVC component that retrieves its data from a table in the database. Currently, there is no way to add data to the database except to do it manually using another tool. In this tutorial, we will develop an administrator section for our component which will make it possible to manage the entries in the database.&lt;br /&gt;
&lt;br /&gt;
== Creating the Basic Framework ==&lt;br /&gt;
&lt;br /&gt;
The basic framework of the administrator panel is very similar to the site portion. The main entry point for the administrator section of the component is hello.php. This file is identical to the hello.php file that was used in the site portion except the name of the controller it loads will be changed to HellosController. The default controller is also called controller.php and this file is identical to the default controller in the site portion, with the exception that the controller is named HellosController instead of HelloController. This difference is so that JController will by default load the hellos view, which will display a list of our greetings.&lt;br /&gt;
&lt;br /&gt;
Here is the listing for hello.php:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_4&lt;br /&gt;
 * @license    GNU/GPL&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
// No direct access&lt;br /&gt;
&lt;br /&gt;
defined( &#039;_JEXEC&#039; ) or die( &#039;Restricted access&#039; );&lt;br /&gt;
&lt;br /&gt;
// Require the base controller&lt;br /&gt;
&lt;br /&gt;
require_once( JPATH_COMPONENT.DS.&#039;controller.php&#039; );&lt;br /&gt;
&lt;br /&gt;
// Require specific controller if requested&lt;br /&gt;
if($controller = JRequest::getWord(&#039;controller&#039;)) {&lt;br /&gt;
    $path = JPATH_COMPONENT.DS.&#039;controllers&#039;.DS.$controller.&#039;.php&#039;;&lt;br /&gt;
    if (file_exists($path)) {&lt;br /&gt;
        require_once $path;&lt;br /&gt;
    } else {&lt;br /&gt;
        $controller = &#039;&#039;;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Create the controller&lt;br /&gt;
$classname    = &#039;HellosController&#039;.$controller;&lt;br /&gt;
$controller   = new $classname( );&lt;br /&gt;
&lt;br /&gt;
// Perform the Request task&lt;br /&gt;
$controller-&amp;gt;execute( JRequest::getVar( &#039;task&#039; ) );&lt;br /&gt;
&lt;br /&gt;
// Redirect if set by the controller&lt;br /&gt;
$controller-&amp;gt;redirect();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The view and model that we will start with is the hellos view and the hellos model. We will start with the model.&lt;br /&gt;
&lt;br /&gt;
==== The Hellos Model ====&lt;br /&gt;
&lt;br /&gt;
The Hellos Model will be very simple. The only operation that we currently need is the ability to retrieve the list of hellos from the database. This operation will be implemented in a method called getData().&lt;br /&gt;
&lt;br /&gt;
The JModel class has a built in protected method called _getList(). This method can be used to simplify the task of retrieving a list of records from the database. We simply need to pass it the query and it will return the list of records.&lt;br /&gt;
&lt;br /&gt;
At a later point in time, we might want to use our query from within another method. Therefore, we will create a private method called _buildQuery() which will return the query that will be passed to _getList(). This makes it easier to change the query as well since it is localized in one place.&lt;br /&gt;
&lt;br /&gt;
Therefore we need two methods in our class: getData() and _buildQuery().&lt;br /&gt;
&lt;br /&gt;
_buildQuery() simply returns the query. It looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source  lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Returns the query&lt;br /&gt;
 * @return string The query to be used to retrieve the rows from the database&lt;br /&gt;
 */&lt;br /&gt;
function _buildQuery()&lt;br /&gt;
{&lt;br /&gt;
    $query = &#039; SELECT * &#039;&lt;br /&gt;
           . &#039; FROM #__hello &#039;&lt;br /&gt;
    ;&lt;br /&gt;
&lt;br /&gt;
    return $query;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
getData() will obtain the query and retrieve the records from the database. Now it might happen that we need to retrieve this list of data twice in one page load. It would be a waste to have to query the database twice. Therefore, we will have this method store the data in a protected property so that on subsequent requests it can simply return the data it has already retrieved. This property will be called _data.&lt;br /&gt;
&lt;br /&gt;
Here is the getData() method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source  lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Retrieves the hello data&lt;br /&gt;
 * @return array Array of objects containing the data from the database&lt;br /&gt;
 */&lt;br /&gt;
function getData()&lt;br /&gt;
{&lt;br /&gt;
    // Lets load the data if it doesn&#039;t already exist&lt;br /&gt;
    if (empty( $this-&amp;gt;_data ))&lt;br /&gt;
    {&lt;br /&gt;
        $query = $this-&amp;gt;_buildQuery();&lt;br /&gt;
        $this-&amp;gt;_data = $this-&amp;gt;_getList( $query );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return $this-&amp;gt;_data;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The completed model looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * Hellos Model for Hello World Component&lt;br /&gt;
 * &lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_4&lt;br /&gt;
 * @license        GNU/GPL&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// Check to ensure this file is included in Joomla!&lt;br /&gt;
defined(&#039;_JEXEC&#039;) or die();&lt;br /&gt;
&lt;br /&gt;
jimport( &#039;joomla.application.component.model&#039; );&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Hello Model&lt;br /&gt;
 *&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 */&lt;br /&gt;
class HellosModelHellos extends JModel&lt;br /&gt;
{&lt;br /&gt;
    /**&lt;br /&gt;
     * Hellos data array&lt;br /&gt;
     *&lt;br /&gt;
     * @var array&lt;br /&gt;
     */&lt;br /&gt;
    var $_data;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Returns the query&lt;br /&gt;
     * @return string The query to be used to retrieve the rows from the database&lt;br /&gt;
     */&lt;br /&gt;
    function _buildQuery()&lt;br /&gt;
    {&lt;br /&gt;
        $query = &#039; SELECT * &#039;&lt;br /&gt;
            . &#039; FROM #__hello &#039;&lt;br /&gt;
        ;&lt;br /&gt;
        return $query;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Retrieves the hello data&lt;br /&gt;
     * @return array Array of objects containing the data from the database&lt;br /&gt;
     */&lt;br /&gt;
    function getData()&lt;br /&gt;
    {&lt;br /&gt;
        // Lets load the data if it doesn&#039;t already exist&lt;br /&gt;
        if (empty( $this-&amp;gt;_data ))&lt;br /&gt;
        {&lt;br /&gt;
            $query = $this-&amp;gt;_buildQuery();&lt;br /&gt;
            $this-&amp;gt;_data = $this-&amp;gt;_getList( $query );&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        return $this-&amp;gt;_data;&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This file is saved as models/hellos.php.&lt;br /&gt;
&lt;br /&gt;
==== The Hellos View ====&lt;br /&gt;
&lt;br /&gt;
Now that we have a model to retrieve our data, we need to display it. This view will be fairly similar to the view from the site section as well.&lt;br /&gt;
&lt;br /&gt;
Just as our model was automatically instantiated in the site, so it is in the administrator. Methods that start with get in the model can be accessed using the get() method of the JView class. So our view has three lines: one to retrieve the data from the model, one to push the data into the template, and one to invoke the display method to display the output. Thus we have:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * Hellos View for Hello World Component&lt;br /&gt;
 * &lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_4&lt;br /&gt;
 * @license        GNU/GPL&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// Check to ensure this file is included in Joomla!&lt;br /&gt;
defined(&#039;_JEXEC&#039;) or die();&lt;br /&gt;
&lt;br /&gt;
jimport( &#039;joomla.application.component.view&#039; );&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Hellos View&lt;br /&gt;
 *&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 */&lt;br /&gt;
class HellosViewHellos extends JView&lt;br /&gt;
{&lt;br /&gt;
    /**&lt;br /&gt;
     * Hellos view display method&lt;br /&gt;
     * @return void&lt;br /&gt;
     **/&lt;br /&gt;
    function display($tpl = null)&lt;br /&gt;
    {&lt;br /&gt;
        JToolBarHelper::title( JText::_( &#039;Hello Manager&#039; ), &#039;generic.png&#039; );&lt;br /&gt;
        JToolBarHelper::deleteList();&lt;br /&gt;
        JToolBarHelper::editListX();&lt;br /&gt;
        JToolBarHelper::addNewX();&lt;br /&gt;
&lt;br /&gt;
        // Get data from the model&lt;br /&gt;
        $items =&amp;amp; $this-&amp;gt;get( &#039;Data&#039;);&lt;br /&gt;
&lt;br /&gt;
        $this-&amp;gt;assignRef( &#039;items&#039;, $items );&lt;br /&gt;
&lt;br /&gt;
        parent::display($tpl);&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This file is saved as views/hellos/view.html.php.&lt;br /&gt;
&lt;br /&gt;
==== The Hellos Template ====&lt;br /&gt;
&lt;br /&gt;
The template will take the data pushed into it from the view and produce the output. We will display our output in a simple table. While the frontend template was very simple, in the administrator we will need a minimal amount of extra logic to handle looping through the data.&lt;br /&gt;
&lt;br /&gt;
Here is our template:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;); ?&amp;gt;&lt;br /&gt;
&amp;lt;form action=&amp;quot;index.php&amp;quot; method=&amp;quot;post&amp;quot; name=&amp;quot;adminForm&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;div id=&amp;quot;editcell&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;table class=&amp;quot;adminlist&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;thead&amp;gt;&lt;br /&gt;
        &amp;lt;tr&amp;gt;&lt;br /&gt;
            &amp;lt;th width=&amp;quot;5&amp;quot;&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo JText::_( &#039;ID&#039; ); ?&amp;gt;&lt;br /&gt;
            &amp;lt;/th&amp;gt;&lt;br /&gt;
            &amp;lt;th&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo JText::_( &#039;Greeting&#039; ); ?&amp;gt;&lt;br /&gt;
            &amp;lt;/th&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;            &lt;br /&gt;
    &amp;lt;/thead&amp;gt;&lt;br /&gt;
    &amp;lt;?php&lt;br /&gt;
    $k = 0;&lt;br /&gt;
    for ($i=0, $n=count( $this-&amp;gt;items ); $i &amp;lt; $n; $i++)&lt;br /&gt;
    {&lt;br /&gt;
        $row =&amp;amp; $this-&amp;gt;items[$i];&lt;br /&gt;
        ?&amp;gt;&lt;br /&gt;
        &amp;lt;tr class=&amp;quot;&amp;lt;?php echo &amp;quot;row$k&amp;quot;; ?&amp;gt;&amp;quot;&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo $row-&amp;gt;id; ?&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo $row-&amp;gt;greeting; ?&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;&lt;br /&gt;
        &amp;lt;?php&lt;br /&gt;
        $k = 1 - $k;&lt;br /&gt;
    }&lt;br /&gt;
    ?&amp;gt;&lt;br /&gt;
    &amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;option&amp;quot; value=&amp;quot;com_hello&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;task&amp;quot; value=&amp;quot;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;boxchecked&amp;quot; value=&amp;quot;0&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;controller&amp;quot; value=&amp;quot;hello&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/form&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This template is saved as views/hellos/tmpl/default.php.&lt;br /&gt;
&lt;br /&gt;
You will notice that our output is enclosed in a form. Though this is not necessary now, it will be soon.&lt;br /&gt;
&lt;br /&gt;
We have now completed the basic part of the first view. We have added five files to the admin section of our component:&lt;br /&gt;
&lt;br /&gt;
* hello.php&lt;br /&gt;
* controller.php&lt;br /&gt;
* models/hellos.php&lt;br /&gt;
* views/hellos/view.html.php&lt;br /&gt;
* views/hellos/tmpl/default.php&lt;br /&gt;
&lt;br /&gt;
You can now add these files to the XML install file and give it a try!&lt;br /&gt;
&lt;br /&gt;
== Adding Functionality ==&lt;br /&gt;
&lt;br /&gt;
So far our administrator section is pretty useless. It doesn&#039;t really do anything - all it does is display the entries that we have in our database.&lt;br /&gt;
&lt;br /&gt;
In order to make it useful, we need to add some buttons and links.&lt;br /&gt;
&lt;br /&gt;
==== The Toolbar ====&lt;br /&gt;
&lt;br /&gt;
You may have noticed the toolbar that appears at the top of other Joomla! component administrator panels. Our component needs one as well. Joomla! makes this very easy to do. We will add buttons Delete records, Edit records, and create New records. We will also add a title that will be displayed on our toolbar.&lt;br /&gt;
&lt;br /&gt;
This is done by adding code to the view. To add the buttons, we use static methods from the Joomla! JToolBarHelper class. The code looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;JToolBarHelper::title(   JText::_( &#039;Hello Manager&#039; ), &#039;generic.png&#039; );&lt;br /&gt;
JToolBarHelper::deleteList();&lt;br /&gt;
JToolBarHelper::editListX();&lt;br /&gt;
JToolBarHelper::addNewX();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
These three methods will create the appropriate buttons. The deleteList() method can optionally take up to three parameters - the first parameter is a string to display to the user to confirm that they want to delete the records. The second is the task that should be sent with the query (the default is &#039;remove&#039;), and the third is the text that should be displayed below the button.&lt;br /&gt;
&lt;br /&gt;
The editListX() and addNewX() methods can each take two optional parameters. The first is the task (which are by default edit and add, respectively), and the second is the text that should be displayed below the button.&lt;br /&gt;
&lt;br /&gt;
*You may have noticed the use of the JText::_ method in the template before and here as well. This is a handy function that makes component translation much easier. The JText::_ method will look up the string in your component language file and return the translated string. If not translation is found, it will return the string that you passed it. If you want to translate your component into another language, all you have to do is create a language file that will map the strings within the quotes to the translated version of the string.&lt;br /&gt;
&lt;br /&gt;
==== Checkboxes and Links ====&lt;br /&gt;
&lt;br /&gt;
We now have buttons. Two of those buttons operate on existing records. But how do we know which records to operate on? We have to let the user tell us. To do this, we need to add checkboxes to our table so that the user can select certain records. This is done in our template.&lt;br /&gt;
&lt;br /&gt;
In order to the add the checkboxes, we need to add an extra column into our table. We will add the column in between the two that we already have.&lt;br /&gt;
&lt;br /&gt;
In the header of the column, we will add a checkbox which can be used to toggle all the boxes below it on or off:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;th width=&amp;quot;20&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;input type=&amp;quot;checkbox&amp;quot; name=&amp;quot;toggle&amp;quot; value=&amp;quot;&amp;quot; onclick=&amp;quot;checkAll(&amp;lt;?php echo count( $this-&amp;gt;items ); ?&amp;gt;);&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/th&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Javascript checkAll function is a function that is built into the Joomla! base Javascript package that provides the functionality that we want here.&lt;br /&gt;
&lt;br /&gt;
Now we need to add the checkboxes into the individual rows. Joomla!&#039;s JHTML class has a method, JHTML::_(), which will generate our checkbox for us. We will add the following line to our loop:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$checked    = JHTML::_( &#039;grid.id&#039;, $i, $row-&amp;gt;id );&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
after the line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$row =&amp;amp; $this-&amp;gt;items[$i];&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then we will add a cell in between the two that we already have:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;td&amp;gt;&lt;br /&gt;
    &amp;lt;?php echo $checked; ?&amp;gt;&lt;br /&gt;
&amp;lt;/td&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It can be cumbersome to have to check the box that we want to edit and then move up and click the edit button. Therefore, we will add a link that it will go straight to the greeting&#039;s edit form. We will add the following line after the call to the JHTML::_() method to generate the link HTML:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$link = JRoute::_( &#039;index.php?option=com_hello&amp;gt;controller=hello&amp;gt;task=edit&amp;gt;cid[]=&#039;. $row-&amp;gt;id );&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And we include the link in the cell showing the greeting text:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;td&amp;gt;&lt;br /&gt;
    &amp;lt;a href=&amp;quot;&amp;lt;?php echo $link; ?&amp;gt;&amp;quot;&amp;gt;&amp;lt;?php echo $row-&amp;gt;greeting; ?&amp;gt;&amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;lt;/td&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You will notice that this link points to the hello controller. This controller will handle the data manipulation of our greetings.&lt;br /&gt;
&lt;br /&gt;
If you recall from above, we had four hidden input fields at the bottom of our form. The first input field was named &#039;option&#039;. This field is necessary so that we stay in our component. The second input field was task. This form property gets set when one of the buttons in the toolbar is clicked. A Javascript error will result and the buttons will not work if this input field is omitted. The third input field is the boxchecked field. This field keeps track of the number of boxes that are checked. The edit and delete buttons will check to ensure that this is greater than zero and will not allow the form to be submitted if it is not. The fourth input field is the controller field. This is used to specify that tasks fired from this form will be handled by the hello controller.&lt;br /&gt;
&lt;br /&gt;
Here is the code for the completed default.php file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;); ?&amp;gt;&lt;br /&gt;
&amp;lt;form action=&amp;quot;index.php&amp;quot; method=&amp;quot;post&amp;quot; name=&amp;quot;adminForm&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;div id=&amp;quot;editcell&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;table class=&amp;quot;adminlist&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;thead&amp;gt;&lt;br /&gt;
        &amp;lt;tr&amp;gt;&lt;br /&gt;
            &amp;lt;th width=&amp;quot;5&amp;quot;&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo JText::_( &#039;ID&#039; ); ?&amp;gt;&lt;br /&gt;
            &amp;lt;/th&amp;gt;&lt;br /&gt;
            &amp;lt;th&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo JText::_( &#039;Greeting&#039; ); ?&amp;gt;&lt;br /&gt;
            &amp;lt;/th&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;            &lt;br /&gt;
    &amp;lt;/thead&amp;gt;&lt;br /&gt;
    &amp;lt;?php&lt;br /&gt;
    $k = 0;&lt;br /&gt;
    for ($i=0, $n=count( $this-&amp;gt;items ); $i &amp;lt; $n; $i++)&lt;br /&gt;
    {&lt;br /&gt;
        $row =&amp;amp; $this-&amp;gt;items[$i];&lt;br /&gt;
        ?&amp;gt;&lt;br /&gt;
        &amp;lt;tr class=&amp;quot;&amp;lt;?php echo &amp;quot;row$k&amp;quot;; ?&amp;gt;&amp;quot;&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo $row-&amp;gt;id; ?&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo $row-&amp;gt;greeting; ?&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;&lt;br /&gt;
        &amp;lt;?php&lt;br /&gt;
        $k = 1 - $k;&lt;br /&gt;
    }&lt;br /&gt;
    ?&amp;gt;&lt;br /&gt;
    &amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;option&amp;quot; value=&amp;quot;com_hello&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;task&amp;quot; value=&amp;quot;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;boxchecked&amp;quot; value=&amp;quot;0&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;controller&amp;quot; value=&amp;quot;hello&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/form&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Our hellos view is now complete.&lt;br /&gt;
&lt;br /&gt;
== Getting Down and Dirty: Doing the Real Work ==&lt;br /&gt;
&lt;br /&gt;
Now that the Hellos view is done, it is time to move to the Hello view and model. This is where the real work will get done.&lt;br /&gt;
&lt;br /&gt;
==== The Hello Controller ====&lt;br /&gt;
&lt;br /&gt;
Our default controller just isn&#039;t going to cut it when it comes to doing work - all it is capable of doing is displaying views.&lt;br /&gt;
&lt;br /&gt;
We need to be able to handle the tasks that we are launching from the Hellos view: add, edit and remove.&lt;br /&gt;
&lt;br /&gt;
Add and edit are essentially the same task: they both display a form to the user that allows a greeting to be edited. The only difference is that new displays a blank form, and edit displays a form with data already in it. Since they are similar, we will map the add task onto the edit task handler. This is specified in our constructor:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * constructor (registers additional tasks to methods)&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function __construct()&lt;br /&gt;
{&lt;br /&gt;
    parent::__construct();&lt;br /&gt;
&lt;br /&gt;
    // Register Extra tasks&lt;br /&gt;
    $this-&amp;gt;registerTask( &#039;add&#039;  ,     &#039;edit&#039; );&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first parameter of JController::registerTask is the task to map, and the second is the method to map it to.&lt;br /&gt;
&lt;br /&gt;
We will start with handling the edit task. The controller&#039;s job is fairly simple for the edit task. All it has to do is specify the view and layout to load (the hello view and the form layout). We will also tell Joomla! to disable the mainmenu while we are editing our greeting. This prevents users from leaving unsaved records open.&lt;br /&gt;
&lt;br /&gt;
Our edit task handler looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * display the edit form&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function edit()&lt;br /&gt;
{&lt;br /&gt;
    JRequest::setVar( &#039;view&#039;, &#039;hello&#039; );&lt;br /&gt;
    JRequest::setVar( &#039;layout&#039;, &#039;form&#039;  );&lt;br /&gt;
    JRequest::setVar(&#039;hidemainmenu&#039;, 1);&lt;br /&gt;
&lt;br /&gt;
    parent::display();&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== The Hello View ====&lt;br /&gt;
&lt;br /&gt;
The Hello view will display a form which will allow the user to edit a greeting. The display method if the hello view has to do a few simple tasks:&lt;br /&gt;
&lt;br /&gt;
* retrieve the data from the model&lt;br /&gt;
* create the toolbar&lt;br /&gt;
* pass the data into the template&lt;br /&gt;
* invoke the display() method to render the template&lt;br /&gt;
&lt;br /&gt;
This becomes a bit more complicated because the one view handles both the edit and add tasks. In our toolbar we want the user to know what whether they are adding or editing, so we have to determine which task was fired.&lt;br /&gt;
&lt;br /&gt;
Since we are already retrieving the record that we want to display from the model, we can use this data to determine what task was fired. If the task was edit, then the id field of our record will have been set. If the task was new, then it will not have been set. This can be used to determine if we have a new record or an existing record.&lt;br /&gt;
&lt;br /&gt;
We will add two buttons to the toolbar: save and cancel. Though the functionality will be the same, we want to display different buttons depending on whether it is a new or existing record. If it is a new record, we will display cancel. If it already exists, we will display close.&lt;br /&gt;
&lt;br /&gt;
Thus our display method looks like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * display method of Hello view&lt;br /&gt;
 * @return void&lt;br /&gt;
 **/&lt;br /&gt;
function display($tpl = null)&lt;br /&gt;
{&lt;br /&gt;
    //get the hello&lt;br /&gt;
    $hello        =&amp;amp; $this-&amp;gt;get(&#039;Data&#039;);&lt;br /&gt;
    $isNew        = ($hello-&amp;gt;id &amp;lt; 1);&lt;br /&gt;
&lt;br /&gt;
    $text = $isNew ? JText::_( &#039;New&#039; ) : JText::_( &#039;Edit&#039; );&lt;br /&gt;
    JToolBarHelper::title(   JText::_( &#039;Hello&#039; ).&#039;: &amp;lt;small&amp;gt;&amp;lt;small&amp;gt;[ &#039; . $text.&#039; ]&amp;lt;/small&amp;gt;&amp;lt;/small&amp;gt;&#039; );&lt;br /&gt;
    JToolBarHelper::save();&lt;br /&gt;
    if ($isNew)  {&lt;br /&gt;
        JToolBarHelper::cancel();&lt;br /&gt;
    } else {&lt;br /&gt;
        // for existing items the button is renamed `close`&lt;br /&gt;
        JToolBarHelper::cancel( &#039;cancel&#039;, &#039;Close&#039; );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    $this-&amp;gt;assignRef(&#039;hello&#039;, $hello);&lt;br /&gt;
    parent::display($tpl);&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== The Hello Model ====&lt;br /&gt;
&lt;br /&gt;
Our view needs data. Therefore, we need to create a model to model a hello.&lt;br /&gt;
&lt;br /&gt;
Our model will have two properties: _id and _data. _id will hold the id of the greeting and data will hold the data.&lt;br /&gt;
&lt;br /&gt;
We will start with a constructor, which will attempt to retrieve the id from the query:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Constructor that retrieves the ID from the request&lt;br /&gt;
 *&lt;br /&gt;
 * @access    public&lt;br /&gt;
 * @return    void&lt;br /&gt;
 */&lt;br /&gt;
function __construct()&lt;br /&gt;
{&lt;br /&gt;
    parent::__construct();&lt;br /&gt;
&lt;br /&gt;
    $array = JRequest::getVar(&#039;cid&#039;,  0, &#039;&#039;, &#039;array&#039;);&lt;br /&gt;
    $this-&amp;gt;setId((int)$array[0]);&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The JRequest::getVar() method is used to retrieve data from the request. The first parameter is the name of the form variable. The second parameter is the default value to assign if there is no value found. The third parameter is the name of the hash to retrieve the value from (get, post, etc), and the last value is the data type that should be forced on the value.&lt;br /&gt;
&lt;br /&gt;
Our constructor will take the first value from the cid array and assign it to the id.&lt;br /&gt;
&lt;br /&gt;
Our setId() method can be used to set our id. Changing the id that our model points to will mean the id points to the wrong data. Therefore, when we set the id, we will clear the data property:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Method to set the hello identifier&lt;br /&gt;
 *&lt;br /&gt;
 * @access    public&lt;br /&gt;
 * @param    int Hello identifier&lt;br /&gt;
 * @return    void&lt;br /&gt;
 */&lt;br /&gt;
function setId($id)&lt;br /&gt;
{&lt;br /&gt;
    // Set id and wipe data&lt;br /&gt;
    $this-&amp;gt;_id        = $id;&lt;br /&gt;
    $this-&amp;gt;_data    = null;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finally, we need a method to retrieve our data: getData()&lt;br /&gt;
&lt;br /&gt;
getData will check if the _data property has already been set. If it has, it will simply return it. Otherwise, it will load the data from the database.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Method to get a hello&lt;br /&gt;
 * @return object with data&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
function &amp;amp;getData()&lt;br /&gt;
{&lt;br /&gt;
    // Load the data&lt;br /&gt;
    if (empty( $this-&amp;gt;_data )) {&lt;br /&gt;
        $query = &#039; SELECT * FROM #__hello &#039;.&lt;br /&gt;
                &#039;  WHERE id = &#039;.$this-&amp;gt;_id;&lt;br /&gt;
        $this-&amp;gt;_db-&amp;gt;setQuery( $query );&lt;br /&gt;
        $this-&amp;gt;_data = $this-&amp;gt;_db-&amp;gt;loadObject();&lt;br /&gt;
    }&lt;br /&gt;
    if (!$this-&amp;gt;_data) {&lt;br /&gt;
        $this-&amp;gt;_data = new stdClass();&lt;br /&gt;
        $this-&amp;gt;_data-&amp;gt;id = 0;&lt;br /&gt;
        $this-&amp;gt;_data-&amp;gt;greeting = null;&lt;br /&gt;
    }&lt;br /&gt;
    return $this-&amp;gt;_data;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== The Form ====&lt;br /&gt;
&lt;br /&gt;
Now all that is left is to create the form that the data will go into. Since we specified our layout as form, the form will go in a file in the tmpl directory of the hello view called form.php:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;); ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;form action=&amp;quot;index.php&amp;quot; method=&amp;quot;post&amp;quot; name=&amp;quot;adminForm&amp;quot; id=&amp;quot;adminForm&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;div class=&amp;quot;col100&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;fieldset class=&amp;quot;adminform&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;legend&amp;gt;&amp;lt;?php echo JText::_( &#039;Details&#039; ); ?&amp;gt;&amp;lt;/legend&amp;gt;&lt;br /&gt;
        &amp;lt;table class=&amp;quot;admintable&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;tr&amp;gt;&lt;br /&gt;
            &amp;lt;td width=&amp;quot;100&amp;quot; align=&amp;quot;right&amp;quot; class=&amp;quot;key&amp;quot;&amp;gt;&lt;br /&gt;
                &amp;lt;label for=&amp;quot;greeting&amp;quot;&amp;gt;&lt;br /&gt;
                    &amp;lt;?php echo JText::_( &#039;Greeting&#039; ); ?&amp;gt;:&lt;br /&gt;
                &amp;lt;/label&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;input class=&amp;quot;text_area&amp;quot; type=&amp;quot;text&amp;quot; name=&amp;quot;greeting&amp;quot; id=&amp;quot;greeting&amp;quot; size=&amp;quot;32&amp;quot; maxlength=&amp;quot;250&amp;quot; value=&amp;quot;&amp;lt;?php echo $this-&amp;gt;hello-&amp;gt;greeting;?&amp;gt;&amp;quot; /&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;&lt;br /&gt;
    &amp;lt;/table&amp;gt;&lt;br /&gt;
    &amp;lt;/fieldset&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;clr&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;option&amp;quot; value=&amp;quot;com_hello&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;id&amp;quot; value=&amp;quot;&amp;lt;?php echo $this-&amp;gt;hello-&amp;gt;id; ?&amp;gt;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;task&amp;quot; value=&amp;quot;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;controller&amp;quot; value=&amp;quot;hello&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/form&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in addition to the input field, there is a hidden field for the id. The user doesn&#039;t need to edit the id (and shouldn&#039;t), so we silently pass it along in the form.&lt;br /&gt;
&lt;br /&gt;
==== Implementing the Functionality ====&lt;br /&gt;
&lt;br /&gt;
So far, our controller only handles two tasks: edit and new. However, we also have buttons to save, delete and cancel records. We need to write code to handle and perform these tasks.&lt;br /&gt;
&lt;br /&gt;
=== Saving a Record ===&lt;br /&gt;
&lt;br /&gt;
The logical next step is to implement the functionality to save a record. Normally, this would require some switches and logic to handle various cases, such as the difference between creating a new record (an INSERT query), and updating an existing query (an UPDATE query). Also, there are complexities involved in getting the data from the form and putting it into the query.&lt;br /&gt;
&lt;br /&gt;
The Joomla! framework takes care of a lot of this for you. The JTable class makes it easy to manipulate records in the database without having to worry about writing the SQL code that lies behind these updates. It also makes it easy to transfer data from an HTML form into the database.&lt;br /&gt;
&lt;br /&gt;
== Creating the Table Class ==&lt;br /&gt;
&lt;br /&gt;
The JTable class is an abstract class from which you can derive child classes to work with specific tables. To use it, you simply create a class that extends the JTable class, add your database fields as properties, and override the constructor to specify the name of the table and the primary key.&lt;br /&gt;
&lt;br /&gt;
Here is our JTable class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * Hello World table class&lt;br /&gt;
 * &lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_4&lt;br /&gt;
 * @license        GNU/GPL&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// No direct access&lt;br /&gt;
defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;);&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Hello Table class&lt;br /&gt;
 *&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 */&lt;br /&gt;
class TableHello extends JTable&lt;br /&gt;
{&lt;br /&gt;
    /**&lt;br /&gt;
     * Primary Key&lt;br /&gt;
     *&lt;br /&gt;
     * @var int&lt;br /&gt;
     */&lt;br /&gt;
    var $id = null;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * @var string&lt;br /&gt;
     */&lt;br /&gt;
    var $greeting = null;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Constructor&lt;br /&gt;
     *&lt;br /&gt;
     * @param object Database connector object&lt;br /&gt;
     */&lt;br /&gt;
    function TableHello( &amp;amp;$db ) {&lt;br /&gt;
        parent::__construct(&#039;#__hello&#039;, &#039;id&#039;, $db);&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You will see here that we have defined our two fields: the id field and the greeting field. Then we have defined a constructor, which will call the constructor of the parent class and pass it the name of the table (hello), the name of the field which is the primary key (id), and the database connector object.&lt;br /&gt;
&lt;br /&gt;
This file should be called hello.php and it will go in a directory called tables in the administrator section of our component.&lt;br /&gt;
&lt;br /&gt;
== Implementing the Function in our Model ==&lt;br /&gt;
&lt;br /&gt;
We are now ready to add the method to the model which will save our record. We will call this method store. Our store() method will do three things: bind the data from the form to the TableHello object, check to ensure that the record is properly formed, and store the record in the database.&lt;br /&gt;
&lt;br /&gt;
Our method looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Method to store a record&lt;br /&gt;
 *&lt;br /&gt;
 * @access    public&lt;br /&gt;
 * @return    boolean    True on success&lt;br /&gt;
 */&lt;br /&gt;
function store()&lt;br /&gt;
{&lt;br /&gt;
    $row =&amp;amp; $this-&amp;gt;getTable();&lt;br /&gt;
&lt;br /&gt;
    $data = JRequest::get( &#039;post&#039; );&lt;br /&gt;
    // Bind the form fields to the hello table&lt;br /&gt;
    if (!$row-&amp;gt;bind($data)) {&lt;br /&gt;
        $this-&amp;gt;setError($this-&amp;gt;_db-&amp;gt;getErrorMsg());&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // Make sure the hello record is valid&lt;br /&gt;
    if (!$row-&amp;gt;check()) {&lt;br /&gt;
        $this-&amp;gt;setError($this-&amp;gt;_db-&amp;gt;getErrorMsg());&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // Store the web link table to the database&lt;br /&gt;
    if (!$row-&amp;gt;store()) {&lt;br /&gt;
        $this-&amp;gt;setError($this-&amp;gt;_db-&amp;gt;getErrorMsg());&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return true;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This method gets added to the hello model.&lt;br /&gt;
&lt;br /&gt;
The method takes one parameter, which is an associative array of data that we want to store in the database. This can easily be retrieved from the request as will be seen later.&lt;br /&gt;
&lt;br /&gt;
You will see that the first line retrieves a reference to our JTable object. If we name our table properly, we don&#039;t have to specify its name - the JModel class knows where to find it. You may recall that we called our table class TableHello and put it in a file called hello.php in the tables directory. If you follow this convention, the JModel class can create your object automatically.&lt;br /&gt;
&lt;br /&gt;
The second line will retrieve the data from the form. The JRequest class makes this very easy. In this case, we are retrieving all of the variables that were submitted using the &#039;POST&#039; method. These will be returned as an associative array.&lt;br /&gt;
&lt;br /&gt;
The rest is easy - we bind, check and store. The bind() method will copy values from the array into the corresponding property of the table object. In this case, it will take the values of id and greeting and copy them to our TableHello object.&lt;br /&gt;
&lt;br /&gt;
The check() method will perform data verification. In the JTable() class, this method simply returns true. While this doesn&#039;t provide any value for us currently, by calling this method we make it possible to do data checking using our TableHello class in the future. This method can be overridden in our TableHello class with a method that performs the appropriate checks.&lt;br /&gt;
&lt;br /&gt;
The store() method will take the data that is in the object and store it in the database. If the id is 0, it will create a new record (INSERT), otherwise, it will update the existing record (UPDATE).&lt;br /&gt;
&lt;br /&gt;
== Adding the Task to the Controller ==&lt;br /&gt;
&lt;br /&gt;
We are now ready to add our task to the controller. Since the task that we are firing is called &#039;save&#039;, we must call our method &#039;save&#039;. This is simple:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * save a record (and redirect to main page)&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function save()&lt;br /&gt;
{&lt;br /&gt;
    $model = $this-&amp;gt;getModel(&#039;hello&#039;);&lt;br /&gt;
&lt;br /&gt;
    if ($model-&amp;gt;store()) {&lt;br /&gt;
        $msg = JText::_( &#039;Greeting Saved!&#039; );&lt;br /&gt;
    } else {&lt;br /&gt;
        $msg = JText::_( &#039;Error Saving Greeting&#039; );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // Check the table in so it can be edited.... we are done with it anyway&lt;br /&gt;
    $link = &#039;index.php?option=com_hello&#039;;&lt;br /&gt;
    $this-&amp;gt;setRedirect($link, $msg);&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All we do is get our model and invoke the store() method. Then we use the setRedirect() method to redirect back to our list of greetings. We also pass a message along, which will be displayed at the top of the page.&lt;br /&gt;
&lt;br /&gt;
=== Deleting a Record ===&lt;br /&gt;
&lt;br /&gt;
=== Implementing the Function in the Model ===&lt;br /&gt;
&lt;br /&gt;
In the model, we will retrieve the list of IDs to delete and call the JTable class to delete them. Here it is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Method to delete record(s)&lt;br /&gt;
 *&lt;br /&gt;
 * @access    public&lt;br /&gt;
 * @return    boolean    True on success&lt;br /&gt;
 */&lt;br /&gt;
function delete()&lt;br /&gt;
{&lt;br /&gt;
    $cids = JRequest::getVar( &#039;cid&#039;, array(0), &#039;post&#039;, &#039;array&#039; );&lt;br /&gt;
    $row =&amp;amp; $this-&amp;gt;getTable();&lt;br /&gt;
&lt;br /&gt;
    foreach($cids as $cid) {&lt;br /&gt;
        if (!$row-&amp;gt;delete( $cid )) {&lt;br /&gt;
            $this-&amp;gt;setError( $row-&amp;gt;getErrorMsg() );&lt;br /&gt;
            return false;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return true;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We invoke the JRequest::getVar() method to get the data from the request, then we invoke the $row-&amp;gt;delete() method to delete each row. By storing errors in the model we make it possible to retrieve them later if we so choose.&lt;br /&gt;
&lt;br /&gt;
=== Handling the Remove Task in the Controller ===&lt;br /&gt;
&lt;br /&gt;
This is similar to the save() method which handled the save task:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * remove record(s)&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function remove()&lt;br /&gt;
{&lt;br /&gt;
    $model = $this-&amp;gt;getModel(&#039;hello&#039;);&lt;br /&gt;
    if(!$model-&amp;gt;delete()) {&lt;br /&gt;
        $msg = JText::_( &#039;Error: One or More Greetings Could not be Deleted&#039; );&lt;br /&gt;
    } else {&lt;br /&gt;
        $msg = JText::_( &#039;Greeting(s) Deleted&#039; );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    $this-&amp;gt;setRedirect( &#039;index.php?option=com_hello&#039;, $msg );&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Cancelling the Edit Operation ====&lt;br /&gt;
&lt;br /&gt;
To cancel the edit operation, all we have to do is redirect back to the main view:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * cancel editing a record&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function cancel()&lt;br /&gt;
{&lt;br /&gt;
    $msg = JText::_( &#039;Operation Cancelled&#039; );&lt;br /&gt;
    $this-&amp;gt;setRedirect( &#039;index.php?option=com_hello&#039;, $msg );&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
We have now implemented a basic backend to our component. We are now able to edit the entries that are viewed in the frontend. We have demonstrated the interaction between models, views and controllers. We have shown how the JTable class can be extended to provide easy access to tables in the database. It can also be seen how the JToolBarHelper class can be used to create button bars in components to present a standardized look between components.&lt;br /&gt;
&lt;br /&gt;
== Contributors ==&lt;br /&gt;
* staalanden&lt;br /&gt;
&lt;br /&gt;
== Download ==&lt;br /&gt;
&lt;br /&gt;
The component can be downloaded at: [http://joomlacode.org/gf/download/frsrelease/8104/29402/com_hello4a_01.zip com_hello4a_01]&lt;/div&gt;</summary>
		<author><name>Tonie</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Creating_an_Administrator_Interface&amp;diff=9511</id>
		<title>J1.5:Developing a MVC Component/Creating an Administrator Interface</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Creating_an_Administrator_Interface&amp;diff=9511"/>
		<updated>2008-07-27T19:49:12Z</updated>

		<summary type="html">&lt;p&gt;Tonie: /* The Hello View */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
In the first three tutorials, we have developed a MVC component that retrieves its data from a table in the database. Currently, there is no way to add data to the database except to do it manually using another tool. In this tutorial, we will develop an administrator section for our component which will make it possible to manage the entries in the database.&lt;br /&gt;
&lt;br /&gt;
== Creating the Basic Framework ==&lt;br /&gt;
&lt;br /&gt;
The basic framework of the administrator panel is very similar to the site portion. The main entry point for the administrator section of the component is hello.php. This file is identical to the hello.php file that was used in the site portion except the name of the controller it loads will be changed to HellosController. The default controller is also called controller.php and this file is identical to the default controller in the site portion, with the exception that the controller is named HellosController instead of HelloController. This difference is so that JController will by default load the hellos view, which will display a list of our greetings.&lt;br /&gt;
&lt;br /&gt;
Here is the listing for hello.php:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_4&lt;br /&gt;
 * @license    GNU/GPL&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
// No direct access&lt;br /&gt;
&lt;br /&gt;
defined( &#039;_JEXEC&#039; ) or die( &#039;Restricted access&#039; );&lt;br /&gt;
&lt;br /&gt;
// Require the base controller&lt;br /&gt;
&lt;br /&gt;
require_once( JPATH_COMPONENT.DS.&#039;controller.php&#039; );&lt;br /&gt;
&lt;br /&gt;
// Require specific controller if requested&lt;br /&gt;
if($controller = JRequest::getWord(&#039;controller&#039;)) {&lt;br /&gt;
    $path = JPATH_COMPONENT.DS.&#039;controllers&#039;.DS.$controller.&#039;.php&#039;;&lt;br /&gt;
    if (file_exists($path)) {&lt;br /&gt;
        require_once $path;&lt;br /&gt;
    } else {&lt;br /&gt;
        $controller = &#039;&#039;;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Create the controller&lt;br /&gt;
$classname    = &#039;HellosController&#039;.$controller;&lt;br /&gt;
$controller   = new $classname( );&lt;br /&gt;
&lt;br /&gt;
// Perform the Request task&lt;br /&gt;
$controller-&amp;gt;execute( JRequest::getVar( &#039;task&#039; ) );&lt;br /&gt;
&lt;br /&gt;
// Redirect if set by the controller&lt;br /&gt;
$controller-&amp;gt;redirect();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The view and model that we will start with is the hellos view and the hellos model. We will start with the model.&lt;br /&gt;
&lt;br /&gt;
==== The Hellos Model ====&lt;br /&gt;
&lt;br /&gt;
The Hellos Model will be very simple. The only operation that we currently need is the ability to retrieve the list of hellos from the database. This operation will be implemented in a method called getData().&lt;br /&gt;
&lt;br /&gt;
The JModel class has a built in protected method called _getList(). This method can be used to simplify the task of retrieving a list of records from the database. We simply need to pass it the query and it will return the list of records.&lt;br /&gt;
&lt;br /&gt;
At a later point in time, we might want to use our query from within another method. Therefore, we will create a private method called _buildQuery() which will return the query that will be passed to _getList(). This makes it easier to change the query as well since it is localized in one place.&lt;br /&gt;
&lt;br /&gt;
Therefore we need two methods in our class: getData() and _buildQuery().&lt;br /&gt;
&lt;br /&gt;
_buildQuery() simply returns the query. It looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source  lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Returns the query&lt;br /&gt;
 * @return string The query to be used to retrieve the rows from the database&lt;br /&gt;
 */&lt;br /&gt;
function _buildQuery()&lt;br /&gt;
{&lt;br /&gt;
    $query = &#039; SELECT * &#039;&lt;br /&gt;
           . &#039; FROM #__hello &#039;&lt;br /&gt;
    ;&lt;br /&gt;
&lt;br /&gt;
    return $query;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
getData() will obtain the query and retrieve the records from the database. Now it might happen that we need to retrieve this list of data twice in one page load. It would be a waste to have to query the database twice. Therefore, we will have this method store the data in a protected property so that on subsequent requests it can simply return the data it has already retrieved. This property will be called _data.&lt;br /&gt;
&lt;br /&gt;
Here is the getData() method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source  lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Retrieves the hello data&lt;br /&gt;
 * @return array Array of objects containing the data from the database&lt;br /&gt;
 */&lt;br /&gt;
function getData()&lt;br /&gt;
{&lt;br /&gt;
    // Lets load the data if it doesn&#039;t already exist&lt;br /&gt;
    if (empty( $this-&amp;gt;_data ))&lt;br /&gt;
    {&lt;br /&gt;
        $query = $this-&amp;gt;_buildQuery();&lt;br /&gt;
        $this-&amp;gt;_data = $this-&amp;gt;_getList( $query );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return $this-&amp;gt;_data;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The completed model looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * Hellos Model for Hello World Component&lt;br /&gt;
 * &lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_4&lt;br /&gt;
 * @license        GNU/GPL&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// Check to ensure this file is included in Joomla!&lt;br /&gt;
defined(&#039;_JEXEC&#039;) or die();&lt;br /&gt;
&lt;br /&gt;
jimport( &#039;joomla.application.component.model&#039; );&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Hello Model&lt;br /&gt;
 *&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 */&lt;br /&gt;
class HellosModelHellos extends JModel&lt;br /&gt;
{&lt;br /&gt;
    /**&lt;br /&gt;
     * Hellos data array&lt;br /&gt;
     *&lt;br /&gt;
     * @var array&lt;br /&gt;
     */&lt;br /&gt;
    var $_data;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Returns the query&lt;br /&gt;
     * @return string The query to be used to retrieve the rows from the database&lt;br /&gt;
     */&lt;br /&gt;
    function _buildQuery()&lt;br /&gt;
    {&lt;br /&gt;
        $query = &#039; SELECT * &#039;&lt;br /&gt;
            . &#039; FROM #__hello &#039;&lt;br /&gt;
        ;&lt;br /&gt;
        return $query;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Retrieves the hello data&lt;br /&gt;
     * @return array Array of objects containing the data from the database&lt;br /&gt;
     */&lt;br /&gt;
    function getData()&lt;br /&gt;
    {&lt;br /&gt;
        // Lets load the data if it doesn&#039;t already exist&lt;br /&gt;
        if (empty( $this-&amp;gt;_data ))&lt;br /&gt;
        {&lt;br /&gt;
            $query = $this-&amp;gt;_buildQuery();&lt;br /&gt;
            $this-&amp;gt;_data = $this-&amp;gt;_getList( $query );&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        return $this-&amp;gt;_data;&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This file is saved as models/hellos.php.&lt;br /&gt;
&lt;br /&gt;
==== The Hellos View ====&lt;br /&gt;
&lt;br /&gt;
Now that we have a model to retrieve our data, we need to display it. This view will be fairly similar to the view from the site section as well.&lt;br /&gt;
&lt;br /&gt;
Just as our model was automatically instantiated in the site, so it is in the administrator. Methods that start with get in the model can be accessed using the get() method of the JView class. So our view has three lines: one to retrieve the data from the model, one to push the data into the template, and one to invoke the display method to display the output. Thus we have:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * Hellos View for Hello World Component&lt;br /&gt;
 * &lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_4&lt;br /&gt;
 * @license        GNU/GPL&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// Check to ensure this file is included in Joomla!&lt;br /&gt;
defined(&#039;_JEXEC&#039;) or die();&lt;br /&gt;
&lt;br /&gt;
jimport( &#039;joomla.application.component.view&#039; );&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Hellos View&lt;br /&gt;
 *&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 */&lt;br /&gt;
class HellosViewHellos extends JView&lt;br /&gt;
{&lt;br /&gt;
    /**&lt;br /&gt;
     * Hellos view display method&lt;br /&gt;
     * @return void&lt;br /&gt;
     **/&lt;br /&gt;
    function display($tpl = null)&lt;br /&gt;
    {&lt;br /&gt;
        JToolBarHelper::title( JText::_( &#039;Hello Manager&#039; ), &#039;generic.png&#039; );&lt;br /&gt;
        JToolBarHelper::deleteList();&lt;br /&gt;
        JToolBarHelper::editListX();&lt;br /&gt;
        JToolBarHelper::addNewX();&lt;br /&gt;
&lt;br /&gt;
        // Get data from the model&lt;br /&gt;
        $items =&amp;amp; $this-&amp;gt;get( &#039;Data&#039;);&lt;br /&gt;
&lt;br /&gt;
        $this-&amp;gt;assignRef( &#039;items&#039;, $items );&lt;br /&gt;
&lt;br /&gt;
        parent::display($tpl);&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This file is saved as views/hellos/view.html.php.&lt;br /&gt;
&lt;br /&gt;
==== The Hellos Template ====&lt;br /&gt;
&lt;br /&gt;
The template will take the data pushed into it from the view and produce the output. We will display our output in a simple table. While the frontend template was very simple, in the administrator we will need a minimal amount of extra logic to handle looping through the data.&lt;br /&gt;
&lt;br /&gt;
Here is our template:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;); ?&amp;gt;&lt;br /&gt;
&amp;lt;form action=&amp;quot;index.php&amp;quot; method=&amp;quot;post&amp;quot; name=&amp;quot;adminForm&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;div id=&amp;quot;editcell&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;table class=&amp;quot;adminlist&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;thead&amp;gt;&lt;br /&gt;
        &amp;lt;tr&amp;gt;&lt;br /&gt;
            &amp;lt;th width=&amp;quot;5&amp;quot;&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo JText::_( &#039;ID&#039; ); ?&amp;gt;&lt;br /&gt;
            &amp;lt;/th&amp;gt;&lt;br /&gt;
            &amp;lt;th&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo JText::_( &#039;Greeting&#039; ); ?&amp;gt;&lt;br /&gt;
            &amp;lt;/th&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;            &lt;br /&gt;
    &amp;lt;/thead&amp;gt;&lt;br /&gt;
    &amp;lt;?php&lt;br /&gt;
    $k = 0;&lt;br /&gt;
    for ($i=0, $n=count( $this-&amp;gt;items ); $i &amp;lt; $n; $i++)&lt;br /&gt;
    {&lt;br /&gt;
        $row =&amp;amp; $this-&amp;gt;items[$i];&lt;br /&gt;
        ?&amp;gt;&lt;br /&gt;
        &amp;lt;tr class=&amp;quot;&amp;lt;?php echo &amp;quot;row$k&amp;quot;; ?&amp;gt;&amp;quot;&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo $row-&amp;gt;id; ?&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo $row-&amp;gt;greeting; ?&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;&lt;br /&gt;
        &amp;lt;?php&lt;br /&gt;
        $k = 1 - $k;&lt;br /&gt;
    }&lt;br /&gt;
    ?&amp;gt;&lt;br /&gt;
    &amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;option&amp;quot; value=&amp;quot;com_hello&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;task&amp;quot; value=&amp;quot;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;boxchecked&amp;quot; value=&amp;quot;0&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;controller&amp;quot; value=&amp;quot;hello&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/form&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This template is saved as views/hellos/tmpl/default.php.&lt;br /&gt;
&lt;br /&gt;
You will notice that our output is enclosed in a form. Though this is not necessary now, it will be soon.&lt;br /&gt;
&lt;br /&gt;
We have now completed the basic part of the first view. We have added five files to the admin section of our component:&lt;br /&gt;
&lt;br /&gt;
* hello.php&lt;br /&gt;
* controller.php&lt;br /&gt;
* models/hellos.php&lt;br /&gt;
* views/hellos/view.html.php&lt;br /&gt;
* views/hellos/tmpl/default.php&lt;br /&gt;
&lt;br /&gt;
You can now add these files to the XML install file and give it a try!&lt;br /&gt;
&lt;br /&gt;
== Adding Functionality ==&lt;br /&gt;
&lt;br /&gt;
So far our administrator section is pretty useless. It doesn&#039;t really do anything - all it does is display the entries that we have in our database.&lt;br /&gt;
&lt;br /&gt;
In order to make it useful, we need to add some buttons and links.&lt;br /&gt;
&lt;br /&gt;
==== The Toolbar ====&lt;br /&gt;
&lt;br /&gt;
You may have noticed the toolbar that appears at the top of other Joomla! component administrator panels. Our component needs one as well. Joomla! makes this very easy to do. We will add buttons Delete records, Edit records, and create New records. We will also add a title that will be displayed on our toolbar.&lt;br /&gt;
&lt;br /&gt;
This is done by adding code to the view. To add the buttons, we use static methods from the Joomla! JToolBarHelper class. The code looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;JToolBarHelper::title(   JText::_( &#039;Hello Manager&#039; ), &#039;generic.png&#039; );&lt;br /&gt;
JToolBarHelper::deleteList();&lt;br /&gt;
JToolBarHelper::editListX();&lt;br /&gt;
JToolBarHelper::addNewX();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
These three methods will create the appropriate buttons. The deleteList() method can optionally take up to three parameters - the first parameter is a string to display to the user to confirm that they want to delete the records. The second is the task that should be sent with the query (the default is &#039;remove&#039;), and the third is the text that should be displayed below the button.&lt;br /&gt;
&lt;br /&gt;
The editListX() and addNewX() methods can each take two optional parameters. The first is the task (which are by default edit and add, respectively), and the second is the text that should be displayed below the button.&lt;br /&gt;
&lt;br /&gt;
*You may have noticed the use of the JText::_ method in the template before and here as well. This is a handy function that makes component translation much easier. The JText::_ method will look up the string in your component language file and return the translated string. If not translation is found, it will return the string that you passed it. If you want to translate your component into another language, all you have to do is create a language file that will map the strings within the quotes to the translated version of the string.&lt;br /&gt;
&lt;br /&gt;
==== Checkboxes and Links ====&lt;br /&gt;
&lt;br /&gt;
We now have buttons. Two of those buttons operate on existing records. But how do we know which records to operate on? We have to let the user tell us. To do this, we need to add checkboxes to our table so that the user can select certain records. This is done in our template.&lt;br /&gt;
&lt;br /&gt;
In order to the add the checkboxes, we need to add an extra column into our table. We will add the column in between the two that we already have.&lt;br /&gt;
&lt;br /&gt;
In the header of the column, we will add a checkbox which can be used to toggle all the boxes below it on or off:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;th width=&amp;quot;20&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;input type=&amp;quot;checkbox&amp;quot; name=&amp;quot;toggle&amp;quot; value=&amp;quot;&amp;quot; onclick=&amp;quot;checkAll(&amp;lt;?php echo count( $this-&amp;gt;items ); ?&amp;gt;);&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/th&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Javascript checkAll function is a function that is built into the Joomla! base Javascript package that provides the functionality that we want here.&lt;br /&gt;
&lt;br /&gt;
Now we need to add the checkboxes into the individual rows. Joomla!&#039;s JHTML class has a method, JHTML::_(), which will generate our checkbox for us. We will add the following line to our loop:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$checked    = JHTML::_( &#039;grid.id&#039;, $i, $row-&amp;gt;id );&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
after the line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$row =&amp;amp; $this-&amp;gt;items[$i];&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then we will add a cell in between the two that we already have:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;td&amp;gt;&lt;br /&gt;
    &amp;lt;?php echo $checked; ?&amp;gt;&lt;br /&gt;
&amp;lt;/td&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It can be cumbersome to have to check the box that we want to edit and then move up and click the edit button. Therefore, we will add a link that it will go straight to the greeting&#039;s edit form. We will add the following line after the call to the JHTML::_() method to generate the link HTML:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$link = JRoute::_( &#039;index.php?option=com_hello&amp;gt;controller=hello&amp;gt;task=edit&amp;gt;cid[]=&#039;. $row-&amp;gt;id );&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And we include the link in the cell showing the greeting text:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;td&amp;gt;&lt;br /&gt;
    &amp;lt;a href=&amp;quot;&amp;lt;?php echo $link; ?&amp;gt;&amp;quot;&amp;gt;&amp;lt;?php echo $row-&amp;gt;greeting; ?&amp;gt;&amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;lt;/td&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You will notice that this link points to the hello controller. This controller will handle the data manipulation of our greetings.&lt;br /&gt;
&lt;br /&gt;
If you recall from above, we had four hidden input fields at the bottom of our form. The first input field was named &#039;option&#039;. This field is necessary so that we stay in our component. The second input field was task. This form property gets set when one of the buttons in the toolbar is clicked. A Javascript error will result and the buttons will not work if this input field is omitted. The third input field is the boxchecked field. This field keeps track of the number of boxes that are checked. The edit and delete buttons will check to ensure that this is greater than zero and will not allow the form to be submitted if it is not. The fourth input field is the controller field. This is used to specify that tasks fired from this form will be handled by the hello controller.&lt;br /&gt;
&lt;br /&gt;
Here is the code for the completed default.php file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;); ?&amp;gt;&lt;br /&gt;
&amp;lt;form action=&amp;quot;index.php&amp;quot; method=&amp;quot;post&amp;quot; name=&amp;quot;adminForm&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;div id=&amp;quot;editcell&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;table class=&amp;quot;adminlist&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;thead&amp;gt;&lt;br /&gt;
        &amp;lt;tr&amp;gt;&lt;br /&gt;
            &amp;lt;th width=&amp;quot;5&amp;quot;&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo JText::_( &#039;ID&#039; ); ?&amp;gt;&lt;br /&gt;
            &amp;lt;/th&amp;gt;&lt;br /&gt;
            &amp;lt;th&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo JText::_( &#039;Greeting&#039; ); ?&amp;gt;&lt;br /&gt;
            &amp;lt;/th&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;            &lt;br /&gt;
    &amp;lt;/thead&amp;gt;&lt;br /&gt;
    &amp;lt;?php&lt;br /&gt;
    $k = 0;&lt;br /&gt;
    for ($i=0, $n=count( $this-&amp;gt;items ); $i &amp;lt; $n; $i++)&lt;br /&gt;
    {&lt;br /&gt;
        $row =&amp;amp; $this-&amp;gt;items[$i];&lt;br /&gt;
        ?&amp;gt;&lt;br /&gt;
        &amp;lt;tr class=&amp;quot;&amp;lt;?php echo &amp;quot;row$k&amp;quot;; ?&amp;gt;&amp;quot;&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo $row-&amp;gt;id; ?&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo $row-&amp;gt;greeting; ?&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;&lt;br /&gt;
        &amp;lt;?php&lt;br /&gt;
        $k = 1 - $k;&lt;br /&gt;
    }&lt;br /&gt;
    ?&amp;gt;&lt;br /&gt;
    &amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;option&amp;quot; value=&amp;quot;com_hello&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;task&amp;quot; value=&amp;quot;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;boxchecked&amp;quot; value=&amp;quot;0&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;controller&amp;quot; value=&amp;quot;hello&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/form&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Our hellos view is now complete.&lt;br /&gt;
&lt;br /&gt;
== Getting Down and Dirty: Doing the Real Work ==&lt;br /&gt;
&lt;br /&gt;
Now that the Hellos view is done, it is time to move to the Hello view and model. This is where the real work will get done.&lt;br /&gt;
&lt;br /&gt;
==== The Hello Controller ====&lt;br /&gt;
&lt;br /&gt;
Our default controller just isn&#039;t going to cut it when it comes to doing work - all it is capable of doing is displaying views.&lt;br /&gt;
&lt;br /&gt;
We need to be able to handle the tasks that we are launching from the Hellos view: add, edit and remove.&lt;br /&gt;
&lt;br /&gt;
Add and edit are essentially the same task: they both display a form to the user that allows a greeting to be edited. The only difference is that new displays a blank form, and edit displays a form with data already in it. Since they are similar, we will map the add task onto the edit task handler. This is specified in our constructor:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * constructor (registers additional tasks to methods)&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function __construct()&lt;br /&gt;
{&lt;br /&gt;
    parent::__construct();&lt;br /&gt;
&lt;br /&gt;
    // Register Extra tasks&lt;br /&gt;
    $this-&amp;gt;registerTask( &#039;add&#039;  ,     &#039;edit&#039; );&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first parameter of JController::registerTask is the task to map, and the second is the method to map it to.&lt;br /&gt;
&lt;br /&gt;
We will start with handling the edit task. The controller&#039;s job is fairly simple for the edit task. All it has to do is specify the view and layout to load (the hello view and the form layout). We will also tell Joomla! to disable the mainmenu while we are editing our greeting. This prevents users from leaving unsaved records open.&lt;br /&gt;
&lt;br /&gt;
Our edit task handler looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * display the edit form&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function edit()&lt;br /&gt;
{&lt;br /&gt;
    JRequest::setVar( &#039;view&#039;, &#039;hello&#039; );&lt;br /&gt;
    JRequest::setVar( &#039;layout&#039;, &#039;form&#039;  );&lt;br /&gt;
    JRequest::setVar(&#039;hidemainmenu&#039;, 1);&lt;br /&gt;
&lt;br /&gt;
    parent::display();&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== The Hello View ====&lt;br /&gt;
&lt;br /&gt;
The Hello view will display a form which will allow the user to edit a greeting. The display method if the hello view has to do a few simple tasks:&lt;br /&gt;
&lt;br /&gt;
* retrieve the data from the model&lt;br /&gt;
* create the toolbar&lt;br /&gt;
* pass the data into the template&lt;br /&gt;
* invoke the display() method to render the template&lt;br /&gt;
&lt;br /&gt;
This becomes a bit more complicated because the one view handles both the edit and add tasks. In our toolbar we want the user to know what whether they are adding or editing, so we have to determine which task was fired.&lt;br /&gt;
&lt;br /&gt;
Since we are already retrieving the record that we want to display from the model, we can use this data to determine what task was fired. If the task was edit, then the id field of our record will have been set. If the task was new, then it will not have been set. This can be used to determine if we have a new record or an existing record.&lt;br /&gt;
&lt;br /&gt;
We will add two buttons to the toolbar: save and cancel. Though the functionality will be the same, we want to display different buttons depending on whether it is a new or existing record. If it is a new record, we will display cancel. If it already exists, we will display close.&lt;br /&gt;
&lt;br /&gt;
Thus our display method looks like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * display method of Hello view&lt;br /&gt;
 * @return void&lt;br /&gt;
 **/&lt;br /&gt;
function display($tpl = null)&lt;br /&gt;
{&lt;br /&gt;
    //get the hello&lt;br /&gt;
    $hello        =&amp;amp; $this-&amp;gt;get(&#039;Data&#039;);&lt;br /&gt;
    $isNew        = ($hello-&amp;gt;id &amp;lt; 1);&lt;br /&gt;
&lt;br /&gt;
    $text = $isNew ? JText::_( &#039;New&#039; ) : JText::_( &#039;Edit&#039; );&lt;br /&gt;
    JToolBarHelper::title(   JText::_( &#039;Hello&#039; ).&#039;: &amp;lt;small&amp;gt;&amp;lt;small&amp;gt;[ &#039; . $text.&#039; ]&amp;lt;/small&amp;gt;&amp;lt;/small&amp;gt;&#039; );&lt;br /&gt;
    JToolBarHelper::save();&lt;br /&gt;
    if ($isNew)  {&lt;br /&gt;
        JToolBarHelper::cancel();&lt;br /&gt;
    } else {&lt;br /&gt;
        // for existing items the button is renamed `close`&lt;br /&gt;
        JToolBarHelper::cancel( &#039;cancel&#039;, &#039;Close&#039; );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    $this-&amp;gt;assignRef(&#039;hello&#039;, $hello);&lt;br /&gt;
    parent::display($tpl);&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== The Hello Model ====&lt;br /&gt;
&lt;br /&gt;
Our view needs data. Therefore, we need to create a model to model a hello.&lt;br /&gt;
&lt;br /&gt;
Our model will have two properties: _id and _data. _id will hold the id of the greeting and data will hold the data.&lt;br /&gt;
&lt;br /&gt;
We will start with a constructor, which will attempt to retrieve the id from the query:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Constructor that retrieves the ID from the request&lt;br /&gt;
 *&lt;br /&gt;
 * @access    public&lt;br /&gt;
 * @return    void&lt;br /&gt;
 */&lt;br /&gt;
function __construct()&lt;br /&gt;
{&lt;br /&gt;
    parent::__construct();&lt;br /&gt;
&lt;br /&gt;
    $array = JRequest::getVar(&#039;cid&#039;,  0, &#039;&#039;, &#039;array&#039;);&lt;br /&gt;
    $this-&amp;gt;setId((int)$array[0]);&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The JRequest::getVar() method is used to retrieve data from the request. The first parameter is the name of the form variable. The second parameter is the default value to assign if there is no value found. The third parameter is the name of the hash to retrieve the value from (get, post, etc), and the last value is the data type that should be forced on the value.&lt;br /&gt;
&lt;br /&gt;
Our constructor will take the first value from the cid array and assign it to the id.&lt;br /&gt;
&lt;br /&gt;
Our setId() method can be used to set our id. Changing the id that our model points to will mean the id points to the wrong data. Therefore, when we set the id, we will clear the data property:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Method to set the hello identifier&lt;br /&gt;
 *&lt;br /&gt;
 * @access    public&lt;br /&gt;
 * @param    int Hello identifier&lt;br /&gt;
 * @return    void&lt;br /&gt;
 */&lt;br /&gt;
function setId($id)&lt;br /&gt;
{&lt;br /&gt;
    // Set id and wipe data&lt;br /&gt;
    $this-&amp;gt;_id        = $id;&lt;br /&gt;
    $this-&amp;gt;_data    = null;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finally, we need a method to retrieve our data: getData()&lt;br /&gt;
&lt;br /&gt;
getData will check if the _data property has already been set. If it has, it will simply return it. Otherwise, it will load the data from the database.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Method to get a hello&lt;br /&gt;
 * @return object with data&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
function &amp;amp;getData()&lt;br /&gt;
{&lt;br /&gt;
    // Load the data&lt;br /&gt;
    if (empty( $this-&amp;gt;_data )) {&lt;br /&gt;
        $query = &#039; SELECT * FROM #__hello &#039;.&lt;br /&gt;
                &#039;  WHERE id = &#039;.$this-&amp;gt;_id;&lt;br /&gt;
        $this-&amp;gt;_db-&amp;gt;setQuery( $query );&lt;br /&gt;
        $this-&amp;gt;_data = $this-&amp;gt;_db-&amp;gt;loadObject();&lt;br /&gt;
    }&lt;br /&gt;
    if (!$this-&amp;gt;_data) {&lt;br /&gt;
        $this-&amp;gt;_data = new stdClass();&lt;br /&gt;
        $this-&amp;gt;_data-&amp;gt;id = 0;&lt;br /&gt;
        $this-&amp;gt;_data-&amp;gt;greeting = null;&lt;br /&gt;
    }&lt;br /&gt;
    return $this-&amp;gt;_data;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== The Form ====&lt;br /&gt;
&lt;br /&gt;
Now all that is left is to create the form that the data will go into. Since we specified our layout as form, the form will go in a file in the tmpl directory of the hello view called form.php:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;); ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;form action=&amp;quot;index.php&amp;quot; method=&amp;quot;post&amp;quot; name=&amp;quot;adminForm&amp;quot; id=&amp;quot;adminForm&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;div class=&amp;quot;col100&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;fieldset class=&amp;quot;adminform&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;legend&amp;gt;&amp;lt;?php echo JText::_( &#039;Details&#039; ); ?&amp;gt;&amp;lt;/legend&amp;gt;&lt;br /&gt;
        &amp;lt;table class=&amp;quot;admintable&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;tr&amp;gt;&lt;br /&gt;
            &amp;lt;td width=&amp;quot;100&amp;quot; align=&amp;quot;right&amp;quot; class=&amp;quot;key&amp;quot;&amp;gt;&lt;br /&gt;
                &amp;lt;label for=&amp;quot;greeting&amp;quot;&amp;gt;&lt;br /&gt;
                    &amp;lt;?php echo JText::_( &#039;Greeting&#039; ); ?&amp;gt;:&lt;br /&gt;
                &amp;lt;/label&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;input class=&amp;quot;text_area&amp;quot; type=&amp;quot;text&amp;quot; name=&amp;quot;greeting&amp;quot; id=&amp;quot;greeting&amp;quot; size=&amp;quot;32&amp;quot; maxlength=&amp;quot;250&amp;quot; value=&amp;quot;&amp;lt;?php echo $this-&amp;gt;hello-&amp;gt;greeting;?&amp;gt;&amp;quot; /&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;&lt;br /&gt;
    &amp;lt;/table&amp;gt;&lt;br /&gt;
    &amp;lt;/fieldset&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;clr&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;option&amp;quot; value=&amp;quot;com_hello&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;id&amp;quot; value=&amp;quot;&amp;lt;?php echo $this-&amp;gt;hello-&amp;gt;id; ?&amp;gt;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;task&amp;quot; value=&amp;quot;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;controller&amp;quot; value=&amp;quot;hello&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/form&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in addition to the input field, there is a hidden field for the id. The user doesn&#039;t need to edit the id (and shouldn&#039;t), so we silently pass it along in the form.&lt;br /&gt;
&lt;br /&gt;
==== Implementing the Functionality ====&lt;br /&gt;
&lt;br /&gt;
So far, our controller only handles two tasks: edit and new. However, we also have buttons to save, delete and cancel records. We need to write code to handle and perform these tasks.&lt;br /&gt;
&lt;br /&gt;
=== Saving a Record ===&lt;br /&gt;
&lt;br /&gt;
The logical next step is to implement the functionality to save a record. Normally, this would require some switches and logic to handle various cases, such as the difference between creating a new record (an INSERT query), and updating an existing query (an UPDATE query). Also, there are complexities involved in getting the data from the form and putting it into the query.&lt;br /&gt;
&lt;br /&gt;
The Joomla! framework takes care of a lot of this for you. The JTable class makes it easy to manipulate records in the database without having to worry about writing the SQL code that lies behind these updates. It also makes it easy to transfer data from an HTML form into the database.&lt;br /&gt;
&lt;br /&gt;
== Creating the Table Class ==&lt;br /&gt;
&lt;br /&gt;
The JTable class is an abstract class from which you can derive child classes to work with specific tables. To use it, you simply create a class that extends the JTable class, add your database fields as properties, and override the constructor to specify the name of the table and the primary key.&lt;br /&gt;
&lt;br /&gt;
Here is our JTable class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * Hello World table class&lt;br /&gt;
 * &lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://dev.joomla.org/component/option,com_jd-wiki/Itemid,31/id,tutorials:components/&lt;br /&gt;
 * @license        GNU/GPL&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// no direct access&lt;br /&gt;
defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;);&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Hello Table class&lt;br /&gt;
 *&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 */&lt;br /&gt;
class TableHello extends JTable&lt;br /&gt;
{&lt;br /&gt;
    /**&lt;br /&gt;
     * Primary Key&lt;br /&gt;
     *&lt;br /&gt;
     * @var int&lt;br /&gt;
     */&lt;br /&gt;
    var $id = null;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * @var string&lt;br /&gt;
     */&lt;br /&gt;
    var $greeting = null;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Constructor&lt;br /&gt;
     *&lt;br /&gt;
     * @param object Database connector object&lt;br /&gt;
     */&lt;br /&gt;
    function TableHello( &amp;amp;$db ) {&lt;br /&gt;
        parent::__construct(&#039;#__hello&#039;, &#039;id&#039;, $db);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You will see here that we have defined our two fields: the id field and the greeting field. Then we have defined a constructor, which will call the constructor of the parent class and pass it the name of the table (hello), the name of the field which is the primary key (id), and the database connector object.&lt;br /&gt;
&lt;br /&gt;
This file should be called hello.php and it will go in a directory called tables in the administrator section of our component.&lt;br /&gt;
&lt;br /&gt;
== Implementing the Function in our Model ==&lt;br /&gt;
&lt;br /&gt;
We are now ready to add the method to the model which will save our record. We will call this method store. Our store() method will do three things: bind the data from the form to the TableHello object, check to ensure that the record is properly formed, and store the record in the database.&lt;br /&gt;
&lt;br /&gt;
Our method looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Method to store a record&lt;br /&gt;
 *&lt;br /&gt;
 * @access    public&lt;br /&gt;
 * @return    boolean    True on success&lt;br /&gt;
 */&lt;br /&gt;
function store()&lt;br /&gt;
{&lt;br /&gt;
    $row =&amp;amp; $this-&amp;gt;getTable();&lt;br /&gt;
&lt;br /&gt;
    $data = JRequest::get( &#039;post&#039; );&lt;br /&gt;
    // Bind the form fields to the hello table&lt;br /&gt;
    if (!$row-&amp;gt;bind($data)) {&lt;br /&gt;
        $this-&amp;gt;setError($this-&amp;gt;_db-&amp;gt;getErrorMsg());&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // Make sure the hello record is valid&lt;br /&gt;
    if (!$row-&amp;gt;check()) {&lt;br /&gt;
        $this-&amp;gt;setError($this-&amp;gt;_db-&amp;gt;getErrorMsg());&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // Store the web link table to the database&lt;br /&gt;
    if (!$row-&amp;gt;store()) {&lt;br /&gt;
        $this-&amp;gt;setError($this-&amp;gt;_db-&amp;gt;getErrorMsg());&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return true;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This method gets added to the hello model.&lt;br /&gt;
&lt;br /&gt;
The method takes one parameter, which is an associative array of data that we want to store in the database. This can easily be retrieved from the request as will be seen later.&lt;br /&gt;
&lt;br /&gt;
You will see that the first line retrieves a reference to our JTable object. If we name our table properly, we don&#039;t have to specify its name - the JModel class knows where to find it. You may recall that we called our table class TableHello and put it in a file called hello.php in the tables directory. If you follow this convention, the JModel class can create your object automatically.&lt;br /&gt;
&lt;br /&gt;
The second line will retrieve the data from the form. The JRequest class makes this very easy. In this case, we are retrieving all of the variables that were submitted using the &#039;POST&#039; method. These will be returned as an associative array.&lt;br /&gt;
&lt;br /&gt;
The rest is easy - we bind, check and store. The bind() method will copy values from the array into the corresponding property of the table object. In this case, it will take the values of id and greeting and copy them to our TableHello object.&lt;br /&gt;
&lt;br /&gt;
The check() method will perform data verification. In the JTable() class, this method simply returns true. While this doesn&#039;t provide any value for us currently, by calling this method we make it possible to do data checking using our TableHello class in the future. This method can be overridden in our TableHello class with a method that performs the appropriate checks.&lt;br /&gt;
&lt;br /&gt;
The store() method will take the data that is in the object and store it in the database. If the id is 0, it will create a new record (INSERT), otherwise, it will update the existing record (UPDATE).&lt;br /&gt;
&lt;br /&gt;
== Adding the Task to the Controller ==&lt;br /&gt;
&lt;br /&gt;
We are now ready to add our task to the controller. Since the task that we are firing is called &#039;save&#039;, we must call our method &#039;save&#039;. This is simple:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * save a record (and redirect to main page)&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function save()&lt;br /&gt;
{&lt;br /&gt;
    $model = $this-&amp;gt;getModel(&#039;hello&#039;);&lt;br /&gt;
&lt;br /&gt;
    if ($model-&amp;gt;store()) {&lt;br /&gt;
        $msg = JText::_( &#039;Greeting Saved!&#039; );&lt;br /&gt;
    } else {&lt;br /&gt;
        $msg = JText::_( &#039;Error Saving Greeting&#039; );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // Check the table in so it can be edited.... we are done with it anyway&lt;br /&gt;
    $link = &#039;index.php?option=com_hello&#039;;&lt;br /&gt;
    $this-&amp;gt;setRedirect($link, $msg);&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All we do is get our model and invoke the store() method. Then we use the setRedirect() method to redirect back to our list of greetings. We also pass a message along, which will be displayed at the top of the page.&lt;br /&gt;
&lt;br /&gt;
=== Deleting a Record ===&lt;br /&gt;
&lt;br /&gt;
=== Implementing the Function in the Model ===&lt;br /&gt;
&lt;br /&gt;
In the model, we will retrieve the list of IDs to delete and call the JTable class to delete them. Here it is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Method to delete record(s)&lt;br /&gt;
 *&lt;br /&gt;
 * @access    public&lt;br /&gt;
 * @return    boolean    True on success&lt;br /&gt;
 */&lt;br /&gt;
function delete()&lt;br /&gt;
{&lt;br /&gt;
    $cids = JRequest::getVar( &#039;cid&#039;, array(0), &#039;post&#039;, &#039;array&#039; );&lt;br /&gt;
    $row =&amp;amp; $this-&amp;gt;getTable();&lt;br /&gt;
&lt;br /&gt;
    foreach($cids as $cid) {&lt;br /&gt;
        if (!$row-&amp;gt;delete( $cid )) {&lt;br /&gt;
            $this-&amp;gt;setError( $row-&amp;gt;getErrorMsg() );&lt;br /&gt;
            return false;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return true;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We invoke the JRequest::getVar() method to get the data from the request, then we invoke the $row-&amp;gt;delete() method to delete each row. By storing errors in the model we make it possible to retrieve them later if we so choose.&lt;br /&gt;
&lt;br /&gt;
=== Handling the Remove Task in the Controller ===&lt;br /&gt;
&lt;br /&gt;
This is similar to the save() method which handled the save task:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * remove record(s)&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function remove()&lt;br /&gt;
{&lt;br /&gt;
    $model = $this-&amp;gt;getModel(&#039;hello&#039;);&lt;br /&gt;
    if(!$model-&amp;gt;delete()) {&lt;br /&gt;
        $msg = JText::_( &#039;Error: One or More Greetings Could not be Deleted&#039; );&lt;br /&gt;
    } else {&lt;br /&gt;
        $msg = JText::_( &#039;Greeting(s) Deleted&#039; );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    $this-&amp;gt;setRedirect( &#039;index.php?option=com_hello&#039;, $msg );&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Cancelling the Edit Operation ====&lt;br /&gt;
&lt;br /&gt;
To cancel the edit operation, all we have to do is redirect back to the main view:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * cancel editing a record&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function cancel()&lt;br /&gt;
{&lt;br /&gt;
    $msg = JText::_( &#039;Operation Cancelled&#039; );&lt;br /&gt;
    $this-&amp;gt;setRedirect( &#039;index.php?option=com_hello&#039;, $msg );&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
We have now implemented a basic backend to our component. We are now able to edit the entries that are viewed in the frontend. We have demonstrated the interaction between models, views and controllers. We have shown how the JTable class can be extended to provide easy access to tables in the database. It can also be seen how the JToolBarHelper class can be used to create button bars in components to present a standardized look between components.&lt;br /&gt;
&lt;br /&gt;
== Contributors ==&lt;br /&gt;
* staalanden&lt;br /&gt;
&lt;br /&gt;
== Download ==&lt;br /&gt;
&lt;br /&gt;
The component can be downloaded at: [http://joomlacode.org/gf/download/frsrelease/8104/29402/com_hello4a_01.zip com_hello4a_01]&lt;/div&gt;</summary>
		<author><name>Tonie</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Creating_an_Administrator_Interface&amp;diff=9510</id>
		<title>J1.5:Developing a MVC Component/Creating an Administrator Interface</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Creating_an_Administrator_Interface&amp;diff=9510"/>
		<updated>2008-07-27T19:48:46Z</updated>

		<summary type="html">&lt;p&gt;Tonie: /* Checkboxes and Links */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
In the first three tutorials, we have developed a MVC component that retrieves its data from a table in the database. Currently, there is no way to add data to the database except to do it manually using another tool. In this tutorial, we will develop an administrator section for our component which will make it possible to manage the entries in the database.&lt;br /&gt;
&lt;br /&gt;
== Creating the Basic Framework ==&lt;br /&gt;
&lt;br /&gt;
The basic framework of the administrator panel is very similar to the site portion. The main entry point for the administrator section of the component is hello.php. This file is identical to the hello.php file that was used in the site portion except the name of the controller it loads will be changed to HellosController. The default controller is also called controller.php and this file is identical to the default controller in the site portion, with the exception that the controller is named HellosController instead of HelloController. This difference is so that JController will by default load the hellos view, which will display a list of our greetings.&lt;br /&gt;
&lt;br /&gt;
Here is the listing for hello.php:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_4&lt;br /&gt;
 * @license    GNU/GPL&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
// No direct access&lt;br /&gt;
&lt;br /&gt;
defined( &#039;_JEXEC&#039; ) or die( &#039;Restricted access&#039; );&lt;br /&gt;
&lt;br /&gt;
// Require the base controller&lt;br /&gt;
&lt;br /&gt;
require_once( JPATH_COMPONENT.DS.&#039;controller.php&#039; );&lt;br /&gt;
&lt;br /&gt;
// Require specific controller if requested&lt;br /&gt;
if($controller = JRequest::getWord(&#039;controller&#039;)) {&lt;br /&gt;
    $path = JPATH_COMPONENT.DS.&#039;controllers&#039;.DS.$controller.&#039;.php&#039;;&lt;br /&gt;
    if (file_exists($path)) {&lt;br /&gt;
        require_once $path;&lt;br /&gt;
    } else {&lt;br /&gt;
        $controller = &#039;&#039;;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Create the controller&lt;br /&gt;
$classname    = &#039;HellosController&#039;.$controller;&lt;br /&gt;
$controller   = new $classname( );&lt;br /&gt;
&lt;br /&gt;
// Perform the Request task&lt;br /&gt;
$controller-&amp;gt;execute( JRequest::getVar( &#039;task&#039; ) );&lt;br /&gt;
&lt;br /&gt;
// Redirect if set by the controller&lt;br /&gt;
$controller-&amp;gt;redirect();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The view and model that we will start with is the hellos view and the hellos model. We will start with the model.&lt;br /&gt;
&lt;br /&gt;
==== The Hellos Model ====&lt;br /&gt;
&lt;br /&gt;
The Hellos Model will be very simple. The only operation that we currently need is the ability to retrieve the list of hellos from the database. This operation will be implemented in a method called getData().&lt;br /&gt;
&lt;br /&gt;
The JModel class has a built in protected method called _getList(). This method can be used to simplify the task of retrieving a list of records from the database. We simply need to pass it the query and it will return the list of records.&lt;br /&gt;
&lt;br /&gt;
At a later point in time, we might want to use our query from within another method. Therefore, we will create a private method called _buildQuery() which will return the query that will be passed to _getList(). This makes it easier to change the query as well since it is localized in one place.&lt;br /&gt;
&lt;br /&gt;
Therefore we need two methods in our class: getData() and _buildQuery().&lt;br /&gt;
&lt;br /&gt;
_buildQuery() simply returns the query. It looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source  lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Returns the query&lt;br /&gt;
 * @return string The query to be used to retrieve the rows from the database&lt;br /&gt;
 */&lt;br /&gt;
function _buildQuery()&lt;br /&gt;
{&lt;br /&gt;
    $query = &#039; SELECT * &#039;&lt;br /&gt;
           . &#039; FROM #__hello &#039;&lt;br /&gt;
    ;&lt;br /&gt;
&lt;br /&gt;
    return $query;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
getData() will obtain the query and retrieve the records from the database. Now it might happen that we need to retrieve this list of data twice in one page load. It would be a waste to have to query the database twice. Therefore, we will have this method store the data in a protected property so that on subsequent requests it can simply return the data it has already retrieved. This property will be called _data.&lt;br /&gt;
&lt;br /&gt;
Here is the getData() method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source  lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Retrieves the hello data&lt;br /&gt;
 * @return array Array of objects containing the data from the database&lt;br /&gt;
 */&lt;br /&gt;
function getData()&lt;br /&gt;
{&lt;br /&gt;
    // Lets load the data if it doesn&#039;t already exist&lt;br /&gt;
    if (empty( $this-&amp;gt;_data ))&lt;br /&gt;
    {&lt;br /&gt;
        $query = $this-&amp;gt;_buildQuery();&lt;br /&gt;
        $this-&amp;gt;_data = $this-&amp;gt;_getList( $query );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return $this-&amp;gt;_data;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The completed model looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * Hellos Model for Hello World Component&lt;br /&gt;
 * &lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_4&lt;br /&gt;
 * @license        GNU/GPL&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// Check to ensure this file is included in Joomla!&lt;br /&gt;
defined(&#039;_JEXEC&#039;) or die();&lt;br /&gt;
&lt;br /&gt;
jimport( &#039;joomla.application.component.model&#039; );&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Hello Model&lt;br /&gt;
 *&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 */&lt;br /&gt;
class HellosModelHellos extends JModel&lt;br /&gt;
{&lt;br /&gt;
    /**&lt;br /&gt;
     * Hellos data array&lt;br /&gt;
     *&lt;br /&gt;
     * @var array&lt;br /&gt;
     */&lt;br /&gt;
    var $_data;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Returns the query&lt;br /&gt;
     * @return string The query to be used to retrieve the rows from the database&lt;br /&gt;
     */&lt;br /&gt;
    function _buildQuery()&lt;br /&gt;
    {&lt;br /&gt;
        $query = &#039; SELECT * &#039;&lt;br /&gt;
            . &#039; FROM #__hello &#039;&lt;br /&gt;
        ;&lt;br /&gt;
        return $query;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Retrieves the hello data&lt;br /&gt;
     * @return array Array of objects containing the data from the database&lt;br /&gt;
     */&lt;br /&gt;
    function getData()&lt;br /&gt;
    {&lt;br /&gt;
        // Lets load the data if it doesn&#039;t already exist&lt;br /&gt;
        if (empty( $this-&amp;gt;_data ))&lt;br /&gt;
        {&lt;br /&gt;
            $query = $this-&amp;gt;_buildQuery();&lt;br /&gt;
            $this-&amp;gt;_data = $this-&amp;gt;_getList( $query );&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        return $this-&amp;gt;_data;&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This file is saved as models/hellos.php.&lt;br /&gt;
&lt;br /&gt;
==== The Hellos View ====&lt;br /&gt;
&lt;br /&gt;
Now that we have a model to retrieve our data, we need to display it. This view will be fairly similar to the view from the site section as well.&lt;br /&gt;
&lt;br /&gt;
Just as our model was automatically instantiated in the site, so it is in the administrator. Methods that start with get in the model can be accessed using the get() method of the JView class. So our view has three lines: one to retrieve the data from the model, one to push the data into the template, and one to invoke the display method to display the output. Thus we have:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * Hellos View for Hello World Component&lt;br /&gt;
 * &lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_4&lt;br /&gt;
 * @license        GNU/GPL&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// Check to ensure this file is included in Joomla!&lt;br /&gt;
defined(&#039;_JEXEC&#039;) or die();&lt;br /&gt;
&lt;br /&gt;
jimport( &#039;joomla.application.component.view&#039; );&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Hellos View&lt;br /&gt;
 *&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 */&lt;br /&gt;
class HellosViewHellos extends JView&lt;br /&gt;
{&lt;br /&gt;
    /**&lt;br /&gt;
     * Hellos view display method&lt;br /&gt;
     * @return void&lt;br /&gt;
     **/&lt;br /&gt;
    function display($tpl = null)&lt;br /&gt;
    {&lt;br /&gt;
        JToolBarHelper::title( JText::_( &#039;Hello Manager&#039; ), &#039;generic.png&#039; );&lt;br /&gt;
        JToolBarHelper::deleteList();&lt;br /&gt;
        JToolBarHelper::editListX();&lt;br /&gt;
        JToolBarHelper::addNewX();&lt;br /&gt;
&lt;br /&gt;
        // Get data from the model&lt;br /&gt;
        $items =&amp;amp; $this-&amp;gt;get( &#039;Data&#039;);&lt;br /&gt;
&lt;br /&gt;
        $this-&amp;gt;assignRef( &#039;items&#039;, $items );&lt;br /&gt;
&lt;br /&gt;
        parent::display($tpl);&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This file is saved as views/hellos/view.html.php.&lt;br /&gt;
&lt;br /&gt;
==== The Hellos Template ====&lt;br /&gt;
&lt;br /&gt;
The template will take the data pushed into it from the view and produce the output. We will display our output in a simple table. While the frontend template was very simple, in the administrator we will need a minimal amount of extra logic to handle looping through the data.&lt;br /&gt;
&lt;br /&gt;
Here is our template:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;); ?&amp;gt;&lt;br /&gt;
&amp;lt;form action=&amp;quot;index.php&amp;quot; method=&amp;quot;post&amp;quot; name=&amp;quot;adminForm&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;div id=&amp;quot;editcell&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;table class=&amp;quot;adminlist&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;thead&amp;gt;&lt;br /&gt;
        &amp;lt;tr&amp;gt;&lt;br /&gt;
            &amp;lt;th width=&amp;quot;5&amp;quot;&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo JText::_( &#039;ID&#039; ); ?&amp;gt;&lt;br /&gt;
            &amp;lt;/th&amp;gt;&lt;br /&gt;
            &amp;lt;th&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo JText::_( &#039;Greeting&#039; ); ?&amp;gt;&lt;br /&gt;
            &amp;lt;/th&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;            &lt;br /&gt;
    &amp;lt;/thead&amp;gt;&lt;br /&gt;
    &amp;lt;?php&lt;br /&gt;
    $k = 0;&lt;br /&gt;
    for ($i=0, $n=count( $this-&amp;gt;items ); $i &amp;lt; $n; $i++)&lt;br /&gt;
    {&lt;br /&gt;
        $row =&amp;amp; $this-&amp;gt;items[$i];&lt;br /&gt;
        ?&amp;gt;&lt;br /&gt;
        &amp;lt;tr class=&amp;quot;&amp;lt;?php echo &amp;quot;row$k&amp;quot;; ?&amp;gt;&amp;quot;&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo $row-&amp;gt;id; ?&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo $row-&amp;gt;greeting; ?&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;&lt;br /&gt;
        &amp;lt;?php&lt;br /&gt;
        $k = 1 - $k;&lt;br /&gt;
    }&lt;br /&gt;
    ?&amp;gt;&lt;br /&gt;
    &amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;option&amp;quot; value=&amp;quot;com_hello&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;task&amp;quot; value=&amp;quot;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;boxchecked&amp;quot; value=&amp;quot;0&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;controller&amp;quot; value=&amp;quot;hello&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/form&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This template is saved as views/hellos/tmpl/default.php.&lt;br /&gt;
&lt;br /&gt;
You will notice that our output is enclosed in a form. Though this is not necessary now, it will be soon.&lt;br /&gt;
&lt;br /&gt;
We have now completed the basic part of the first view. We have added five files to the admin section of our component:&lt;br /&gt;
&lt;br /&gt;
* hello.php&lt;br /&gt;
* controller.php&lt;br /&gt;
* models/hellos.php&lt;br /&gt;
* views/hellos/view.html.php&lt;br /&gt;
* views/hellos/tmpl/default.php&lt;br /&gt;
&lt;br /&gt;
You can now add these files to the XML install file and give it a try!&lt;br /&gt;
&lt;br /&gt;
== Adding Functionality ==&lt;br /&gt;
&lt;br /&gt;
So far our administrator section is pretty useless. It doesn&#039;t really do anything - all it does is display the entries that we have in our database.&lt;br /&gt;
&lt;br /&gt;
In order to make it useful, we need to add some buttons and links.&lt;br /&gt;
&lt;br /&gt;
==== The Toolbar ====&lt;br /&gt;
&lt;br /&gt;
You may have noticed the toolbar that appears at the top of other Joomla! component administrator panels. Our component needs one as well. Joomla! makes this very easy to do. We will add buttons Delete records, Edit records, and create New records. We will also add a title that will be displayed on our toolbar.&lt;br /&gt;
&lt;br /&gt;
This is done by adding code to the view. To add the buttons, we use static methods from the Joomla! JToolBarHelper class. The code looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;JToolBarHelper::title(   JText::_( &#039;Hello Manager&#039; ), &#039;generic.png&#039; );&lt;br /&gt;
JToolBarHelper::deleteList();&lt;br /&gt;
JToolBarHelper::editListX();&lt;br /&gt;
JToolBarHelper::addNewX();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
These three methods will create the appropriate buttons. The deleteList() method can optionally take up to three parameters - the first parameter is a string to display to the user to confirm that they want to delete the records. The second is the task that should be sent with the query (the default is &#039;remove&#039;), and the third is the text that should be displayed below the button.&lt;br /&gt;
&lt;br /&gt;
The editListX() and addNewX() methods can each take two optional parameters. The first is the task (which are by default edit and add, respectively), and the second is the text that should be displayed below the button.&lt;br /&gt;
&lt;br /&gt;
*You may have noticed the use of the JText::_ method in the template before and here as well. This is a handy function that makes component translation much easier. The JText::_ method will look up the string in your component language file and return the translated string. If not translation is found, it will return the string that you passed it. If you want to translate your component into another language, all you have to do is create a language file that will map the strings within the quotes to the translated version of the string.&lt;br /&gt;
&lt;br /&gt;
==== Checkboxes and Links ====&lt;br /&gt;
&lt;br /&gt;
We now have buttons. Two of those buttons operate on existing records. But how do we know which records to operate on? We have to let the user tell us. To do this, we need to add checkboxes to our table so that the user can select certain records. This is done in our template.&lt;br /&gt;
&lt;br /&gt;
In order to the add the checkboxes, we need to add an extra column into our table. We will add the column in between the two that we already have.&lt;br /&gt;
&lt;br /&gt;
In the header of the column, we will add a checkbox which can be used to toggle all the boxes below it on or off:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;th width=&amp;quot;20&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;input type=&amp;quot;checkbox&amp;quot; name=&amp;quot;toggle&amp;quot; value=&amp;quot;&amp;quot; onclick=&amp;quot;checkAll(&amp;lt;?php echo count( $this-&amp;gt;items ); ?&amp;gt;);&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/th&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Javascript checkAll function is a function that is built into the Joomla! base Javascript package that provides the functionality that we want here.&lt;br /&gt;
&lt;br /&gt;
Now we need to add the checkboxes into the individual rows. Joomla!&#039;s JHTML class has a method, JHTML::_(), which will generate our checkbox for us. We will add the following line to our loop:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$checked    = JHTML::_( &#039;grid.id&#039;, $i, $row-&amp;gt;id );&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
after the line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$row =&amp;amp; $this-&amp;gt;items[$i];&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then we will add a cell in between the two that we already have:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;td&amp;gt;&lt;br /&gt;
    &amp;lt;?php echo $checked; ?&amp;gt;&lt;br /&gt;
&amp;lt;/td&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It can be cumbersome to have to check the box that we want to edit and then move up and click the edit button. Therefore, we will add a link that it will go straight to the greeting&#039;s edit form. We will add the following line after the call to the JHTML::_() method to generate the link HTML:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$link = JRoute::_( &#039;index.php?option=com_hello&amp;gt;controller=hello&amp;gt;task=edit&amp;gt;cid[]=&#039;. $row-&amp;gt;id );&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And we include the link in the cell showing the greeting text:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;td&amp;gt;&lt;br /&gt;
    &amp;lt;a href=&amp;quot;&amp;lt;?php echo $link; ?&amp;gt;&amp;quot;&amp;gt;&amp;lt;?php echo $row-&amp;gt;greeting; ?&amp;gt;&amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;lt;/td&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You will notice that this link points to the hello controller. This controller will handle the data manipulation of our greetings.&lt;br /&gt;
&lt;br /&gt;
If you recall from above, we had four hidden input fields at the bottom of our form. The first input field was named &#039;option&#039;. This field is necessary so that we stay in our component. The second input field was task. This form property gets set when one of the buttons in the toolbar is clicked. A Javascript error will result and the buttons will not work if this input field is omitted. The third input field is the boxchecked field. This field keeps track of the number of boxes that are checked. The edit and delete buttons will check to ensure that this is greater than zero and will not allow the form to be submitted if it is not. The fourth input field is the controller field. This is used to specify that tasks fired from this form will be handled by the hello controller.&lt;br /&gt;
&lt;br /&gt;
Here is the code for the completed default.php file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;); ?&amp;gt;&lt;br /&gt;
&amp;lt;form action=&amp;quot;index.php&amp;quot; method=&amp;quot;post&amp;quot; name=&amp;quot;adminForm&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;div id=&amp;quot;editcell&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;table class=&amp;quot;adminlist&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;thead&amp;gt;&lt;br /&gt;
        &amp;lt;tr&amp;gt;&lt;br /&gt;
            &amp;lt;th width=&amp;quot;5&amp;quot;&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo JText::_( &#039;ID&#039; ); ?&amp;gt;&lt;br /&gt;
            &amp;lt;/th&amp;gt;&lt;br /&gt;
            &amp;lt;th&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo JText::_( &#039;Greeting&#039; ); ?&amp;gt;&lt;br /&gt;
            &amp;lt;/th&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;            &lt;br /&gt;
    &amp;lt;/thead&amp;gt;&lt;br /&gt;
    &amp;lt;?php&lt;br /&gt;
    $k = 0;&lt;br /&gt;
    for ($i=0, $n=count( $this-&amp;gt;items ); $i &amp;lt; $n; $i++)&lt;br /&gt;
    {&lt;br /&gt;
        $row =&amp;amp; $this-&amp;gt;items[$i];&lt;br /&gt;
        ?&amp;gt;&lt;br /&gt;
        &amp;lt;tr class=&amp;quot;&amp;lt;?php echo &amp;quot;row$k&amp;quot;; ?&amp;gt;&amp;quot;&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo $row-&amp;gt;id; ?&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo $row-&amp;gt;greeting; ?&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;&lt;br /&gt;
        &amp;lt;?php&lt;br /&gt;
        $k = 1 - $k;&lt;br /&gt;
    }&lt;br /&gt;
    ?&amp;gt;&lt;br /&gt;
    &amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;option&amp;quot; value=&amp;quot;com_hello&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;task&amp;quot; value=&amp;quot;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;boxchecked&amp;quot; value=&amp;quot;0&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;controller&amp;quot; value=&amp;quot;hello&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/form&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Our hellos view is now complete.&lt;br /&gt;
&lt;br /&gt;
== Getting Down and Dirty: Doing the Real Work ==&lt;br /&gt;
&lt;br /&gt;
Now that the Hellos view is done, it is time to move to the Hello view and model. This is where the real work will get done.&lt;br /&gt;
&lt;br /&gt;
==== The Hello Controller ====&lt;br /&gt;
&lt;br /&gt;
Our default controller just isn&#039;t going to cut it when it comes to doing work - all it is capable of doing is displaying views.&lt;br /&gt;
&lt;br /&gt;
We need to be able to handle the tasks that we are launching from the Hellos view: add, edit and remove.&lt;br /&gt;
&lt;br /&gt;
Add and edit are essentially the same task: they both display a form to the user that allows a greeting to be edited. The only difference is that new displays a blank form, and edit displays a form with data already in it. Since they are similar, we will map the add task onto the edit task handler. This is specified in our constructor:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * constructor (registers additional tasks to methods)&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function __construct()&lt;br /&gt;
{&lt;br /&gt;
    parent::__construct();&lt;br /&gt;
&lt;br /&gt;
    // Register Extra tasks&lt;br /&gt;
    $this-&amp;gt;registerTask( &#039;add&#039;  ,     &#039;edit&#039; );&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first parameter of JController::registerTask is the task to map, and the second is the method to map it to.&lt;br /&gt;
&lt;br /&gt;
We will start with handling the edit task. The controller&#039;s job is fairly simple for the edit task. All it has to do is specify the view and layout to load (the hello view and the form layout). We will also tell Joomla! to disable the mainmenu while we are editing our greeting. This prevents users from leaving unsaved records open.&lt;br /&gt;
&lt;br /&gt;
Our edit task handler looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * display the edit form&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function edit()&lt;br /&gt;
{&lt;br /&gt;
    JRequest::setVar( &#039;view&#039;, &#039;hello&#039; );&lt;br /&gt;
    JRequest::setVar( &#039;layout&#039;, &#039;form&#039;  );&lt;br /&gt;
    JRequest::setVar(&#039;hidemainmenu&#039;, 1);&lt;br /&gt;
&lt;br /&gt;
    parent::display();&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== The Hello View ====&lt;br /&gt;
&lt;br /&gt;
The Hello view will display a form which will allow the user to edit a greeting. The display method if the hello view has to do a few simple tasks:&lt;br /&gt;
&lt;br /&gt;
  * retrieve the data from the model&lt;br /&gt;
&lt;br /&gt;
  * create the toolbar&lt;br /&gt;
&lt;br /&gt;
  * pass the data into the template&lt;br /&gt;
&lt;br /&gt;
  * invoke the display() method to render the template&lt;br /&gt;
&lt;br /&gt;
This becomes a bit more complicated because the one view handles both the edit and add tasks. In our toolbar we want the user to know what whether they are adding or editing, so we have to determine which task was fired.&lt;br /&gt;
&lt;br /&gt;
Since we are already retrieving the record that we want to display from the model, we can use this data to determine what task was fired. If the task was edit, then the id field of our record will have been set. If the task was new, then it will not have been set. This can be used to determine if we have a new record or an existing record.&lt;br /&gt;
&lt;br /&gt;
We will add two buttons to the toolbar: save and cancel. Though the functionality will be the same, we want to display different buttons depending on whether it is a new or existing record. If it is a new record, we will display cancel. If it already exists, we will display close.&lt;br /&gt;
&lt;br /&gt;
Thus our display method looks like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * display method of Hello view&lt;br /&gt;
 * @return void&lt;br /&gt;
 **/&lt;br /&gt;
function display($tpl = null)&lt;br /&gt;
{&lt;br /&gt;
    //get the hello&lt;br /&gt;
    $hello        =&amp;amp; $this-&amp;gt;get(&#039;Data&#039;);&lt;br /&gt;
    $isNew        = ($hello-&amp;gt;id &amp;lt; 1);&lt;br /&gt;
&lt;br /&gt;
    $text = $isNew ? JText::_( &#039;New&#039; ) : JText::_( &#039;Edit&#039; );&lt;br /&gt;
    JToolBarHelper::title(   JText::_( &#039;Hello&#039; ).&#039;: &amp;lt;small&amp;gt;&amp;lt;small&amp;gt;[ &#039; . $text.&#039; ]&amp;lt;/small&amp;gt;&amp;lt;/small&amp;gt;&#039; );&lt;br /&gt;
    JToolBarHelper::save();&lt;br /&gt;
    if ($isNew)  {&lt;br /&gt;
        JToolBarHelper::cancel();&lt;br /&gt;
    } else {&lt;br /&gt;
        // for existing items the button is renamed `close`&lt;br /&gt;
        JToolBarHelper::cancel( &#039;cancel&#039;, &#039;Close&#039; );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    $this-&amp;gt;assignRef(&#039;hello&#039;, $hello);&lt;br /&gt;
    parent::display($tpl);&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== The Hello Model ====&lt;br /&gt;
&lt;br /&gt;
Our view needs data. Therefore, we need to create a model to model a hello.&lt;br /&gt;
&lt;br /&gt;
Our model will have two properties: _id and _data. _id will hold the id of the greeting and data will hold the data.&lt;br /&gt;
&lt;br /&gt;
We will start with a constructor, which will attempt to retrieve the id from the query:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Constructor that retrieves the ID from the request&lt;br /&gt;
 *&lt;br /&gt;
 * @access    public&lt;br /&gt;
 * @return    void&lt;br /&gt;
 */&lt;br /&gt;
function __construct()&lt;br /&gt;
{&lt;br /&gt;
    parent::__construct();&lt;br /&gt;
&lt;br /&gt;
    $array = JRequest::getVar(&#039;cid&#039;,  0, &#039;&#039;, &#039;array&#039;);&lt;br /&gt;
    $this-&amp;gt;setId((int)$array[0]);&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The JRequest::getVar() method is used to retrieve data from the request. The first parameter is the name of the form variable. The second parameter is the default value to assign if there is no value found. The third parameter is the name of the hash to retrieve the value from (get, post, etc), and the last value is the data type that should be forced on the value.&lt;br /&gt;
&lt;br /&gt;
Our constructor will take the first value from the cid array and assign it to the id.&lt;br /&gt;
&lt;br /&gt;
Our setId() method can be used to set our id. Changing the id that our model points to will mean the id points to the wrong data. Therefore, when we set the id, we will clear the data property:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Method to set the hello identifier&lt;br /&gt;
 *&lt;br /&gt;
 * @access    public&lt;br /&gt;
 * @param    int Hello identifier&lt;br /&gt;
 * @return    void&lt;br /&gt;
 */&lt;br /&gt;
function setId($id)&lt;br /&gt;
{&lt;br /&gt;
    // Set id and wipe data&lt;br /&gt;
    $this-&amp;gt;_id        = $id;&lt;br /&gt;
    $this-&amp;gt;_data    = null;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finally, we need a method to retrieve our data: getData()&lt;br /&gt;
&lt;br /&gt;
getData will check if the _data property has already been set. If it has, it will simply return it. Otherwise, it will load the data from the database.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Method to get a hello&lt;br /&gt;
 * @return object with data&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
function &amp;amp;getData()&lt;br /&gt;
{&lt;br /&gt;
    // Load the data&lt;br /&gt;
    if (empty( $this-&amp;gt;_data )) {&lt;br /&gt;
        $query = &#039; SELECT * FROM #__hello &#039;.&lt;br /&gt;
                &#039;  WHERE id = &#039;.$this-&amp;gt;_id;&lt;br /&gt;
        $this-&amp;gt;_db-&amp;gt;setQuery( $query );&lt;br /&gt;
        $this-&amp;gt;_data = $this-&amp;gt;_db-&amp;gt;loadObject();&lt;br /&gt;
    }&lt;br /&gt;
    if (!$this-&amp;gt;_data) {&lt;br /&gt;
        $this-&amp;gt;_data = new stdClass();&lt;br /&gt;
        $this-&amp;gt;_data-&amp;gt;id = 0;&lt;br /&gt;
        $this-&amp;gt;_data-&amp;gt;greeting = null;&lt;br /&gt;
    }&lt;br /&gt;
    return $this-&amp;gt;_data;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== The Form ====&lt;br /&gt;
&lt;br /&gt;
Now all that is left is to create the form that the data will go into. Since we specified our layout as form, the form will go in a file in the tmpl directory of the hello view called form.php:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;); ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;form action=&amp;quot;index.php&amp;quot; method=&amp;quot;post&amp;quot; name=&amp;quot;adminForm&amp;quot; id=&amp;quot;adminForm&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;div class=&amp;quot;col100&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;fieldset class=&amp;quot;adminform&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;legend&amp;gt;&amp;lt;?php echo JText::_( &#039;Details&#039; ); ?&amp;gt;&amp;lt;/legend&amp;gt;&lt;br /&gt;
        &amp;lt;table class=&amp;quot;admintable&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;tr&amp;gt;&lt;br /&gt;
            &amp;lt;td width=&amp;quot;100&amp;quot; align=&amp;quot;right&amp;quot; class=&amp;quot;key&amp;quot;&amp;gt;&lt;br /&gt;
                &amp;lt;label for=&amp;quot;greeting&amp;quot;&amp;gt;&lt;br /&gt;
                    &amp;lt;?php echo JText::_( &#039;Greeting&#039; ); ?&amp;gt;:&lt;br /&gt;
                &amp;lt;/label&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;input class=&amp;quot;text_area&amp;quot; type=&amp;quot;text&amp;quot; name=&amp;quot;greeting&amp;quot; id=&amp;quot;greeting&amp;quot; size=&amp;quot;32&amp;quot; maxlength=&amp;quot;250&amp;quot; value=&amp;quot;&amp;lt;?php echo $this-&amp;gt;hello-&amp;gt;greeting;?&amp;gt;&amp;quot; /&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;&lt;br /&gt;
    &amp;lt;/table&amp;gt;&lt;br /&gt;
    &amp;lt;/fieldset&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;clr&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;option&amp;quot; value=&amp;quot;com_hello&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;id&amp;quot; value=&amp;quot;&amp;lt;?php echo $this-&amp;gt;hello-&amp;gt;id; ?&amp;gt;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;task&amp;quot; value=&amp;quot;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;controller&amp;quot; value=&amp;quot;hello&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/form&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in addition to the input field, there is a hidden field for the id. The user doesn&#039;t need to edit the id (and shouldn&#039;t), so we silently pass it along in the form.&lt;br /&gt;
&lt;br /&gt;
==== Implementing the Functionality ====&lt;br /&gt;
&lt;br /&gt;
So far, our controller only handles two tasks: edit and new. However, we also have buttons to save, delete and cancel records. We need to write code to handle and perform these tasks.&lt;br /&gt;
&lt;br /&gt;
=== Saving a Record ===&lt;br /&gt;
&lt;br /&gt;
The logical next step is to implement the functionality to save a record. Normally, this would require some switches and logic to handle various cases, such as the difference between creating a new record (an INSERT query), and updating an existing query (an UPDATE query). Also, there are complexities involved in getting the data from the form and putting it into the query.&lt;br /&gt;
&lt;br /&gt;
The Joomla! framework takes care of a lot of this for you. The JTable class makes it easy to manipulate records in the database without having to worry about writing the SQL code that lies behind these updates. It also makes it easy to transfer data from an HTML form into the database.&lt;br /&gt;
&lt;br /&gt;
== Creating the Table Class ==&lt;br /&gt;
&lt;br /&gt;
The JTable class is an abstract class from which you can derive child classes to work with specific tables. To use it, you simply create a class that extends the JTable class, add your database fields as properties, and override the constructor to specify the name of the table and the primary key.&lt;br /&gt;
&lt;br /&gt;
Here is our JTable class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * Hello World table class&lt;br /&gt;
 * &lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://dev.joomla.org/component/option,com_jd-wiki/Itemid,31/id,tutorials:components/&lt;br /&gt;
 * @license        GNU/GPL&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// no direct access&lt;br /&gt;
defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;);&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Hello Table class&lt;br /&gt;
 *&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 */&lt;br /&gt;
class TableHello extends JTable&lt;br /&gt;
{&lt;br /&gt;
    /**&lt;br /&gt;
     * Primary Key&lt;br /&gt;
     *&lt;br /&gt;
     * @var int&lt;br /&gt;
     */&lt;br /&gt;
    var $id = null;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * @var string&lt;br /&gt;
     */&lt;br /&gt;
    var $greeting = null;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Constructor&lt;br /&gt;
     *&lt;br /&gt;
     * @param object Database connector object&lt;br /&gt;
     */&lt;br /&gt;
    function TableHello( &amp;amp;$db ) {&lt;br /&gt;
        parent::__construct(&#039;#__hello&#039;, &#039;id&#039;, $db);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You will see here that we have defined our two fields: the id field and the greeting field. Then we have defined a constructor, which will call the constructor of the parent class and pass it the name of the table (hello), the name of the field which is the primary key (id), and the database connector object.&lt;br /&gt;
&lt;br /&gt;
This file should be called hello.php and it will go in a directory called tables in the administrator section of our component.&lt;br /&gt;
&lt;br /&gt;
== Implementing the Function in our Model ==&lt;br /&gt;
&lt;br /&gt;
We are now ready to add the method to the model which will save our record. We will call this method store. Our store() method will do three things: bind the data from the form to the TableHello object, check to ensure that the record is properly formed, and store the record in the database.&lt;br /&gt;
&lt;br /&gt;
Our method looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Method to store a record&lt;br /&gt;
 *&lt;br /&gt;
 * @access    public&lt;br /&gt;
 * @return    boolean    True on success&lt;br /&gt;
 */&lt;br /&gt;
function store()&lt;br /&gt;
{&lt;br /&gt;
    $row =&amp;amp; $this-&amp;gt;getTable();&lt;br /&gt;
&lt;br /&gt;
    $data = JRequest::get( &#039;post&#039; );&lt;br /&gt;
    // Bind the form fields to the hello table&lt;br /&gt;
    if (!$row-&amp;gt;bind($data)) {&lt;br /&gt;
        $this-&amp;gt;setError($this-&amp;gt;_db-&amp;gt;getErrorMsg());&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // Make sure the hello record is valid&lt;br /&gt;
    if (!$row-&amp;gt;check()) {&lt;br /&gt;
        $this-&amp;gt;setError($this-&amp;gt;_db-&amp;gt;getErrorMsg());&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // Store the web link table to the database&lt;br /&gt;
    if (!$row-&amp;gt;store()) {&lt;br /&gt;
        $this-&amp;gt;setError($this-&amp;gt;_db-&amp;gt;getErrorMsg());&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return true;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This method gets added to the hello model.&lt;br /&gt;
&lt;br /&gt;
The method takes one parameter, which is an associative array of data that we want to store in the database. This can easily be retrieved from the request as will be seen later.&lt;br /&gt;
&lt;br /&gt;
You will see that the first line retrieves a reference to our JTable object. If we name our table properly, we don&#039;t have to specify its name - the JModel class knows where to find it. You may recall that we called our table class TableHello and put it in a file called hello.php in the tables directory. If you follow this convention, the JModel class can create your object automatically.&lt;br /&gt;
&lt;br /&gt;
The second line will retrieve the data from the form. The JRequest class makes this very easy. In this case, we are retrieving all of the variables that were submitted using the &#039;POST&#039; method. These will be returned as an associative array.&lt;br /&gt;
&lt;br /&gt;
The rest is easy - we bind, check and store. The bind() method will copy values from the array into the corresponding property of the table object. In this case, it will take the values of id and greeting and copy them to our TableHello object.&lt;br /&gt;
&lt;br /&gt;
The check() method will perform data verification. In the JTable() class, this method simply returns true. While this doesn&#039;t provide any value for us currently, by calling this method we make it possible to do data checking using our TableHello class in the future. This method can be overridden in our TableHello class with a method that performs the appropriate checks.&lt;br /&gt;
&lt;br /&gt;
The store() method will take the data that is in the object and store it in the database. If the id is 0, it will create a new record (INSERT), otherwise, it will update the existing record (UPDATE).&lt;br /&gt;
&lt;br /&gt;
== Adding the Task to the Controller ==&lt;br /&gt;
&lt;br /&gt;
We are now ready to add our task to the controller. Since the task that we are firing is called &#039;save&#039;, we must call our method &#039;save&#039;. This is simple:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * save a record (and redirect to main page)&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function save()&lt;br /&gt;
{&lt;br /&gt;
    $model = $this-&amp;gt;getModel(&#039;hello&#039;);&lt;br /&gt;
&lt;br /&gt;
    if ($model-&amp;gt;store()) {&lt;br /&gt;
        $msg = JText::_( &#039;Greeting Saved!&#039; );&lt;br /&gt;
    } else {&lt;br /&gt;
        $msg = JText::_( &#039;Error Saving Greeting&#039; );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // Check the table in so it can be edited.... we are done with it anyway&lt;br /&gt;
    $link = &#039;index.php?option=com_hello&#039;;&lt;br /&gt;
    $this-&amp;gt;setRedirect($link, $msg);&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All we do is get our model and invoke the store() method. Then we use the setRedirect() method to redirect back to our list of greetings. We also pass a message along, which will be displayed at the top of the page.&lt;br /&gt;
&lt;br /&gt;
=== Deleting a Record ===&lt;br /&gt;
&lt;br /&gt;
=== Implementing the Function in the Model ===&lt;br /&gt;
&lt;br /&gt;
In the model, we will retrieve the list of IDs to delete and call the JTable class to delete them. Here it is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Method to delete record(s)&lt;br /&gt;
 *&lt;br /&gt;
 * @access    public&lt;br /&gt;
 * @return    boolean    True on success&lt;br /&gt;
 */&lt;br /&gt;
function delete()&lt;br /&gt;
{&lt;br /&gt;
    $cids = JRequest::getVar( &#039;cid&#039;, array(0), &#039;post&#039;, &#039;array&#039; );&lt;br /&gt;
    $row =&amp;amp; $this-&amp;gt;getTable();&lt;br /&gt;
&lt;br /&gt;
    foreach($cids as $cid) {&lt;br /&gt;
        if (!$row-&amp;gt;delete( $cid )) {&lt;br /&gt;
            $this-&amp;gt;setError( $row-&amp;gt;getErrorMsg() );&lt;br /&gt;
            return false;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return true;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We invoke the JRequest::getVar() method to get the data from the request, then we invoke the $row-&amp;gt;delete() method to delete each row. By storing errors in the model we make it possible to retrieve them later if we so choose.&lt;br /&gt;
&lt;br /&gt;
=== Handling the Remove Task in the Controller ===&lt;br /&gt;
&lt;br /&gt;
This is similar to the save() method which handled the save task:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * remove record(s)&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function remove()&lt;br /&gt;
{&lt;br /&gt;
    $model = $this-&amp;gt;getModel(&#039;hello&#039;);&lt;br /&gt;
    if(!$model-&amp;gt;delete()) {&lt;br /&gt;
        $msg = JText::_( &#039;Error: One or More Greetings Could not be Deleted&#039; );&lt;br /&gt;
    } else {&lt;br /&gt;
        $msg = JText::_( &#039;Greeting(s) Deleted&#039; );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    $this-&amp;gt;setRedirect( &#039;index.php?option=com_hello&#039;, $msg );&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Cancelling the Edit Operation ====&lt;br /&gt;
&lt;br /&gt;
To cancel the edit operation, all we have to do is redirect back to the main view:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * cancel editing a record&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function cancel()&lt;br /&gt;
{&lt;br /&gt;
    $msg = JText::_( &#039;Operation Cancelled&#039; );&lt;br /&gt;
    $this-&amp;gt;setRedirect( &#039;index.php?option=com_hello&#039;, $msg );&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
We have now implemented a basic backend to our component. We are now able to edit the entries that are viewed in the frontend. We have demonstrated the interaction between models, views and controllers. We have shown how the JTable class can be extended to provide easy access to tables in the database. It can also be seen how the JToolBarHelper class can be used to create button bars in components to present a standardized look between components.&lt;br /&gt;
&lt;br /&gt;
== Contributors ==&lt;br /&gt;
* staalanden&lt;br /&gt;
&lt;br /&gt;
== Download ==&lt;br /&gt;
&lt;br /&gt;
The component can be downloaded at: [http://joomlacode.org/gf/download/frsrelease/8104/29402/com_hello4a_01.zip com_hello4a_01]&lt;/div&gt;</summary>
		<author><name>Tonie</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Creating_an_Administrator_Interface&amp;diff=9509</id>
		<title>J1.5:Developing a MVC Component/Creating an Administrator Interface</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Creating_an_Administrator_Interface&amp;diff=9509"/>
		<updated>2008-07-27T19:48:03Z</updated>

		<summary type="html">&lt;p&gt;Tonie: /* The Toolbar */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
In the first three tutorials, we have developed a MVC component that retrieves its data from a table in the database. Currently, there is no way to add data to the database except to do it manually using another tool. In this tutorial, we will develop an administrator section for our component which will make it possible to manage the entries in the database.&lt;br /&gt;
&lt;br /&gt;
== Creating the Basic Framework ==&lt;br /&gt;
&lt;br /&gt;
The basic framework of the administrator panel is very similar to the site portion. The main entry point for the administrator section of the component is hello.php. This file is identical to the hello.php file that was used in the site portion except the name of the controller it loads will be changed to HellosController. The default controller is also called controller.php and this file is identical to the default controller in the site portion, with the exception that the controller is named HellosController instead of HelloController. This difference is so that JController will by default load the hellos view, which will display a list of our greetings.&lt;br /&gt;
&lt;br /&gt;
Here is the listing for hello.php:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_4&lt;br /&gt;
 * @license    GNU/GPL&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
// No direct access&lt;br /&gt;
&lt;br /&gt;
defined( &#039;_JEXEC&#039; ) or die( &#039;Restricted access&#039; );&lt;br /&gt;
&lt;br /&gt;
// Require the base controller&lt;br /&gt;
&lt;br /&gt;
require_once( JPATH_COMPONENT.DS.&#039;controller.php&#039; );&lt;br /&gt;
&lt;br /&gt;
// Require specific controller if requested&lt;br /&gt;
if($controller = JRequest::getWord(&#039;controller&#039;)) {&lt;br /&gt;
    $path = JPATH_COMPONENT.DS.&#039;controllers&#039;.DS.$controller.&#039;.php&#039;;&lt;br /&gt;
    if (file_exists($path)) {&lt;br /&gt;
        require_once $path;&lt;br /&gt;
    } else {&lt;br /&gt;
        $controller = &#039;&#039;;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Create the controller&lt;br /&gt;
$classname    = &#039;HellosController&#039;.$controller;&lt;br /&gt;
$controller   = new $classname( );&lt;br /&gt;
&lt;br /&gt;
// Perform the Request task&lt;br /&gt;
$controller-&amp;gt;execute( JRequest::getVar( &#039;task&#039; ) );&lt;br /&gt;
&lt;br /&gt;
// Redirect if set by the controller&lt;br /&gt;
$controller-&amp;gt;redirect();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The view and model that we will start with is the hellos view and the hellos model. We will start with the model.&lt;br /&gt;
&lt;br /&gt;
==== The Hellos Model ====&lt;br /&gt;
&lt;br /&gt;
The Hellos Model will be very simple. The only operation that we currently need is the ability to retrieve the list of hellos from the database. This operation will be implemented in a method called getData().&lt;br /&gt;
&lt;br /&gt;
The JModel class has a built in protected method called _getList(). This method can be used to simplify the task of retrieving a list of records from the database. We simply need to pass it the query and it will return the list of records.&lt;br /&gt;
&lt;br /&gt;
At a later point in time, we might want to use our query from within another method. Therefore, we will create a private method called _buildQuery() which will return the query that will be passed to _getList(). This makes it easier to change the query as well since it is localized in one place.&lt;br /&gt;
&lt;br /&gt;
Therefore we need two methods in our class: getData() and _buildQuery().&lt;br /&gt;
&lt;br /&gt;
_buildQuery() simply returns the query. It looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source  lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Returns the query&lt;br /&gt;
 * @return string The query to be used to retrieve the rows from the database&lt;br /&gt;
 */&lt;br /&gt;
function _buildQuery()&lt;br /&gt;
{&lt;br /&gt;
    $query = &#039; SELECT * &#039;&lt;br /&gt;
           . &#039; FROM #__hello &#039;&lt;br /&gt;
    ;&lt;br /&gt;
&lt;br /&gt;
    return $query;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
getData() will obtain the query and retrieve the records from the database. Now it might happen that we need to retrieve this list of data twice in one page load. It would be a waste to have to query the database twice. Therefore, we will have this method store the data in a protected property so that on subsequent requests it can simply return the data it has already retrieved. This property will be called _data.&lt;br /&gt;
&lt;br /&gt;
Here is the getData() method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source  lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Retrieves the hello data&lt;br /&gt;
 * @return array Array of objects containing the data from the database&lt;br /&gt;
 */&lt;br /&gt;
function getData()&lt;br /&gt;
{&lt;br /&gt;
    // Lets load the data if it doesn&#039;t already exist&lt;br /&gt;
    if (empty( $this-&amp;gt;_data ))&lt;br /&gt;
    {&lt;br /&gt;
        $query = $this-&amp;gt;_buildQuery();&lt;br /&gt;
        $this-&amp;gt;_data = $this-&amp;gt;_getList( $query );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return $this-&amp;gt;_data;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The completed model looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * Hellos Model for Hello World Component&lt;br /&gt;
 * &lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_4&lt;br /&gt;
 * @license        GNU/GPL&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// Check to ensure this file is included in Joomla!&lt;br /&gt;
defined(&#039;_JEXEC&#039;) or die();&lt;br /&gt;
&lt;br /&gt;
jimport( &#039;joomla.application.component.model&#039; );&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Hello Model&lt;br /&gt;
 *&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 */&lt;br /&gt;
class HellosModelHellos extends JModel&lt;br /&gt;
{&lt;br /&gt;
    /**&lt;br /&gt;
     * Hellos data array&lt;br /&gt;
     *&lt;br /&gt;
     * @var array&lt;br /&gt;
     */&lt;br /&gt;
    var $_data;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Returns the query&lt;br /&gt;
     * @return string The query to be used to retrieve the rows from the database&lt;br /&gt;
     */&lt;br /&gt;
    function _buildQuery()&lt;br /&gt;
    {&lt;br /&gt;
        $query = &#039; SELECT * &#039;&lt;br /&gt;
            . &#039; FROM #__hello &#039;&lt;br /&gt;
        ;&lt;br /&gt;
        return $query;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Retrieves the hello data&lt;br /&gt;
     * @return array Array of objects containing the data from the database&lt;br /&gt;
     */&lt;br /&gt;
    function getData()&lt;br /&gt;
    {&lt;br /&gt;
        // Lets load the data if it doesn&#039;t already exist&lt;br /&gt;
        if (empty( $this-&amp;gt;_data ))&lt;br /&gt;
        {&lt;br /&gt;
            $query = $this-&amp;gt;_buildQuery();&lt;br /&gt;
            $this-&amp;gt;_data = $this-&amp;gt;_getList( $query );&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        return $this-&amp;gt;_data;&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This file is saved as models/hellos.php.&lt;br /&gt;
&lt;br /&gt;
==== The Hellos View ====&lt;br /&gt;
&lt;br /&gt;
Now that we have a model to retrieve our data, we need to display it. This view will be fairly similar to the view from the site section as well.&lt;br /&gt;
&lt;br /&gt;
Just as our model was automatically instantiated in the site, so it is in the administrator. Methods that start with get in the model can be accessed using the get() method of the JView class. So our view has three lines: one to retrieve the data from the model, one to push the data into the template, and one to invoke the display method to display the output. Thus we have:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * Hellos View for Hello World Component&lt;br /&gt;
 * &lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_4&lt;br /&gt;
 * @license        GNU/GPL&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// Check to ensure this file is included in Joomla!&lt;br /&gt;
defined(&#039;_JEXEC&#039;) or die();&lt;br /&gt;
&lt;br /&gt;
jimport( &#039;joomla.application.component.view&#039; );&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Hellos View&lt;br /&gt;
 *&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 */&lt;br /&gt;
class HellosViewHellos extends JView&lt;br /&gt;
{&lt;br /&gt;
    /**&lt;br /&gt;
     * Hellos view display method&lt;br /&gt;
     * @return void&lt;br /&gt;
     **/&lt;br /&gt;
    function display($tpl = null)&lt;br /&gt;
    {&lt;br /&gt;
        JToolBarHelper::title( JText::_( &#039;Hello Manager&#039; ), &#039;generic.png&#039; );&lt;br /&gt;
        JToolBarHelper::deleteList();&lt;br /&gt;
        JToolBarHelper::editListX();&lt;br /&gt;
        JToolBarHelper::addNewX();&lt;br /&gt;
&lt;br /&gt;
        // Get data from the model&lt;br /&gt;
        $items =&amp;amp; $this-&amp;gt;get( &#039;Data&#039;);&lt;br /&gt;
&lt;br /&gt;
        $this-&amp;gt;assignRef( &#039;items&#039;, $items );&lt;br /&gt;
&lt;br /&gt;
        parent::display($tpl);&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This file is saved as views/hellos/view.html.php.&lt;br /&gt;
&lt;br /&gt;
==== The Hellos Template ====&lt;br /&gt;
&lt;br /&gt;
The template will take the data pushed into it from the view and produce the output. We will display our output in a simple table. While the frontend template was very simple, in the administrator we will need a minimal amount of extra logic to handle looping through the data.&lt;br /&gt;
&lt;br /&gt;
Here is our template:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;); ?&amp;gt;&lt;br /&gt;
&amp;lt;form action=&amp;quot;index.php&amp;quot; method=&amp;quot;post&amp;quot; name=&amp;quot;adminForm&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;div id=&amp;quot;editcell&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;table class=&amp;quot;adminlist&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;thead&amp;gt;&lt;br /&gt;
        &amp;lt;tr&amp;gt;&lt;br /&gt;
            &amp;lt;th width=&amp;quot;5&amp;quot;&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo JText::_( &#039;ID&#039; ); ?&amp;gt;&lt;br /&gt;
            &amp;lt;/th&amp;gt;&lt;br /&gt;
            &amp;lt;th&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo JText::_( &#039;Greeting&#039; ); ?&amp;gt;&lt;br /&gt;
            &amp;lt;/th&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;            &lt;br /&gt;
    &amp;lt;/thead&amp;gt;&lt;br /&gt;
    &amp;lt;?php&lt;br /&gt;
    $k = 0;&lt;br /&gt;
    for ($i=0, $n=count( $this-&amp;gt;items ); $i &amp;lt; $n; $i++)&lt;br /&gt;
    {&lt;br /&gt;
        $row =&amp;amp; $this-&amp;gt;items[$i];&lt;br /&gt;
        ?&amp;gt;&lt;br /&gt;
        &amp;lt;tr class=&amp;quot;&amp;lt;?php echo &amp;quot;row$k&amp;quot;; ?&amp;gt;&amp;quot;&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo $row-&amp;gt;id; ?&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo $row-&amp;gt;greeting; ?&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;&lt;br /&gt;
        &amp;lt;?php&lt;br /&gt;
        $k = 1 - $k;&lt;br /&gt;
    }&lt;br /&gt;
    ?&amp;gt;&lt;br /&gt;
    &amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;option&amp;quot; value=&amp;quot;com_hello&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;task&amp;quot; value=&amp;quot;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;boxchecked&amp;quot; value=&amp;quot;0&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;controller&amp;quot; value=&amp;quot;hello&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/form&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This template is saved as views/hellos/tmpl/default.php.&lt;br /&gt;
&lt;br /&gt;
You will notice that our output is enclosed in a form. Though this is not necessary now, it will be soon.&lt;br /&gt;
&lt;br /&gt;
We have now completed the basic part of the first view. We have added five files to the admin section of our component:&lt;br /&gt;
&lt;br /&gt;
* hello.php&lt;br /&gt;
* controller.php&lt;br /&gt;
* models/hellos.php&lt;br /&gt;
* views/hellos/view.html.php&lt;br /&gt;
* views/hellos/tmpl/default.php&lt;br /&gt;
&lt;br /&gt;
You can now add these files to the XML install file and give it a try!&lt;br /&gt;
&lt;br /&gt;
== Adding Functionality ==&lt;br /&gt;
&lt;br /&gt;
So far our administrator section is pretty useless. It doesn&#039;t really do anything - all it does is display the entries that we have in our database.&lt;br /&gt;
&lt;br /&gt;
In order to make it useful, we need to add some buttons and links.&lt;br /&gt;
&lt;br /&gt;
==== The Toolbar ====&lt;br /&gt;
&lt;br /&gt;
You may have noticed the toolbar that appears at the top of other Joomla! component administrator panels. Our component needs one as well. Joomla! makes this very easy to do. We will add buttons Delete records, Edit records, and create New records. We will also add a title that will be displayed on our toolbar.&lt;br /&gt;
&lt;br /&gt;
This is done by adding code to the view. To add the buttons, we use static methods from the Joomla! JToolBarHelper class. The code looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;JToolBarHelper::title(   JText::_( &#039;Hello Manager&#039; ), &#039;generic.png&#039; );&lt;br /&gt;
JToolBarHelper::deleteList();&lt;br /&gt;
JToolBarHelper::editListX();&lt;br /&gt;
JToolBarHelper::addNewX();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
These three methods will create the appropriate buttons. The deleteList() method can optionally take up to three parameters - the first parameter is a string to display to the user to confirm that they want to delete the records. The second is the task that should be sent with the query (the default is &#039;remove&#039;), and the third is the text that should be displayed below the button.&lt;br /&gt;
&lt;br /&gt;
The editListX() and addNewX() methods can each take two optional parameters. The first is the task (which are by default edit and add, respectively), and the second is the text that should be displayed below the button.&lt;br /&gt;
&lt;br /&gt;
*You may have noticed the use of the JText::_ method in the template before and here as well. This is a handy function that makes component translation much easier. The JText::_ method will look up the string in your component language file and return the translated string. If not translation is found, it will return the string that you passed it. If you want to translate your component into another language, all you have to do is create a language file that will map the strings within the quotes to the translated version of the string.&lt;br /&gt;
&lt;br /&gt;
==== Checkboxes and Links ====&lt;br /&gt;
&lt;br /&gt;
We now have buttons. Two of those buttons operate on existing records. But how do we know which records to operate on? We have to let the user tell us. To do this, we need to add checkboxes to our table so that the user can select certain records. This is done in our template.&lt;br /&gt;
&lt;br /&gt;
In order to the add the checkboxes, we need to add an extra column into our table. We will add the column in between the two that we already have.&lt;br /&gt;
&lt;br /&gt;
In the header of the column, we will add a checkbox which can be used to toggle all the boxes below it on or off:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;th width=&amp;quot;20&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;input type=&amp;quot;checkbox&amp;quot; name=&amp;quot;toggle&amp;quot; value=&amp;quot;&amp;quot; onclick=&amp;quot;checkAll(&amp;lt;?php echo count( $this-&amp;gt;items ); ?&amp;gt;);&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/th&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Javascript checkAll function is a function that is built into the Joomla! base Javascript package that provides the functionality that we want here.&lt;br /&gt;
&lt;br /&gt;
Now we need to add the checkboxes into the individual rows. Joomla!&#039;s JHTML class has a method, JHTML::_(), which will generate our checkbox for us. We will add the following line to our loop:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$checked    = JHTML::_( &#039;grid.id&#039;, $i, $row-&amp;gt;id );&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
after the line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$row =&amp;amp; $this-&amp;gt;items[$i];&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then we will add a cell in between the two that we already have:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;td&amp;gt;&lt;br /&gt;
    &amp;lt;?php echo $checked; ?&amp;gt;&lt;br /&gt;
&amp;lt;/td&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It can be cumbersome to have to check the box that we want to edit and then move up and click the edit button. Therefore, we will add a link that it will go straight to the greeting&#039;s edit form. We will add the following line after the call to the JHTML::_() method to generate the link HTML:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$link = JRoute::_( &#039;index.php?option=com_hello&amp;gt;controller=hello&amp;gt;task=edit&amp;gt;cid[]=&#039;. $row-&amp;gt;id );&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And we include the link in the cell showing the greeting text:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;td&amp;gt;&lt;br /&gt;
    &amp;lt;a href=&amp;quot;&amp;lt;?php echo $link; ?&amp;gt;&amp;quot;&amp;gt;&amp;lt;?php echo $row-&amp;gt;greeting; ?&amp;gt;&amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;lt;/td&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You will notice that this link points to the hello controller. This controller will handle the data manipulation of our greetings.&lt;br /&gt;
&lt;br /&gt;
If you recall from above, we had four hidden input fields at the bottom of our form. The first input field was named &#039;option&#039;. This field is necessary so that we stay in our component. The second input field was task. This form property gets set when one of the buttons in the toolbar is clicked. A Javascript error will result and the buttons will not work if this input field is omitted. The third input field is the boxchecked field. This field keeps track of the number of boxes that are checked. The edit and delete buttons will check to ensure that this is greater than zero and will not allow the form to be submitted if it is not. The fourth input field is the controller field. This is used to specify that tasks fired from this form will be handled by the hello controller.&lt;br /&gt;
&lt;br /&gt;
Here is the code for the completed default.php file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;); ?&amp;gt;&lt;br /&gt;
&amp;lt;form action=&amp;quot;index.php&amp;quot; method=&amp;quot;post&amp;quot; name=&amp;quot;adminForm&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;div id=&amp;quot;editcell&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;table class=&amp;quot;adminlist&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;thead&amp;gt;&lt;br /&gt;
        &amp;lt;tr&amp;gt;&lt;br /&gt;
            &amp;lt;th width=&amp;quot;5&amp;quot;&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo JText::_( &#039;ID&#039; ); ?&amp;gt;&lt;br /&gt;
            &amp;lt;/th&amp;gt;&lt;br /&gt;
            &amp;lt;th&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo JText::_( &#039;Greeting&#039; ); ?&amp;gt;&lt;br /&gt;
            &amp;lt;/th&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;            &lt;br /&gt;
    &amp;lt;/thead&amp;gt;&lt;br /&gt;
    &amp;lt;?php&lt;br /&gt;
    $k = 0;&lt;br /&gt;
    for ($i=0, $n=count( $this-&amp;gt;items ); $i &amp;lt; $n; $i++)&lt;br /&gt;
    {&lt;br /&gt;
        $row =&amp;amp; $this-&amp;gt;items[$i];&lt;br /&gt;
        ?&amp;gt;&lt;br /&gt;
        &amp;lt;tr class=&amp;quot;&amp;lt;?php echo &amp;quot;row$k&amp;quot;; ?&amp;gt;&amp;quot;&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo $row-&amp;gt;id; ?&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo $row-&amp;gt;greeting; ?&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;&lt;br /&gt;
        &amp;lt;?php&lt;br /&gt;
        $k = 1 - $k;&lt;br /&gt;
    }&lt;br /&gt;
    ?&amp;gt;&lt;br /&gt;
    &amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;option&amp;quot; value=&amp;quot;com_hello&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;task&amp;quot; value=&amp;quot;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;boxchecked&amp;quot; value=&amp;quot;0&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;controller&amp;quot; value=&amp;quot;hello&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/form&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Our hellos view is now complete. You can try out the component now to see the results.&lt;br /&gt;
&lt;br /&gt;
The component can be downloaded at: [[http://dev.joomla.org/components/com_jd-wiki/data/media/components/com_hello4a.zip|com_hello4a.zip]]&lt;br /&gt;
&lt;br /&gt;
== Getting Down and Dirty: Doing the Real Work ==&lt;br /&gt;
&lt;br /&gt;
Now that the Hellos view is done, it is time to move to the Hello view and model. This is where the real work will get done.&lt;br /&gt;
&lt;br /&gt;
==== The Hello Controller ====&lt;br /&gt;
&lt;br /&gt;
Our default controller just isn&#039;t going to cut it when it comes to doing work - all it is capable of doing is displaying views.&lt;br /&gt;
&lt;br /&gt;
We need to be able to handle the tasks that we are launching from the Hellos view: add, edit and remove.&lt;br /&gt;
&lt;br /&gt;
Add and edit are essentially the same task: they both display a form to the user that allows a greeting to be edited. The only difference is that new displays a blank form, and edit displays a form with data already in it. Since they are similar, we will map the add task onto the edit task handler. This is specified in our constructor:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * constructor (registers additional tasks to methods)&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function __construct()&lt;br /&gt;
{&lt;br /&gt;
    parent::__construct();&lt;br /&gt;
&lt;br /&gt;
    // Register Extra tasks&lt;br /&gt;
    $this-&amp;gt;registerTask( &#039;add&#039;  ,     &#039;edit&#039; );&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first parameter of JController::registerTask is the task to map, and the second is the method to map it to.&lt;br /&gt;
&lt;br /&gt;
We will start with handling the edit task. The controller&#039;s job is fairly simple for the edit task. All it has to do is specify the view and layout to load (the hello view and the form layout). We will also tell Joomla! to disable the mainmenu while we are editing our greeting. This prevents users from leaving unsaved records open.&lt;br /&gt;
&lt;br /&gt;
Our edit task handler looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * display the edit form&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function edit()&lt;br /&gt;
{&lt;br /&gt;
    JRequest::setVar( &#039;view&#039;, &#039;hello&#039; );&lt;br /&gt;
    JRequest::setVar( &#039;layout&#039;, &#039;form&#039;  );&lt;br /&gt;
    JRequest::setVar(&#039;hidemainmenu&#039;, 1);&lt;br /&gt;
&lt;br /&gt;
    parent::display();&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== The Hello View ====&lt;br /&gt;
&lt;br /&gt;
The Hello view will display a form which will allow the user to edit a greeting. The display method if the hello view has to do a few simple tasks:&lt;br /&gt;
&lt;br /&gt;
  * retrieve the data from the model&lt;br /&gt;
&lt;br /&gt;
  * create the toolbar&lt;br /&gt;
&lt;br /&gt;
  * pass the data into the template&lt;br /&gt;
&lt;br /&gt;
  * invoke the display() method to render the template&lt;br /&gt;
&lt;br /&gt;
This becomes a bit more complicated because the one view handles both the edit and add tasks. In our toolbar we want the user to know what whether they are adding or editing, so we have to determine which task was fired.&lt;br /&gt;
&lt;br /&gt;
Since we are already retrieving the record that we want to display from the model, we can use this data to determine what task was fired. If the task was edit, then the id field of our record will have been set. If the task was new, then it will not have been set. This can be used to determine if we have a new record or an existing record.&lt;br /&gt;
&lt;br /&gt;
We will add two buttons to the toolbar: save and cancel. Though the functionality will be the same, we want to display different buttons depending on whether it is a new or existing record. If it is a new record, we will display cancel. If it already exists, we will display close.&lt;br /&gt;
&lt;br /&gt;
Thus our display method looks like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * display method of Hello view&lt;br /&gt;
 * @return void&lt;br /&gt;
 **/&lt;br /&gt;
function display($tpl = null)&lt;br /&gt;
{&lt;br /&gt;
    //get the hello&lt;br /&gt;
    $hello        =&amp;amp; $this-&amp;gt;get(&#039;Data&#039;);&lt;br /&gt;
    $isNew        = ($hello-&amp;gt;id &amp;lt; 1);&lt;br /&gt;
&lt;br /&gt;
    $text = $isNew ? JText::_( &#039;New&#039; ) : JText::_( &#039;Edit&#039; );&lt;br /&gt;
    JToolBarHelper::title(   JText::_( &#039;Hello&#039; ).&#039;: &amp;lt;small&amp;gt;&amp;lt;small&amp;gt;[ &#039; . $text.&#039; ]&amp;lt;/small&amp;gt;&amp;lt;/small&amp;gt;&#039; );&lt;br /&gt;
    JToolBarHelper::save();&lt;br /&gt;
    if ($isNew)  {&lt;br /&gt;
        JToolBarHelper::cancel();&lt;br /&gt;
    } else {&lt;br /&gt;
        // for existing items the button is renamed `close`&lt;br /&gt;
        JToolBarHelper::cancel( &#039;cancel&#039;, &#039;Close&#039; );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    $this-&amp;gt;assignRef(&#039;hello&#039;, $hello);&lt;br /&gt;
    parent::display($tpl);&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== The Hello Model ====&lt;br /&gt;
&lt;br /&gt;
Our view needs data. Therefore, we need to create a model to model a hello.&lt;br /&gt;
&lt;br /&gt;
Our model will have two properties: _id and _data. _id will hold the id of the greeting and data will hold the data.&lt;br /&gt;
&lt;br /&gt;
We will start with a constructor, which will attempt to retrieve the id from the query:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Constructor that retrieves the ID from the request&lt;br /&gt;
 *&lt;br /&gt;
 * @access    public&lt;br /&gt;
 * @return    void&lt;br /&gt;
 */&lt;br /&gt;
function __construct()&lt;br /&gt;
{&lt;br /&gt;
    parent::__construct();&lt;br /&gt;
&lt;br /&gt;
    $array = JRequest::getVar(&#039;cid&#039;,  0, &#039;&#039;, &#039;array&#039;);&lt;br /&gt;
    $this-&amp;gt;setId((int)$array[0]);&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The JRequest::getVar() method is used to retrieve data from the request. The first parameter is the name of the form variable. The second parameter is the default value to assign if there is no value found. The third parameter is the name of the hash to retrieve the value from (get, post, etc), and the last value is the data type that should be forced on the value.&lt;br /&gt;
&lt;br /&gt;
Our constructor will take the first value from the cid array and assign it to the id.&lt;br /&gt;
&lt;br /&gt;
Our setId() method can be used to set our id. Changing the id that our model points to will mean the id points to the wrong data. Therefore, when we set the id, we will clear the data property:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Method to set the hello identifier&lt;br /&gt;
 *&lt;br /&gt;
 * @access    public&lt;br /&gt;
 * @param    int Hello identifier&lt;br /&gt;
 * @return    void&lt;br /&gt;
 */&lt;br /&gt;
function setId($id)&lt;br /&gt;
{&lt;br /&gt;
    // Set id and wipe data&lt;br /&gt;
    $this-&amp;gt;_id        = $id;&lt;br /&gt;
    $this-&amp;gt;_data    = null;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finally, we need a method to retrieve our data: getData()&lt;br /&gt;
&lt;br /&gt;
getData will check if the _data property has already been set. If it has, it will simply return it. Otherwise, it will load the data from the database.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Method to get a hello&lt;br /&gt;
 * @return object with data&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
function &amp;amp;getData()&lt;br /&gt;
{&lt;br /&gt;
    // Load the data&lt;br /&gt;
    if (empty( $this-&amp;gt;_data )) {&lt;br /&gt;
        $query = &#039; SELECT * FROM #__hello &#039;.&lt;br /&gt;
                &#039;  WHERE id = &#039;.$this-&amp;gt;_id;&lt;br /&gt;
        $this-&amp;gt;_db-&amp;gt;setQuery( $query );&lt;br /&gt;
        $this-&amp;gt;_data = $this-&amp;gt;_db-&amp;gt;loadObject();&lt;br /&gt;
    }&lt;br /&gt;
    if (!$this-&amp;gt;_data) {&lt;br /&gt;
        $this-&amp;gt;_data = new stdClass();&lt;br /&gt;
        $this-&amp;gt;_data-&amp;gt;id = 0;&lt;br /&gt;
        $this-&amp;gt;_data-&amp;gt;greeting = null;&lt;br /&gt;
    }&lt;br /&gt;
    return $this-&amp;gt;_data;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== The Form ====&lt;br /&gt;
&lt;br /&gt;
Now all that is left is to create the form that the data will go into. Since we specified our layout as form, the form will go in a file in the tmpl directory of the hello view called form.php:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;); ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;form action=&amp;quot;index.php&amp;quot; method=&amp;quot;post&amp;quot; name=&amp;quot;adminForm&amp;quot; id=&amp;quot;adminForm&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;div class=&amp;quot;col100&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;fieldset class=&amp;quot;adminform&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;legend&amp;gt;&amp;lt;?php echo JText::_( &#039;Details&#039; ); ?&amp;gt;&amp;lt;/legend&amp;gt;&lt;br /&gt;
        &amp;lt;table class=&amp;quot;admintable&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;tr&amp;gt;&lt;br /&gt;
            &amp;lt;td width=&amp;quot;100&amp;quot; align=&amp;quot;right&amp;quot; class=&amp;quot;key&amp;quot;&amp;gt;&lt;br /&gt;
                &amp;lt;label for=&amp;quot;greeting&amp;quot;&amp;gt;&lt;br /&gt;
                    &amp;lt;?php echo JText::_( &#039;Greeting&#039; ); ?&amp;gt;:&lt;br /&gt;
                &amp;lt;/label&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;input class=&amp;quot;text_area&amp;quot; type=&amp;quot;text&amp;quot; name=&amp;quot;greeting&amp;quot; id=&amp;quot;greeting&amp;quot; size=&amp;quot;32&amp;quot; maxlength=&amp;quot;250&amp;quot; value=&amp;quot;&amp;lt;?php echo $this-&amp;gt;hello-&amp;gt;greeting;?&amp;gt;&amp;quot; /&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;&lt;br /&gt;
    &amp;lt;/table&amp;gt;&lt;br /&gt;
    &amp;lt;/fieldset&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;clr&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;option&amp;quot; value=&amp;quot;com_hello&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;id&amp;quot; value=&amp;quot;&amp;lt;?php echo $this-&amp;gt;hello-&amp;gt;id; ?&amp;gt;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;task&amp;quot; value=&amp;quot;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;controller&amp;quot; value=&amp;quot;hello&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/form&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in addition to the input field, there is a hidden field for the id. The user doesn&#039;t need to edit the id (and shouldn&#039;t), so we silently pass it along in the form.&lt;br /&gt;
&lt;br /&gt;
==== Implementing the Functionality ====&lt;br /&gt;
&lt;br /&gt;
So far, our controller only handles two tasks: edit and new. However, we also have buttons to save, delete and cancel records. We need to write code to handle and perform these tasks.&lt;br /&gt;
&lt;br /&gt;
=== Saving a Record ===&lt;br /&gt;
&lt;br /&gt;
The logical next step is to implement the functionality to save a record. Normally, this would require some switches and logic to handle various cases, such as the difference between creating a new record (an INSERT query), and updating an existing query (an UPDATE query). Also, there are complexities involved in getting the data from the form and putting it into the query.&lt;br /&gt;
&lt;br /&gt;
The Joomla! framework takes care of a lot of this for you. The JTable class makes it easy to manipulate records in the database without having to worry about writing the SQL code that lies behind these updates. It also makes it easy to transfer data from an HTML form into the database.&lt;br /&gt;
&lt;br /&gt;
== Creating the Table Class ==&lt;br /&gt;
&lt;br /&gt;
The JTable class is an abstract class from which you can derive child classes to work with specific tables. To use it, you simply create a class that extends the JTable class, add your database fields as properties, and override the constructor to specify the name of the table and the primary key.&lt;br /&gt;
&lt;br /&gt;
Here is our JTable class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * Hello World table class&lt;br /&gt;
 * &lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://dev.joomla.org/component/option,com_jd-wiki/Itemid,31/id,tutorials:components/&lt;br /&gt;
 * @license        GNU/GPL&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// no direct access&lt;br /&gt;
defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;);&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Hello Table class&lt;br /&gt;
 *&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 */&lt;br /&gt;
class TableHello extends JTable&lt;br /&gt;
{&lt;br /&gt;
    /**&lt;br /&gt;
     * Primary Key&lt;br /&gt;
     *&lt;br /&gt;
     * @var int&lt;br /&gt;
     */&lt;br /&gt;
    var $id = null;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * @var string&lt;br /&gt;
     */&lt;br /&gt;
    var $greeting = null;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Constructor&lt;br /&gt;
     *&lt;br /&gt;
     * @param object Database connector object&lt;br /&gt;
     */&lt;br /&gt;
    function TableHello( &amp;amp;$db ) {&lt;br /&gt;
        parent::__construct(&#039;#__hello&#039;, &#039;id&#039;, $db);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You will see here that we have defined our two fields: the id field and the greeting field. Then we have defined a constructor, which will call the constructor of the parent class and pass it the name of the table (hello), the name of the field which is the primary key (id), and the database connector object.&lt;br /&gt;
&lt;br /&gt;
This file should be called hello.php and it will go in a directory called tables in the administrator section of our component.&lt;br /&gt;
&lt;br /&gt;
== Implementing the Function in our Model ==&lt;br /&gt;
&lt;br /&gt;
We are now ready to add the method to the model which will save our record. We will call this method store. Our store() method will do three things: bind the data from the form to the TableHello object, check to ensure that the record is properly formed, and store the record in the database.&lt;br /&gt;
&lt;br /&gt;
Our method looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Method to store a record&lt;br /&gt;
 *&lt;br /&gt;
 * @access    public&lt;br /&gt;
 * @return    boolean    True on success&lt;br /&gt;
 */&lt;br /&gt;
function store()&lt;br /&gt;
{&lt;br /&gt;
    $row =&amp;amp; $this-&amp;gt;getTable();&lt;br /&gt;
&lt;br /&gt;
    $data = JRequest::get( &#039;post&#039; );&lt;br /&gt;
    // Bind the form fields to the hello table&lt;br /&gt;
    if (!$row-&amp;gt;bind($data)) {&lt;br /&gt;
        $this-&amp;gt;setError($this-&amp;gt;_db-&amp;gt;getErrorMsg());&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // Make sure the hello record is valid&lt;br /&gt;
    if (!$row-&amp;gt;check()) {&lt;br /&gt;
        $this-&amp;gt;setError($this-&amp;gt;_db-&amp;gt;getErrorMsg());&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // Store the web link table to the database&lt;br /&gt;
    if (!$row-&amp;gt;store()) {&lt;br /&gt;
        $this-&amp;gt;setError($this-&amp;gt;_db-&amp;gt;getErrorMsg());&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return true;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This method gets added to the hello model.&lt;br /&gt;
&lt;br /&gt;
The method takes one parameter, which is an associative array of data that we want to store in the database. This can easily be retrieved from the request as will be seen later.&lt;br /&gt;
&lt;br /&gt;
You will see that the first line retrieves a reference to our JTable object. If we name our table properly, we don&#039;t have to specify its name - the JModel class knows where to find it. You may recall that we called our table class TableHello and put it in a file called hello.php in the tables directory. If you follow this convention, the JModel class can create your object automatically.&lt;br /&gt;
&lt;br /&gt;
The second line will retrieve the data from the form. The JRequest class makes this very easy. In this case, we are retrieving all of the variables that were submitted using the &#039;POST&#039; method. These will be returned as an associative array.&lt;br /&gt;
&lt;br /&gt;
The rest is easy - we bind, check and store. The bind() method will copy values from the array into the corresponding property of the table object. In this case, it will take the values of id and greeting and copy them to our TableHello object.&lt;br /&gt;
&lt;br /&gt;
The check() method will perform data verification. In the JTable() class, this method simply returns true. While this doesn&#039;t provide any value for us currently, by calling this method we make it possible to do data checking using our TableHello class in the future. This method can be overridden in our TableHello class with a method that performs the appropriate checks.&lt;br /&gt;
&lt;br /&gt;
The store() method will take the data that is in the object and store it in the database. If the id is 0, it will create a new record (INSERT), otherwise, it will update the existing record (UPDATE).&lt;br /&gt;
&lt;br /&gt;
== Adding the Task to the Controller ==&lt;br /&gt;
&lt;br /&gt;
We are now ready to add our task to the controller. Since the task that we are firing is called &#039;save&#039;, we must call our method &#039;save&#039;. This is simple:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * save a record (and redirect to main page)&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function save()&lt;br /&gt;
{&lt;br /&gt;
    $model = $this-&amp;gt;getModel(&#039;hello&#039;);&lt;br /&gt;
&lt;br /&gt;
    if ($model-&amp;gt;store()) {&lt;br /&gt;
        $msg = JText::_( &#039;Greeting Saved!&#039; );&lt;br /&gt;
    } else {&lt;br /&gt;
        $msg = JText::_( &#039;Error Saving Greeting&#039; );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // Check the table in so it can be edited.... we are done with it anyway&lt;br /&gt;
    $link = &#039;index.php?option=com_hello&#039;;&lt;br /&gt;
    $this-&amp;gt;setRedirect($link, $msg);&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All we do is get our model and invoke the store() method. Then we use the setRedirect() method to redirect back to our list of greetings. We also pass a message along, which will be displayed at the top of the page.&lt;br /&gt;
&lt;br /&gt;
=== Deleting a Record ===&lt;br /&gt;
&lt;br /&gt;
=== Implementing the Function in the Model ===&lt;br /&gt;
&lt;br /&gt;
In the model, we will retrieve the list of IDs to delete and call the JTable class to delete them. Here it is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Method to delete record(s)&lt;br /&gt;
 *&lt;br /&gt;
 * @access    public&lt;br /&gt;
 * @return    boolean    True on success&lt;br /&gt;
 */&lt;br /&gt;
function delete()&lt;br /&gt;
{&lt;br /&gt;
    $cids = JRequest::getVar( &#039;cid&#039;, array(0), &#039;post&#039;, &#039;array&#039; );&lt;br /&gt;
    $row =&amp;amp; $this-&amp;gt;getTable();&lt;br /&gt;
&lt;br /&gt;
    foreach($cids as $cid) {&lt;br /&gt;
        if (!$row-&amp;gt;delete( $cid )) {&lt;br /&gt;
            $this-&amp;gt;setError( $row-&amp;gt;getErrorMsg() );&lt;br /&gt;
            return false;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return true;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We invoke the JRequest::getVar() method to get the data from the request, then we invoke the $row-&amp;gt;delete() method to delete each row. By storing errors in the model we make it possible to retrieve them later if we so choose.&lt;br /&gt;
&lt;br /&gt;
=== Handling the Remove Task in the Controller ===&lt;br /&gt;
&lt;br /&gt;
This is similar to the save() method which handled the save task:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * remove record(s)&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function remove()&lt;br /&gt;
{&lt;br /&gt;
    $model = $this-&amp;gt;getModel(&#039;hello&#039;);&lt;br /&gt;
    if(!$model-&amp;gt;delete()) {&lt;br /&gt;
        $msg = JText::_( &#039;Error: One or More Greetings Could not be Deleted&#039; );&lt;br /&gt;
    } else {&lt;br /&gt;
        $msg = JText::_( &#039;Greeting(s) Deleted&#039; );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    $this-&amp;gt;setRedirect( &#039;index.php?option=com_hello&#039;, $msg );&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Cancelling the Edit Operation ====&lt;br /&gt;
&lt;br /&gt;
To cancel the edit operation, all we have to do is redirect back to the main view:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * cancel editing a record&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function cancel()&lt;br /&gt;
{&lt;br /&gt;
    $msg = JText::_( &#039;Operation Cancelled&#039; );&lt;br /&gt;
    $this-&amp;gt;setRedirect( &#039;index.php?option=com_hello&#039;, $msg );&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
We have now implemented a basic backend to our component. We are now able to edit the entries that are viewed in the frontend. We have demonstrated the interaction between models, views and controllers. We have shown how the JTable class can be extended to provide easy access to tables in the database. It can also be seen how the JToolBarHelper class can be used to create button bars in components to present a standardized look between components.&lt;br /&gt;
&lt;br /&gt;
== Contributors ==&lt;br /&gt;
* staalanden&lt;br /&gt;
&lt;br /&gt;
== Download ==&lt;br /&gt;
&lt;br /&gt;
The component can be downloaded at: [http://joomlacode.org/gf/download/frsrelease/8104/29402/com_hello4a_01.zip com_hello4a_01]&lt;/div&gt;</summary>
		<author><name>Tonie</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Creating_an_Administrator_Interface&amp;diff=9508</id>
		<title>J1.5:Developing a MVC Component/Creating an Administrator Interface</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Creating_an_Administrator_Interface&amp;diff=9508"/>
		<updated>2008-07-27T19:47:13Z</updated>

		<summary type="html">&lt;p&gt;Tonie: /* The Hellos View */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
In the first three tutorials, we have developed a MVC component that retrieves its data from a table in the database. Currently, there is no way to add data to the database except to do it manually using another tool. In this tutorial, we will develop an administrator section for our component which will make it possible to manage the entries in the database.&lt;br /&gt;
&lt;br /&gt;
== Creating the Basic Framework ==&lt;br /&gt;
&lt;br /&gt;
The basic framework of the administrator panel is very similar to the site portion. The main entry point for the administrator section of the component is hello.php. This file is identical to the hello.php file that was used in the site portion except the name of the controller it loads will be changed to HellosController. The default controller is also called controller.php and this file is identical to the default controller in the site portion, with the exception that the controller is named HellosController instead of HelloController. This difference is so that JController will by default load the hellos view, which will display a list of our greetings.&lt;br /&gt;
&lt;br /&gt;
Here is the listing for hello.php:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_4&lt;br /&gt;
 * @license    GNU/GPL&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
// No direct access&lt;br /&gt;
&lt;br /&gt;
defined( &#039;_JEXEC&#039; ) or die( &#039;Restricted access&#039; );&lt;br /&gt;
&lt;br /&gt;
// Require the base controller&lt;br /&gt;
&lt;br /&gt;
require_once( JPATH_COMPONENT.DS.&#039;controller.php&#039; );&lt;br /&gt;
&lt;br /&gt;
// Require specific controller if requested&lt;br /&gt;
if($controller = JRequest::getWord(&#039;controller&#039;)) {&lt;br /&gt;
    $path = JPATH_COMPONENT.DS.&#039;controllers&#039;.DS.$controller.&#039;.php&#039;;&lt;br /&gt;
    if (file_exists($path)) {&lt;br /&gt;
        require_once $path;&lt;br /&gt;
    } else {&lt;br /&gt;
        $controller = &#039;&#039;;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Create the controller&lt;br /&gt;
$classname    = &#039;HellosController&#039;.$controller;&lt;br /&gt;
$controller   = new $classname( );&lt;br /&gt;
&lt;br /&gt;
// Perform the Request task&lt;br /&gt;
$controller-&amp;gt;execute( JRequest::getVar( &#039;task&#039; ) );&lt;br /&gt;
&lt;br /&gt;
// Redirect if set by the controller&lt;br /&gt;
$controller-&amp;gt;redirect();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The view and model that we will start with is the hellos view and the hellos model. We will start with the model.&lt;br /&gt;
&lt;br /&gt;
==== The Hellos Model ====&lt;br /&gt;
&lt;br /&gt;
The Hellos Model will be very simple. The only operation that we currently need is the ability to retrieve the list of hellos from the database. This operation will be implemented in a method called getData().&lt;br /&gt;
&lt;br /&gt;
The JModel class has a built in protected method called _getList(). This method can be used to simplify the task of retrieving a list of records from the database. We simply need to pass it the query and it will return the list of records.&lt;br /&gt;
&lt;br /&gt;
At a later point in time, we might want to use our query from within another method. Therefore, we will create a private method called _buildQuery() which will return the query that will be passed to _getList(). This makes it easier to change the query as well since it is localized in one place.&lt;br /&gt;
&lt;br /&gt;
Therefore we need two methods in our class: getData() and _buildQuery().&lt;br /&gt;
&lt;br /&gt;
_buildQuery() simply returns the query. It looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source  lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Returns the query&lt;br /&gt;
 * @return string The query to be used to retrieve the rows from the database&lt;br /&gt;
 */&lt;br /&gt;
function _buildQuery()&lt;br /&gt;
{&lt;br /&gt;
    $query = &#039; SELECT * &#039;&lt;br /&gt;
           . &#039; FROM #__hello &#039;&lt;br /&gt;
    ;&lt;br /&gt;
&lt;br /&gt;
    return $query;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
getData() will obtain the query and retrieve the records from the database. Now it might happen that we need to retrieve this list of data twice in one page load. It would be a waste to have to query the database twice. Therefore, we will have this method store the data in a protected property so that on subsequent requests it can simply return the data it has already retrieved. This property will be called _data.&lt;br /&gt;
&lt;br /&gt;
Here is the getData() method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source  lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Retrieves the hello data&lt;br /&gt;
 * @return array Array of objects containing the data from the database&lt;br /&gt;
 */&lt;br /&gt;
function getData()&lt;br /&gt;
{&lt;br /&gt;
    // Lets load the data if it doesn&#039;t already exist&lt;br /&gt;
    if (empty( $this-&amp;gt;_data ))&lt;br /&gt;
    {&lt;br /&gt;
        $query = $this-&amp;gt;_buildQuery();&lt;br /&gt;
        $this-&amp;gt;_data = $this-&amp;gt;_getList( $query );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return $this-&amp;gt;_data;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The completed model looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * Hellos Model for Hello World Component&lt;br /&gt;
 * &lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_4&lt;br /&gt;
 * @license        GNU/GPL&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// Check to ensure this file is included in Joomla!&lt;br /&gt;
defined(&#039;_JEXEC&#039;) or die();&lt;br /&gt;
&lt;br /&gt;
jimport( &#039;joomla.application.component.model&#039; );&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Hello Model&lt;br /&gt;
 *&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 */&lt;br /&gt;
class HellosModelHellos extends JModel&lt;br /&gt;
{&lt;br /&gt;
    /**&lt;br /&gt;
     * Hellos data array&lt;br /&gt;
     *&lt;br /&gt;
     * @var array&lt;br /&gt;
     */&lt;br /&gt;
    var $_data;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Returns the query&lt;br /&gt;
     * @return string The query to be used to retrieve the rows from the database&lt;br /&gt;
     */&lt;br /&gt;
    function _buildQuery()&lt;br /&gt;
    {&lt;br /&gt;
        $query = &#039; SELECT * &#039;&lt;br /&gt;
            . &#039; FROM #__hello &#039;&lt;br /&gt;
        ;&lt;br /&gt;
        return $query;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Retrieves the hello data&lt;br /&gt;
     * @return array Array of objects containing the data from the database&lt;br /&gt;
     */&lt;br /&gt;
    function getData()&lt;br /&gt;
    {&lt;br /&gt;
        // Lets load the data if it doesn&#039;t already exist&lt;br /&gt;
        if (empty( $this-&amp;gt;_data ))&lt;br /&gt;
        {&lt;br /&gt;
            $query = $this-&amp;gt;_buildQuery();&lt;br /&gt;
            $this-&amp;gt;_data = $this-&amp;gt;_getList( $query );&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        return $this-&amp;gt;_data;&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This file is saved as models/hellos.php.&lt;br /&gt;
&lt;br /&gt;
==== The Hellos View ====&lt;br /&gt;
&lt;br /&gt;
Now that we have a model to retrieve our data, we need to display it. This view will be fairly similar to the view from the site section as well.&lt;br /&gt;
&lt;br /&gt;
Just as our model was automatically instantiated in the site, so it is in the administrator. Methods that start with get in the model can be accessed using the get() method of the JView class. So our view has three lines: one to retrieve the data from the model, one to push the data into the template, and one to invoke the display method to display the output. Thus we have:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * Hellos View for Hello World Component&lt;br /&gt;
 * &lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_4&lt;br /&gt;
 * @license        GNU/GPL&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// Check to ensure this file is included in Joomla!&lt;br /&gt;
defined(&#039;_JEXEC&#039;) or die();&lt;br /&gt;
&lt;br /&gt;
jimport( &#039;joomla.application.component.view&#039; );&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Hellos View&lt;br /&gt;
 *&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 */&lt;br /&gt;
class HellosViewHellos extends JView&lt;br /&gt;
{&lt;br /&gt;
    /**&lt;br /&gt;
     * Hellos view display method&lt;br /&gt;
     * @return void&lt;br /&gt;
     **/&lt;br /&gt;
    function display($tpl = null)&lt;br /&gt;
    {&lt;br /&gt;
        JToolBarHelper::title( JText::_( &#039;Hello Manager&#039; ), &#039;generic.png&#039; );&lt;br /&gt;
        JToolBarHelper::deleteList();&lt;br /&gt;
        JToolBarHelper::editListX();&lt;br /&gt;
        JToolBarHelper::addNewX();&lt;br /&gt;
&lt;br /&gt;
        // Get data from the model&lt;br /&gt;
        $items =&amp;amp; $this-&amp;gt;get( &#039;Data&#039;);&lt;br /&gt;
&lt;br /&gt;
        $this-&amp;gt;assignRef( &#039;items&#039;, $items );&lt;br /&gt;
&lt;br /&gt;
        parent::display($tpl);&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This file is saved as views/hellos/view.html.php.&lt;br /&gt;
&lt;br /&gt;
==== The Hellos Template ====&lt;br /&gt;
&lt;br /&gt;
The template will take the data pushed into it from the view and produce the output. We will display our output in a simple table. While the frontend template was very simple, in the administrator we will need a minimal amount of extra logic to handle looping through the data.&lt;br /&gt;
&lt;br /&gt;
Here is our template:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;); ?&amp;gt;&lt;br /&gt;
&amp;lt;form action=&amp;quot;index.php&amp;quot; method=&amp;quot;post&amp;quot; name=&amp;quot;adminForm&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;div id=&amp;quot;editcell&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;table class=&amp;quot;adminlist&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;thead&amp;gt;&lt;br /&gt;
        &amp;lt;tr&amp;gt;&lt;br /&gt;
            &amp;lt;th width=&amp;quot;5&amp;quot;&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo JText::_( &#039;ID&#039; ); ?&amp;gt;&lt;br /&gt;
            &amp;lt;/th&amp;gt;&lt;br /&gt;
            &amp;lt;th&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo JText::_( &#039;Greeting&#039; ); ?&amp;gt;&lt;br /&gt;
            &amp;lt;/th&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;            &lt;br /&gt;
    &amp;lt;/thead&amp;gt;&lt;br /&gt;
    &amp;lt;?php&lt;br /&gt;
    $k = 0;&lt;br /&gt;
    for ($i=0, $n=count( $this-&amp;gt;items ); $i &amp;lt; $n; $i++)&lt;br /&gt;
    {&lt;br /&gt;
        $row =&amp;amp; $this-&amp;gt;items[$i];&lt;br /&gt;
        ?&amp;gt;&lt;br /&gt;
        &amp;lt;tr class=&amp;quot;&amp;lt;?php echo &amp;quot;row$k&amp;quot;; ?&amp;gt;&amp;quot;&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo $row-&amp;gt;id; ?&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo $row-&amp;gt;greeting; ?&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;&lt;br /&gt;
        &amp;lt;?php&lt;br /&gt;
        $k = 1 - $k;&lt;br /&gt;
    }&lt;br /&gt;
    ?&amp;gt;&lt;br /&gt;
    &amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;option&amp;quot; value=&amp;quot;com_hello&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;task&amp;quot; value=&amp;quot;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;boxchecked&amp;quot; value=&amp;quot;0&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;controller&amp;quot; value=&amp;quot;hello&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/form&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This template is saved as views/hellos/tmpl/default.php.&lt;br /&gt;
&lt;br /&gt;
You will notice that our output is enclosed in a form. Though this is not necessary now, it will be soon.&lt;br /&gt;
&lt;br /&gt;
We have now completed the basic part of the first view. We have added five files to the admin section of our component:&lt;br /&gt;
&lt;br /&gt;
* hello.php&lt;br /&gt;
* controller.php&lt;br /&gt;
* models/hellos.php&lt;br /&gt;
* views/hellos/view.html.php&lt;br /&gt;
* views/hellos/tmpl/default.php&lt;br /&gt;
&lt;br /&gt;
You can now add these files to the XML install file and give it a try!&lt;br /&gt;
&lt;br /&gt;
== Adding Functionality ==&lt;br /&gt;
&lt;br /&gt;
So far our administrator section is pretty useless. It doesn&#039;t really do anything - all it does is display the entries that we have in our database.&lt;br /&gt;
&lt;br /&gt;
In order to make it useful, we need to add some buttons and links.&lt;br /&gt;
&lt;br /&gt;
==== The Toolbar ====&lt;br /&gt;
&lt;br /&gt;
You may have noticed the toolbar that appears at the top of other Joomla! component administrator panels. Our component needs one as well. Joomla! makes this very easy to do. We will add buttons Delete records, Edit records, and create New records. We will also add a title that will be displayed on our toolbar.&lt;br /&gt;
&lt;br /&gt;
This is done by adding code to the view. To add the buttons, we use static methods from the Joomla! JToolBarHelper class. The code looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;JToolBarHelper::title(   JText::_( &#039;Hello Manager&#039; ), &#039;generic.png&#039; );&lt;br /&gt;
JToolBarHelper::deleteList();&lt;br /&gt;
JToolBarHelper::editListX();&lt;br /&gt;
JToolBarHelper::addNewX();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
These three methods will create the appropriate buttons. The deleteList() method can optionally take up to three parameters - the first parameter is a string to display to the user to confirm that they want to delete the records. The second is the task that should be sent with the query (the default is &#039;remove&#039;), and the third is the text that should be displayed below the button.&lt;br /&gt;
&lt;br /&gt;
The editListX() and addNewX() methods can each take two optional parameters. The first is the task (which are by default edit and add, respectively), and the second is the text that should be displayed below the button.&lt;br /&gt;
&lt;br /&gt;
 &#039;&#039;*You may have noticed the use of the JText::_ method in the template before and here as well. This is a handy function that makes component translation much easier. The JText::_ method will look up the string in your component language file and return the translated string. If not translation is found, it will return the string that you passed it. If you want to translate your component into another language, all you have to do is create a language file that will map the strings within the quotes to the translated version of the string.&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==== Checkboxes and Links ====&lt;br /&gt;
&lt;br /&gt;
We now have buttons. Two of those buttons operate on existing records. But how do we know which records to operate on? We have to let the user tell us. To do this, we need to add checkboxes to our table so that the user can select certain records. This is done in our template.&lt;br /&gt;
&lt;br /&gt;
In order to the add the checkboxes, we need to add an extra column into our table. We will add the column in between the two that we already have.&lt;br /&gt;
&lt;br /&gt;
In the header of the column, we will add a checkbox which can be used to toggle all the boxes below it on or off:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;th width=&amp;quot;20&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;input type=&amp;quot;checkbox&amp;quot; name=&amp;quot;toggle&amp;quot; value=&amp;quot;&amp;quot; onclick=&amp;quot;checkAll(&amp;lt;?php echo count( $this-&amp;gt;items ); ?&amp;gt;);&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/th&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Javascript checkAll function is a function that is built into the Joomla! base Javascript package that provides the functionality that we want here.&lt;br /&gt;
&lt;br /&gt;
Now we need to add the checkboxes into the individual rows. Joomla!&#039;s JHTML class has a method, JHTML::_(), which will generate our checkbox for us. We will add the following line to our loop:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$checked    = JHTML::_( &#039;grid.id&#039;, $i, $row-&amp;gt;id );&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
after the line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$row =&amp;amp; $this-&amp;gt;items[$i];&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then we will add a cell in between the two that we already have:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;td&amp;gt;&lt;br /&gt;
    &amp;lt;?php echo $checked; ?&amp;gt;&lt;br /&gt;
&amp;lt;/td&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It can be cumbersome to have to check the box that we want to edit and then move up and click the edit button. Therefore, we will add a link that it will go straight to the greeting&#039;s edit form. We will add the following line after the call to the JHTML::_() method to generate the link HTML:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$link = JRoute::_( &#039;index.php?option=com_hello&amp;gt;controller=hello&amp;gt;task=edit&amp;gt;cid[]=&#039;. $row-&amp;gt;id );&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And we include the link in the cell showing the greeting text:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;td&amp;gt;&lt;br /&gt;
    &amp;lt;a href=&amp;quot;&amp;lt;?php echo $link; ?&amp;gt;&amp;quot;&amp;gt;&amp;lt;?php echo $row-&amp;gt;greeting; ?&amp;gt;&amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;lt;/td&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You will notice that this link points to the hello controller. This controller will handle the data manipulation of our greetings.&lt;br /&gt;
&lt;br /&gt;
If you recall from above, we had four hidden input fields at the bottom of our form. The first input field was named &#039;option&#039;. This field is necessary so that we stay in our component. The second input field was task. This form property gets set when one of the buttons in the toolbar is clicked. A Javascript error will result and the buttons will not work if this input field is omitted. The third input field is the boxchecked field. This field keeps track of the number of boxes that are checked. The edit and delete buttons will check to ensure that this is greater than zero and will not allow the form to be submitted if it is not. The fourth input field is the controller field. This is used to specify that tasks fired from this form will be handled by the hello controller.&lt;br /&gt;
&lt;br /&gt;
Here is the code for the completed default.php file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;); ?&amp;gt;&lt;br /&gt;
&amp;lt;form action=&amp;quot;index.php&amp;quot; method=&amp;quot;post&amp;quot; name=&amp;quot;adminForm&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;div id=&amp;quot;editcell&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;table class=&amp;quot;adminlist&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;thead&amp;gt;&lt;br /&gt;
        &amp;lt;tr&amp;gt;&lt;br /&gt;
            &amp;lt;th width=&amp;quot;5&amp;quot;&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo JText::_( &#039;ID&#039; ); ?&amp;gt;&lt;br /&gt;
            &amp;lt;/th&amp;gt;&lt;br /&gt;
            &amp;lt;th&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo JText::_( &#039;Greeting&#039; ); ?&amp;gt;&lt;br /&gt;
            &amp;lt;/th&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;            &lt;br /&gt;
    &amp;lt;/thead&amp;gt;&lt;br /&gt;
    &amp;lt;?php&lt;br /&gt;
    $k = 0;&lt;br /&gt;
    for ($i=0, $n=count( $this-&amp;gt;items ); $i &amp;lt; $n; $i++)&lt;br /&gt;
    {&lt;br /&gt;
        $row =&amp;amp; $this-&amp;gt;items[$i];&lt;br /&gt;
        ?&amp;gt;&lt;br /&gt;
        &amp;lt;tr class=&amp;quot;&amp;lt;?php echo &amp;quot;row$k&amp;quot;; ?&amp;gt;&amp;quot;&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo $row-&amp;gt;id; ?&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo $row-&amp;gt;greeting; ?&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;&lt;br /&gt;
        &amp;lt;?php&lt;br /&gt;
        $k = 1 - $k;&lt;br /&gt;
    }&lt;br /&gt;
    ?&amp;gt;&lt;br /&gt;
    &amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;option&amp;quot; value=&amp;quot;com_hello&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;task&amp;quot; value=&amp;quot;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;boxchecked&amp;quot; value=&amp;quot;0&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;controller&amp;quot; value=&amp;quot;hello&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/form&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Our hellos view is now complete. You can try out the component now to see the results.&lt;br /&gt;
&lt;br /&gt;
The component can be downloaded at: [[http://dev.joomla.org/components/com_jd-wiki/data/media/components/com_hello4a.zip|com_hello4a.zip]]&lt;br /&gt;
&lt;br /&gt;
== Getting Down and Dirty: Doing the Real Work ==&lt;br /&gt;
&lt;br /&gt;
Now that the Hellos view is done, it is time to move to the Hello view and model. This is where the real work will get done.&lt;br /&gt;
&lt;br /&gt;
==== The Hello Controller ====&lt;br /&gt;
&lt;br /&gt;
Our default controller just isn&#039;t going to cut it when it comes to doing work - all it is capable of doing is displaying views.&lt;br /&gt;
&lt;br /&gt;
We need to be able to handle the tasks that we are launching from the Hellos view: add, edit and remove.&lt;br /&gt;
&lt;br /&gt;
Add and edit are essentially the same task: they both display a form to the user that allows a greeting to be edited. The only difference is that new displays a blank form, and edit displays a form with data already in it. Since they are similar, we will map the add task onto the edit task handler. This is specified in our constructor:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * constructor (registers additional tasks to methods)&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function __construct()&lt;br /&gt;
{&lt;br /&gt;
    parent::__construct();&lt;br /&gt;
&lt;br /&gt;
    // Register Extra tasks&lt;br /&gt;
    $this-&amp;gt;registerTask( &#039;add&#039;  ,     &#039;edit&#039; );&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first parameter of JController::registerTask is the task to map, and the second is the method to map it to.&lt;br /&gt;
&lt;br /&gt;
We will start with handling the edit task. The controller&#039;s job is fairly simple for the edit task. All it has to do is specify the view and layout to load (the hello view and the form layout). We will also tell Joomla! to disable the mainmenu while we are editing our greeting. This prevents users from leaving unsaved records open.&lt;br /&gt;
&lt;br /&gt;
Our edit task handler looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * display the edit form&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function edit()&lt;br /&gt;
{&lt;br /&gt;
    JRequest::setVar( &#039;view&#039;, &#039;hello&#039; );&lt;br /&gt;
    JRequest::setVar( &#039;layout&#039;, &#039;form&#039;  );&lt;br /&gt;
    JRequest::setVar(&#039;hidemainmenu&#039;, 1);&lt;br /&gt;
&lt;br /&gt;
    parent::display();&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== The Hello View ====&lt;br /&gt;
&lt;br /&gt;
The Hello view will display a form which will allow the user to edit a greeting. The display method if the hello view has to do a few simple tasks:&lt;br /&gt;
&lt;br /&gt;
  * retrieve the data from the model&lt;br /&gt;
&lt;br /&gt;
  * create the toolbar&lt;br /&gt;
&lt;br /&gt;
  * pass the data into the template&lt;br /&gt;
&lt;br /&gt;
  * invoke the display() method to render the template&lt;br /&gt;
&lt;br /&gt;
This becomes a bit more complicated because the one view handles both the edit and add tasks. In our toolbar we want the user to know what whether they are adding or editing, so we have to determine which task was fired.&lt;br /&gt;
&lt;br /&gt;
Since we are already retrieving the record that we want to display from the model, we can use this data to determine what task was fired. If the task was edit, then the id field of our record will have been set. If the task was new, then it will not have been set. This can be used to determine if we have a new record or an existing record.&lt;br /&gt;
&lt;br /&gt;
We will add two buttons to the toolbar: save and cancel. Though the functionality will be the same, we want to display different buttons depending on whether it is a new or existing record. If it is a new record, we will display cancel. If it already exists, we will display close.&lt;br /&gt;
&lt;br /&gt;
Thus our display method looks like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * display method of Hello view&lt;br /&gt;
 * @return void&lt;br /&gt;
 **/&lt;br /&gt;
function display($tpl = null)&lt;br /&gt;
{&lt;br /&gt;
    //get the hello&lt;br /&gt;
    $hello        =&amp;amp; $this-&amp;gt;get(&#039;Data&#039;);&lt;br /&gt;
    $isNew        = ($hello-&amp;gt;id &amp;lt; 1);&lt;br /&gt;
&lt;br /&gt;
    $text = $isNew ? JText::_( &#039;New&#039; ) : JText::_( &#039;Edit&#039; );&lt;br /&gt;
    JToolBarHelper::title(   JText::_( &#039;Hello&#039; ).&#039;: &amp;lt;small&amp;gt;&amp;lt;small&amp;gt;[ &#039; . $text.&#039; ]&amp;lt;/small&amp;gt;&amp;lt;/small&amp;gt;&#039; );&lt;br /&gt;
    JToolBarHelper::save();&lt;br /&gt;
    if ($isNew)  {&lt;br /&gt;
        JToolBarHelper::cancel();&lt;br /&gt;
    } else {&lt;br /&gt;
        // for existing items the button is renamed `close`&lt;br /&gt;
        JToolBarHelper::cancel( &#039;cancel&#039;, &#039;Close&#039; );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    $this-&amp;gt;assignRef(&#039;hello&#039;, $hello);&lt;br /&gt;
    parent::display($tpl);&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== The Hello Model ====&lt;br /&gt;
&lt;br /&gt;
Our view needs data. Therefore, we need to create a model to model a hello.&lt;br /&gt;
&lt;br /&gt;
Our model will have two properties: _id and _data. _id will hold the id of the greeting and data will hold the data.&lt;br /&gt;
&lt;br /&gt;
We will start with a constructor, which will attempt to retrieve the id from the query:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Constructor that retrieves the ID from the request&lt;br /&gt;
 *&lt;br /&gt;
 * @access    public&lt;br /&gt;
 * @return    void&lt;br /&gt;
 */&lt;br /&gt;
function __construct()&lt;br /&gt;
{&lt;br /&gt;
    parent::__construct();&lt;br /&gt;
&lt;br /&gt;
    $array = JRequest::getVar(&#039;cid&#039;,  0, &#039;&#039;, &#039;array&#039;);&lt;br /&gt;
    $this-&amp;gt;setId((int)$array[0]);&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The JRequest::getVar() method is used to retrieve data from the request. The first parameter is the name of the form variable. The second parameter is the default value to assign if there is no value found. The third parameter is the name of the hash to retrieve the value from (get, post, etc), and the last value is the data type that should be forced on the value.&lt;br /&gt;
&lt;br /&gt;
Our constructor will take the first value from the cid array and assign it to the id.&lt;br /&gt;
&lt;br /&gt;
Our setId() method can be used to set our id. Changing the id that our model points to will mean the id points to the wrong data. Therefore, when we set the id, we will clear the data property:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Method to set the hello identifier&lt;br /&gt;
 *&lt;br /&gt;
 * @access    public&lt;br /&gt;
 * @param    int Hello identifier&lt;br /&gt;
 * @return    void&lt;br /&gt;
 */&lt;br /&gt;
function setId($id)&lt;br /&gt;
{&lt;br /&gt;
    // Set id and wipe data&lt;br /&gt;
    $this-&amp;gt;_id        = $id;&lt;br /&gt;
    $this-&amp;gt;_data    = null;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finally, we need a method to retrieve our data: getData()&lt;br /&gt;
&lt;br /&gt;
getData will check if the _data property has already been set. If it has, it will simply return it. Otherwise, it will load the data from the database.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Method to get a hello&lt;br /&gt;
 * @return object with data&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
function &amp;amp;getData()&lt;br /&gt;
{&lt;br /&gt;
    // Load the data&lt;br /&gt;
    if (empty( $this-&amp;gt;_data )) {&lt;br /&gt;
        $query = &#039; SELECT * FROM #__hello &#039;.&lt;br /&gt;
                &#039;  WHERE id = &#039;.$this-&amp;gt;_id;&lt;br /&gt;
        $this-&amp;gt;_db-&amp;gt;setQuery( $query );&lt;br /&gt;
        $this-&amp;gt;_data = $this-&amp;gt;_db-&amp;gt;loadObject();&lt;br /&gt;
    }&lt;br /&gt;
    if (!$this-&amp;gt;_data) {&lt;br /&gt;
        $this-&amp;gt;_data = new stdClass();&lt;br /&gt;
        $this-&amp;gt;_data-&amp;gt;id = 0;&lt;br /&gt;
        $this-&amp;gt;_data-&amp;gt;greeting = null;&lt;br /&gt;
    }&lt;br /&gt;
    return $this-&amp;gt;_data;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== The Form ====&lt;br /&gt;
&lt;br /&gt;
Now all that is left is to create the form that the data will go into. Since we specified our layout as form, the form will go in a file in the tmpl directory of the hello view called form.php:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;); ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;form action=&amp;quot;index.php&amp;quot; method=&amp;quot;post&amp;quot; name=&amp;quot;adminForm&amp;quot; id=&amp;quot;adminForm&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;div class=&amp;quot;col100&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;fieldset class=&amp;quot;adminform&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;legend&amp;gt;&amp;lt;?php echo JText::_( &#039;Details&#039; ); ?&amp;gt;&amp;lt;/legend&amp;gt;&lt;br /&gt;
        &amp;lt;table class=&amp;quot;admintable&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;tr&amp;gt;&lt;br /&gt;
            &amp;lt;td width=&amp;quot;100&amp;quot; align=&amp;quot;right&amp;quot; class=&amp;quot;key&amp;quot;&amp;gt;&lt;br /&gt;
                &amp;lt;label for=&amp;quot;greeting&amp;quot;&amp;gt;&lt;br /&gt;
                    &amp;lt;?php echo JText::_( &#039;Greeting&#039; ); ?&amp;gt;:&lt;br /&gt;
                &amp;lt;/label&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;input class=&amp;quot;text_area&amp;quot; type=&amp;quot;text&amp;quot; name=&amp;quot;greeting&amp;quot; id=&amp;quot;greeting&amp;quot; size=&amp;quot;32&amp;quot; maxlength=&amp;quot;250&amp;quot; value=&amp;quot;&amp;lt;?php echo $this-&amp;gt;hello-&amp;gt;greeting;?&amp;gt;&amp;quot; /&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;&lt;br /&gt;
    &amp;lt;/table&amp;gt;&lt;br /&gt;
    &amp;lt;/fieldset&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;clr&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;option&amp;quot; value=&amp;quot;com_hello&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;id&amp;quot; value=&amp;quot;&amp;lt;?php echo $this-&amp;gt;hello-&amp;gt;id; ?&amp;gt;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;task&amp;quot; value=&amp;quot;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;controller&amp;quot; value=&amp;quot;hello&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/form&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in addition to the input field, there is a hidden field for the id. The user doesn&#039;t need to edit the id (and shouldn&#039;t), so we silently pass it along in the form.&lt;br /&gt;
&lt;br /&gt;
==== Implementing the Functionality ====&lt;br /&gt;
&lt;br /&gt;
So far, our controller only handles two tasks: edit and new. However, we also have buttons to save, delete and cancel records. We need to write code to handle and perform these tasks.&lt;br /&gt;
&lt;br /&gt;
=== Saving a Record ===&lt;br /&gt;
&lt;br /&gt;
The logical next step is to implement the functionality to save a record. Normally, this would require some switches and logic to handle various cases, such as the difference between creating a new record (an INSERT query), and updating an existing query (an UPDATE query). Also, there are complexities involved in getting the data from the form and putting it into the query.&lt;br /&gt;
&lt;br /&gt;
The Joomla! framework takes care of a lot of this for you. The JTable class makes it easy to manipulate records in the database without having to worry about writing the SQL code that lies behind these updates. It also makes it easy to transfer data from an HTML form into the database.&lt;br /&gt;
&lt;br /&gt;
== Creating the Table Class ==&lt;br /&gt;
&lt;br /&gt;
The JTable class is an abstract class from which you can derive child classes to work with specific tables. To use it, you simply create a class that extends the JTable class, add your database fields as properties, and override the constructor to specify the name of the table and the primary key.&lt;br /&gt;
&lt;br /&gt;
Here is our JTable class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * Hello World table class&lt;br /&gt;
 * &lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://dev.joomla.org/component/option,com_jd-wiki/Itemid,31/id,tutorials:components/&lt;br /&gt;
 * @license        GNU/GPL&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// no direct access&lt;br /&gt;
defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;);&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Hello Table class&lt;br /&gt;
 *&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 */&lt;br /&gt;
class TableHello extends JTable&lt;br /&gt;
{&lt;br /&gt;
    /**&lt;br /&gt;
     * Primary Key&lt;br /&gt;
     *&lt;br /&gt;
     * @var int&lt;br /&gt;
     */&lt;br /&gt;
    var $id = null;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * @var string&lt;br /&gt;
     */&lt;br /&gt;
    var $greeting = null;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Constructor&lt;br /&gt;
     *&lt;br /&gt;
     * @param object Database connector object&lt;br /&gt;
     */&lt;br /&gt;
    function TableHello( &amp;amp;$db ) {&lt;br /&gt;
        parent::__construct(&#039;#__hello&#039;, &#039;id&#039;, $db);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You will see here that we have defined our two fields: the id field and the greeting field. Then we have defined a constructor, which will call the constructor of the parent class and pass it the name of the table (hello), the name of the field which is the primary key (id), and the database connector object.&lt;br /&gt;
&lt;br /&gt;
This file should be called hello.php and it will go in a directory called tables in the administrator section of our component.&lt;br /&gt;
&lt;br /&gt;
== Implementing the Function in our Model ==&lt;br /&gt;
&lt;br /&gt;
We are now ready to add the method to the model which will save our record. We will call this method store. Our store() method will do three things: bind the data from the form to the TableHello object, check to ensure that the record is properly formed, and store the record in the database.&lt;br /&gt;
&lt;br /&gt;
Our method looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Method to store a record&lt;br /&gt;
 *&lt;br /&gt;
 * @access    public&lt;br /&gt;
 * @return    boolean    True on success&lt;br /&gt;
 */&lt;br /&gt;
function store()&lt;br /&gt;
{&lt;br /&gt;
    $row =&amp;amp; $this-&amp;gt;getTable();&lt;br /&gt;
&lt;br /&gt;
    $data = JRequest::get( &#039;post&#039; );&lt;br /&gt;
    // Bind the form fields to the hello table&lt;br /&gt;
    if (!$row-&amp;gt;bind($data)) {&lt;br /&gt;
        $this-&amp;gt;setError($this-&amp;gt;_db-&amp;gt;getErrorMsg());&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // Make sure the hello record is valid&lt;br /&gt;
    if (!$row-&amp;gt;check()) {&lt;br /&gt;
        $this-&amp;gt;setError($this-&amp;gt;_db-&amp;gt;getErrorMsg());&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // Store the web link table to the database&lt;br /&gt;
    if (!$row-&amp;gt;store()) {&lt;br /&gt;
        $this-&amp;gt;setError($this-&amp;gt;_db-&amp;gt;getErrorMsg());&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return true;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This method gets added to the hello model.&lt;br /&gt;
&lt;br /&gt;
The method takes one parameter, which is an associative array of data that we want to store in the database. This can easily be retrieved from the request as will be seen later.&lt;br /&gt;
&lt;br /&gt;
You will see that the first line retrieves a reference to our JTable object. If we name our table properly, we don&#039;t have to specify its name - the JModel class knows where to find it. You may recall that we called our table class TableHello and put it in a file called hello.php in the tables directory. If you follow this convention, the JModel class can create your object automatically.&lt;br /&gt;
&lt;br /&gt;
The second line will retrieve the data from the form. The JRequest class makes this very easy. In this case, we are retrieving all of the variables that were submitted using the &#039;POST&#039; method. These will be returned as an associative array.&lt;br /&gt;
&lt;br /&gt;
The rest is easy - we bind, check and store. The bind() method will copy values from the array into the corresponding property of the table object. In this case, it will take the values of id and greeting and copy them to our TableHello object.&lt;br /&gt;
&lt;br /&gt;
The check() method will perform data verification. In the JTable() class, this method simply returns true. While this doesn&#039;t provide any value for us currently, by calling this method we make it possible to do data checking using our TableHello class in the future. This method can be overridden in our TableHello class with a method that performs the appropriate checks.&lt;br /&gt;
&lt;br /&gt;
The store() method will take the data that is in the object and store it in the database. If the id is 0, it will create a new record (INSERT), otherwise, it will update the existing record (UPDATE).&lt;br /&gt;
&lt;br /&gt;
== Adding the Task to the Controller ==&lt;br /&gt;
&lt;br /&gt;
We are now ready to add our task to the controller. Since the task that we are firing is called &#039;save&#039;, we must call our method &#039;save&#039;. This is simple:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * save a record (and redirect to main page)&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function save()&lt;br /&gt;
{&lt;br /&gt;
    $model = $this-&amp;gt;getModel(&#039;hello&#039;);&lt;br /&gt;
&lt;br /&gt;
    if ($model-&amp;gt;store()) {&lt;br /&gt;
        $msg = JText::_( &#039;Greeting Saved!&#039; );&lt;br /&gt;
    } else {&lt;br /&gt;
        $msg = JText::_( &#039;Error Saving Greeting&#039; );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // Check the table in so it can be edited.... we are done with it anyway&lt;br /&gt;
    $link = &#039;index.php?option=com_hello&#039;;&lt;br /&gt;
    $this-&amp;gt;setRedirect($link, $msg);&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All we do is get our model and invoke the store() method. Then we use the setRedirect() method to redirect back to our list of greetings. We also pass a message along, which will be displayed at the top of the page.&lt;br /&gt;
&lt;br /&gt;
=== Deleting a Record ===&lt;br /&gt;
&lt;br /&gt;
=== Implementing the Function in the Model ===&lt;br /&gt;
&lt;br /&gt;
In the model, we will retrieve the list of IDs to delete and call the JTable class to delete them. Here it is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Method to delete record(s)&lt;br /&gt;
 *&lt;br /&gt;
 * @access    public&lt;br /&gt;
 * @return    boolean    True on success&lt;br /&gt;
 */&lt;br /&gt;
function delete()&lt;br /&gt;
{&lt;br /&gt;
    $cids = JRequest::getVar( &#039;cid&#039;, array(0), &#039;post&#039;, &#039;array&#039; );&lt;br /&gt;
    $row =&amp;amp; $this-&amp;gt;getTable();&lt;br /&gt;
&lt;br /&gt;
    foreach($cids as $cid) {&lt;br /&gt;
        if (!$row-&amp;gt;delete( $cid )) {&lt;br /&gt;
            $this-&amp;gt;setError( $row-&amp;gt;getErrorMsg() );&lt;br /&gt;
            return false;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return true;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We invoke the JRequest::getVar() method to get the data from the request, then we invoke the $row-&amp;gt;delete() method to delete each row. By storing errors in the model we make it possible to retrieve them later if we so choose.&lt;br /&gt;
&lt;br /&gt;
=== Handling the Remove Task in the Controller ===&lt;br /&gt;
&lt;br /&gt;
This is similar to the save() method which handled the save task:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * remove record(s)&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function remove()&lt;br /&gt;
{&lt;br /&gt;
    $model = $this-&amp;gt;getModel(&#039;hello&#039;);&lt;br /&gt;
    if(!$model-&amp;gt;delete()) {&lt;br /&gt;
        $msg = JText::_( &#039;Error: One or More Greetings Could not be Deleted&#039; );&lt;br /&gt;
    } else {&lt;br /&gt;
        $msg = JText::_( &#039;Greeting(s) Deleted&#039; );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    $this-&amp;gt;setRedirect( &#039;index.php?option=com_hello&#039;, $msg );&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Cancelling the Edit Operation ====&lt;br /&gt;
&lt;br /&gt;
To cancel the edit operation, all we have to do is redirect back to the main view:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * cancel editing a record&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function cancel()&lt;br /&gt;
{&lt;br /&gt;
    $msg = JText::_( &#039;Operation Cancelled&#039; );&lt;br /&gt;
    $this-&amp;gt;setRedirect( &#039;index.php?option=com_hello&#039;, $msg );&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
We have now implemented a basic backend to our component. We are now able to edit the entries that are viewed in the frontend. We have demonstrated the interaction between models, views and controllers. We have shown how the JTable class can be extended to provide easy access to tables in the database. It can also be seen how the JToolBarHelper class can be used to create button bars in components to present a standardized look between components.&lt;br /&gt;
&lt;br /&gt;
== Contributors ==&lt;br /&gt;
* staalanden&lt;br /&gt;
&lt;br /&gt;
== Download ==&lt;br /&gt;
&lt;br /&gt;
The component can be downloaded at: [http://joomlacode.org/gf/download/frsrelease/8104/29402/com_hello4a_01.zip com_hello4a_01]&lt;/div&gt;</summary>
		<author><name>Tonie</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Creating_an_Administrator_Interface&amp;diff=9507</id>
		<title>J1.5:Developing a MVC Component/Creating an Administrator Interface</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Creating_an_Administrator_Interface&amp;diff=9507"/>
		<updated>2008-07-27T19:46:46Z</updated>

		<summary type="html">&lt;p&gt;Tonie: /* The Hellos Model */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
In the first three tutorials, we have developed a MVC component that retrieves its data from a table in the database. Currently, there is no way to add data to the database except to do it manually using another tool. In this tutorial, we will develop an administrator section for our component which will make it possible to manage the entries in the database.&lt;br /&gt;
&lt;br /&gt;
== Creating the Basic Framework ==&lt;br /&gt;
&lt;br /&gt;
The basic framework of the administrator panel is very similar to the site portion. The main entry point for the administrator section of the component is hello.php. This file is identical to the hello.php file that was used in the site portion except the name of the controller it loads will be changed to HellosController. The default controller is also called controller.php and this file is identical to the default controller in the site portion, with the exception that the controller is named HellosController instead of HelloController. This difference is so that JController will by default load the hellos view, which will display a list of our greetings.&lt;br /&gt;
&lt;br /&gt;
Here is the listing for hello.php:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_4&lt;br /&gt;
 * @license    GNU/GPL&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
// No direct access&lt;br /&gt;
&lt;br /&gt;
defined( &#039;_JEXEC&#039; ) or die( &#039;Restricted access&#039; );&lt;br /&gt;
&lt;br /&gt;
// Require the base controller&lt;br /&gt;
&lt;br /&gt;
require_once( JPATH_COMPONENT.DS.&#039;controller.php&#039; );&lt;br /&gt;
&lt;br /&gt;
// Require specific controller if requested&lt;br /&gt;
if($controller = JRequest::getWord(&#039;controller&#039;)) {&lt;br /&gt;
    $path = JPATH_COMPONENT.DS.&#039;controllers&#039;.DS.$controller.&#039;.php&#039;;&lt;br /&gt;
    if (file_exists($path)) {&lt;br /&gt;
        require_once $path;&lt;br /&gt;
    } else {&lt;br /&gt;
        $controller = &#039;&#039;;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Create the controller&lt;br /&gt;
$classname    = &#039;HellosController&#039;.$controller;&lt;br /&gt;
$controller   = new $classname( );&lt;br /&gt;
&lt;br /&gt;
// Perform the Request task&lt;br /&gt;
$controller-&amp;gt;execute( JRequest::getVar( &#039;task&#039; ) );&lt;br /&gt;
&lt;br /&gt;
// Redirect if set by the controller&lt;br /&gt;
$controller-&amp;gt;redirect();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The view and model that we will start with is the hellos view and the hellos model. We will start with the model.&lt;br /&gt;
&lt;br /&gt;
==== The Hellos Model ====&lt;br /&gt;
&lt;br /&gt;
The Hellos Model will be very simple. The only operation that we currently need is the ability to retrieve the list of hellos from the database. This operation will be implemented in a method called getData().&lt;br /&gt;
&lt;br /&gt;
The JModel class has a built in protected method called _getList(). This method can be used to simplify the task of retrieving a list of records from the database. We simply need to pass it the query and it will return the list of records.&lt;br /&gt;
&lt;br /&gt;
At a later point in time, we might want to use our query from within another method. Therefore, we will create a private method called _buildQuery() which will return the query that will be passed to _getList(). This makes it easier to change the query as well since it is localized in one place.&lt;br /&gt;
&lt;br /&gt;
Therefore we need two methods in our class: getData() and _buildQuery().&lt;br /&gt;
&lt;br /&gt;
_buildQuery() simply returns the query. It looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source  lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Returns the query&lt;br /&gt;
 * @return string The query to be used to retrieve the rows from the database&lt;br /&gt;
 */&lt;br /&gt;
function _buildQuery()&lt;br /&gt;
{&lt;br /&gt;
    $query = &#039; SELECT * &#039;&lt;br /&gt;
           . &#039; FROM #__hello &#039;&lt;br /&gt;
    ;&lt;br /&gt;
&lt;br /&gt;
    return $query;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
getData() will obtain the query and retrieve the records from the database. Now it might happen that we need to retrieve this list of data twice in one page load. It would be a waste to have to query the database twice. Therefore, we will have this method store the data in a protected property so that on subsequent requests it can simply return the data it has already retrieved. This property will be called _data.&lt;br /&gt;
&lt;br /&gt;
Here is the getData() method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source  lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Retrieves the hello data&lt;br /&gt;
 * @return array Array of objects containing the data from the database&lt;br /&gt;
 */&lt;br /&gt;
function getData()&lt;br /&gt;
{&lt;br /&gt;
    // Lets load the data if it doesn&#039;t already exist&lt;br /&gt;
    if (empty( $this-&amp;gt;_data ))&lt;br /&gt;
    {&lt;br /&gt;
        $query = $this-&amp;gt;_buildQuery();&lt;br /&gt;
        $this-&amp;gt;_data = $this-&amp;gt;_getList( $query );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return $this-&amp;gt;_data;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The completed model looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * Hellos Model for Hello World Component&lt;br /&gt;
 * &lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_4&lt;br /&gt;
 * @license        GNU/GPL&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// Check to ensure this file is included in Joomla!&lt;br /&gt;
defined(&#039;_JEXEC&#039;) or die();&lt;br /&gt;
&lt;br /&gt;
jimport( &#039;joomla.application.component.model&#039; );&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Hello Model&lt;br /&gt;
 *&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 */&lt;br /&gt;
class HellosModelHellos extends JModel&lt;br /&gt;
{&lt;br /&gt;
    /**&lt;br /&gt;
     * Hellos data array&lt;br /&gt;
     *&lt;br /&gt;
     * @var array&lt;br /&gt;
     */&lt;br /&gt;
    var $_data;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Returns the query&lt;br /&gt;
     * @return string The query to be used to retrieve the rows from the database&lt;br /&gt;
     */&lt;br /&gt;
    function _buildQuery()&lt;br /&gt;
    {&lt;br /&gt;
        $query = &#039; SELECT * &#039;&lt;br /&gt;
            . &#039; FROM #__hello &#039;&lt;br /&gt;
        ;&lt;br /&gt;
        return $query;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Retrieves the hello data&lt;br /&gt;
     * @return array Array of objects containing the data from the database&lt;br /&gt;
     */&lt;br /&gt;
    function getData()&lt;br /&gt;
    {&lt;br /&gt;
        // Lets load the data if it doesn&#039;t already exist&lt;br /&gt;
        if (empty( $this-&amp;gt;_data ))&lt;br /&gt;
        {&lt;br /&gt;
            $query = $this-&amp;gt;_buildQuery();&lt;br /&gt;
            $this-&amp;gt;_data = $this-&amp;gt;_getList( $query );&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        return $this-&amp;gt;_data;&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This file is saved as models/hellos.php.&lt;br /&gt;
&lt;br /&gt;
==== The Hellos View ====&lt;br /&gt;
&lt;br /&gt;
Now that we have a model to retrieve our data, we need to display it. This view will be fairly similar to the view from the site section as well.&lt;br /&gt;
&lt;br /&gt;
Just as our model was automatically instantiated in the site, so it is in the administrator. Methods that start with get in the model can be accessed using the get() method of the JView class. So our view has three lines: one to retrieve the data from the model, one to push the data into the template, and one to invoke the display method to display the output. Thus we have:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * Hellos View for Hello World Component&lt;br /&gt;
 * &lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://dev.joomla.org/component/option,com_jd-wiki/Itemid,31/id,tutorials:components/&lt;br /&gt;
 * @license        GNU/GPL&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// Check to ensure this file is included in Joomla!&lt;br /&gt;
defined(&#039;_JEXEC&#039;) or die();&lt;br /&gt;
&lt;br /&gt;
jimport( &#039;joomla.application.component.view&#039; );&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Hellos View&lt;br /&gt;
 *&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 */&lt;br /&gt;
class HellosViewHellos extends JView&lt;br /&gt;
{&lt;br /&gt;
    /**&lt;br /&gt;
     * Hellos view display method&lt;br /&gt;
     * @return void&lt;br /&gt;
     **/&lt;br /&gt;
    function display($tpl = null)&lt;br /&gt;
    {&lt;br /&gt;
        JToolBarHelper::title( JText::_( &#039;Hello Manager&#039; ), &#039;generic.png&#039; );&lt;br /&gt;
        JToolBarHelper::deleteList();&lt;br /&gt;
        JToolBarHelper::editListX();&lt;br /&gt;
        JToolBarHelper::addNewX();&lt;br /&gt;
&lt;br /&gt;
        // Get data from the model&lt;br /&gt;
        $items =&amp;amp; $this-&amp;gt;get( &#039;Data&#039;);&lt;br /&gt;
&lt;br /&gt;
        $this-&amp;gt;assignRef( &#039;items&#039;, $items );&lt;br /&gt;
&lt;br /&gt;
        parent::display($tpl);&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This file is saved as views/hellos/view.html.php.&lt;br /&gt;
&lt;br /&gt;
==== The Hellos Template ====&lt;br /&gt;
&lt;br /&gt;
The template will take the data pushed into it from the view and produce the output. We will display our output in a simple table. While the frontend template was very simple, in the administrator we will need a minimal amount of extra logic to handle looping through the data.&lt;br /&gt;
&lt;br /&gt;
Here is our template:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;); ?&amp;gt;&lt;br /&gt;
&amp;lt;form action=&amp;quot;index.php&amp;quot; method=&amp;quot;post&amp;quot; name=&amp;quot;adminForm&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;div id=&amp;quot;editcell&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;table class=&amp;quot;adminlist&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;thead&amp;gt;&lt;br /&gt;
        &amp;lt;tr&amp;gt;&lt;br /&gt;
            &amp;lt;th width=&amp;quot;5&amp;quot;&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo JText::_( &#039;ID&#039; ); ?&amp;gt;&lt;br /&gt;
            &amp;lt;/th&amp;gt;&lt;br /&gt;
            &amp;lt;th&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo JText::_( &#039;Greeting&#039; ); ?&amp;gt;&lt;br /&gt;
            &amp;lt;/th&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;            &lt;br /&gt;
    &amp;lt;/thead&amp;gt;&lt;br /&gt;
    &amp;lt;?php&lt;br /&gt;
    $k = 0;&lt;br /&gt;
    for ($i=0, $n=count( $this-&amp;gt;items ); $i &amp;lt; $n; $i++)&lt;br /&gt;
    {&lt;br /&gt;
        $row =&amp;amp; $this-&amp;gt;items[$i];&lt;br /&gt;
        ?&amp;gt;&lt;br /&gt;
        &amp;lt;tr class=&amp;quot;&amp;lt;?php echo &amp;quot;row$k&amp;quot;; ?&amp;gt;&amp;quot;&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo $row-&amp;gt;id; ?&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo $row-&amp;gt;greeting; ?&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;&lt;br /&gt;
        &amp;lt;?php&lt;br /&gt;
        $k = 1 - $k;&lt;br /&gt;
    }&lt;br /&gt;
    ?&amp;gt;&lt;br /&gt;
    &amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;option&amp;quot; value=&amp;quot;com_hello&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;task&amp;quot; value=&amp;quot;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;boxchecked&amp;quot; value=&amp;quot;0&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;controller&amp;quot; value=&amp;quot;hello&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/form&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This template is saved as views/hellos/tmpl/default.php.&lt;br /&gt;
&lt;br /&gt;
You will notice that our output is enclosed in a form. Though this is not necessary now, it will be soon.&lt;br /&gt;
&lt;br /&gt;
We have now completed the basic part of the first view. We have added five files to the admin section of our component:&lt;br /&gt;
&lt;br /&gt;
* hello.php&lt;br /&gt;
* controller.php&lt;br /&gt;
* models/hellos.php&lt;br /&gt;
* views/hellos/view.html.php&lt;br /&gt;
* views/hellos/tmpl/default.php&lt;br /&gt;
&lt;br /&gt;
You can now add these files to the XML install file and give it a try!&lt;br /&gt;
&lt;br /&gt;
== Adding Functionality ==&lt;br /&gt;
&lt;br /&gt;
So far our administrator section is pretty useless. It doesn&#039;t really do anything - all it does is display the entries that we have in our database.&lt;br /&gt;
&lt;br /&gt;
In order to make it useful, we need to add some buttons and links.&lt;br /&gt;
&lt;br /&gt;
==== The Toolbar ====&lt;br /&gt;
&lt;br /&gt;
You may have noticed the toolbar that appears at the top of other Joomla! component administrator panels. Our component needs one as well. Joomla! makes this very easy to do. We will add buttons Delete records, Edit records, and create New records. We will also add a title that will be displayed on our toolbar.&lt;br /&gt;
&lt;br /&gt;
This is done by adding code to the view. To add the buttons, we use static methods from the Joomla! JToolBarHelper class. The code looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;JToolBarHelper::title(   JText::_( &#039;Hello Manager&#039; ), &#039;generic.png&#039; );&lt;br /&gt;
JToolBarHelper::deleteList();&lt;br /&gt;
JToolBarHelper::editListX();&lt;br /&gt;
JToolBarHelper::addNewX();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
These three methods will create the appropriate buttons. The deleteList() method can optionally take up to three parameters - the first parameter is a string to display to the user to confirm that they want to delete the records. The second is the task that should be sent with the query (the default is &#039;remove&#039;), and the third is the text that should be displayed below the button.&lt;br /&gt;
&lt;br /&gt;
The editListX() and addNewX() methods can each take two optional parameters. The first is the task (which are by default edit and add, respectively), and the second is the text that should be displayed below the button.&lt;br /&gt;
&lt;br /&gt;
 &#039;&#039;*You may have noticed the use of the JText::_ method in the template before and here as well. This is a handy function that makes component translation much easier. The JText::_ method will look up the string in your component language file and return the translated string. If not translation is found, it will return the string that you passed it. If you want to translate your component into another language, all you have to do is create a language file that will map the strings within the quotes to the translated version of the string.&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==== Checkboxes and Links ====&lt;br /&gt;
&lt;br /&gt;
We now have buttons. Two of those buttons operate on existing records. But how do we know which records to operate on? We have to let the user tell us. To do this, we need to add checkboxes to our table so that the user can select certain records. This is done in our template.&lt;br /&gt;
&lt;br /&gt;
In order to the add the checkboxes, we need to add an extra column into our table. We will add the column in between the two that we already have.&lt;br /&gt;
&lt;br /&gt;
In the header of the column, we will add a checkbox which can be used to toggle all the boxes below it on or off:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;th width=&amp;quot;20&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;input type=&amp;quot;checkbox&amp;quot; name=&amp;quot;toggle&amp;quot; value=&amp;quot;&amp;quot; onclick=&amp;quot;checkAll(&amp;lt;?php echo count( $this-&amp;gt;items ); ?&amp;gt;);&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/th&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Javascript checkAll function is a function that is built into the Joomla! base Javascript package that provides the functionality that we want here.&lt;br /&gt;
&lt;br /&gt;
Now we need to add the checkboxes into the individual rows. Joomla!&#039;s JHTML class has a method, JHTML::_(), which will generate our checkbox for us. We will add the following line to our loop:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$checked    = JHTML::_( &#039;grid.id&#039;, $i, $row-&amp;gt;id );&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
after the line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$row =&amp;amp; $this-&amp;gt;items[$i];&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then we will add a cell in between the two that we already have:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;td&amp;gt;&lt;br /&gt;
    &amp;lt;?php echo $checked; ?&amp;gt;&lt;br /&gt;
&amp;lt;/td&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It can be cumbersome to have to check the box that we want to edit and then move up and click the edit button. Therefore, we will add a link that it will go straight to the greeting&#039;s edit form. We will add the following line after the call to the JHTML::_() method to generate the link HTML:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$link = JRoute::_( &#039;index.php?option=com_hello&amp;gt;controller=hello&amp;gt;task=edit&amp;gt;cid[]=&#039;. $row-&amp;gt;id );&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And we include the link in the cell showing the greeting text:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;td&amp;gt;&lt;br /&gt;
    &amp;lt;a href=&amp;quot;&amp;lt;?php echo $link; ?&amp;gt;&amp;quot;&amp;gt;&amp;lt;?php echo $row-&amp;gt;greeting; ?&amp;gt;&amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;lt;/td&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You will notice that this link points to the hello controller. This controller will handle the data manipulation of our greetings.&lt;br /&gt;
&lt;br /&gt;
If you recall from above, we had four hidden input fields at the bottom of our form. The first input field was named &#039;option&#039;. This field is necessary so that we stay in our component. The second input field was task. This form property gets set when one of the buttons in the toolbar is clicked. A Javascript error will result and the buttons will not work if this input field is omitted. The third input field is the boxchecked field. This field keeps track of the number of boxes that are checked. The edit and delete buttons will check to ensure that this is greater than zero and will not allow the form to be submitted if it is not. The fourth input field is the controller field. This is used to specify that tasks fired from this form will be handled by the hello controller.&lt;br /&gt;
&lt;br /&gt;
Here is the code for the completed default.php file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;); ?&amp;gt;&lt;br /&gt;
&amp;lt;form action=&amp;quot;index.php&amp;quot; method=&amp;quot;post&amp;quot; name=&amp;quot;adminForm&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;div id=&amp;quot;editcell&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;table class=&amp;quot;adminlist&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;thead&amp;gt;&lt;br /&gt;
        &amp;lt;tr&amp;gt;&lt;br /&gt;
            &amp;lt;th width=&amp;quot;5&amp;quot;&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo JText::_( &#039;ID&#039; ); ?&amp;gt;&lt;br /&gt;
            &amp;lt;/th&amp;gt;&lt;br /&gt;
            &amp;lt;th&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo JText::_( &#039;Greeting&#039; ); ?&amp;gt;&lt;br /&gt;
            &amp;lt;/th&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;            &lt;br /&gt;
    &amp;lt;/thead&amp;gt;&lt;br /&gt;
    &amp;lt;?php&lt;br /&gt;
    $k = 0;&lt;br /&gt;
    for ($i=0, $n=count( $this-&amp;gt;items ); $i &amp;lt; $n; $i++)&lt;br /&gt;
    {&lt;br /&gt;
        $row =&amp;amp; $this-&amp;gt;items[$i];&lt;br /&gt;
        ?&amp;gt;&lt;br /&gt;
        &amp;lt;tr class=&amp;quot;&amp;lt;?php echo &amp;quot;row$k&amp;quot;; ?&amp;gt;&amp;quot;&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo $row-&amp;gt;id; ?&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo $row-&amp;gt;greeting; ?&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;&lt;br /&gt;
        &amp;lt;?php&lt;br /&gt;
        $k = 1 - $k;&lt;br /&gt;
    }&lt;br /&gt;
    ?&amp;gt;&lt;br /&gt;
    &amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;option&amp;quot; value=&amp;quot;com_hello&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;task&amp;quot; value=&amp;quot;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;boxchecked&amp;quot; value=&amp;quot;0&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;controller&amp;quot; value=&amp;quot;hello&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/form&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Our hellos view is now complete. You can try out the component now to see the results.&lt;br /&gt;
&lt;br /&gt;
The component can be downloaded at: [[http://dev.joomla.org/components/com_jd-wiki/data/media/components/com_hello4a.zip|com_hello4a.zip]]&lt;br /&gt;
&lt;br /&gt;
== Getting Down and Dirty: Doing the Real Work ==&lt;br /&gt;
&lt;br /&gt;
Now that the Hellos view is done, it is time to move to the Hello view and model. This is where the real work will get done.&lt;br /&gt;
&lt;br /&gt;
==== The Hello Controller ====&lt;br /&gt;
&lt;br /&gt;
Our default controller just isn&#039;t going to cut it when it comes to doing work - all it is capable of doing is displaying views.&lt;br /&gt;
&lt;br /&gt;
We need to be able to handle the tasks that we are launching from the Hellos view: add, edit and remove.&lt;br /&gt;
&lt;br /&gt;
Add and edit are essentially the same task: they both display a form to the user that allows a greeting to be edited. The only difference is that new displays a blank form, and edit displays a form with data already in it. Since they are similar, we will map the add task onto the edit task handler. This is specified in our constructor:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * constructor (registers additional tasks to methods)&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function __construct()&lt;br /&gt;
{&lt;br /&gt;
    parent::__construct();&lt;br /&gt;
&lt;br /&gt;
    // Register Extra tasks&lt;br /&gt;
    $this-&amp;gt;registerTask( &#039;add&#039;  ,     &#039;edit&#039; );&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first parameter of JController::registerTask is the task to map, and the second is the method to map it to.&lt;br /&gt;
&lt;br /&gt;
We will start with handling the edit task. The controller&#039;s job is fairly simple for the edit task. All it has to do is specify the view and layout to load (the hello view and the form layout). We will also tell Joomla! to disable the mainmenu while we are editing our greeting. This prevents users from leaving unsaved records open.&lt;br /&gt;
&lt;br /&gt;
Our edit task handler looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * display the edit form&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function edit()&lt;br /&gt;
{&lt;br /&gt;
    JRequest::setVar( &#039;view&#039;, &#039;hello&#039; );&lt;br /&gt;
    JRequest::setVar( &#039;layout&#039;, &#039;form&#039;  );&lt;br /&gt;
    JRequest::setVar(&#039;hidemainmenu&#039;, 1);&lt;br /&gt;
&lt;br /&gt;
    parent::display();&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== The Hello View ====&lt;br /&gt;
&lt;br /&gt;
The Hello view will display a form which will allow the user to edit a greeting. The display method if the hello view has to do a few simple tasks:&lt;br /&gt;
&lt;br /&gt;
  * retrieve the data from the model&lt;br /&gt;
&lt;br /&gt;
  * create the toolbar&lt;br /&gt;
&lt;br /&gt;
  * pass the data into the template&lt;br /&gt;
&lt;br /&gt;
  * invoke the display() method to render the template&lt;br /&gt;
&lt;br /&gt;
This becomes a bit more complicated because the one view handles both the edit and add tasks. In our toolbar we want the user to know what whether they are adding or editing, so we have to determine which task was fired.&lt;br /&gt;
&lt;br /&gt;
Since we are already retrieving the record that we want to display from the model, we can use this data to determine what task was fired. If the task was edit, then the id field of our record will have been set. If the task was new, then it will not have been set. This can be used to determine if we have a new record or an existing record.&lt;br /&gt;
&lt;br /&gt;
We will add two buttons to the toolbar: save and cancel. Though the functionality will be the same, we want to display different buttons depending on whether it is a new or existing record. If it is a new record, we will display cancel. If it already exists, we will display close.&lt;br /&gt;
&lt;br /&gt;
Thus our display method looks like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * display method of Hello view&lt;br /&gt;
 * @return void&lt;br /&gt;
 **/&lt;br /&gt;
function display($tpl = null)&lt;br /&gt;
{&lt;br /&gt;
    //get the hello&lt;br /&gt;
    $hello        =&amp;amp; $this-&amp;gt;get(&#039;Data&#039;);&lt;br /&gt;
    $isNew        = ($hello-&amp;gt;id &amp;lt; 1);&lt;br /&gt;
&lt;br /&gt;
    $text = $isNew ? JText::_( &#039;New&#039; ) : JText::_( &#039;Edit&#039; );&lt;br /&gt;
    JToolBarHelper::title(   JText::_( &#039;Hello&#039; ).&#039;: &amp;lt;small&amp;gt;&amp;lt;small&amp;gt;[ &#039; . $text.&#039; ]&amp;lt;/small&amp;gt;&amp;lt;/small&amp;gt;&#039; );&lt;br /&gt;
    JToolBarHelper::save();&lt;br /&gt;
    if ($isNew)  {&lt;br /&gt;
        JToolBarHelper::cancel();&lt;br /&gt;
    } else {&lt;br /&gt;
        // for existing items the button is renamed `close`&lt;br /&gt;
        JToolBarHelper::cancel( &#039;cancel&#039;, &#039;Close&#039; );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    $this-&amp;gt;assignRef(&#039;hello&#039;, $hello);&lt;br /&gt;
    parent::display($tpl);&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== The Hello Model ====&lt;br /&gt;
&lt;br /&gt;
Our view needs data. Therefore, we need to create a model to model a hello.&lt;br /&gt;
&lt;br /&gt;
Our model will have two properties: _id and _data. _id will hold the id of the greeting and data will hold the data.&lt;br /&gt;
&lt;br /&gt;
We will start with a constructor, which will attempt to retrieve the id from the query:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Constructor that retrieves the ID from the request&lt;br /&gt;
 *&lt;br /&gt;
 * @access    public&lt;br /&gt;
 * @return    void&lt;br /&gt;
 */&lt;br /&gt;
function __construct()&lt;br /&gt;
{&lt;br /&gt;
    parent::__construct();&lt;br /&gt;
&lt;br /&gt;
    $array = JRequest::getVar(&#039;cid&#039;,  0, &#039;&#039;, &#039;array&#039;);&lt;br /&gt;
    $this-&amp;gt;setId((int)$array[0]);&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The JRequest::getVar() method is used to retrieve data from the request. The first parameter is the name of the form variable. The second parameter is the default value to assign if there is no value found. The third parameter is the name of the hash to retrieve the value from (get, post, etc), and the last value is the data type that should be forced on the value.&lt;br /&gt;
&lt;br /&gt;
Our constructor will take the first value from the cid array and assign it to the id.&lt;br /&gt;
&lt;br /&gt;
Our setId() method can be used to set our id. Changing the id that our model points to will mean the id points to the wrong data. Therefore, when we set the id, we will clear the data property:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Method to set the hello identifier&lt;br /&gt;
 *&lt;br /&gt;
 * @access    public&lt;br /&gt;
 * @param    int Hello identifier&lt;br /&gt;
 * @return    void&lt;br /&gt;
 */&lt;br /&gt;
function setId($id)&lt;br /&gt;
{&lt;br /&gt;
    // Set id and wipe data&lt;br /&gt;
    $this-&amp;gt;_id        = $id;&lt;br /&gt;
    $this-&amp;gt;_data    = null;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finally, we need a method to retrieve our data: getData()&lt;br /&gt;
&lt;br /&gt;
getData will check if the _data property has already been set. If it has, it will simply return it. Otherwise, it will load the data from the database.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Method to get a hello&lt;br /&gt;
 * @return object with data&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
function &amp;amp;getData()&lt;br /&gt;
{&lt;br /&gt;
    // Load the data&lt;br /&gt;
    if (empty( $this-&amp;gt;_data )) {&lt;br /&gt;
        $query = &#039; SELECT * FROM #__hello &#039;.&lt;br /&gt;
                &#039;  WHERE id = &#039;.$this-&amp;gt;_id;&lt;br /&gt;
        $this-&amp;gt;_db-&amp;gt;setQuery( $query );&lt;br /&gt;
        $this-&amp;gt;_data = $this-&amp;gt;_db-&amp;gt;loadObject();&lt;br /&gt;
    }&lt;br /&gt;
    if (!$this-&amp;gt;_data) {&lt;br /&gt;
        $this-&amp;gt;_data = new stdClass();&lt;br /&gt;
        $this-&amp;gt;_data-&amp;gt;id = 0;&lt;br /&gt;
        $this-&amp;gt;_data-&amp;gt;greeting = null;&lt;br /&gt;
    }&lt;br /&gt;
    return $this-&amp;gt;_data;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== The Form ====&lt;br /&gt;
&lt;br /&gt;
Now all that is left is to create the form that the data will go into. Since we specified our layout as form, the form will go in a file in the tmpl directory of the hello view called form.php:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;); ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;form action=&amp;quot;index.php&amp;quot; method=&amp;quot;post&amp;quot; name=&amp;quot;adminForm&amp;quot; id=&amp;quot;adminForm&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;div class=&amp;quot;col100&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;fieldset class=&amp;quot;adminform&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;legend&amp;gt;&amp;lt;?php echo JText::_( &#039;Details&#039; ); ?&amp;gt;&amp;lt;/legend&amp;gt;&lt;br /&gt;
        &amp;lt;table class=&amp;quot;admintable&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;tr&amp;gt;&lt;br /&gt;
            &amp;lt;td width=&amp;quot;100&amp;quot; align=&amp;quot;right&amp;quot; class=&amp;quot;key&amp;quot;&amp;gt;&lt;br /&gt;
                &amp;lt;label for=&amp;quot;greeting&amp;quot;&amp;gt;&lt;br /&gt;
                    &amp;lt;?php echo JText::_( &#039;Greeting&#039; ); ?&amp;gt;:&lt;br /&gt;
                &amp;lt;/label&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;input class=&amp;quot;text_area&amp;quot; type=&amp;quot;text&amp;quot; name=&amp;quot;greeting&amp;quot; id=&amp;quot;greeting&amp;quot; size=&amp;quot;32&amp;quot; maxlength=&amp;quot;250&amp;quot; value=&amp;quot;&amp;lt;?php echo $this-&amp;gt;hello-&amp;gt;greeting;?&amp;gt;&amp;quot; /&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;&lt;br /&gt;
    &amp;lt;/table&amp;gt;&lt;br /&gt;
    &amp;lt;/fieldset&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;clr&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;option&amp;quot; value=&amp;quot;com_hello&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;id&amp;quot; value=&amp;quot;&amp;lt;?php echo $this-&amp;gt;hello-&amp;gt;id; ?&amp;gt;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;task&amp;quot; value=&amp;quot;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;controller&amp;quot; value=&amp;quot;hello&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/form&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in addition to the input field, there is a hidden field for the id. The user doesn&#039;t need to edit the id (and shouldn&#039;t), so we silently pass it along in the form.&lt;br /&gt;
&lt;br /&gt;
==== Implementing the Functionality ====&lt;br /&gt;
&lt;br /&gt;
So far, our controller only handles two tasks: edit and new. However, we also have buttons to save, delete and cancel records. We need to write code to handle and perform these tasks.&lt;br /&gt;
&lt;br /&gt;
=== Saving a Record ===&lt;br /&gt;
&lt;br /&gt;
The logical next step is to implement the functionality to save a record. Normally, this would require some switches and logic to handle various cases, such as the difference between creating a new record (an INSERT query), and updating an existing query (an UPDATE query). Also, there are complexities involved in getting the data from the form and putting it into the query.&lt;br /&gt;
&lt;br /&gt;
The Joomla! framework takes care of a lot of this for you. The JTable class makes it easy to manipulate records in the database without having to worry about writing the SQL code that lies behind these updates. It also makes it easy to transfer data from an HTML form into the database.&lt;br /&gt;
&lt;br /&gt;
== Creating the Table Class ==&lt;br /&gt;
&lt;br /&gt;
The JTable class is an abstract class from which you can derive child classes to work with specific tables. To use it, you simply create a class that extends the JTable class, add your database fields as properties, and override the constructor to specify the name of the table and the primary key.&lt;br /&gt;
&lt;br /&gt;
Here is our JTable class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * Hello World table class&lt;br /&gt;
 * &lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://dev.joomla.org/component/option,com_jd-wiki/Itemid,31/id,tutorials:components/&lt;br /&gt;
 * @license        GNU/GPL&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// no direct access&lt;br /&gt;
defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;);&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Hello Table class&lt;br /&gt;
 *&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 */&lt;br /&gt;
class TableHello extends JTable&lt;br /&gt;
{&lt;br /&gt;
    /**&lt;br /&gt;
     * Primary Key&lt;br /&gt;
     *&lt;br /&gt;
     * @var int&lt;br /&gt;
     */&lt;br /&gt;
    var $id = null;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * @var string&lt;br /&gt;
     */&lt;br /&gt;
    var $greeting = null;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Constructor&lt;br /&gt;
     *&lt;br /&gt;
     * @param object Database connector object&lt;br /&gt;
     */&lt;br /&gt;
    function TableHello( &amp;amp;$db ) {&lt;br /&gt;
        parent::__construct(&#039;#__hello&#039;, &#039;id&#039;, $db);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You will see here that we have defined our two fields: the id field and the greeting field. Then we have defined a constructor, which will call the constructor of the parent class and pass it the name of the table (hello), the name of the field which is the primary key (id), and the database connector object.&lt;br /&gt;
&lt;br /&gt;
This file should be called hello.php and it will go in a directory called tables in the administrator section of our component.&lt;br /&gt;
&lt;br /&gt;
== Implementing the Function in our Model ==&lt;br /&gt;
&lt;br /&gt;
We are now ready to add the method to the model which will save our record. We will call this method store. Our store() method will do three things: bind the data from the form to the TableHello object, check to ensure that the record is properly formed, and store the record in the database.&lt;br /&gt;
&lt;br /&gt;
Our method looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Method to store a record&lt;br /&gt;
 *&lt;br /&gt;
 * @access    public&lt;br /&gt;
 * @return    boolean    True on success&lt;br /&gt;
 */&lt;br /&gt;
function store()&lt;br /&gt;
{&lt;br /&gt;
    $row =&amp;amp; $this-&amp;gt;getTable();&lt;br /&gt;
&lt;br /&gt;
    $data = JRequest::get( &#039;post&#039; );&lt;br /&gt;
    // Bind the form fields to the hello table&lt;br /&gt;
    if (!$row-&amp;gt;bind($data)) {&lt;br /&gt;
        $this-&amp;gt;setError($this-&amp;gt;_db-&amp;gt;getErrorMsg());&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // Make sure the hello record is valid&lt;br /&gt;
    if (!$row-&amp;gt;check()) {&lt;br /&gt;
        $this-&amp;gt;setError($this-&amp;gt;_db-&amp;gt;getErrorMsg());&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // Store the web link table to the database&lt;br /&gt;
    if (!$row-&amp;gt;store()) {&lt;br /&gt;
        $this-&amp;gt;setError($this-&amp;gt;_db-&amp;gt;getErrorMsg());&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return true;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This method gets added to the hello model.&lt;br /&gt;
&lt;br /&gt;
The method takes one parameter, which is an associative array of data that we want to store in the database. This can easily be retrieved from the request as will be seen later.&lt;br /&gt;
&lt;br /&gt;
You will see that the first line retrieves a reference to our JTable object. If we name our table properly, we don&#039;t have to specify its name - the JModel class knows where to find it. You may recall that we called our table class TableHello and put it in a file called hello.php in the tables directory. If you follow this convention, the JModel class can create your object automatically.&lt;br /&gt;
&lt;br /&gt;
The second line will retrieve the data from the form. The JRequest class makes this very easy. In this case, we are retrieving all of the variables that were submitted using the &#039;POST&#039; method. These will be returned as an associative array.&lt;br /&gt;
&lt;br /&gt;
The rest is easy - we bind, check and store. The bind() method will copy values from the array into the corresponding property of the table object. In this case, it will take the values of id and greeting and copy them to our TableHello object.&lt;br /&gt;
&lt;br /&gt;
The check() method will perform data verification. In the JTable() class, this method simply returns true. While this doesn&#039;t provide any value for us currently, by calling this method we make it possible to do data checking using our TableHello class in the future. This method can be overridden in our TableHello class with a method that performs the appropriate checks.&lt;br /&gt;
&lt;br /&gt;
The store() method will take the data that is in the object and store it in the database. If the id is 0, it will create a new record (INSERT), otherwise, it will update the existing record (UPDATE).&lt;br /&gt;
&lt;br /&gt;
== Adding the Task to the Controller ==&lt;br /&gt;
&lt;br /&gt;
We are now ready to add our task to the controller. Since the task that we are firing is called &#039;save&#039;, we must call our method &#039;save&#039;. This is simple:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * save a record (and redirect to main page)&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function save()&lt;br /&gt;
{&lt;br /&gt;
    $model = $this-&amp;gt;getModel(&#039;hello&#039;);&lt;br /&gt;
&lt;br /&gt;
    if ($model-&amp;gt;store()) {&lt;br /&gt;
        $msg = JText::_( &#039;Greeting Saved!&#039; );&lt;br /&gt;
    } else {&lt;br /&gt;
        $msg = JText::_( &#039;Error Saving Greeting&#039; );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // Check the table in so it can be edited.... we are done with it anyway&lt;br /&gt;
    $link = &#039;index.php?option=com_hello&#039;;&lt;br /&gt;
    $this-&amp;gt;setRedirect($link, $msg);&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All we do is get our model and invoke the store() method. Then we use the setRedirect() method to redirect back to our list of greetings. We also pass a message along, which will be displayed at the top of the page.&lt;br /&gt;
&lt;br /&gt;
=== Deleting a Record ===&lt;br /&gt;
&lt;br /&gt;
=== Implementing the Function in the Model ===&lt;br /&gt;
&lt;br /&gt;
In the model, we will retrieve the list of IDs to delete and call the JTable class to delete them. Here it is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Method to delete record(s)&lt;br /&gt;
 *&lt;br /&gt;
 * @access    public&lt;br /&gt;
 * @return    boolean    True on success&lt;br /&gt;
 */&lt;br /&gt;
function delete()&lt;br /&gt;
{&lt;br /&gt;
    $cids = JRequest::getVar( &#039;cid&#039;, array(0), &#039;post&#039;, &#039;array&#039; );&lt;br /&gt;
    $row =&amp;amp; $this-&amp;gt;getTable();&lt;br /&gt;
&lt;br /&gt;
    foreach($cids as $cid) {&lt;br /&gt;
        if (!$row-&amp;gt;delete( $cid )) {&lt;br /&gt;
            $this-&amp;gt;setError( $row-&amp;gt;getErrorMsg() );&lt;br /&gt;
            return false;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return true;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We invoke the JRequest::getVar() method to get the data from the request, then we invoke the $row-&amp;gt;delete() method to delete each row. By storing errors in the model we make it possible to retrieve them later if we so choose.&lt;br /&gt;
&lt;br /&gt;
=== Handling the Remove Task in the Controller ===&lt;br /&gt;
&lt;br /&gt;
This is similar to the save() method which handled the save task:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * remove record(s)&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function remove()&lt;br /&gt;
{&lt;br /&gt;
    $model = $this-&amp;gt;getModel(&#039;hello&#039;);&lt;br /&gt;
    if(!$model-&amp;gt;delete()) {&lt;br /&gt;
        $msg = JText::_( &#039;Error: One or More Greetings Could not be Deleted&#039; );&lt;br /&gt;
    } else {&lt;br /&gt;
        $msg = JText::_( &#039;Greeting(s) Deleted&#039; );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    $this-&amp;gt;setRedirect( &#039;index.php?option=com_hello&#039;, $msg );&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Cancelling the Edit Operation ====&lt;br /&gt;
&lt;br /&gt;
To cancel the edit operation, all we have to do is redirect back to the main view:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * cancel editing a record&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function cancel()&lt;br /&gt;
{&lt;br /&gt;
    $msg = JText::_( &#039;Operation Cancelled&#039; );&lt;br /&gt;
    $this-&amp;gt;setRedirect( &#039;index.php?option=com_hello&#039;, $msg );&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
We have now implemented a basic backend to our component. We are now able to edit the entries that are viewed in the frontend. We have demonstrated the interaction between models, views and controllers. We have shown how the JTable class can be extended to provide easy access to tables in the database. It can also be seen how the JToolBarHelper class can be used to create button bars in components to present a standardized look between components.&lt;br /&gt;
&lt;br /&gt;
== Contributors ==&lt;br /&gt;
* staalanden&lt;br /&gt;
&lt;br /&gt;
== Download ==&lt;br /&gt;
&lt;br /&gt;
The component can be downloaded at: [http://joomlacode.org/gf/download/frsrelease/8104/29402/com_hello4a_01.zip com_hello4a_01]&lt;/div&gt;</summary>
		<author><name>Tonie</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Creating_an_Administrator_Interface&amp;diff=9506</id>
		<title>J1.5:Developing a MVC Component/Creating an Administrator Interface</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Creating_an_Administrator_Interface&amp;diff=9506"/>
		<updated>2008-07-27T19:00:34Z</updated>

		<summary type="html">&lt;p&gt;Tonie: /* Creating the Basic Framework */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
In the first three tutorials, we have developed a MVC component that retrieves its data from a table in the database. Currently, there is no way to add data to the database except to do it manually using another tool. In this tutorial, we will develop an administrator section for our component which will make it possible to manage the entries in the database.&lt;br /&gt;
&lt;br /&gt;
== Creating the Basic Framework ==&lt;br /&gt;
&lt;br /&gt;
The basic framework of the administrator panel is very similar to the site portion. The main entry point for the administrator section of the component is hello.php. This file is identical to the hello.php file that was used in the site portion except the name of the controller it loads will be changed to HellosController. The default controller is also called controller.php and this file is identical to the default controller in the site portion, with the exception that the controller is named HellosController instead of HelloController. This difference is so that JController will by default load the hellos view, which will display a list of our greetings.&lt;br /&gt;
&lt;br /&gt;
Here is the listing for hello.php:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_4&lt;br /&gt;
 * @license    GNU/GPL&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
// No direct access&lt;br /&gt;
&lt;br /&gt;
defined( &#039;_JEXEC&#039; ) or die( &#039;Restricted access&#039; );&lt;br /&gt;
&lt;br /&gt;
// Require the base controller&lt;br /&gt;
&lt;br /&gt;
require_once( JPATH_COMPONENT.DS.&#039;controller.php&#039; );&lt;br /&gt;
&lt;br /&gt;
// Require specific controller if requested&lt;br /&gt;
if($controller = JRequest::getWord(&#039;controller&#039;)) {&lt;br /&gt;
    $path = JPATH_COMPONENT.DS.&#039;controllers&#039;.DS.$controller.&#039;.php&#039;;&lt;br /&gt;
    if (file_exists($path)) {&lt;br /&gt;
        require_once $path;&lt;br /&gt;
    } else {&lt;br /&gt;
        $controller = &#039;&#039;;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Create the controller&lt;br /&gt;
$classname    = &#039;HellosController&#039;.$controller;&lt;br /&gt;
$controller   = new $classname( );&lt;br /&gt;
&lt;br /&gt;
// Perform the Request task&lt;br /&gt;
$controller-&amp;gt;execute( JRequest::getVar( &#039;task&#039; ) );&lt;br /&gt;
&lt;br /&gt;
// Redirect if set by the controller&lt;br /&gt;
$controller-&amp;gt;redirect();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The view and model that we will start with is the hellos view and the hellos model. We will start with the model.&lt;br /&gt;
&lt;br /&gt;
==== The Hellos Model ====&lt;br /&gt;
&lt;br /&gt;
The Hellos Model will be very simple. The only operation that we currently need is the ability to retrieve the list of hellos from the database. This operation will be implemented in a method called getData().&lt;br /&gt;
&lt;br /&gt;
The JModel class has a built in protected method called _getList(). This method can be used to simplify the task of retrieving a list of records from the database. We simply need to pass it the query and it will return the list of records.&lt;br /&gt;
&lt;br /&gt;
At a later point in time, we might want to use our query from within another method. Therefore, we will create a private method called _buildQuery() which will return the query that will be passed to _getList(). This makes it easier to change the query as well since it is localized in one place.&lt;br /&gt;
&lt;br /&gt;
Therefore we need two methods in our class: getData() and _buildQuery().&lt;br /&gt;
&lt;br /&gt;
_buildQuery() simply returns the query. It looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source  lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Returns the query&lt;br /&gt;
 * @return string The query to be used to retrieve the rows from the database&lt;br /&gt;
 */&lt;br /&gt;
function _buildQuery()&lt;br /&gt;
{&lt;br /&gt;
    $query = &#039; SELECT * &#039;&lt;br /&gt;
           . &#039; FROM #__hello &#039;&lt;br /&gt;
    ;&lt;br /&gt;
&lt;br /&gt;
    return $query;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
getData() will obtain the query and retrieve the records from the database. Now it might happen that we need to retrieve this list of data twice in one page load. It would be a waste to have to query the database twice. Therefore, we will have this method store the data in a protected property so that on subsequent requests it can simply return the data it has already retrieved. This property will be called _data.&lt;br /&gt;
&lt;br /&gt;
Here is the getData() method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source  lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Retrieves the hello data&lt;br /&gt;
 * @return array Array of objects containing the data from the database&lt;br /&gt;
 */&lt;br /&gt;
function getData()&lt;br /&gt;
{&lt;br /&gt;
    // Lets load the data if it doesn&#039;t already exist&lt;br /&gt;
    if (empty( $this-&amp;gt;_data ))&lt;br /&gt;
    {&lt;br /&gt;
        $query = $this-&amp;gt;_buildQuery();&lt;br /&gt;
        $this-&amp;gt;_data = $this-&amp;gt;_getList( $query );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return $this-&amp;gt;_data;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The completed model looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * Hellos Model for Hello World Component&lt;br /&gt;
 * &lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://dev.joomla.org/component/option,com_jd-wiki/Itemid,31/id,tutorials:components/&lt;br /&gt;
 * @license        GNU/GPL&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// Check to ensure this file is included in Joomla!&lt;br /&gt;
defined(&#039;_JEXEC&#039;) or die();&lt;br /&gt;
&lt;br /&gt;
jimport( &#039;joomla.application.component.model&#039; );&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Hello Model&lt;br /&gt;
 *&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 */&lt;br /&gt;
class HellosModelHellos extends JModel&lt;br /&gt;
{&lt;br /&gt;
    /**&lt;br /&gt;
     * Hellos data array&lt;br /&gt;
     *&lt;br /&gt;
     * @var array&lt;br /&gt;
     */&lt;br /&gt;
    var $_data;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Returns the query&lt;br /&gt;
     * @return string The query to be used to retrieve the rows from the database&lt;br /&gt;
     */&lt;br /&gt;
    function _buildQuery()&lt;br /&gt;
    {&lt;br /&gt;
        $query = &#039; SELECT * &#039;&lt;br /&gt;
            . &#039; FROM #__hello &#039;&lt;br /&gt;
        ;&lt;br /&gt;
        return $query;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Retrieves the hello data&lt;br /&gt;
     * @return array Array of objects containing the data from the database&lt;br /&gt;
     */&lt;br /&gt;
    function getData()&lt;br /&gt;
    {&lt;br /&gt;
        // Lets load the data if it doesn&#039;t already exist&lt;br /&gt;
        if (empty( $this-&amp;gt;_data ))&lt;br /&gt;
        {&lt;br /&gt;
            $query = $this-&amp;gt;_buildQuery();&lt;br /&gt;
            $this-&amp;gt;_data = $this-&amp;gt;_getList( $query );&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        return $this-&amp;gt;_data;&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This file is saved as models/hellos.php.&lt;br /&gt;
&lt;br /&gt;
==== The Hellos View ====&lt;br /&gt;
&lt;br /&gt;
Now that we have a model to retrieve our data, we need to display it. This view will be fairly similar to the view from the site section as well.&lt;br /&gt;
&lt;br /&gt;
Just as our model was automatically instantiated in the site, so it is in the administrator. Methods that start with get in the model can be accessed using the get() method of the JView class. So our view has three lines: one to retrieve the data from the model, one to push the data into the template, and one to invoke the display method to display the output. Thus we have:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * Hellos View for Hello World Component&lt;br /&gt;
 * &lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://dev.joomla.org/component/option,com_jd-wiki/Itemid,31/id,tutorials:components/&lt;br /&gt;
 * @license        GNU/GPL&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// Check to ensure this file is included in Joomla!&lt;br /&gt;
defined(&#039;_JEXEC&#039;) or die();&lt;br /&gt;
&lt;br /&gt;
jimport( &#039;joomla.application.component.view&#039; );&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Hellos View&lt;br /&gt;
 *&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 */&lt;br /&gt;
class HellosViewHellos extends JView&lt;br /&gt;
{&lt;br /&gt;
    /**&lt;br /&gt;
     * Hellos view display method&lt;br /&gt;
     * @return void&lt;br /&gt;
     **/&lt;br /&gt;
    function display($tpl = null)&lt;br /&gt;
    {&lt;br /&gt;
        JToolBarHelper::title( JText::_( &#039;Hello Manager&#039; ), &#039;generic.png&#039; );&lt;br /&gt;
        JToolBarHelper::deleteList();&lt;br /&gt;
        JToolBarHelper::editListX();&lt;br /&gt;
        JToolBarHelper::addNewX();&lt;br /&gt;
&lt;br /&gt;
        // Get data from the model&lt;br /&gt;
        $items =&amp;amp; $this-&amp;gt;get( &#039;Data&#039;);&lt;br /&gt;
&lt;br /&gt;
        $this-&amp;gt;assignRef( &#039;items&#039;, $items );&lt;br /&gt;
&lt;br /&gt;
        parent::display($tpl);&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This file is saved as views/hellos/view.html.php.&lt;br /&gt;
&lt;br /&gt;
==== The Hellos Template ====&lt;br /&gt;
&lt;br /&gt;
The template will take the data pushed into it from the view and produce the output. We will display our output in a simple table. While the frontend template was very simple, in the administrator we will need a minimal amount of extra logic to handle looping through the data.&lt;br /&gt;
&lt;br /&gt;
Here is our template:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;); ?&amp;gt;&lt;br /&gt;
&amp;lt;form action=&amp;quot;index.php&amp;quot; method=&amp;quot;post&amp;quot; name=&amp;quot;adminForm&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;div id=&amp;quot;editcell&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;table class=&amp;quot;adminlist&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;thead&amp;gt;&lt;br /&gt;
        &amp;lt;tr&amp;gt;&lt;br /&gt;
            &amp;lt;th width=&amp;quot;5&amp;quot;&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo JText::_( &#039;ID&#039; ); ?&amp;gt;&lt;br /&gt;
            &amp;lt;/th&amp;gt;&lt;br /&gt;
            &amp;lt;th&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo JText::_( &#039;Greeting&#039; ); ?&amp;gt;&lt;br /&gt;
            &amp;lt;/th&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;            &lt;br /&gt;
    &amp;lt;/thead&amp;gt;&lt;br /&gt;
    &amp;lt;?php&lt;br /&gt;
    $k = 0;&lt;br /&gt;
    for ($i=0, $n=count( $this-&amp;gt;items ); $i &amp;lt; $n; $i++)&lt;br /&gt;
    {&lt;br /&gt;
        $row =&amp;amp; $this-&amp;gt;items[$i];&lt;br /&gt;
        ?&amp;gt;&lt;br /&gt;
        &amp;lt;tr class=&amp;quot;&amp;lt;?php echo &amp;quot;row$k&amp;quot;; ?&amp;gt;&amp;quot;&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo $row-&amp;gt;id; ?&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo $row-&amp;gt;greeting; ?&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;&lt;br /&gt;
        &amp;lt;?php&lt;br /&gt;
        $k = 1 - $k;&lt;br /&gt;
    }&lt;br /&gt;
    ?&amp;gt;&lt;br /&gt;
    &amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;option&amp;quot; value=&amp;quot;com_hello&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;task&amp;quot; value=&amp;quot;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;boxchecked&amp;quot; value=&amp;quot;0&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;controller&amp;quot; value=&amp;quot;hello&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/form&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This template is saved as views/hellos/tmpl/default.php.&lt;br /&gt;
&lt;br /&gt;
You will notice that our output is enclosed in a form. Though this is not necessary now, it will be soon.&lt;br /&gt;
&lt;br /&gt;
We have now completed the basic part of the first view. We have added five files to the admin section of our component:&lt;br /&gt;
&lt;br /&gt;
* hello.php&lt;br /&gt;
* controller.php&lt;br /&gt;
* models/hellos.php&lt;br /&gt;
* views/hellos/view.html.php&lt;br /&gt;
* views/hellos/tmpl/default.php&lt;br /&gt;
&lt;br /&gt;
You can now add these files to the XML install file and give it a try!&lt;br /&gt;
&lt;br /&gt;
== Adding Functionality ==&lt;br /&gt;
&lt;br /&gt;
So far our administrator section is pretty useless. It doesn&#039;t really do anything - all it does is display the entries that we have in our database.&lt;br /&gt;
&lt;br /&gt;
In order to make it useful, we need to add some buttons and links.&lt;br /&gt;
&lt;br /&gt;
==== The Toolbar ====&lt;br /&gt;
&lt;br /&gt;
You may have noticed the toolbar that appears at the top of other Joomla! component administrator panels. Our component needs one as well. Joomla! makes this very easy to do. We will add buttons Delete records, Edit records, and create New records. We will also add a title that will be displayed on our toolbar.&lt;br /&gt;
&lt;br /&gt;
This is done by adding code to the view. To add the buttons, we use static methods from the Joomla! JToolBarHelper class. The code looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;JToolBarHelper::title(   JText::_( &#039;Hello Manager&#039; ), &#039;generic.png&#039; );&lt;br /&gt;
JToolBarHelper::deleteList();&lt;br /&gt;
JToolBarHelper::editListX();&lt;br /&gt;
JToolBarHelper::addNewX();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
These three methods will create the appropriate buttons. The deleteList() method can optionally take up to three parameters - the first parameter is a string to display to the user to confirm that they want to delete the records. The second is the task that should be sent with the query (the default is &#039;remove&#039;), and the third is the text that should be displayed below the button.&lt;br /&gt;
&lt;br /&gt;
The editListX() and addNewX() methods can each take two optional parameters. The first is the task (which are by default edit and add, respectively), and the second is the text that should be displayed below the button.&lt;br /&gt;
&lt;br /&gt;
 &#039;&#039;*You may have noticed the use of the JText::_ method in the template before and here as well. This is a handy function that makes component translation much easier. The JText::_ method will look up the string in your component language file and return the translated string. If not translation is found, it will return the string that you passed it. If you want to translate your component into another language, all you have to do is create a language file that will map the strings within the quotes to the translated version of the string.&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==== Checkboxes and Links ====&lt;br /&gt;
&lt;br /&gt;
We now have buttons. Two of those buttons operate on existing records. But how do we know which records to operate on? We have to let the user tell us. To do this, we need to add checkboxes to our table so that the user can select certain records. This is done in our template.&lt;br /&gt;
&lt;br /&gt;
In order to the add the checkboxes, we need to add an extra column into our table. We will add the column in between the two that we already have.&lt;br /&gt;
&lt;br /&gt;
In the header of the column, we will add a checkbox which can be used to toggle all the boxes below it on or off:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;th width=&amp;quot;20&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;input type=&amp;quot;checkbox&amp;quot; name=&amp;quot;toggle&amp;quot; value=&amp;quot;&amp;quot; onclick=&amp;quot;checkAll(&amp;lt;?php echo count( $this-&amp;gt;items ); ?&amp;gt;);&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/th&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Javascript checkAll function is a function that is built into the Joomla! base Javascript package that provides the functionality that we want here.&lt;br /&gt;
&lt;br /&gt;
Now we need to add the checkboxes into the individual rows. Joomla!&#039;s JHTML class has a method, JHTML::_(), which will generate our checkbox for us. We will add the following line to our loop:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$checked    = JHTML::_( &#039;grid.id&#039;, $i, $row-&amp;gt;id );&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
after the line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$row =&amp;amp; $this-&amp;gt;items[$i];&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then we will add a cell in between the two that we already have:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;td&amp;gt;&lt;br /&gt;
    &amp;lt;?php echo $checked; ?&amp;gt;&lt;br /&gt;
&amp;lt;/td&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It can be cumbersome to have to check the box that we want to edit and then move up and click the edit button. Therefore, we will add a link that it will go straight to the greeting&#039;s edit form. We will add the following line after the call to the JHTML::_() method to generate the link HTML:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$link = JRoute::_( &#039;index.php?option=com_hello&amp;gt;controller=hello&amp;gt;task=edit&amp;gt;cid[]=&#039;. $row-&amp;gt;id );&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And we include the link in the cell showing the greeting text:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;td&amp;gt;&lt;br /&gt;
    &amp;lt;a href=&amp;quot;&amp;lt;?php echo $link; ?&amp;gt;&amp;quot;&amp;gt;&amp;lt;?php echo $row-&amp;gt;greeting; ?&amp;gt;&amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;lt;/td&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You will notice that this link points to the hello controller. This controller will handle the data manipulation of our greetings.&lt;br /&gt;
&lt;br /&gt;
If you recall from above, we had four hidden input fields at the bottom of our form. The first input field was named &#039;option&#039;. This field is necessary so that we stay in our component. The second input field was task. This form property gets set when one of the buttons in the toolbar is clicked. A Javascript error will result and the buttons will not work if this input field is omitted. The third input field is the boxchecked field. This field keeps track of the number of boxes that are checked. The edit and delete buttons will check to ensure that this is greater than zero and will not allow the form to be submitted if it is not. The fourth input field is the controller field. This is used to specify that tasks fired from this form will be handled by the hello controller.&lt;br /&gt;
&lt;br /&gt;
Here is the code for the completed default.php file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;); ?&amp;gt;&lt;br /&gt;
&amp;lt;form action=&amp;quot;index.php&amp;quot; method=&amp;quot;post&amp;quot; name=&amp;quot;adminForm&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;div id=&amp;quot;editcell&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;table class=&amp;quot;adminlist&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;thead&amp;gt;&lt;br /&gt;
        &amp;lt;tr&amp;gt;&lt;br /&gt;
            &amp;lt;th width=&amp;quot;5&amp;quot;&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo JText::_( &#039;ID&#039; ); ?&amp;gt;&lt;br /&gt;
            &amp;lt;/th&amp;gt;&lt;br /&gt;
            &amp;lt;th&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo JText::_( &#039;Greeting&#039; ); ?&amp;gt;&lt;br /&gt;
            &amp;lt;/th&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;            &lt;br /&gt;
    &amp;lt;/thead&amp;gt;&lt;br /&gt;
    &amp;lt;?php&lt;br /&gt;
    $k = 0;&lt;br /&gt;
    for ($i=0, $n=count( $this-&amp;gt;items ); $i &amp;lt; $n; $i++)&lt;br /&gt;
    {&lt;br /&gt;
        $row =&amp;amp; $this-&amp;gt;items[$i];&lt;br /&gt;
        ?&amp;gt;&lt;br /&gt;
        &amp;lt;tr class=&amp;quot;&amp;lt;?php echo &amp;quot;row$k&amp;quot;; ?&amp;gt;&amp;quot;&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo $row-&amp;gt;id; ?&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo $row-&amp;gt;greeting; ?&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;&lt;br /&gt;
        &amp;lt;?php&lt;br /&gt;
        $k = 1 - $k;&lt;br /&gt;
    }&lt;br /&gt;
    ?&amp;gt;&lt;br /&gt;
    &amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;option&amp;quot; value=&amp;quot;com_hello&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;task&amp;quot; value=&amp;quot;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;boxchecked&amp;quot; value=&amp;quot;0&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;controller&amp;quot; value=&amp;quot;hello&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/form&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Our hellos view is now complete. You can try out the component now to see the results.&lt;br /&gt;
&lt;br /&gt;
The component can be downloaded at: [[http://dev.joomla.org/components/com_jd-wiki/data/media/components/com_hello4a.zip|com_hello4a.zip]]&lt;br /&gt;
&lt;br /&gt;
== Getting Down and Dirty: Doing the Real Work ==&lt;br /&gt;
&lt;br /&gt;
Now that the Hellos view is done, it is time to move to the Hello view and model. This is where the real work will get done.&lt;br /&gt;
&lt;br /&gt;
==== The Hello Controller ====&lt;br /&gt;
&lt;br /&gt;
Our default controller just isn&#039;t going to cut it when it comes to doing work - all it is capable of doing is displaying views.&lt;br /&gt;
&lt;br /&gt;
We need to be able to handle the tasks that we are launching from the Hellos view: add, edit and remove.&lt;br /&gt;
&lt;br /&gt;
Add and edit are essentially the same task: they both display a form to the user that allows a greeting to be edited. The only difference is that new displays a blank form, and edit displays a form with data already in it. Since they are similar, we will map the add task onto the edit task handler. This is specified in our constructor:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * constructor (registers additional tasks to methods)&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function __construct()&lt;br /&gt;
{&lt;br /&gt;
    parent::__construct();&lt;br /&gt;
&lt;br /&gt;
    // Register Extra tasks&lt;br /&gt;
    $this-&amp;gt;registerTask( &#039;add&#039;  ,     &#039;edit&#039; );&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first parameter of JController::registerTask is the task to map, and the second is the method to map it to.&lt;br /&gt;
&lt;br /&gt;
We will start with handling the edit task. The controller&#039;s job is fairly simple for the edit task. All it has to do is specify the view and layout to load (the hello view and the form layout). We will also tell Joomla! to disable the mainmenu while we are editing our greeting. This prevents users from leaving unsaved records open.&lt;br /&gt;
&lt;br /&gt;
Our edit task handler looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * display the edit form&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function edit()&lt;br /&gt;
{&lt;br /&gt;
    JRequest::setVar( &#039;view&#039;, &#039;hello&#039; );&lt;br /&gt;
    JRequest::setVar( &#039;layout&#039;, &#039;form&#039;  );&lt;br /&gt;
    JRequest::setVar(&#039;hidemainmenu&#039;, 1);&lt;br /&gt;
&lt;br /&gt;
    parent::display();&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== The Hello View ====&lt;br /&gt;
&lt;br /&gt;
The Hello view will display a form which will allow the user to edit a greeting. The display method if the hello view has to do a few simple tasks:&lt;br /&gt;
&lt;br /&gt;
  * retrieve the data from the model&lt;br /&gt;
&lt;br /&gt;
  * create the toolbar&lt;br /&gt;
&lt;br /&gt;
  * pass the data into the template&lt;br /&gt;
&lt;br /&gt;
  * invoke the display() method to render the template&lt;br /&gt;
&lt;br /&gt;
This becomes a bit more complicated because the one view handles both the edit and add tasks. In our toolbar we want the user to know what whether they are adding or editing, so we have to determine which task was fired.&lt;br /&gt;
&lt;br /&gt;
Since we are already retrieving the record that we want to display from the model, we can use this data to determine what task was fired. If the task was edit, then the id field of our record will have been set. If the task was new, then it will not have been set. This can be used to determine if we have a new record or an existing record.&lt;br /&gt;
&lt;br /&gt;
We will add two buttons to the toolbar: save and cancel. Though the functionality will be the same, we want to display different buttons depending on whether it is a new or existing record. If it is a new record, we will display cancel. If it already exists, we will display close.&lt;br /&gt;
&lt;br /&gt;
Thus our display method looks like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * display method of Hello view&lt;br /&gt;
 * @return void&lt;br /&gt;
 **/&lt;br /&gt;
function display($tpl = null)&lt;br /&gt;
{&lt;br /&gt;
    //get the hello&lt;br /&gt;
    $hello        =&amp;amp; $this-&amp;gt;get(&#039;Data&#039;);&lt;br /&gt;
    $isNew        = ($hello-&amp;gt;id &amp;lt; 1);&lt;br /&gt;
&lt;br /&gt;
    $text = $isNew ? JText::_( &#039;New&#039; ) : JText::_( &#039;Edit&#039; );&lt;br /&gt;
    JToolBarHelper::title(   JText::_( &#039;Hello&#039; ).&#039;: &amp;lt;small&amp;gt;&amp;lt;small&amp;gt;[ &#039; . $text.&#039; ]&amp;lt;/small&amp;gt;&amp;lt;/small&amp;gt;&#039; );&lt;br /&gt;
    JToolBarHelper::save();&lt;br /&gt;
    if ($isNew)  {&lt;br /&gt;
        JToolBarHelper::cancel();&lt;br /&gt;
    } else {&lt;br /&gt;
        // for existing items the button is renamed `close`&lt;br /&gt;
        JToolBarHelper::cancel( &#039;cancel&#039;, &#039;Close&#039; );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    $this-&amp;gt;assignRef(&#039;hello&#039;, $hello);&lt;br /&gt;
    parent::display($tpl);&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== The Hello Model ====&lt;br /&gt;
&lt;br /&gt;
Our view needs data. Therefore, we need to create a model to model a hello.&lt;br /&gt;
&lt;br /&gt;
Our model will have two properties: _id and _data. _id will hold the id of the greeting and data will hold the data.&lt;br /&gt;
&lt;br /&gt;
We will start with a constructor, which will attempt to retrieve the id from the query:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Constructor that retrieves the ID from the request&lt;br /&gt;
 *&lt;br /&gt;
 * @access    public&lt;br /&gt;
 * @return    void&lt;br /&gt;
 */&lt;br /&gt;
function __construct()&lt;br /&gt;
{&lt;br /&gt;
    parent::__construct();&lt;br /&gt;
&lt;br /&gt;
    $array = JRequest::getVar(&#039;cid&#039;,  0, &#039;&#039;, &#039;array&#039;);&lt;br /&gt;
    $this-&amp;gt;setId((int)$array[0]);&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The JRequest::getVar() method is used to retrieve data from the request. The first parameter is the name of the form variable. The second parameter is the default value to assign if there is no value found. The third parameter is the name of the hash to retrieve the value from (get, post, etc), and the last value is the data type that should be forced on the value.&lt;br /&gt;
&lt;br /&gt;
Our constructor will take the first value from the cid array and assign it to the id.&lt;br /&gt;
&lt;br /&gt;
Our setId() method can be used to set our id. Changing the id that our model points to will mean the id points to the wrong data. Therefore, when we set the id, we will clear the data property:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Method to set the hello identifier&lt;br /&gt;
 *&lt;br /&gt;
 * @access    public&lt;br /&gt;
 * @param    int Hello identifier&lt;br /&gt;
 * @return    void&lt;br /&gt;
 */&lt;br /&gt;
function setId($id)&lt;br /&gt;
{&lt;br /&gt;
    // Set id and wipe data&lt;br /&gt;
    $this-&amp;gt;_id        = $id;&lt;br /&gt;
    $this-&amp;gt;_data    = null;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finally, we need a method to retrieve our data: getData()&lt;br /&gt;
&lt;br /&gt;
getData will check if the _data property has already been set. If it has, it will simply return it. Otherwise, it will load the data from the database.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Method to get a hello&lt;br /&gt;
 * @return object with data&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
function &amp;amp;getData()&lt;br /&gt;
{&lt;br /&gt;
    // Load the data&lt;br /&gt;
    if (empty( $this-&amp;gt;_data )) {&lt;br /&gt;
        $query = &#039; SELECT * FROM #__hello &#039;.&lt;br /&gt;
                &#039;  WHERE id = &#039;.$this-&amp;gt;_id;&lt;br /&gt;
        $this-&amp;gt;_db-&amp;gt;setQuery( $query );&lt;br /&gt;
        $this-&amp;gt;_data = $this-&amp;gt;_db-&amp;gt;loadObject();&lt;br /&gt;
    }&lt;br /&gt;
    if (!$this-&amp;gt;_data) {&lt;br /&gt;
        $this-&amp;gt;_data = new stdClass();&lt;br /&gt;
        $this-&amp;gt;_data-&amp;gt;id = 0;&lt;br /&gt;
        $this-&amp;gt;_data-&amp;gt;greeting = null;&lt;br /&gt;
    }&lt;br /&gt;
    return $this-&amp;gt;_data;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== The Form ====&lt;br /&gt;
&lt;br /&gt;
Now all that is left is to create the form that the data will go into. Since we specified our layout as form, the form will go in a file in the tmpl directory of the hello view called form.php:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;); ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;form action=&amp;quot;index.php&amp;quot; method=&amp;quot;post&amp;quot; name=&amp;quot;adminForm&amp;quot; id=&amp;quot;adminForm&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;div class=&amp;quot;col100&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;fieldset class=&amp;quot;adminform&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;legend&amp;gt;&amp;lt;?php echo JText::_( &#039;Details&#039; ); ?&amp;gt;&amp;lt;/legend&amp;gt;&lt;br /&gt;
        &amp;lt;table class=&amp;quot;admintable&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;tr&amp;gt;&lt;br /&gt;
            &amp;lt;td width=&amp;quot;100&amp;quot; align=&amp;quot;right&amp;quot; class=&amp;quot;key&amp;quot;&amp;gt;&lt;br /&gt;
                &amp;lt;label for=&amp;quot;greeting&amp;quot;&amp;gt;&lt;br /&gt;
                    &amp;lt;?php echo JText::_( &#039;Greeting&#039; ); ?&amp;gt;:&lt;br /&gt;
                &amp;lt;/label&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;input class=&amp;quot;text_area&amp;quot; type=&amp;quot;text&amp;quot; name=&amp;quot;greeting&amp;quot; id=&amp;quot;greeting&amp;quot; size=&amp;quot;32&amp;quot; maxlength=&amp;quot;250&amp;quot; value=&amp;quot;&amp;lt;?php echo $this-&amp;gt;hello-&amp;gt;greeting;?&amp;gt;&amp;quot; /&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;&lt;br /&gt;
    &amp;lt;/table&amp;gt;&lt;br /&gt;
    &amp;lt;/fieldset&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;clr&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;option&amp;quot; value=&amp;quot;com_hello&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;id&amp;quot; value=&amp;quot;&amp;lt;?php echo $this-&amp;gt;hello-&amp;gt;id; ?&amp;gt;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;task&amp;quot; value=&amp;quot;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;controller&amp;quot; value=&amp;quot;hello&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/form&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in addition to the input field, there is a hidden field for the id. The user doesn&#039;t need to edit the id (and shouldn&#039;t), so we silently pass it along in the form.&lt;br /&gt;
&lt;br /&gt;
==== Implementing the Functionality ====&lt;br /&gt;
&lt;br /&gt;
So far, our controller only handles two tasks: edit and new. However, we also have buttons to save, delete and cancel records. We need to write code to handle and perform these tasks.&lt;br /&gt;
&lt;br /&gt;
=== Saving a Record ===&lt;br /&gt;
&lt;br /&gt;
The logical next step is to implement the functionality to save a record. Normally, this would require some switches and logic to handle various cases, such as the difference between creating a new record (an INSERT query), and updating an existing query (an UPDATE query). Also, there are complexities involved in getting the data from the form and putting it into the query.&lt;br /&gt;
&lt;br /&gt;
The Joomla! framework takes care of a lot of this for you. The JTable class makes it easy to manipulate records in the database without having to worry about writing the SQL code that lies behind these updates. It also makes it easy to transfer data from an HTML form into the database.&lt;br /&gt;
&lt;br /&gt;
== Creating the Table Class ==&lt;br /&gt;
&lt;br /&gt;
The JTable class is an abstract class from which you can derive child classes to work with specific tables. To use it, you simply create a class that extends the JTable class, add your database fields as properties, and override the constructor to specify the name of the table and the primary key.&lt;br /&gt;
&lt;br /&gt;
Here is our JTable class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * Hello World table class&lt;br /&gt;
 * &lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://dev.joomla.org/component/option,com_jd-wiki/Itemid,31/id,tutorials:components/&lt;br /&gt;
 * @license        GNU/GPL&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// no direct access&lt;br /&gt;
defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;);&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Hello Table class&lt;br /&gt;
 *&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 */&lt;br /&gt;
class TableHello extends JTable&lt;br /&gt;
{&lt;br /&gt;
    /**&lt;br /&gt;
     * Primary Key&lt;br /&gt;
     *&lt;br /&gt;
     * @var int&lt;br /&gt;
     */&lt;br /&gt;
    var $id = null;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * @var string&lt;br /&gt;
     */&lt;br /&gt;
    var $greeting = null;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Constructor&lt;br /&gt;
     *&lt;br /&gt;
     * @param object Database connector object&lt;br /&gt;
     */&lt;br /&gt;
    function TableHello( &amp;amp;$db ) {&lt;br /&gt;
        parent::__construct(&#039;#__hello&#039;, &#039;id&#039;, $db);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You will see here that we have defined our two fields: the id field and the greeting field. Then we have defined a constructor, which will call the constructor of the parent class and pass it the name of the table (hello), the name of the field which is the primary key (id), and the database connector object.&lt;br /&gt;
&lt;br /&gt;
This file should be called hello.php and it will go in a directory called tables in the administrator section of our component.&lt;br /&gt;
&lt;br /&gt;
== Implementing the Function in our Model ==&lt;br /&gt;
&lt;br /&gt;
We are now ready to add the method to the model which will save our record. We will call this method store. Our store() method will do three things: bind the data from the form to the TableHello object, check to ensure that the record is properly formed, and store the record in the database.&lt;br /&gt;
&lt;br /&gt;
Our method looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Method to store a record&lt;br /&gt;
 *&lt;br /&gt;
 * @access    public&lt;br /&gt;
 * @return    boolean    True on success&lt;br /&gt;
 */&lt;br /&gt;
function store()&lt;br /&gt;
{&lt;br /&gt;
    $row =&amp;amp; $this-&amp;gt;getTable();&lt;br /&gt;
&lt;br /&gt;
    $data = JRequest::get( &#039;post&#039; );&lt;br /&gt;
    // Bind the form fields to the hello table&lt;br /&gt;
    if (!$row-&amp;gt;bind($data)) {&lt;br /&gt;
        $this-&amp;gt;setError($this-&amp;gt;_db-&amp;gt;getErrorMsg());&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // Make sure the hello record is valid&lt;br /&gt;
    if (!$row-&amp;gt;check()) {&lt;br /&gt;
        $this-&amp;gt;setError($this-&amp;gt;_db-&amp;gt;getErrorMsg());&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // Store the web link table to the database&lt;br /&gt;
    if (!$row-&amp;gt;store()) {&lt;br /&gt;
        $this-&amp;gt;setError($this-&amp;gt;_db-&amp;gt;getErrorMsg());&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return true;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This method gets added to the hello model.&lt;br /&gt;
&lt;br /&gt;
The method takes one parameter, which is an associative array of data that we want to store in the database. This can easily be retrieved from the request as will be seen later.&lt;br /&gt;
&lt;br /&gt;
You will see that the first line retrieves a reference to our JTable object. If we name our table properly, we don&#039;t have to specify its name - the JModel class knows where to find it. You may recall that we called our table class TableHello and put it in a file called hello.php in the tables directory. If you follow this convention, the JModel class can create your object automatically.&lt;br /&gt;
&lt;br /&gt;
The second line will retrieve the data from the form. The JRequest class makes this very easy. In this case, we are retrieving all of the variables that were submitted using the &#039;POST&#039; method. These will be returned as an associative array.&lt;br /&gt;
&lt;br /&gt;
The rest is easy - we bind, check and store. The bind() method will copy values from the array into the corresponding property of the table object. In this case, it will take the values of id and greeting and copy them to our TableHello object.&lt;br /&gt;
&lt;br /&gt;
The check() method will perform data verification. In the JTable() class, this method simply returns true. While this doesn&#039;t provide any value for us currently, by calling this method we make it possible to do data checking using our TableHello class in the future. This method can be overridden in our TableHello class with a method that performs the appropriate checks.&lt;br /&gt;
&lt;br /&gt;
The store() method will take the data that is in the object and store it in the database. If the id is 0, it will create a new record (INSERT), otherwise, it will update the existing record (UPDATE).&lt;br /&gt;
&lt;br /&gt;
== Adding the Task to the Controller ==&lt;br /&gt;
&lt;br /&gt;
We are now ready to add our task to the controller. Since the task that we are firing is called &#039;save&#039;, we must call our method &#039;save&#039;. This is simple:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * save a record (and redirect to main page)&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function save()&lt;br /&gt;
{&lt;br /&gt;
    $model = $this-&amp;gt;getModel(&#039;hello&#039;);&lt;br /&gt;
&lt;br /&gt;
    if ($model-&amp;gt;store()) {&lt;br /&gt;
        $msg = JText::_( &#039;Greeting Saved!&#039; );&lt;br /&gt;
    } else {&lt;br /&gt;
        $msg = JText::_( &#039;Error Saving Greeting&#039; );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // Check the table in so it can be edited.... we are done with it anyway&lt;br /&gt;
    $link = &#039;index.php?option=com_hello&#039;;&lt;br /&gt;
    $this-&amp;gt;setRedirect($link, $msg);&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All we do is get our model and invoke the store() method. Then we use the setRedirect() method to redirect back to our list of greetings. We also pass a message along, which will be displayed at the top of the page.&lt;br /&gt;
&lt;br /&gt;
=== Deleting a Record ===&lt;br /&gt;
&lt;br /&gt;
=== Implementing the Function in the Model ===&lt;br /&gt;
&lt;br /&gt;
In the model, we will retrieve the list of IDs to delete and call the JTable class to delete them. Here it is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Method to delete record(s)&lt;br /&gt;
 *&lt;br /&gt;
 * @access    public&lt;br /&gt;
 * @return    boolean    True on success&lt;br /&gt;
 */&lt;br /&gt;
function delete()&lt;br /&gt;
{&lt;br /&gt;
    $cids = JRequest::getVar( &#039;cid&#039;, array(0), &#039;post&#039;, &#039;array&#039; );&lt;br /&gt;
    $row =&amp;amp; $this-&amp;gt;getTable();&lt;br /&gt;
&lt;br /&gt;
    foreach($cids as $cid) {&lt;br /&gt;
        if (!$row-&amp;gt;delete( $cid )) {&lt;br /&gt;
            $this-&amp;gt;setError( $row-&amp;gt;getErrorMsg() );&lt;br /&gt;
            return false;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return true;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We invoke the JRequest::getVar() method to get the data from the request, then we invoke the $row-&amp;gt;delete() method to delete each row. By storing errors in the model we make it possible to retrieve them later if we so choose.&lt;br /&gt;
&lt;br /&gt;
=== Handling the Remove Task in the Controller ===&lt;br /&gt;
&lt;br /&gt;
This is similar to the save() method which handled the save task:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * remove record(s)&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function remove()&lt;br /&gt;
{&lt;br /&gt;
    $model = $this-&amp;gt;getModel(&#039;hello&#039;);&lt;br /&gt;
    if(!$model-&amp;gt;delete()) {&lt;br /&gt;
        $msg = JText::_( &#039;Error: One or More Greetings Could not be Deleted&#039; );&lt;br /&gt;
    } else {&lt;br /&gt;
        $msg = JText::_( &#039;Greeting(s) Deleted&#039; );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    $this-&amp;gt;setRedirect( &#039;index.php?option=com_hello&#039;, $msg );&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Cancelling the Edit Operation ====&lt;br /&gt;
&lt;br /&gt;
To cancel the edit operation, all we have to do is redirect back to the main view:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * cancel editing a record&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function cancel()&lt;br /&gt;
{&lt;br /&gt;
    $msg = JText::_( &#039;Operation Cancelled&#039; );&lt;br /&gt;
    $this-&amp;gt;setRedirect( &#039;index.php?option=com_hello&#039;, $msg );&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
We have now implemented a basic backend to our component. We are now able to edit the entries that are viewed in the frontend. We have demonstrated the interaction between models, views and controllers. We have shown how the JTable class can be extended to provide easy access to tables in the database. It can also be seen how the JToolBarHelper class can be used to create button bars in components to present a standardized look between components.&lt;br /&gt;
&lt;br /&gt;
== Contributors ==&lt;br /&gt;
* staalanden&lt;br /&gt;
&lt;br /&gt;
== Download ==&lt;br /&gt;
&lt;br /&gt;
The component can be downloaded at: [http://joomlacode.org/gf/download/frsrelease/8104/29402/com_hello4a_01.zip com_hello4a_01]&lt;/div&gt;</summary>
		<author><name>Tonie</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Creating_an_Administrator_Interface&amp;diff=9505</id>
		<title>J1.5:Developing a MVC Component/Creating an Administrator Interface</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Creating_an_Administrator_Interface&amp;diff=9505"/>
		<updated>2008-07-27T18:47:43Z</updated>

		<summary type="html">&lt;p&gt;Tonie: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
In the first three tutorials, we have developed a MVC component that retrieves its data from a table in the database. Currently, there is no way to add data to the database except to do it manually using another tool. In this tutorial, we will develop an administrator section for our component which will make it possible to manage the entries in the database.&lt;br /&gt;
&lt;br /&gt;
== Creating the Basic Framework ==&lt;br /&gt;
&lt;br /&gt;
The basic framework of the administrator panel is very similar to the site portion. The main entry point for the administrator section of the component is hello.php. This file is identical to the hello.php file that was used in the site portion except the name of the controller it loads will be changed to HellosController. The default controller is also called controller.php and this file is identical to the default controller in the site portion, with the exception that the controller is named HellosController instead of HelloController. This difference is so that JController will by default load the hellos view, which will display a list of our greetings.&lt;br /&gt;
&lt;br /&gt;
Here is the listing for hello.php:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://dev.joomla.org/component/option,com_jd-wiki/Itemid,31/id,tutorials:components/&lt;br /&gt;
 * @license    GNU/GPL&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
// no direct access&lt;br /&gt;
&lt;br /&gt;
defined( &#039;_JEXEC&#039; ) or die( &#039;Restricted access&#039; );&lt;br /&gt;
&lt;br /&gt;
// Require the base controller&lt;br /&gt;
&lt;br /&gt;
require_once( JPATH_COMPONENT.DS.&#039;controller.php&#039; );&lt;br /&gt;
&lt;br /&gt;
// Require specific controller if requested&lt;br /&gt;
if($controller = JRequest::getWord(&#039;controller&#039;)) {&lt;br /&gt;
    $path = JPATH_COMPONENT.DS.&#039;controllers&#039;.DS.$controller.&#039;.php&#039;;&lt;br /&gt;
    if (file_exists($path)) {&lt;br /&gt;
        require_once $path;&lt;br /&gt;
    } else {&lt;br /&gt;
        $controller = &#039;&#039;;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Create the controller&lt;br /&gt;
$classname    = &#039;HellosController&#039;.$controller;&lt;br /&gt;
$controller   = new $classname( );&lt;br /&gt;
&lt;br /&gt;
// Perform the Request task&lt;br /&gt;
$controller-&amp;gt;execute( JRequest::getVar( &#039;task&#039; ) );&lt;br /&gt;
&lt;br /&gt;
// Redirect if set by the controller&lt;br /&gt;
$controller-&amp;gt;redirect();&lt;br /&gt;
&lt;br /&gt;
?&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The view and model that we will start with is the hellos view and the hellos model. We will start with the model.&lt;br /&gt;
&lt;br /&gt;
==== The Hellos Model ====&lt;br /&gt;
&lt;br /&gt;
The Hellos Model will be very simple. The only operation that we currently need is the ability to retrieve the list of hellos from the database. This operation will be implemented in a method called getData().&lt;br /&gt;
&lt;br /&gt;
The JModel class has a built in protected method called _getList(). This method can be used to simplify the task of retrieving a list of records from the database. We simply need to pass it the query and it will return the list of records.&lt;br /&gt;
&lt;br /&gt;
At a later point in time, we might want to use our query from within another method. Therefore, we will create a private method called _buildQuery() which will return the query that will be passed to _getList(). This makes it easier to change the query as well since it is localized in one place.&lt;br /&gt;
&lt;br /&gt;
Therefore we need two methods in our class: getData() and _buildQuery().&lt;br /&gt;
&lt;br /&gt;
_buildQuery() simply returns the query. It looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source  lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Returns the query&lt;br /&gt;
 * @return string The query to be used to retrieve the rows from the database&lt;br /&gt;
 */&lt;br /&gt;
function _buildQuery()&lt;br /&gt;
{&lt;br /&gt;
    $query = &#039; SELECT * &#039;&lt;br /&gt;
           . &#039; FROM #__hello &#039;&lt;br /&gt;
    ;&lt;br /&gt;
&lt;br /&gt;
    return $query;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
getData() will obtain the query and retrieve the records from the database. Now it might happen that we need to retrieve this list of data twice in one page load. It would be a waste to have to query the database twice. Therefore, we will have this method store the data in a protected property so that on subsequent requests it can simply return the data it has already retrieved. This property will be called _data.&lt;br /&gt;
&lt;br /&gt;
Here is the getData() method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source  lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Retrieves the hello data&lt;br /&gt;
 * @return array Array of objects containing the data from the database&lt;br /&gt;
 */&lt;br /&gt;
function getData()&lt;br /&gt;
{&lt;br /&gt;
    // Lets load the data if it doesn&#039;t already exist&lt;br /&gt;
    if (empty( $this-&amp;gt;_data ))&lt;br /&gt;
    {&lt;br /&gt;
        $query = $this-&amp;gt;_buildQuery();&lt;br /&gt;
        $this-&amp;gt;_data = $this-&amp;gt;_getList( $query );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return $this-&amp;gt;_data;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The completed model looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * Hellos Model for Hello World Component&lt;br /&gt;
 * &lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://dev.joomla.org/component/option,com_jd-wiki/Itemid,31/id,tutorials:components/&lt;br /&gt;
 * @license        GNU/GPL&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// Check to ensure this file is included in Joomla!&lt;br /&gt;
defined(&#039;_JEXEC&#039;) or die();&lt;br /&gt;
&lt;br /&gt;
jimport( &#039;joomla.application.component.model&#039; );&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Hello Model&lt;br /&gt;
 *&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 */&lt;br /&gt;
class HellosModelHellos extends JModel&lt;br /&gt;
{&lt;br /&gt;
    /**&lt;br /&gt;
     * Hellos data array&lt;br /&gt;
     *&lt;br /&gt;
     * @var array&lt;br /&gt;
     */&lt;br /&gt;
    var $_data;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Returns the query&lt;br /&gt;
     * @return string The query to be used to retrieve the rows from the database&lt;br /&gt;
     */&lt;br /&gt;
    function _buildQuery()&lt;br /&gt;
    {&lt;br /&gt;
        $query = &#039; SELECT * &#039;&lt;br /&gt;
            . &#039; FROM #__hello &#039;&lt;br /&gt;
        ;&lt;br /&gt;
        return $query;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Retrieves the hello data&lt;br /&gt;
     * @return array Array of objects containing the data from the database&lt;br /&gt;
     */&lt;br /&gt;
    function getData()&lt;br /&gt;
    {&lt;br /&gt;
        // Lets load the data if it doesn&#039;t already exist&lt;br /&gt;
        if (empty( $this-&amp;gt;_data ))&lt;br /&gt;
        {&lt;br /&gt;
            $query = $this-&amp;gt;_buildQuery();&lt;br /&gt;
            $this-&amp;gt;_data = $this-&amp;gt;_getList( $query );&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        return $this-&amp;gt;_data;&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This file is saved as models/hellos.php.&lt;br /&gt;
&lt;br /&gt;
==== The Hellos View ====&lt;br /&gt;
&lt;br /&gt;
Now that we have a model to retrieve our data, we need to display it. This view will be fairly similar to the view from the site section as well.&lt;br /&gt;
&lt;br /&gt;
Just as our model was automatically instantiated in the site, so it is in the administrator. Methods that start with get in the model can be accessed using the get() method of the JView class. So our view has three lines: one to retrieve the data from the model, one to push the data into the template, and one to invoke the display method to display the output. Thus we have:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * Hellos View for Hello World Component&lt;br /&gt;
 * &lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://dev.joomla.org/component/option,com_jd-wiki/Itemid,31/id,tutorials:components/&lt;br /&gt;
 * @license        GNU/GPL&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// Check to ensure this file is included in Joomla!&lt;br /&gt;
defined(&#039;_JEXEC&#039;) or die();&lt;br /&gt;
&lt;br /&gt;
jimport( &#039;joomla.application.component.view&#039; );&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Hellos View&lt;br /&gt;
 *&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 */&lt;br /&gt;
class HellosViewHellos extends JView&lt;br /&gt;
{&lt;br /&gt;
    /**&lt;br /&gt;
     * Hellos view display method&lt;br /&gt;
     * @return void&lt;br /&gt;
     **/&lt;br /&gt;
    function display($tpl = null)&lt;br /&gt;
    {&lt;br /&gt;
        JToolBarHelper::title( JText::_( &#039;Hello Manager&#039; ), &#039;generic.png&#039; );&lt;br /&gt;
        JToolBarHelper::deleteList();&lt;br /&gt;
        JToolBarHelper::editListX();&lt;br /&gt;
        JToolBarHelper::addNewX();&lt;br /&gt;
&lt;br /&gt;
        // Get data from the model&lt;br /&gt;
        $items =&amp;amp; $this-&amp;gt;get( &#039;Data&#039;);&lt;br /&gt;
&lt;br /&gt;
        $this-&amp;gt;assignRef( &#039;items&#039;, $items );&lt;br /&gt;
&lt;br /&gt;
        parent::display($tpl);&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This file is saved as views/hellos/view.html.php.&lt;br /&gt;
&lt;br /&gt;
==== The Hellos Template ====&lt;br /&gt;
&lt;br /&gt;
The template will take the data pushed into it from the view and produce the output. We will display our output in a simple table. While the frontend template was very simple, in the administrator we will need a minimal amount of extra logic to handle looping through the data.&lt;br /&gt;
&lt;br /&gt;
Here is our template:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;); ?&amp;gt;&lt;br /&gt;
&amp;lt;form action=&amp;quot;index.php&amp;quot; method=&amp;quot;post&amp;quot; name=&amp;quot;adminForm&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;div id=&amp;quot;editcell&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;table class=&amp;quot;adminlist&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;thead&amp;gt;&lt;br /&gt;
        &amp;lt;tr&amp;gt;&lt;br /&gt;
            &amp;lt;th width=&amp;quot;5&amp;quot;&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo JText::_( &#039;ID&#039; ); ?&amp;gt;&lt;br /&gt;
            &amp;lt;/th&amp;gt;&lt;br /&gt;
            &amp;lt;th&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo JText::_( &#039;Greeting&#039; ); ?&amp;gt;&lt;br /&gt;
            &amp;lt;/th&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;            &lt;br /&gt;
    &amp;lt;/thead&amp;gt;&lt;br /&gt;
    &amp;lt;?php&lt;br /&gt;
    $k = 0;&lt;br /&gt;
    for ($i=0, $n=count( $this-&amp;gt;items ); $i &amp;lt; $n; $i++)&lt;br /&gt;
    {&lt;br /&gt;
        $row =&amp;amp; $this-&amp;gt;items[$i];&lt;br /&gt;
        ?&amp;gt;&lt;br /&gt;
        &amp;lt;tr class=&amp;quot;&amp;lt;?php echo &amp;quot;row$k&amp;quot;; ?&amp;gt;&amp;quot;&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo $row-&amp;gt;id; ?&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo $row-&amp;gt;greeting; ?&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;&lt;br /&gt;
        &amp;lt;?php&lt;br /&gt;
        $k = 1 - $k;&lt;br /&gt;
    }&lt;br /&gt;
    ?&amp;gt;&lt;br /&gt;
    &amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;option&amp;quot; value=&amp;quot;com_hello&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;task&amp;quot; value=&amp;quot;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;boxchecked&amp;quot; value=&amp;quot;0&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;controller&amp;quot; value=&amp;quot;hello&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/form&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This template is saved as views/hellos/tmpl/default.php.&lt;br /&gt;
&lt;br /&gt;
You will notice that our output is enclosed in a form. Though this is not necessary now, it will be soon.&lt;br /&gt;
&lt;br /&gt;
We have now completed the basic part of the first view. We have added five files to the admin section of our component:&lt;br /&gt;
&lt;br /&gt;
* hello.php&lt;br /&gt;
* controller.php&lt;br /&gt;
* models/hellos.php&lt;br /&gt;
* views/hellos/view.html.php&lt;br /&gt;
* views/hellos/tmpl/default.php&lt;br /&gt;
&lt;br /&gt;
You can now add these files to the XML install file and give it a try!&lt;br /&gt;
&lt;br /&gt;
== Adding Functionality ==&lt;br /&gt;
&lt;br /&gt;
So far our administrator section is pretty useless. It doesn&#039;t really do anything - all it does is display the entries that we have in our database.&lt;br /&gt;
&lt;br /&gt;
In order to make it useful, we need to add some buttons and links.&lt;br /&gt;
&lt;br /&gt;
==== The Toolbar ====&lt;br /&gt;
&lt;br /&gt;
You may have noticed the toolbar that appears at the top of other Joomla! component administrator panels. Our component needs one as well. Joomla! makes this very easy to do. We will add buttons Delete records, Edit records, and create New records. We will also add a title that will be displayed on our toolbar.&lt;br /&gt;
&lt;br /&gt;
This is done by adding code to the view. To add the buttons, we use static methods from the Joomla! JToolBarHelper class. The code looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;JToolBarHelper::title(   JText::_( &#039;Hello Manager&#039; ), &#039;generic.png&#039; );&lt;br /&gt;
JToolBarHelper::deleteList();&lt;br /&gt;
JToolBarHelper::editListX();&lt;br /&gt;
JToolBarHelper::addNewX();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
These three methods will create the appropriate buttons. The deleteList() method can optionally take up to three parameters - the first parameter is a string to display to the user to confirm that they want to delete the records. The second is the task that should be sent with the query (the default is &#039;remove&#039;), and the third is the text that should be displayed below the button.&lt;br /&gt;
&lt;br /&gt;
The editListX() and addNewX() methods can each take two optional parameters. The first is the task (which are by default edit and add, respectively), and the second is the text that should be displayed below the button.&lt;br /&gt;
&lt;br /&gt;
 &#039;&#039;*You may have noticed the use of the JText::_ method in the template before and here as well. This is a handy function that makes component translation much easier. The JText::_ method will look up the string in your component language file and return the translated string. If not translation is found, it will return the string that you passed it. If you want to translate your component into another language, all you have to do is create a language file that will map the strings within the quotes to the translated version of the string.&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==== Checkboxes and Links ====&lt;br /&gt;
&lt;br /&gt;
We now have buttons. Two of those buttons operate on existing records. But how do we know which records to operate on? We have to let the user tell us. To do this, we need to add checkboxes to our table so that the user can select certain records. This is done in our template.&lt;br /&gt;
&lt;br /&gt;
In order to the add the checkboxes, we need to add an extra column into our table. We will add the column in between the two that we already have.&lt;br /&gt;
&lt;br /&gt;
In the header of the column, we will add a checkbox which can be used to toggle all the boxes below it on or off:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;th width=&amp;quot;20&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;input type=&amp;quot;checkbox&amp;quot; name=&amp;quot;toggle&amp;quot; value=&amp;quot;&amp;quot; onclick=&amp;quot;checkAll(&amp;lt;?php echo count( $this-&amp;gt;items ); ?&amp;gt;);&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/th&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Javascript checkAll function is a function that is built into the Joomla! base Javascript package that provides the functionality that we want here.&lt;br /&gt;
&lt;br /&gt;
Now we need to add the checkboxes into the individual rows. Joomla!&#039;s JHTML class has a method, JHTML::_(), which will generate our checkbox for us. We will add the following line to our loop:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$checked    = JHTML::_( &#039;grid.id&#039;, $i, $row-&amp;gt;id );&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
after the line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$row =&amp;amp; $this-&amp;gt;items[$i];&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then we will add a cell in between the two that we already have:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;td&amp;gt;&lt;br /&gt;
    &amp;lt;?php echo $checked; ?&amp;gt;&lt;br /&gt;
&amp;lt;/td&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It can be cumbersome to have to check the box that we want to edit and then move up and click the edit button. Therefore, we will add a link that it will go straight to the greeting&#039;s edit form. We will add the following line after the call to the JHTML::_() method to generate the link HTML:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$link = JRoute::_( &#039;index.php?option=com_hello&amp;gt;controller=hello&amp;gt;task=edit&amp;gt;cid[]=&#039;. $row-&amp;gt;id );&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And we include the link in the cell showing the greeting text:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;td&amp;gt;&lt;br /&gt;
    &amp;lt;a href=&amp;quot;&amp;lt;?php echo $link; ?&amp;gt;&amp;quot;&amp;gt;&amp;lt;?php echo $row-&amp;gt;greeting; ?&amp;gt;&amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;lt;/td&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You will notice that this link points to the hello controller. This controller will handle the data manipulation of our greetings.&lt;br /&gt;
&lt;br /&gt;
If you recall from above, we had four hidden input fields at the bottom of our form. The first input field was named &#039;option&#039;. This field is necessary so that we stay in our component. The second input field was task. This form property gets set when one of the buttons in the toolbar is clicked. A Javascript error will result and the buttons will not work if this input field is omitted. The third input field is the boxchecked field. This field keeps track of the number of boxes that are checked. The edit and delete buttons will check to ensure that this is greater than zero and will not allow the form to be submitted if it is not. The fourth input field is the controller field. This is used to specify that tasks fired from this form will be handled by the hello controller.&lt;br /&gt;
&lt;br /&gt;
Here is the code for the completed default.php file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;); ?&amp;gt;&lt;br /&gt;
&amp;lt;form action=&amp;quot;index.php&amp;quot; method=&amp;quot;post&amp;quot; name=&amp;quot;adminForm&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;div id=&amp;quot;editcell&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;table class=&amp;quot;adminlist&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;thead&amp;gt;&lt;br /&gt;
        &amp;lt;tr&amp;gt;&lt;br /&gt;
            &amp;lt;th width=&amp;quot;5&amp;quot;&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo JText::_( &#039;ID&#039; ); ?&amp;gt;&lt;br /&gt;
            &amp;lt;/th&amp;gt;&lt;br /&gt;
            &amp;lt;th&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo JText::_( &#039;Greeting&#039; ); ?&amp;gt;&lt;br /&gt;
            &amp;lt;/th&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;            &lt;br /&gt;
    &amp;lt;/thead&amp;gt;&lt;br /&gt;
    &amp;lt;?php&lt;br /&gt;
    $k = 0;&lt;br /&gt;
    for ($i=0, $n=count( $this-&amp;gt;items ); $i &amp;lt; $n; $i++)&lt;br /&gt;
    {&lt;br /&gt;
        $row =&amp;amp; $this-&amp;gt;items[$i];&lt;br /&gt;
        ?&amp;gt;&lt;br /&gt;
        &amp;lt;tr class=&amp;quot;&amp;lt;?php echo &amp;quot;row$k&amp;quot;; ?&amp;gt;&amp;quot;&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo $row-&amp;gt;id; ?&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo $row-&amp;gt;greeting; ?&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;&lt;br /&gt;
        &amp;lt;?php&lt;br /&gt;
        $k = 1 - $k;&lt;br /&gt;
    }&lt;br /&gt;
    ?&amp;gt;&lt;br /&gt;
    &amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;option&amp;quot; value=&amp;quot;com_hello&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;task&amp;quot; value=&amp;quot;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;boxchecked&amp;quot; value=&amp;quot;0&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;controller&amp;quot; value=&amp;quot;hello&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/form&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Our hellos view is now complete. You can try out the component now to see the results.&lt;br /&gt;
&lt;br /&gt;
The component can be downloaded at: [[http://dev.joomla.org/components/com_jd-wiki/data/media/components/com_hello4a.zip|com_hello4a.zip]]&lt;br /&gt;
&lt;br /&gt;
== Getting Down and Dirty: Doing the Real Work ==&lt;br /&gt;
&lt;br /&gt;
Now that the Hellos view is done, it is time to move to the Hello view and model. This is where the real work will get done.&lt;br /&gt;
&lt;br /&gt;
==== The Hello Controller ====&lt;br /&gt;
&lt;br /&gt;
Our default controller just isn&#039;t going to cut it when it comes to doing work - all it is capable of doing is displaying views.&lt;br /&gt;
&lt;br /&gt;
We need to be able to handle the tasks that we are launching from the Hellos view: add, edit and remove.&lt;br /&gt;
&lt;br /&gt;
Add and edit are essentially the same task: they both display a form to the user that allows a greeting to be edited. The only difference is that new displays a blank form, and edit displays a form with data already in it. Since they are similar, we will map the add task onto the edit task handler. This is specified in our constructor:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * constructor (registers additional tasks to methods)&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function __construct()&lt;br /&gt;
{&lt;br /&gt;
    parent::__construct();&lt;br /&gt;
&lt;br /&gt;
    // Register Extra tasks&lt;br /&gt;
    $this-&amp;gt;registerTask( &#039;add&#039;  ,     &#039;edit&#039; );&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first parameter of JController::registerTask is the task to map, and the second is the method to map it to.&lt;br /&gt;
&lt;br /&gt;
We will start with handling the edit task. The controller&#039;s job is fairly simple for the edit task. All it has to do is specify the view and layout to load (the hello view and the form layout). We will also tell Joomla! to disable the mainmenu while we are editing our greeting. This prevents users from leaving unsaved records open.&lt;br /&gt;
&lt;br /&gt;
Our edit task handler looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * display the edit form&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function edit()&lt;br /&gt;
{&lt;br /&gt;
    JRequest::setVar( &#039;view&#039;, &#039;hello&#039; );&lt;br /&gt;
    JRequest::setVar( &#039;layout&#039;, &#039;form&#039;  );&lt;br /&gt;
    JRequest::setVar(&#039;hidemainmenu&#039;, 1);&lt;br /&gt;
&lt;br /&gt;
    parent::display();&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== The Hello View ====&lt;br /&gt;
&lt;br /&gt;
The Hello view will display a form which will allow the user to edit a greeting. The display method if the hello view has to do a few simple tasks:&lt;br /&gt;
&lt;br /&gt;
  * retrieve the data from the model&lt;br /&gt;
&lt;br /&gt;
  * create the toolbar&lt;br /&gt;
&lt;br /&gt;
  * pass the data into the template&lt;br /&gt;
&lt;br /&gt;
  * invoke the display() method to render the template&lt;br /&gt;
&lt;br /&gt;
This becomes a bit more complicated because the one view handles both the edit and add tasks. In our toolbar we want the user to know what whether they are adding or editing, so we have to determine which task was fired.&lt;br /&gt;
&lt;br /&gt;
Since we are already retrieving the record that we want to display from the model, we can use this data to determine what task was fired. If the task was edit, then the id field of our record will have been set. If the task was new, then it will not have been set. This can be used to determine if we have a new record or an existing record.&lt;br /&gt;
&lt;br /&gt;
We will add two buttons to the toolbar: save and cancel. Though the functionality will be the same, we want to display different buttons depending on whether it is a new or existing record. If it is a new record, we will display cancel. If it already exists, we will display close.&lt;br /&gt;
&lt;br /&gt;
Thus our display method looks like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * display method of Hello view&lt;br /&gt;
 * @return void&lt;br /&gt;
 **/&lt;br /&gt;
function display($tpl = null)&lt;br /&gt;
{&lt;br /&gt;
    //get the hello&lt;br /&gt;
    $hello        =&amp;amp; $this-&amp;gt;get(&#039;Data&#039;);&lt;br /&gt;
    $isNew        = ($hello-&amp;gt;id &amp;lt; 1);&lt;br /&gt;
&lt;br /&gt;
    $text = $isNew ? JText::_( &#039;New&#039; ) : JText::_( &#039;Edit&#039; );&lt;br /&gt;
    JToolBarHelper::title(   JText::_( &#039;Hello&#039; ).&#039;: &amp;lt;small&amp;gt;&amp;lt;small&amp;gt;[ &#039; . $text.&#039; ]&amp;lt;/small&amp;gt;&amp;lt;/small&amp;gt;&#039; );&lt;br /&gt;
    JToolBarHelper::save();&lt;br /&gt;
    if ($isNew)  {&lt;br /&gt;
        JToolBarHelper::cancel();&lt;br /&gt;
    } else {&lt;br /&gt;
        // for existing items the button is renamed `close`&lt;br /&gt;
        JToolBarHelper::cancel( &#039;cancel&#039;, &#039;Close&#039; );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    $this-&amp;gt;assignRef(&#039;hello&#039;, $hello);&lt;br /&gt;
    parent::display($tpl);&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== The Hello Model ====&lt;br /&gt;
&lt;br /&gt;
Our view needs data. Therefore, we need to create a model to model a hello.&lt;br /&gt;
&lt;br /&gt;
Our model will have two properties: _id and _data. _id will hold the id of the greeting and data will hold the data.&lt;br /&gt;
&lt;br /&gt;
We will start with a constructor, which will attempt to retrieve the id from the query:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Constructor that retrieves the ID from the request&lt;br /&gt;
 *&lt;br /&gt;
 * @access    public&lt;br /&gt;
 * @return    void&lt;br /&gt;
 */&lt;br /&gt;
function __construct()&lt;br /&gt;
{&lt;br /&gt;
    parent::__construct();&lt;br /&gt;
&lt;br /&gt;
    $array = JRequest::getVar(&#039;cid&#039;,  0, &#039;&#039;, &#039;array&#039;);&lt;br /&gt;
    $this-&amp;gt;setId((int)$array[0]);&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The JRequest::getVar() method is used to retrieve data from the request. The first parameter is the name of the form variable. The second parameter is the default value to assign if there is no value found. The third parameter is the name of the hash to retrieve the value from (get, post, etc), and the last value is the data type that should be forced on the value.&lt;br /&gt;
&lt;br /&gt;
Our constructor will take the first value from the cid array and assign it to the id.&lt;br /&gt;
&lt;br /&gt;
Our setId() method can be used to set our id. Changing the id that our model points to will mean the id points to the wrong data. Therefore, when we set the id, we will clear the data property:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Method to set the hello identifier&lt;br /&gt;
 *&lt;br /&gt;
 * @access    public&lt;br /&gt;
 * @param    int Hello identifier&lt;br /&gt;
 * @return    void&lt;br /&gt;
 */&lt;br /&gt;
function setId($id)&lt;br /&gt;
{&lt;br /&gt;
    // Set id and wipe data&lt;br /&gt;
    $this-&amp;gt;_id        = $id;&lt;br /&gt;
    $this-&amp;gt;_data    = null;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finally, we need a method to retrieve our data: getData()&lt;br /&gt;
&lt;br /&gt;
getData will check if the _data property has already been set. If it has, it will simply return it. Otherwise, it will load the data from the database.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Method to get a hello&lt;br /&gt;
 * @return object with data&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
function &amp;amp;getData()&lt;br /&gt;
{&lt;br /&gt;
    // Load the data&lt;br /&gt;
    if (empty( $this-&amp;gt;_data )) {&lt;br /&gt;
        $query = &#039; SELECT * FROM #__hello &#039;.&lt;br /&gt;
                &#039;  WHERE id = &#039;.$this-&amp;gt;_id;&lt;br /&gt;
        $this-&amp;gt;_db-&amp;gt;setQuery( $query );&lt;br /&gt;
        $this-&amp;gt;_data = $this-&amp;gt;_db-&amp;gt;loadObject();&lt;br /&gt;
    }&lt;br /&gt;
    if (!$this-&amp;gt;_data) {&lt;br /&gt;
        $this-&amp;gt;_data = new stdClass();&lt;br /&gt;
        $this-&amp;gt;_data-&amp;gt;id = 0;&lt;br /&gt;
        $this-&amp;gt;_data-&amp;gt;greeting = null;&lt;br /&gt;
    }&lt;br /&gt;
    return $this-&amp;gt;_data;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== The Form ====&lt;br /&gt;
&lt;br /&gt;
Now all that is left is to create the form that the data will go into. Since we specified our layout as form, the form will go in a file in the tmpl directory of the hello view called form.php:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;); ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;form action=&amp;quot;index.php&amp;quot; method=&amp;quot;post&amp;quot; name=&amp;quot;adminForm&amp;quot; id=&amp;quot;adminForm&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;div class=&amp;quot;col100&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;fieldset class=&amp;quot;adminform&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;legend&amp;gt;&amp;lt;?php echo JText::_( &#039;Details&#039; ); ?&amp;gt;&amp;lt;/legend&amp;gt;&lt;br /&gt;
        &amp;lt;table class=&amp;quot;admintable&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;tr&amp;gt;&lt;br /&gt;
            &amp;lt;td width=&amp;quot;100&amp;quot; align=&amp;quot;right&amp;quot; class=&amp;quot;key&amp;quot;&amp;gt;&lt;br /&gt;
                &amp;lt;label for=&amp;quot;greeting&amp;quot;&amp;gt;&lt;br /&gt;
                    &amp;lt;?php echo JText::_( &#039;Greeting&#039; ); ?&amp;gt;:&lt;br /&gt;
                &amp;lt;/label&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;input class=&amp;quot;text_area&amp;quot; type=&amp;quot;text&amp;quot; name=&amp;quot;greeting&amp;quot; id=&amp;quot;greeting&amp;quot; size=&amp;quot;32&amp;quot; maxlength=&amp;quot;250&amp;quot; value=&amp;quot;&amp;lt;?php echo $this-&amp;gt;hello-&amp;gt;greeting;?&amp;gt;&amp;quot; /&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;&lt;br /&gt;
    &amp;lt;/table&amp;gt;&lt;br /&gt;
    &amp;lt;/fieldset&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;clr&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;option&amp;quot; value=&amp;quot;com_hello&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;id&amp;quot; value=&amp;quot;&amp;lt;?php echo $this-&amp;gt;hello-&amp;gt;id; ?&amp;gt;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;task&amp;quot; value=&amp;quot;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;controller&amp;quot; value=&amp;quot;hello&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/form&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in addition to the input field, there is a hidden field for the id. The user doesn&#039;t need to edit the id (and shouldn&#039;t), so we silently pass it along in the form.&lt;br /&gt;
&lt;br /&gt;
==== Implementing the Functionality ====&lt;br /&gt;
&lt;br /&gt;
So far, our controller only handles two tasks: edit and new. However, we also have buttons to save, delete and cancel records. We need to write code to handle and perform these tasks.&lt;br /&gt;
&lt;br /&gt;
=== Saving a Record ===&lt;br /&gt;
&lt;br /&gt;
The logical next step is to implement the functionality to save a record. Normally, this would require some switches and logic to handle various cases, such as the difference between creating a new record (an INSERT query), and updating an existing query (an UPDATE query). Also, there are complexities involved in getting the data from the form and putting it into the query.&lt;br /&gt;
&lt;br /&gt;
The Joomla! framework takes care of a lot of this for you. The JTable class makes it easy to manipulate records in the database without having to worry about writing the SQL code that lies behind these updates. It also makes it easy to transfer data from an HTML form into the database.&lt;br /&gt;
&lt;br /&gt;
== Creating the Table Class ==&lt;br /&gt;
&lt;br /&gt;
The JTable class is an abstract class from which you can derive child classes to work with specific tables. To use it, you simply create a class that extends the JTable class, add your database fields as properties, and override the constructor to specify the name of the table and the primary key.&lt;br /&gt;
&lt;br /&gt;
Here is our JTable class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * Hello World table class&lt;br /&gt;
 * &lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://dev.joomla.org/component/option,com_jd-wiki/Itemid,31/id,tutorials:components/&lt;br /&gt;
 * @license        GNU/GPL&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// no direct access&lt;br /&gt;
defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;);&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Hello Table class&lt;br /&gt;
 *&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 */&lt;br /&gt;
class TableHello extends JTable&lt;br /&gt;
{&lt;br /&gt;
    /**&lt;br /&gt;
     * Primary Key&lt;br /&gt;
     *&lt;br /&gt;
     * @var int&lt;br /&gt;
     */&lt;br /&gt;
    var $id = null;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * @var string&lt;br /&gt;
     */&lt;br /&gt;
    var $greeting = null;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Constructor&lt;br /&gt;
     *&lt;br /&gt;
     * @param object Database connector object&lt;br /&gt;
     */&lt;br /&gt;
    function TableHello( &amp;amp;$db ) {&lt;br /&gt;
        parent::__construct(&#039;#__hello&#039;, &#039;id&#039;, $db);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You will see here that we have defined our two fields: the id field and the greeting field. Then we have defined a constructor, which will call the constructor of the parent class and pass it the name of the table (hello), the name of the field which is the primary key (id), and the database connector object.&lt;br /&gt;
&lt;br /&gt;
This file should be called hello.php and it will go in a directory called tables in the administrator section of our component.&lt;br /&gt;
&lt;br /&gt;
== Implementing the Function in our Model ==&lt;br /&gt;
&lt;br /&gt;
We are now ready to add the method to the model which will save our record. We will call this method store. Our store() method will do three things: bind the data from the form to the TableHello object, check to ensure that the record is properly formed, and store the record in the database.&lt;br /&gt;
&lt;br /&gt;
Our method looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Method to store a record&lt;br /&gt;
 *&lt;br /&gt;
 * @access    public&lt;br /&gt;
 * @return    boolean    True on success&lt;br /&gt;
 */&lt;br /&gt;
function store()&lt;br /&gt;
{&lt;br /&gt;
    $row =&amp;amp; $this-&amp;gt;getTable();&lt;br /&gt;
&lt;br /&gt;
    $data = JRequest::get( &#039;post&#039; );&lt;br /&gt;
    // Bind the form fields to the hello table&lt;br /&gt;
    if (!$row-&amp;gt;bind($data)) {&lt;br /&gt;
        $this-&amp;gt;setError($this-&amp;gt;_db-&amp;gt;getErrorMsg());&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // Make sure the hello record is valid&lt;br /&gt;
    if (!$row-&amp;gt;check()) {&lt;br /&gt;
        $this-&amp;gt;setError($this-&amp;gt;_db-&amp;gt;getErrorMsg());&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // Store the web link table to the database&lt;br /&gt;
    if (!$row-&amp;gt;store()) {&lt;br /&gt;
        $this-&amp;gt;setError($this-&amp;gt;_db-&amp;gt;getErrorMsg());&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return true;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This method gets added to the hello model.&lt;br /&gt;
&lt;br /&gt;
The method takes one parameter, which is an associative array of data that we want to store in the database. This can easily be retrieved from the request as will be seen later.&lt;br /&gt;
&lt;br /&gt;
You will see that the first line retrieves a reference to our JTable object. If we name our table properly, we don&#039;t have to specify its name - the JModel class knows where to find it. You may recall that we called our table class TableHello and put it in a file called hello.php in the tables directory. If you follow this convention, the JModel class can create your object automatically.&lt;br /&gt;
&lt;br /&gt;
The second line will retrieve the data from the form. The JRequest class makes this very easy. In this case, we are retrieving all of the variables that were submitted using the &#039;POST&#039; method. These will be returned as an associative array.&lt;br /&gt;
&lt;br /&gt;
The rest is easy - we bind, check and store. The bind() method will copy values from the array into the corresponding property of the table object. In this case, it will take the values of id and greeting and copy them to our TableHello object.&lt;br /&gt;
&lt;br /&gt;
The check() method will perform data verification. In the JTable() class, this method simply returns true. While this doesn&#039;t provide any value for us currently, by calling this method we make it possible to do data checking using our TableHello class in the future. This method can be overridden in our TableHello class with a method that performs the appropriate checks.&lt;br /&gt;
&lt;br /&gt;
The store() method will take the data that is in the object and store it in the database. If the id is 0, it will create a new record (INSERT), otherwise, it will update the existing record (UPDATE).&lt;br /&gt;
&lt;br /&gt;
== Adding the Task to the Controller ==&lt;br /&gt;
&lt;br /&gt;
We are now ready to add our task to the controller. Since the task that we are firing is called &#039;save&#039;, we must call our method &#039;save&#039;. This is simple:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * save a record (and redirect to main page)&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function save()&lt;br /&gt;
{&lt;br /&gt;
    $model = $this-&amp;gt;getModel(&#039;hello&#039;);&lt;br /&gt;
&lt;br /&gt;
    if ($model-&amp;gt;store()) {&lt;br /&gt;
        $msg = JText::_( &#039;Greeting Saved!&#039; );&lt;br /&gt;
    } else {&lt;br /&gt;
        $msg = JText::_( &#039;Error Saving Greeting&#039; );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // Check the table in so it can be edited.... we are done with it anyway&lt;br /&gt;
    $link = &#039;index.php?option=com_hello&#039;;&lt;br /&gt;
    $this-&amp;gt;setRedirect($link, $msg);&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All we do is get our model and invoke the store() method. Then we use the setRedirect() method to redirect back to our list of greetings. We also pass a message along, which will be displayed at the top of the page.&lt;br /&gt;
&lt;br /&gt;
=== Deleting a Record ===&lt;br /&gt;
&lt;br /&gt;
=== Implementing the Function in the Model ===&lt;br /&gt;
&lt;br /&gt;
In the model, we will retrieve the list of IDs to delete and call the JTable class to delete them. Here it is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Method to delete record(s)&lt;br /&gt;
 *&lt;br /&gt;
 * @access    public&lt;br /&gt;
 * @return    boolean    True on success&lt;br /&gt;
 */&lt;br /&gt;
function delete()&lt;br /&gt;
{&lt;br /&gt;
    $cids = JRequest::getVar( &#039;cid&#039;, array(0), &#039;post&#039;, &#039;array&#039; );&lt;br /&gt;
    $row =&amp;amp; $this-&amp;gt;getTable();&lt;br /&gt;
&lt;br /&gt;
    foreach($cids as $cid) {&lt;br /&gt;
        if (!$row-&amp;gt;delete( $cid )) {&lt;br /&gt;
            $this-&amp;gt;setError( $row-&amp;gt;getErrorMsg() );&lt;br /&gt;
            return false;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return true;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We invoke the JRequest::getVar() method to get the data from the request, then we invoke the $row-&amp;gt;delete() method to delete each row. By storing errors in the model we make it possible to retrieve them later if we so choose.&lt;br /&gt;
&lt;br /&gt;
=== Handling the Remove Task in the Controller ===&lt;br /&gt;
&lt;br /&gt;
This is similar to the save() method which handled the save task:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * remove record(s)&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function remove()&lt;br /&gt;
{&lt;br /&gt;
    $model = $this-&amp;gt;getModel(&#039;hello&#039;);&lt;br /&gt;
    if(!$model-&amp;gt;delete()) {&lt;br /&gt;
        $msg = JText::_( &#039;Error: One or More Greetings Could not be Deleted&#039; );&lt;br /&gt;
    } else {&lt;br /&gt;
        $msg = JText::_( &#039;Greeting(s) Deleted&#039; );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    $this-&amp;gt;setRedirect( &#039;index.php?option=com_hello&#039;, $msg );&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Cancelling the Edit Operation ====&lt;br /&gt;
&lt;br /&gt;
To cancel the edit operation, all we have to do is redirect back to the main view:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * cancel editing a record&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function cancel()&lt;br /&gt;
{&lt;br /&gt;
    $msg = JText::_( &#039;Operation Cancelled&#039; );&lt;br /&gt;
    $this-&amp;gt;setRedirect( &#039;index.php?option=com_hello&#039;, $msg );&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
We have now implemented a basic backend to our component. We are now able to edit the entries that are viewed in the frontend. We have demonstrated the interaction between models, views and controllers. We have shown how the JTable class can be extended to provide easy access to tables in the database. It can also be seen how the JToolBarHelper class can be used to create button bars in components to present a standardized look between components.&lt;br /&gt;
&lt;br /&gt;
== Contributors ==&lt;br /&gt;
* staalanden&lt;br /&gt;
&lt;br /&gt;
== Download ==&lt;br /&gt;
&lt;br /&gt;
The component can be downloaded at: [http://joomlacode.org/gf/download/frsrelease/8104/29402/com_hello4a_01.zip com_hello4a_01]&lt;/div&gt;</summary>
		<author><name>Tonie</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Introduction&amp;diff=9504</id>
		<title>J1.5:Developing a MVC Component/Introduction</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Introduction&amp;diff=9504"/>
		<updated>2008-07-27T18:41:52Z</updated>

		<summary type="html">&lt;p&gt;Tonie: /* Wrapping It All Up - Creating the hello.xml File */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
A software framework is the base of an application that can be used by a developer. The framework in Joomla! 1.5 unleashes a great deal of power for them. The Joomla! code has been completely overhauled and cleaned up. This tutorial will guide you through the process of developing a component using the framework.&lt;br /&gt;
&lt;br /&gt;
The scope of this project will be to develop a simple Hello World! component. In future tutorials, this simple framework will be built upon to show the full power and versatility of the MVC design pattern in Joomla!&lt;br /&gt;
&lt;br /&gt;
== Requirements ==&lt;br /&gt;
You need Joomla! 1.5 or greater for this tutorial.&lt;br /&gt;
&lt;br /&gt;
== Introduction to Model-View-Controller ==&lt;br /&gt;
While the idea behind a component may seem extremely simple, code can quickly become very complex as additional features are added or the interface is customized.&lt;br /&gt;
&lt;br /&gt;
Model-View-Controller (herein referred to as MVC) is a software design pattern that can be used to organize code in such a way that the business logic and data presentation are separate. The premise behind this approach is that if the business logic is grouped into one section, then the interface and user interaction that surrounds the data can revised and customized without having to reprogram the business logic.&lt;br /&gt;
&lt;br /&gt;
There are three main parts of an MVC component. They are described here in brief, but for a more thorough explanation, please refer to the links provided at the end of this tutorial.&lt;br /&gt;
&lt;br /&gt;
=== Model ===&lt;br /&gt;
The model is the part of the component that encapsulates the application&#039;s data. It will often provide routines to manage and manipulate this data in a meaningful way in addition to routines that retrieve the data from the model. In our case, the model will contain methods to add, remove and update information about the greetings in the database. It will also contain methods to retrieve the list of greetings from the database. In general, the underlying data access technique should be encapsulated in the model. In this way, if an application is to be moved from a system that utilizes a flat file to store its information to a system that uses a database, the model is the only element that needs to be changed, not the view or the controller.&lt;br /&gt;
&lt;br /&gt;
=== View ===&lt;br /&gt;
The view is the part of the component that is used to render the data from the model in a manner that is suitable for interaction. For a web-based application, the view would generally be an HTML page that is returned to the data. The view pulls data from the model (which is passed to it from the controller) and feeds the data into a template which is populated and presented to the user. The view does not cause the data to be modified in any way, it only displays data retrieved from the model.&lt;br /&gt;
&lt;br /&gt;
=== Controller ===&lt;br /&gt;
The controller is responsible for responding to user actions. In the case of a web application, a user action is (generally) a page request. The controller will determine what request is being made by the user and respond appropriately by triggering the model to manipulate the data appropriately and passing the model into the view. The controller does not display the data in the model, it only triggers methods in the model which modify the data, and then pass the model into the view which displays the data.&lt;br /&gt;
&lt;br /&gt;
== Joomla! MVC Implementation ==&lt;br /&gt;
In Joomla!, the MVC pattern is implemented using three classes: [http://api.joomla.org/Joomla-Framework/Application/JModel.html JModel], [http://api.joomla.org/Joomla-Framework/Application/JView.html JView] and [http://api.joomla.org/Joomla-Framework/Application/JController.html JController]. For more detailed information about these classes, please refer to the API reference documentation (WIP).&lt;br /&gt;
&lt;br /&gt;
== Creating a Component ==&lt;br /&gt;
For our basic component, we only require five files:&lt;br /&gt;
&lt;br /&gt;
* hello.php - this is the entry point to our component&lt;br /&gt;
* controller.php - this file contains our base controller&lt;br /&gt;
* views/hello/view.html.php - this file retrieves the necessary data and pushes it into the template&lt;br /&gt;
* views/hello/tmpl/default.php - this is the template for our output&lt;br /&gt;
* hello.xml - this is an XML file that tells Joomla! how to install our component.&lt;br /&gt;
&lt;br /&gt;
=== Creating the Entry Point ===&lt;br /&gt;
Joomla! is always accessed through a single point of entry: index.php for the Site Application or administrator/index.php for the Administrator Application. The application will then load the required component, based on the value of &#039;option&#039; in the URL or in the POST data. For our component, the URL would be:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;index.php?option=com_hello&amp;amp;view=hello&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will load our main file, which can be seen as the single point of entry for our component: components/com_hello/hello.php.&lt;br /&gt;
&lt;br /&gt;
The code for this file is fairly typical across components.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * components/com_hello/hello.php&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_1&lt;br /&gt;
 * @license    GNU/GPL&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
// No direct access&lt;br /&gt;
&lt;br /&gt;
defined( &#039;_JEXEC&#039; ) or die( &#039;Restricted access&#039; );&lt;br /&gt;
&lt;br /&gt;
// Require the base controller&lt;br /&gt;
&lt;br /&gt;
require_once( JPATH_COMPONENT.DS.&#039;controller.php&#039; );&lt;br /&gt;
&lt;br /&gt;
// Require specific controller if requested&lt;br /&gt;
if($controller = JRequest::getWord(&#039;controller&#039;)) {&lt;br /&gt;
    $path = JPATH_COMPONENT.DS.&#039;controllers&#039;.DS.$controller.&#039;.php&#039;;&lt;br /&gt;
    if (file_exists($path)) {&lt;br /&gt;
        require_once $path;&lt;br /&gt;
    } else {&lt;br /&gt;
        $controller = &#039;&#039;;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Create the controller&lt;br /&gt;
$classname    = &#039;HelloController&#039;.$controller;&lt;br /&gt;
$controller   = new $classname( );&lt;br /&gt;
&lt;br /&gt;
// Perform the Request task&lt;br /&gt;
$controller-&amp;gt;execute( JRequest::getVar( &#039;task&#039; ) );&lt;br /&gt;
&lt;br /&gt;
// Redirect if set by the controller&lt;br /&gt;
$controller-&amp;gt;redirect();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first statement is a security check.&lt;br /&gt;
&lt;br /&gt;
JPATH_COMPONENT is the absolute path to the current component, in our case components/com_hello. If you specifically need either the Site component or the Administrator component, you can use JPATH_COMPONENT_SITE or JPATH_COMPONENT_ADMINISTRATOR.&lt;br /&gt;
&lt;br /&gt;
DS is the directory separator of your system: either &#039;/&#039; or &#039;\&#039;. This is automatically set by the framework so the developer doesn&#039;t have to worry about developing different versions for different server OSs. DS should always be used when referring to files on the local server.&lt;br /&gt;
&lt;br /&gt;
After loading the base controller, we check if a specific controller is needed. In this component, the base controller is the only controller, but we will leave this here for future use.&lt;br /&gt;
&lt;br /&gt;
JRequest:getVar() finds a variable in the URL or the POST data. So if our URL is index.php?option=com_hello&amp;gt;controller=controller_name, then we can retrieve our controller name in our component using: echo JRequest::getVar(&#039;controller&#039;);&lt;br /&gt;
&lt;br /&gt;
Now we have our base controller &#039;HelloController&#039; in com_hello/controller.php, and, if needed, additional controllers like &#039;HelloControllerController1&#039; in com_hello/controllers/controller1.php. Using this standard naming scheme will make things easy later on: &#039;{Componentname}{Controller}{Controllername}&#039;&lt;br /&gt;
&lt;br /&gt;
After the controller is created, we instruct the controller to execute the task, as defined in the URL: index.php?option=com_hello&amp;amp;task=sometask. If no task is set, the default task &#039;display&#039; will be assumed. When display is used, the &#039;view&#039; variable will decide what will be displayed. Other common tasks are save, edit, new...&lt;br /&gt;
&lt;br /&gt;
The controller might decide to redirect the page, usually after a task like &#039;save&#039; has been completed. This last statement takes care of the actual redirection.&lt;br /&gt;
&lt;br /&gt;
The main entry point (hello.php) essentially passes control to the controller, which handles performing the task that was specified in the request.&lt;br /&gt;
&lt;br /&gt;
Note that we don&#039;t use a closing php tag in this file: ?&amp;gt;. The reason for this is that we will not have any unwanted whitespace in the output code. This is default practice since Joomla! 1.5, and will be used for all php-only files.&lt;br /&gt;
&lt;br /&gt;
=== Creating the Controller ===&lt;br /&gt;
Our component only has one task - greet the world. Therefore, the controller will be very simple. No data manipulation is required. All that needs to be done is the appropriate view loaded. We will have only one method in our controller: display(). Most of the required functionality is built into the JController class, so all that we need to do is invoke the JController::display() method.&lt;br /&gt;
&lt;br /&gt;
The code for the base controller is:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_1&lt;br /&gt;
 * @license    GNU/GPL&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// No direct access&lt;br /&gt;
&lt;br /&gt;
defined( &#039;_JEXEC&#039; ) or die( &#039;Restricted access&#039; );&lt;br /&gt;
&lt;br /&gt;
jimport(&#039;joomla.application.component.controller&#039;);&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Hello World Component Controller&lt;br /&gt;
 *&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 */&lt;br /&gt;
class HelloController extends JController&lt;br /&gt;
{&lt;br /&gt;
    /**&lt;br /&gt;
     * Method to display the view&lt;br /&gt;
     *&lt;br /&gt;
     * @access    public&lt;br /&gt;
     */&lt;br /&gt;
    function display()&lt;br /&gt;
    {&lt;br /&gt;
        parent::display();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The JController constructor will always register a display() task and unless otherwise specified (using the registerDefaultTask() method), it will set it as the default task.&lt;br /&gt;
&lt;br /&gt;
This barebones display() method isn&#039;t really even necessary since all it does is invoke the parent constructor. However, it is a good visual clue to indicate what is happening in the controller.&lt;br /&gt;
&lt;br /&gt;
The JController::display() method will determine the name of the view and layout from the request and load that view and set the layout. When you create a menu item for your component, the menu manager will allow the administrator to select the view that they would like the menu link to display and to specify the layout. A view usually refers to a view of a certain set of data (i.e. a list of cars, a list of events, a single car, a single event). A layout is a way that that view is organized.&lt;br /&gt;
&lt;br /&gt;
In our component, we will have a single view called hello, and a single layout (default).&lt;br /&gt;
&lt;br /&gt;
=== Creating the View ===&lt;br /&gt;
The task of the view is very simple: It retrieves the data to be displayed and pushes it into the template. Data is pushed into the template using the JView::assignRef method.&lt;br /&gt;
&lt;br /&gt;
The code for the view is:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_1&lt;br /&gt;
 * @license    GNU/GPL&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
// no direct access&lt;br /&gt;
&lt;br /&gt;
defined( &#039;_JEXEC&#039; ) or die( &#039;Restricted access&#039; );&lt;br /&gt;
&lt;br /&gt;
jimport( &#039;joomla.application.component.view&#039;);&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * HTML View class for the HelloWorld Component&lt;br /&gt;
 *&lt;br /&gt;
 * @package    HelloWorld&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
class HelloViewHello extends JView&lt;br /&gt;
{&lt;br /&gt;
    function display($tpl = null)&lt;br /&gt;
    {&lt;br /&gt;
        $greeting = &amp;quot;Hello World!&amp;quot;;&lt;br /&gt;
        $this-&amp;gt;assignRef( &#039;greeting&#039;, $greeting );&lt;br /&gt;
&lt;br /&gt;
        parent::display($tpl);&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Creating the Template ====&lt;br /&gt;
&lt;br /&gt;
Joomla! templates/layouts are regular PHP files that are used to layout the data from the view in a particular manner. The variables assigned by the JView::assignRef method can be accessed from the template using $this-&amp;gt;{propertyname} (see the template code below for an example).&lt;br /&gt;
&lt;br /&gt;
Our template is very simple: we only want to display the greeting that was passed in from the view:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
&lt;br /&gt;
// No direct access&lt;br /&gt;
&lt;br /&gt;
defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;); ?&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;&amp;lt;?php echo $this-&amp;gt;greeting; ?&amp;gt;&amp;lt;/h1&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Wrapping It All Up - Creating the hello.xml File ===&lt;br /&gt;
It is possible to install a component manually by copying the files using an FTP client and modifying the database tables. It is more efficient to create a package file that will allow the Joomla! Installer to do this for you. This package file contains a variety of information:&lt;br /&gt;
&lt;br /&gt;
* basic descriptive details about your component (i.e. name), and optionally, a description, copyright and license information.&lt;br /&gt;
* a list of files that need to be copied.&lt;br /&gt;
* optionally, a PHP file that performs additional install and uninstall operations.&lt;br /&gt;
* optionally, an SQL file which contains database queries that should be executed upon install/uninstall&lt;br /&gt;
&lt;br /&gt;
The format of the XML file is as follows:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;&lt;br /&gt;
&amp;lt;install type=&amp;quot;component&amp;quot; version=&amp;quot;1.5.0&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;name&amp;gt;Hello&amp;lt;/name&amp;gt;&lt;br /&gt;
 &amp;lt;!-- The following elements are optional and free of formatting conttraints --&amp;gt;&lt;br /&gt;
 &amp;lt;creationDate&amp;gt;2007-02-22&amp;lt;/creationDate&amp;gt;&lt;br /&gt;
 &amp;lt;author&amp;gt;John Doe&amp;lt;/author&amp;gt;&lt;br /&gt;
 &amp;lt;authorEmail&amp;gt;john.doe@example.org&amp;lt;/authorEmail&amp;gt;&lt;br /&gt;
 &amp;lt;authorUrl&amp;gt;http://www.example.org&amp;lt;/authorUrl&amp;gt;&lt;br /&gt;
 &amp;lt;copyright&amp;gt;Copyright Info&amp;lt;/copyright&amp;gt;&lt;br /&gt;
 &amp;lt;license&amp;gt;License Info&amp;lt;/license&amp;gt;&lt;br /&gt;
 &amp;lt;!--  The version string is recorded in the components table --&amp;gt;&lt;br /&gt;
 &amp;lt;version&amp;gt;1.01&amp;lt;/version&amp;gt;&lt;br /&gt;
 &amp;lt;!-- The description is optional and defaults to the name --&amp;gt;&lt;br /&gt;
 &amp;lt;description&amp;gt;Description of the component ...&amp;lt;/description&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;!-- Site Main File Copy Section --&amp;gt;&lt;br /&gt;
 &amp;lt;!-- Note the folder attribute: This attribute describes the folder&lt;br /&gt;
      to copy FROM in the package to install therefore files copied&lt;br /&gt;
      in this section are copied from /site/ in the package --&amp;gt;&lt;br /&gt;
 &amp;lt;files folder=&amp;quot;site&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;controller.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/view.html.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/tmpl/default.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/tmpl/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
 &amp;lt;/files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;administration&amp;gt;&lt;br /&gt;
  &amp;lt;!-- Administration Menu Section --&amp;gt;&lt;br /&gt;
  &amp;lt;menu&amp;gt;Hello World!&amp;lt;/menu&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;!-- Administration Main File Copy Section --&amp;gt;&lt;br /&gt;
  &amp;lt;files folder=&amp;quot;admin&amp;quot;&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;/files&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
 &amp;lt;/administration&amp;gt;&lt;br /&gt;
&amp;lt;/install&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you look closely you will notice that there are some files that will be copied that we have not discussed. These are the index.html files. An index.html file is placed in each directory to prevent prying users from getting a directory listing. If there is no index.html file, some web servers will list the directory contents. This is often undesirable. These files have the simple line:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;html&amp;gt;&amp;lt;body bgcolor=&amp;quot;#FFFFFF&amp;quot;&amp;gt;&amp;lt;/body&amp;gt;&amp;lt;/html&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It will simply display a blank page.&lt;br /&gt;
&lt;br /&gt;
The other file is the hello.php file. This is the entry point for the admin section of our component. Since we don&#039;t have an admin section of our component, it will have the same content as the index.html files at this point in time.&lt;br /&gt;
&lt;br /&gt;
== Contributors ==&lt;br /&gt;
* mjaz&lt;br /&gt;
* staalanden&lt;br /&gt;
&lt;br /&gt;
== Download ==&lt;br /&gt;
The component can be downloaded at: [http://joomlacode.org/gf/download/frsrelease/8104/29402/com_hello1_01.zip com_hello1_01]&lt;/div&gt;</summary>
		<author><name>Tonie</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Creating_an_Administrator_Interface&amp;diff=9503</id>
		<title>J1.5:Developing a MVC Component/Creating an Administrator Interface</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Creating_an_Administrator_Interface&amp;diff=9503"/>
		<updated>2008-07-27T18:40:31Z</updated>

		<summary type="html">&lt;p&gt;Tonie: /* The Hellos Template */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
In the first three tutorials, we have developed a MVC component that retrieves its data from a table in the database. Currently, there is no way to add data to the database except to do it manually using another tool. In this tutorial, we will develop an administrator section for our component which will make it possible to manage the entries in the database.&lt;br /&gt;
&lt;br /&gt;
== Creating the Basic Framework ==&lt;br /&gt;
&lt;br /&gt;
The basic framework of the administrator panel is very similar to the site portion. The main entry point for the administrator section of the component is admin.hello.php. This file is identical to the hello.php file that was used in the site portion except the name of the controller it loads will be changed to HellosController. The default controller is also called controller.php and this file is identical to the default controller in the site portion, with the exception that the controller is named HellosController instead of HelloController. This difference is so that JController will by default load the hellos view, which will display a list of our greetings.&lt;br /&gt;
&lt;br /&gt;
Here is the listing for admin.hello.php:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://dev.joomla.org/component/option,com_jd-wiki/Itemid,31/id,tutorials:components/&lt;br /&gt;
 * @license    GNU/GPL&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
// no direct access&lt;br /&gt;
&lt;br /&gt;
defined( &#039;_JEXEC&#039; ) or die( &#039;Restricted access&#039; );&lt;br /&gt;
&lt;br /&gt;
// Require the base controller&lt;br /&gt;
&lt;br /&gt;
require_once( JPATH_COMPONENT.DS.&#039;controller.php&#039; );&lt;br /&gt;
&lt;br /&gt;
// Require specific controller if requested&lt;br /&gt;
if($controller = JRequest::getWord(&#039;controller&#039;)) {&lt;br /&gt;
    $path = JPATH_COMPONENT.DS.&#039;controllers&#039;.DS.$controller.&#039;.php&#039;;&lt;br /&gt;
    if (file_exists($path)) {&lt;br /&gt;
        require_once $path;&lt;br /&gt;
    } else {&lt;br /&gt;
        $controller = &#039;&#039;;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Create the controller&lt;br /&gt;
$classname    = &#039;HellosController&#039;.$controller;&lt;br /&gt;
$controller   = new $classname( );&lt;br /&gt;
&lt;br /&gt;
// Perform the Request task&lt;br /&gt;
$controller-&amp;gt;execute( JRequest::getVar( &#039;task&#039; ) );&lt;br /&gt;
&lt;br /&gt;
// Redirect if set by the controller&lt;br /&gt;
$controller-&amp;gt;redirect();&lt;br /&gt;
&lt;br /&gt;
?&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The view and model that we will start with is the hellos view and the hellos model. We will start with the model.&lt;br /&gt;
&lt;br /&gt;
==== The Hellos Model ====&lt;br /&gt;
&lt;br /&gt;
The Hellos Model will be very simple. The only operation that we currently need is the ability to retrieve the list of hellos from the database. This operation will be implemented in a method called getData().&lt;br /&gt;
&lt;br /&gt;
The JModel class has a built in protected method called _getList(). This method can be used to simplify the task of retrieving a list of records from the database. We simply need to pass it the query and it will return the list of records.&lt;br /&gt;
&lt;br /&gt;
At a later point in time, we might want to use our query from within another method. Therefore, we will create a private method called _buildQuery() which will return the query that will be passed to _getList(). This makes it easier to change the query as well since it is localized in one place.&lt;br /&gt;
&lt;br /&gt;
Therefore we need two methods in our class: getData() and _buildQuery().&lt;br /&gt;
&lt;br /&gt;
_buildQuery() simply returns the query. It looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source  lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Returns the query&lt;br /&gt;
 * @return string The query to be used to retrieve the rows from the database&lt;br /&gt;
 */&lt;br /&gt;
function _buildQuery()&lt;br /&gt;
{&lt;br /&gt;
    $query = &#039; SELECT * &#039;&lt;br /&gt;
           . &#039; FROM #__hello &#039;&lt;br /&gt;
    ;&lt;br /&gt;
&lt;br /&gt;
    return $query;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
getData() will obtain the query and retrieve the records from the database. Now it might happen that we need to retrieve this list of data twice in one page load. It would be a waste to have to query the database twice. Therefore, we will have this method store the data in a protected property so that on subsequent requests it can simply return the data it has already retrieved. This property will be called _data.&lt;br /&gt;
&lt;br /&gt;
Here is the getData() method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source  lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Retrieves the hello data&lt;br /&gt;
 * @return array Array of objects containing the data from the database&lt;br /&gt;
 */&lt;br /&gt;
function getData()&lt;br /&gt;
{&lt;br /&gt;
    // Lets load the data if it doesn&#039;t already exist&lt;br /&gt;
    if (empty( $this-&amp;gt;_data ))&lt;br /&gt;
    {&lt;br /&gt;
        $query = $this-&amp;gt;_buildQuery();&lt;br /&gt;
        $this-&amp;gt;_data = $this-&amp;gt;_getList( $query );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return $this-&amp;gt;_data;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The completed model looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * Hellos Model for Hello World Component&lt;br /&gt;
 * &lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://dev.joomla.org/component/option,com_jd-wiki/Itemid,31/id,tutorials:components/&lt;br /&gt;
 * @license        GNU/GPL&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// Check to ensure this file is included in Joomla!&lt;br /&gt;
defined(&#039;_JEXEC&#039;) or die();&lt;br /&gt;
&lt;br /&gt;
jimport( &#039;joomla.application.component.model&#039; );&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Hello Model&lt;br /&gt;
 *&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 */&lt;br /&gt;
class HellosModelHellos extends JModel&lt;br /&gt;
{&lt;br /&gt;
    /**&lt;br /&gt;
     * Hellos data array&lt;br /&gt;
     *&lt;br /&gt;
     * @var array&lt;br /&gt;
     */&lt;br /&gt;
    var $_data;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Returns the query&lt;br /&gt;
     * @return string The query to be used to retrieve the rows from the database&lt;br /&gt;
     */&lt;br /&gt;
    function _buildQuery()&lt;br /&gt;
    {&lt;br /&gt;
        $query = &#039; SELECT * &#039;&lt;br /&gt;
            . &#039; FROM #__hello &#039;&lt;br /&gt;
        ;&lt;br /&gt;
        return $query;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Retrieves the hello data&lt;br /&gt;
     * @return array Array of objects containing the data from the database&lt;br /&gt;
     */&lt;br /&gt;
    function getData()&lt;br /&gt;
    {&lt;br /&gt;
        // Lets load the data if it doesn&#039;t already exist&lt;br /&gt;
        if (empty( $this-&amp;gt;_data ))&lt;br /&gt;
        {&lt;br /&gt;
            $query = $this-&amp;gt;_buildQuery();&lt;br /&gt;
            $this-&amp;gt;_data = $this-&amp;gt;_getList( $query );&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        return $this-&amp;gt;_data;&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This file is saved as models/hellos.php.&lt;br /&gt;
&lt;br /&gt;
==== The Hellos View ====&lt;br /&gt;
&lt;br /&gt;
Now that we have a model to retrieve our data, we need to display it. This view will be fairly similar to the view from the site section as well.&lt;br /&gt;
&lt;br /&gt;
Just as our model was automatically instantiated in the site, so it is in the administrator. Methods that start with get in the model can be accessed using the get() method of the JView class. So our view has three lines: one to retrieve the data from the model, one to push the data into the template, and one to invoke the display method to display the output. Thus we have:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * Hellos View for Hello World Component&lt;br /&gt;
 * &lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://dev.joomla.org/component/option,com_jd-wiki/Itemid,31/id,tutorials:components/&lt;br /&gt;
 * @license        GNU/GPL&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// Check to ensure this file is included in Joomla!&lt;br /&gt;
defined(&#039;_JEXEC&#039;) or die();&lt;br /&gt;
&lt;br /&gt;
jimport( &#039;joomla.application.component.view&#039; );&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Hellos View&lt;br /&gt;
 *&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 */&lt;br /&gt;
class HellosViewHellos extends JView&lt;br /&gt;
{&lt;br /&gt;
    /**&lt;br /&gt;
     * Hellos view display method&lt;br /&gt;
     * @return void&lt;br /&gt;
     **/&lt;br /&gt;
    function display($tpl = null)&lt;br /&gt;
    {&lt;br /&gt;
        JToolBarHelper::title( JText::_( &#039;Hello Manager&#039; ), &#039;generic.png&#039; );&lt;br /&gt;
        JToolBarHelper::deleteList();&lt;br /&gt;
        JToolBarHelper::editListX();&lt;br /&gt;
        JToolBarHelper::addNewX();&lt;br /&gt;
&lt;br /&gt;
        // Get data from the model&lt;br /&gt;
        $items =&amp;amp; $this-&amp;gt;get( &#039;Data&#039;);&lt;br /&gt;
&lt;br /&gt;
        $this-&amp;gt;assignRef( &#039;items&#039;, $items );&lt;br /&gt;
&lt;br /&gt;
        parent::display($tpl);&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This file is saved as views/hellos/view.html.php.&lt;br /&gt;
&lt;br /&gt;
==== The Hellos Template ====&lt;br /&gt;
&lt;br /&gt;
The template will take the data pushed into it from the view and produce the output. We will display our output in a simple table. While the frontend template was very simple, in the administrator we will need a minimal amount of extra logic to handle looping through the data.&lt;br /&gt;
&lt;br /&gt;
Here is our template:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;); ?&amp;gt;&lt;br /&gt;
&amp;lt;form action=&amp;quot;index.php&amp;quot; method=&amp;quot;post&amp;quot; name=&amp;quot;adminForm&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;div id=&amp;quot;editcell&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;table class=&amp;quot;adminlist&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;thead&amp;gt;&lt;br /&gt;
        &amp;lt;tr&amp;gt;&lt;br /&gt;
            &amp;lt;th width=&amp;quot;5&amp;quot;&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo JText::_( &#039;ID&#039; ); ?&amp;gt;&lt;br /&gt;
            &amp;lt;/th&amp;gt;&lt;br /&gt;
            &amp;lt;th&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo JText::_( &#039;Greeting&#039; ); ?&amp;gt;&lt;br /&gt;
            &amp;lt;/th&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;            &lt;br /&gt;
    &amp;lt;/thead&amp;gt;&lt;br /&gt;
    &amp;lt;?php&lt;br /&gt;
    $k = 0;&lt;br /&gt;
    for ($i=0, $n=count( $this-&amp;gt;items ); $i &amp;lt; $n; $i++)&lt;br /&gt;
    {&lt;br /&gt;
        $row =&amp;amp; $this-&amp;gt;items[$i];&lt;br /&gt;
        ?&amp;gt;&lt;br /&gt;
        &amp;lt;tr class=&amp;quot;&amp;lt;?php echo &amp;quot;row$k&amp;quot;; ?&amp;gt;&amp;quot;&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo $row-&amp;gt;id; ?&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo $row-&amp;gt;greeting; ?&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;&lt;br /&gt;
        &amp;lt;?php&lt;br /&gt;
        $k = 1 - $k;&lt;br /&gt;
    }&lt;br /&gt;
    ?&amp;gt;&lt;br /&gt;
    &amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;option&amp;quot; value=&amp;quot;com_hello&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;task&amp;quot; value=&amp;quot;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;boxchecked&amp;quot; value=&amp;quot;0&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;controller&amp;quot; value=&amp;quot;hello&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/form&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This template is saved as views/hellos/tmpl/default.php.&lt;br /&gt;
&lt;br /&gt;
You will notice that our output is enclosed in a form. Though this is not necessary now, it will be soon.&lt;br /&gt;
&lt;br /&gt;
We have now completed the basic part of the first view. We have added five files to the admin section of our component:&lt;br /&gt;
&lt;br /&gt;
* admin.hello.php&lt;br /&gt;
* controller.php&lt;br /&gt;
* models/hellos.php&lt;br /&gt;
* views/hellos/view.html.php&lt;br /&gt;
* views/hellos/tmpl/default.php&lt;br /&gt;
&lt;br /&gt;
You can now add these files to the XML install file and give it a try!&lt;br /&gt;
&lt;br /&gt;
== Adding Functionality ==&lt;br /&gt;
&lt;br /&gt;
So far our administrator section is pretty useless. It doesn&#039;t really do anything - all it does is display the entries that we have in our database.&lt;br /&gt;
&lt;br /&gt;
In order to make it useful, we need to add some buttons and links.&lt;br /&gt;
&lt;br /&gt;
==== The Toolbar ====&lt;br /&gt;
&lt;br /&gt;
You may have noticed the toolbar that appears at the top of other Joomla! component administrator panels. Our component needs one as well. Joomla! makes this very easy to do. We will add buttons Delete records, Edit records, and create New records. We will also add a title that will be displayed on our toolbar.&lt;br /&gt;
&lt;br /&gt;
This is done by adding code to the view. To add the buttons, we use static methods from the Joomla! JToolBarHelper class. The code looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;JToolBarHelper::title(   JText::_( &#039;Hello Manager&#039; ), &#039;generic.png&#039; );&lt;br /&gt;
JToolBarHelper::deleteList();&lt;br /&gt;
JToolBarHelper::editListX();&lt;br /&gt;
JToolBarHelper::addNewX();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
These three methods will create the appropriate buttons. The deleteList() method can optionally take up to three parameters - the first parameter is a string to display to the user to confirm that they want to delete the records. The second is the task that should be sent with the query (the default is &#039;remove&#039;), and the third is the text that should be displayed below the button.&lt;br /&gt;
&lt;br /&gt;
The editListX() and addNewX() methods can each take two optional parameters. The first is the task (which are by default edit and add, respectively), and the second is the text that should be displayed below the button.&lt;br /&gt;
&lt;br /&gt;
 &#039;&#039;*You may have noticed the use of the JText::_ method in the template before and here as well. This is a handy function that makes component translation much easier. The JText::_ method will look up the string in your component language file and return the translated string. If not translation is found, it will return the string that you passed it. If you want to translate your component into another language, all you have to do is create a language file that will map the strings within the quotes to the translated version of the string.&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==== Checkboxes and Links ====&lt;br /&gt;
&lt;br /&gt;
We now have buttons. Two of those buttons operate on existing records. But how do we know which records to operate on? We have to let the user tell us. To do this, we need to add checkboxes to our table so that the user can select certain records. This is done in our template.&lt;br /&gt;
&lt;br /&gt;
In order to the add the checkboxes, we need to add an extra column into our table. We will add the column in between the two that we already have.&lt;br /&gt;
&lt;br /&gt;
In the header of the column, we will add a checkbox which can be used to toggle all the boxes below it on or off:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;th width=&amp;quot;20&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;input type=&amp;quot;checkbox&amp;quot; name=&amp;quot;toggle&amp;quot; value=&amp;quot;&amp;quot; onclick=&amp;quot;checkAll(&amp;lt;?php echo count( $this-&amp;gt;items ); ?&amp;gt;);&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/th&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Javascript checkAll function is a function that is built into the Joomla! base Javascript package that provides the functionality that we want here.&lt;br /&gt;
&lt;br /&gt;
Now we need to add the checkboxes into the individual rows. Joomla!&#039;s JHTML class has a method, JHTML::_(), which will generate our checkbox for us. We will add the following line to our loop:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$checked    = JHTML::_( &#039;grid.id&#039;, $i, $row-&amp;gt;id );&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
after the line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$row =&amp;amp; $this-&amp;gt;items[$i];&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then we will add a cell in between the two that we already have:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;td&amp;gt;&lt;br /&gt;
    &amp;lt;?php echo $checked; ?&amp;gt;&lt;br /&gt;
&amp;lt;/td&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It can be cumbersome to have to check the box that we want to edit and then move up and click the edit button. Therefore, we will add a link that it will go straight to the greeting&#039;s edit form. We will add the following line after the call to the JHTML::_() method to generate the link HTML:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$link = JRoute::_( &#039;index.php?option=com_hello&amp;gt;controller=hello&amp;gt;task=edit&amp;gt;cid[]=&#039;. $row-&amp;gt;id );&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And we include the link in the cell showing the greeting text:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;td&amp;gt;&lt;br /&gt;
    &amp;lt;a href=&amp;quot;&amp;lt;?php echo $link; ?&amp;gt;&amp;quot;&amp;gt;&amp;lt;?php echo $row-&amp;gt;greeting; ?&amp;gt;&amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;lt;/td&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You will notice that this link points to the hello controller. This controller will handle the data manipulation of our greetings.&lt;br /&gt;
&lt;br /&gt;
If you recall from above, we had four hidden input fields at the bottom of our form. The first input field was named &#039;option&#039;. This field is necessary so that we stay in our component. The second input field was task. This form property gets set when one of the buttons in the toolbar is clicked. A Javascript error will result and the buttons will not work if this input field is omitted. The third input field is the boxchecked field. This field keeps track of the number of boxes that are checked. The edit and delete buttons will check to ensure that this is greater than zero and will not allow the form to be submitted if it is not. The fourth input field is the controller field. This is used to specify that tasks fired from this form will be handled by the hello controller.&lt;br /&gt;
&lt;br /&gt;
Here is the code for the completed default.php file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;); ?&amp;gt;&lt;br /&gt;
&amp;lt;form action=&amp;quot;index.php&amp;quot; method=&amp;quot;post&amp;quot; name=&amp;quot;adminForm&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;div id=&amp;quot;editcell&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;table class=&amp;quot;adminlist&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;thead&amp;gt;&lt;br /&gt;
        &amp;lt;tr&amp;gt;&lt;br /&gt;
            &amp;lt;th width=&amp;quot;5&amp;quot;&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo JText::_( &#039;ID&#039; ); ?&amp;gt;&lt;br /&gt;
            &amp;lt;/th&amp;gt;&lt;br /&gt;
            &amp;lt;th&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo JText::_( &#039;Greeting&#039; ); ?&amp;gt;&lt;br /&gt;
            &amp;lt;/th&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;            &lt;br /&gt;
    &amp;lt;/thead&amp;gt;&lt;br /&gt;
    &amp;lt;?php&lt;br /&gt;
    $k = 0;&lt;br /&gt;
    for ($i=0, $n=count( $this-&amp;gt;items ); $i &amp;lt; $n; $i++)&lt;br /&gt;
    {&lt;br /&gt;
        $row =&amp;amp; $this-&amp;gt;items[$i];&lt;br /&gt;
        ?&amp;gt;&lt;br /&gt;
        &amp;lt;tr class=&amp;quot;&amp;lt;?php echo &amp;quot;row$k&amp;quot;; ?&amp;gt;&amp;quot;&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo $row-&amp;gt;id; ?&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo $row-&amp;gt;greeting; ?&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;&lt;br /&gt;
        &amp;lt;?php&lt;br /&gt;
        $k = 1 - $k;&lt;br /&gt;
    }&lt;br /&gt;
    ?&amp;gt;&lt;br /&gt;
    &amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;option&amp;quot; value=&amp;quot;com_hello&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;task&amp;quot; value=&amp;quot;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;boxchecked&amp;quot; value=&amp;quot;0&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;controller&amp;quot; value=&amp;quot;hello&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/form&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Our hellos view is now complete. You can try out the component now to see the results.&lt;br /&gt;
&lt;br /&gt;
The component can be downloaded at: [[http://dev.joomla.org/components/com_jd-wiki/data/media/components/com_hello4a.zip|com_hello4a.zip]]&lt;br /&gt;
&lt;br /&gt;
== Getting Down and Dirty: Doing the Real Work ==&lt;br /&gt;
&lt;br /&gt;
Now that the Hellos view is done, it is time to move to the Hello view and model. This is where the real work will get done.&lt;br /&gt;
&lt;br /&gt;
==== The Hello Controller ====&lt;br /&gt;
&lt;br /&gt;
Our default controller just isn&#039;t going to cut it when it comes to doing work - all it is capable of doing is displaying views.&lt;br /&gt;
&lt;br /&gt;
We need to be able to handle the tasks that we are launching from the Hellos view: add, edit and remove.&lt;br /&gt;
&lt;br /&gt;
Add and edit are essentially the same task: they both display a form to the user that allows a greeting to be edited. The only difference is that new displays a blank form, and edit displays a form with data already in it. Since they are similar, we will map the add task onto the edit task handler. This is specified in our constructor:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * constructor (registers additional tasks to methods)&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function __construct()&lt;br /&gt;
{&lt;br /&gt;
    parent::__construct();&lt;br /&gt;
&lt;br /&gt;
    // Register Extra tasks&lt;br /&gt;
    $this-&amp;gt;registerTask( &#039;add&#039;  ,     &#039;edit&#039; );&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first parameter of JController::registerTask is the task to map, and the second is the method to map it to.&lt;br /&gt;
&lt;br /&gt;
We will start with handling the edit task. The controller&#039;s job is fairly simple for the edit task. All it has to do is specify the view and layout to load (the hello view and the form layout). We will also tell Joomla! to disable the mainmenu while we are editing our greeting. This prevents users from leaving unsaved records open.&lt;br /&gt;
&lt;br /&gt;
Our edit task handler looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * display the edit form&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function edit()&lt;br /&gt;
{&lt;br /&gt;
    JRequest::setVar( &#039;view&#039;, &#039;hello&#039; );&lt;br /&gt;
    JRequest::setVar( &#039;layout&#039;, &#039;form&#039;  );&lt;br /&gt;
    JRequest::setVar(&#039;hidemainmenu&#039;, 1);&lt;br /&gt;
&lt;br /&gt;
    parent::display();&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== The Hello View ====&lt;br /&gt;
&lt;br /&gt;
The Hello view will display a form which will allow the user to edit a greeting. The display method if the hello view has to do a few simple tasks:&lt;br /&gt;
&lt;br /&gt;
  * retrieve the data from the model&lt;br /&gt;
&lt;br /&gt;
  * create the toolbar&lt;br /&gt;
&lt;br /&gt;
  * pass the data into the template&lt;br /&gt;
&lt;br /&gt;
  * invoke the display() method to render the template&lt;br /&gt;
&lt;br /&gt;
This becomes a bit more complicated because the one view handles both the edit and add tasks. In our toolbar we want the user to know what whether they are adding or editing, so we have to determine which task was fired.&lt;br /&gt;
&lt;br /&gt;
Since we are already retrieving the record that we want to display from the model, we can use this data to determine what task was fired. If the task was edit, then the id field of our record will have been set. If the task was new, then it will not have been set. This can be used to determine if we have a new record or an existing record.&lt;br /&gt;
&lt;br /&gt;
We will add two buttons to the toolbar: save and cancel. Though the functionality will be the same, we want to display different buttons depending on whether it is a new or existing record. If it is a new record, we will display cancel. If it already exists, we will display close.&lt;br /&gt;
&lt;br /&gt;
Thus our display method looks like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * display method of Hello view&lt;br /&gt;
 * @return void&lt;br /&gt;
 **/&lt;br /&gt;
function display($tpl = null)&lt;br /&gt;
{&lt;br /&gt;
    //get the hello&lt;br /&gt;
    $hello        =&amp;amp; $this-&amp;gt;get(&#039;Data&#039;);&lt;br /&gt;
    $isNew        = ($hello-&amp;gt;id &amp;lt; 1);&lt;br /&gt;
&lt;br /&gt;
    $text = $isNew ? JText::_( &#039;New&#039; ) : JText::_( &#039;Edit&#039; );&lt;br /&gt;
    JToolBarHelper::title(   JText::_( &#039;Hello&#039; ).&#039;: &amp;lt;small&amp;gt;&amp;lt;small&amp;gt;[ &#039; . $text.&#039; ]&amp;lt;/small&amp;gt;&amp;lt;/small&amp;gt;&#039; );&lt;br /&gt;
    JToolBarHelper::save();&lt;br /&gt;
    if ($isNew)  {&lt;br /&gt;
        JToolBarHelper::cancel();&lt;br /&gt;
    } else {&lt;br /&gt;
        // for existing items the button is renamed `close`&lt;br /&gt;
        JToolBarHelper::cancel( &#039;cancel&#039;, &#039;Close&#039; );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    $this-&amp;gt;assignRef(&#039;hello&#039;, $hello);&lt;br /&gt;
    parent::display($tpl);&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== The Hello Model ====&lt;br /&gt;
&lt;br /&gt;
Our view needs data. Therefore, we need to create a model to model a hello.&lt;br /&gt;
&lt;br /&gt;
Our model will have two properties: _id and _data. _id will hold the id of the greeting and data will hold the data.&lt;br /&gt;
&lt;br /&gt;
We will start with a constructor, which will attempt to retrieve the id from the query:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Constructor that retrieves the ID from the request&lt;br /&gt;
 *&lt;br /&gt;
 * @access    public&lt;br /&gt;
 * @return    void&lt;br /&gt;
 */&lt;br /&gt;
function __construct()&lt;br /&gt;
{&lt;br /&gt;
    parent::__construct();&lt;br /&gt;
&lt;br /&gt;
    $array = JRequest::getVar(&#039;cid&#039;,  0, &#039;&#039;, &#039;array&#039;);&lt;br /&gt;
    $this-&amp;gt;setId((int)$array[0]);&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The JRequest::getVar() method is used to retrieve data from the request. The first parameter is the name of the form variable. The second parameter is the default value to assign if there is no value found. The third parameter is the name of the hash to retrieve the value from (get, post, etc), and the last value is the data type that should be forced on the value.&lt;br /&gt;
&lt;br /&gt;
Our constructor will take the first value from the cid array and assign it to the id.&lt;br /&gt;
&lt;br /&gt;
Our setId() method can be used to set our id. Changing the id that our model points to will mean the id points to the wrong data. Therefore, when we set the id, we will clear the data property:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Method to set the hello identifier&lt;br /&gt;
 *&lt;br /&gt;
 * @access    public&lt;br /&gt;
 * @param    int Hello identifier&lt;br /&gt;
 * @return    void&lt;br /&gt;
 */&lt;br /&gt;
function setId($id)&lt;br /&gt;
{&lt;br /&gt;
    // Set id and wipe data&lt;br /&gt;
    $this-&amp;gt;_id        = $id;&lt;br /&gt;
    $this-&amp;gt;_data    = null;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finally, we need a method to retrieve our data: getData()&lt;br /&gt;
&lt;br /&gt;
getData will check if the _data property has already been set. If it has, it will simply return it. Otherwise, it will load the data from the database.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Method to get a hello&lt;br /&gt;
 * @return object with data&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
function &amp;amp;getData()&lt;br /&gt;
{&lt;br /&gt;
    // Load the data&lt;br /&gt;
    if (empty( $this-&amp;gt;_data )) {&lt;br /&gt;
        $query = &#039; SELECT * FROM #__hello &#039;.&lt;br /&gt;
                &#039;  WHERE id = &#039;.$this-&amp;gt;_id;&lt;br /&gt;
        $this-&amp;gt;_db-&amp;gt;setQuery( $query );&lt;br /&gt;
        $this-&amp;gt;_data = $this-&amp;gt;_db-&amp;gt;loadObject();&lt;br /&gt;
    }&lt;br /&gt;
    if (!$this-&amp;gt;_data) {&lt;br /&gt;
        $this-&amp;gt;_data = new stdClass();&lt;br /&gt;
        $this-&amp;gt;_data-&amp;gt;id = 0;&lt;br /&gt;
        $this-&amp;gt;_data-&amp;gt;greeting = null;&lt;br /&gt;
    }&lt;br /&gt;
    return $this-&amp;gt;_data;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== The Form ====&lt;br /&gt;
&lt;br /&gt;
Now all that is left is to create the form that the data will go into. Since we specified our layout as form, the form will go in a file in the tmpl directory of the hello view called form.php:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;); ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;form action=&amp;quot;index.php&amp;quot; method=&amp;quot;post&amp;quot; name=&amp;quot;adminForm&amp;quot; id=&amp;quot;adminForm&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;div class=&amp;quot;col100&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;fieldset class=&amp;quot;adminform&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;legend&amp;gt;&amp;lt;?php echo JText::_( &#039;Details&#039; ); ?&amp;gt;&amp;lt;/legend&amp;gt;&lt;br /&gt;
        &amp;lt;table class=&amp;quot;admintable&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;tr&amp;gt;&lt;br /&gt;
            &amp;lt;td width=&amp;quot;100&amp;quot; align=&amp;quot;right&amp;quot; class=&amp;quot;key&amp;quot;&amp;gt;&lt;br /&gt;
                &amp;lt;label for=&amp;quot;greeting&amp;quot;&amp;gt;&lt;br /&gt;
                    &amp;lt;?php echo JText::_( &#039;Greeting&#039; ); ?&amp;gt;:&lt;br /&gt;
                &amp;lt;/label&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;input class=&amp;quot;text_area&amp;quot; type=&amp;quot;text&amp;quot; name=&amp;quot;greeting&amp;quot; id=&amp;quot;greeting&amp;quot; size=&amp;quot;32&amp;quot; maxlength=&amp;quot;250&amp;quot; value=&amp;quot;&amp;lt;?php echo $this-&amp;gt;hello-&amp;gt;greeting;?&amp;gt;&amp;quot; /&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;&lt;br /&gt;
    &amp;lt;/table&amp;gt;&lt;br /&gt;
    &amp;lt;/fieldset&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;clr&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;option&amp;quot; value=&amp;quot;com_hello&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;id&amp;quot; value=&amp;quot;&amp;lt;?php echo $this-&amp;gt;hello-&amp;gt;id; ?&amp;gt;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;task&amp;quot; value=&amp;quot;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;controller&amp;quot; value=&amp;quot;hello&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/form&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in addition to the input field, there is a hidden field for the id. The user doesn&#039;t need to edit the id (and shouldn&#039;t), so we silently pass it along in the form.&lt;br /&gt;
&lt;br /&gt;
==== Implementing the Functionality ====&lt;br /&gt;
&lt;br /&gt;
So far, our controller only handles two tasks: edit and new. However, we also have buttons to save, delete and cancel records. We need to write code to handle and perform these tasks.&lt;br /&gt;
&lt;br /&gt;
=== Saving a Record ===&lt;br /&gt;
&lt;br /&gt;
The logical next step is to implement the functionality to save a record. Normally, this would require some switches and logic to handle various cases, such as the difference between creating a new record (an INSERT query), and updating an existing query (an UPDATE query). Also, there are complexities involved in getting the data from the form and putting it into the query.&lt;br /&gt;
&lt;br /&gt;
The Joomla! framework takes care of a lot of this for you. The JTable class makes it easy to manipulate records in the database without having to worry about writing the SQL code that lies behind these updates. It also makes it easy to transfer data from an HTML form into the database.&lt;br /&gt;
&lt;br /&gt;
== Creating the Table Class ==&lt;br /&gt;
&lt;br /&gt;
The JTable class is an abstract class from which you can derive child classes to work with specific tables. To use it, you simply create a class that extends the JTable class, add your database fields as properties, and override the constructor to specify the name of the table and the primary key.&lt;br /&gt;
&lt;br /&gt;
Here is our JTable class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * Hello World table class&lt;br /&gt;
 * &lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://dev.joomla.org/component/option,com_jd-wiki/Itemid,31/id,tutorials:components/&lt;br /&gt;
 * @license        GNU/GPL&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// no direct access&lt;br /&gt;
defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;);&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Hello Table class&lt;br /&gt;
 *&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 */&lt;br /&gt;
class TableHello extends JTable&lt;br /&gt;
{&lt;br /&gt;
    /**&lt;br /&gt;
     * Primary Key&lt;br /&gt;
     *&lt;br /&gt;
     * @var int&lt;br /&gt;
     */&lt;br /&gt;
    var $id = null;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * @var string&lt;br /&gt;
     */&lt;br /&gt;
    var $greeting = null;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Constructor&lt;br /&gt;
     *&lt;br /&gt;
     * @param object Database connector object&lt;br /&gt;
     */&lt;br /&gt;
    function TableHello( &amp;amp;$db ) {&lt;br /&gt;
        parent::__construct(&#039;#__hello&#039;, &#039;id&#039;, $db);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You will see here that we have defined our two fields: the id field and the greeting field. Then we have defined a constructor, which will call the constructor of the parent class and pass it the name of the table (hello), the name of the field which is the primary key (id), and the database connector object.&lt;br /&gt;
&lt;br /&gt;
This file should be called hello.php and it will go in a directory called tables in the administrator section of our component.&lt;br /&gt;
&lt;br /&gt;
== Implementing the Function in our Model ==&lt;br /&gt;
&lt;br /&gt;
We are now ready to add the method to the model which will save our record. We will call this method store. Our store() method will do three things: bind the data from the form to the TableHello object, check to ensure that the record is properly formed, and store the record in the database.&lt;br /&gt;
&lt;br /&gt;
Our method looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Method to store a record&lt;br /&gt;
 *&lt;br /&gt;
 * @access    public&lt;br /&gt;
 * @return    boolean    True on success&lt;br /&gt;
 */&lt;br /&gt;
function store()&lt;br /&gt;
{&lt;br /&gt;
    $row =&amp;amp; $this-&amp;gt;getTable();&lt;br /&gt;
&lt;br /&gt;
    $data = JRequest::get( &#039;post&#039; );&lt;br /&gt;
    // Bind the form fields to the hello table&lt;br /&gt;
    if (!$row-&amp;gt;bind($data)) {&lt;br /&gt;
        $this-&amp;gt;setError($this-&amp;gt;_db-&amp;gt;getErrorMsg());&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // Make sure the hello record is valid&lt;br /&gt;
    if (!$row-&amp;gt;check()) {&lt;br /&gt;
        $this-&amp;gt;setError($this-&amp;gt;_db-&amp;gt;getErrorMsg());&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // Store the web link table to the database&lt;br /&gt;
    if (!$row-&amp;gt;store()) {&lt;br /&gt;
        $this-&amp;gt;setError($this-&amp;gt;_db-&amp;gt;getErrorMsg());&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return true;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This method gets added to the hello model.&lt;br /&gt;
&lt;br /&gt;
The method takes one parameter, which is an associative array of data that we want to store in the database. This can easily be retrieved from the request as will be seen later.&lt;br /&gt;
&lt;br /&gt;
You will see that the first line retrieves a reference to our JTable object. If we name our table properly, we don&#039;t have to specify its name - the JModel class knows where to find it. You may recall that we called our table class TableHello and put it in a file called hello.php in the tables directory. If you follow this convention, the JModel class can create your object automatically.&lt;br /&gt;
&lt;br /&gt;
The second line will retrieve the data from the form. The JRequest class makes this very easy. In this case, we are retrieving all of the variables that were submitted using the &#039;POST&#039; method. These will be returned as an associative array.&lt;br /&gt;
&lt;br /&gt;
The rest is easy - we bind, check and store. The bind() method will copy values from the array into the corresponding property of the table object. In this case, it will take the values of id and greeting and copy them to our TableHello object.&lt;br /&gt;
&lt;br /&gt;
The check() method will perform data verification. In the JTable() class, this method simply returns true. While this doesn&#039;t provide any value for us currently, by calling this method we make it possible to do data checking using our TableHello class in the future. This method can be overridden in our TableHello class with a method that performs the appropriate checks.&lt;br /&gt;
&lt;br /&gt;
The store() method will take the data that is in the object and store it in the database. If the id is 0, it will create a new record (INSERT), otherwise, it will update the existing record (UPDATE).&lt;br /&gt;
&lt;br /&gt;
== Adding the Task to the Controller ==&lt;br /&gt;
&lt;br /&gt;
We are now ready to add our task to the controller. Since the task that we are firing is called &#039;save&#039;, we must call our method &#039;save&#039;. This is simple:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * save a record (and redirect to main page)&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function save()&lt;br /&gt;
{&lt;br /&gt;
    $model = $this-&amp;gt;getModel(&#039;hello&#039;);&lt;br /&gt;
&lt;br /&gt;
    if ($model-&amp;gt;store()) {&lt;br /&gt;
        $msg = JText::_( &#039;Greeting Saved!&#039; );&lt;br /&gt;
    } else {&lt;br /&gt;
        $msg = JText::_( &#039;Error Saving Greeting&#039; );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // Check the table in so it can be edited.... we are done with it anyway&lt;br /&gt;
    $link = &#039;index.php?option=com_hello&#039;;&lt;br /&gt;
    $this-&amp;gt;setRedirect($link, $msg);&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All we do is get our model and invoke the store() method. Then we use the setRedirect() method to redirect back to our list of greetings. We also pass a message along, which will be displayed at the top of the page.&lt;br /&gt;
&lt;br /&gt;
=== Deleting a Record ===&lt;br /&gt;
&lt;br /&gt;
=== Implementing the Function in the Model ===&lt;br /&gt;
&lt;br /&gt;
In the model, we will retrieve the list of IDs to delete and call the JTable class to delete them. Here it is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Method to delete record(s)&lt;br /&gt;
 *&lt;br /&gt;
 * @access    public&lt;br /&gt;
 * @return    boolean    True on success&lt;br /&gt;
 */&lt;br /&gt;
function delete()&lt;br /&gt;
{&lt;br /&gt;
    $cids = JRequest::getVar( &#039;cid&#039;, array(0), &#039;post&#039;, &#039;array&#039; );&lt;br /&gt;
    $row =&amp;amp; $this-&amp;gt;getTable();&lt;br /&gt;
&lt;br /&gt;
    foreach($cids as $cid) {&lt;br /&gt;
        if (!$row-&amp;gt;delete( $cid )) {&lt;br /&gt;
            $this-&amp;gt;setError( $row-&amp;gt;getErrorMsg() );&lt;br /&gt;
            return false;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return true;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We invoke the JRequest::getVar() method to get the data from the request, then we invoke the $row-&amp;gt;delete() method to delete each row. By storing errors in the model we make it possible to retrieve them later if we so choose.&lt;br /&gt;
&lt;br /&gt;
=== Handling the Remove Task in the Controller ===&lt;br /&gt;
&lt;br /&gt;
This is similar to the save() method which handled the save task:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * remove record(s)&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function remove()&lt;br /&gt;
{&lt;br /&gt;
    $model = $this-&amp;gt;getModel(&#039;hello&#039;);&lt;br /&gt;
    if(!$model-&amp;gt;delete()) {&lt;br /&gt;
        $msg = JText::_( &#039;Error: One or More Greetings Could not be Deleted&#039; );&lt;br /&gt;
    } else {&lt;br /&gt;
        $msg = JText::_( &#039;Greeting(s) Deleted&#039; );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    $this-&amp;gt;setRedirect( &#039;index.php?option=com_hello&#039;, $msg );&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Cancelling the Edit Operation ====&lt;br /&gt;
&lt;br /&gt;
To cancel the edit operation, all we have to do is redirect back to the main view:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * cancel editing a record&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function cancel()&lt;br /&gt;
{&lt;br /&gt;
    $msg = JText::_( &#039;Operation Cancelled&#039; );&lt;br /&gt;
    $this-&amp;gt;setRedirect( &#039;index.php?option=com_hello&#039;, $msg );&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
We have now implemented a basic backend to our component. We are now able to edit the entries that are viewed in the frontend. We have demonstrated the interaction between models, views and controllers. We have shown how the JTable class can be extended to provide easy access to tables in the database. It can also be seen how the JToolBarHelper class can be used to create button bars in components to present a standardized look between components.&lt;br /&gt;
&lt;br /&gt;
== Contributors ==&lt;br /&gt;
* staalanden&lt;br /&gt;
&lt;br /&gt;
== Download ==&lt;br /&gt;
&lt;br /&gt;
The component can be downloaded at: [http://joomlacode.org/gf/download/frsrelease/8104/29402/com_hello4a_01.zip com_hello4a_01]&lt;/div&gt;</summary>
		<author><name>Tonie</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Creating_an_Administrator_Interface&amp;diff=9502</id>
		<title>J1.5:Developing a MVC Component/Creating an Administrator Interface</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Creating_an_Administrator_Interface&amp;diff=9502"/>
		<updated>2008-07-27T18:39:20Z</updated>

		<summary type="html">&lt;p&gt;Tonie: /* Download */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
In the first three tutorials, we have developed a MVC component that retrieves its data from a table in the database. Currently, there is no way to add data to the database except to do it manually using another tool. In this tutorial, we will develop an administrator section for our component which will make it possible to manage the entries in the database.&lt;br /&gt;
&lt;br /&gt;
== Creating the Basic Framework ==&lt;br /&gt;
&lt;br /&gt;
The basic framework of the administrator panel is very similar to the site portion. The main entry point for the administrator section of the component is admin.hello.php. This file is identical to the hello.php file that was used in the site portion except the name of the controller it loads will be changed to HellosController. The default controller is also called controller.php and this file is identical to the default controller in the site portion, with the exception that the controller is named HellosController instead of HelloController. This difference is so that JController will by default load the hellos view, which will display a list of our greetings.&lt;br /&gt;
&lt;br /&gt;
Here is the listing for admin.hello.php:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://dev.joomla.org/component/option,com_jd-wiki/Itemid,31/id,tutorials:components/&lt;br /&gt;
 * @license    GNU/GPL&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
// no direct access&lt;br /&gt;
&lt;br /&gt;
defined( &#039;_JEXEC&#039; ) or die( &#039;Restricted access&#039; );&lt;br /&gt;
&lt;br /&gt;
// Require the base controller&lt;br /&gt;
&lt;br /&gt;
require_once( JPATH_COMPONENT.DS.&#039;controller.php&#039; );&lt;br /&gt;
&lt;br /&gt;
// Require specific controller if requested&lt;br /&gt;
if($controller = JRequest::getWord(&#039;controller&#039;)) {&lt;br /&gt;
    $path = JPATH_COMPONENT.DS.&#039;controllers&#039;.DS.$controller.&#039;.php&#039;;&lt;br /&gt;
    if (file_exists($path)) {&lt;br /&gt;
        require_once $path;&lt;br /&gt;
    } else {&lt;br /&gt;
        $controller = &#039;&#039;;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Create the controller&lt;br /&gt;
$classname    = &#039;HellosController&#039;.$controller;&lt;br /&gt;
$controller   = new $classname( );&lt;br /&gt;
&lt;br /&gt;
// Perform the Request task&lt;br /&gt;
$controller-&amp;gt;execute( JRequest::getVar( &#039;task&#039; ) );&lt;br /&gt;
&lt;br /&gt;
// Redirect if set by the controller&lt;br /&gt;
$controller-&amp;gt;redirect();&lt;br /&gt;
&lt;br /&gt;
?&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The view and model that we will start with is the hellos view and the hellos model. We will start with the model.&lt;br /&gt;
&lt;br /&gt;
==== The Hellos Model ====&lt;br /&gt;
&lt;br /&gt;
The Hellos Model will be very simple. The only operation that we currently need is the ability to retrieve the list of hellos from the database. This operation will be implemented in a method called getData().&lt;br /&gt;
&lt;br /&gt;
The JModel class has a built in protected method called _getList(). This method can be used to simplify the task of retrieving a list of records from the database. We simply need to pass it the query and it will return the list of records.&lt;br /&gt;
&lt;br /&gt;
At a later point in time, we might want to use our query from within another method. Therefore, we will create a private method called _buildQuery() which will return the query that will be passed to _getList(). This makes it easier to change the query as well since it is localized in one place.&lt;br /&gt;
&lt;br /&gt;
Therefore we need two methods in our class: getData() and _buildQuery().&lt;br /&gt;
&lt;br /&gt;
_buildQuery() simply returns the query. It looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source  lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Returns the query&lt;br /&gt;
 * @return string The query to be used to retrieve the rows from the database&lt;br /&gt;
 */&lt;br /&gt;
function _buildQuery()&lt;br /&gt;
{&lt;br /&gt;
    $query = &#039; SELECT * &#039;&lt;br /&gt;
           . &#039; FROM #__hello &#039;&lt;br /&gt;
    ;&lt;br /&gt;
&lt;br /&gt;
    return $query;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
getData() will obtain the query and retrieve the records from the database. Now it might happen that we need to retrieve this list of data twice in one page load. It would be a waste to have to query the database twice. Therefore, we will have this method store the data in a protected property so that on subsequent requests it can simply return the data it has already retrieved. This property will be called _data.&lt;br /&gt;
&lt;br /&gt;
Here is the getData() method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source  lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Retrieves the hello data&lt;br /&gt;
 * @return array Array of objects containing the data from the database&lt;br /&gt;
 */&lt;br /&gt;
function getData()&lt;br /&gt;
{&lt;br /&gt;
    // Lets load the data if it doesn&#039;t already exist&lt;br /&gt;
    if (empty( $this-&amp;gt;_data ))&lt;br /&gt;
    {&lt;br /&gt;
        $query = $this-&amp;gt;_buildQuery();&lt;br /&gt;
        $this-&amp;gt;_data = $this-&amp;gt;_getList( $query );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return $this-&amp;gt;_data;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The completed model looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * Hellos Model for Hello World Component&lt;br /&gt;
 * &lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://dev.joomla.org/component/option,com_jd-wiki/Itemid,31/id,tutorials:components/&lt;br /&gt;
 * @license        GNU/GPL&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// Check to ensure this file is included in Joomla!&lt;br /&gt;
defined(&#039;_JEXEC&#039;) or die();&lt;br /&gt;
&lt;br /&gt;
jimport( &#039;joomla.application.component.model&#039; );&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Hello Model&lt;br /&gt;
 *&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 */&lt;br /&gt;
class HellosModelHellos extends JModel&lt;br /&gt;
{&lt;br /&gt;
    /**&lt;br /&gt;
     * Hellos data array&lt;br /&gt;
     *&lt;br /&gt;
     * @var array&lt;br /&gt;
     */&lt;br /&gt;
    var $_data;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Returns the query&lt;br /&gt;
     * @return string The query to be used to retrieve the rows from the database&lt;br /&gt;
     */&lt;br /&gt;
    function _buildQuery()&lt;br /&gt;
    {&lt;br /&gt;
        $query = &#039; SELECT * &#039;&lt;br /&gt;
            . &#039; FROM #__hello &#039;&lt;br /&gt;
        ;&lt;br /&gt;
        return $query;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Retrieves the hello data&lt;br /&gt;
     * @return array Array of objects containing the data from the database&lt;br /&gt;
     */&lt;br /&gt;
    function getData()&lt;br /&gt;
    {&lt;br /&gt;
        // Lets load the data if it doesn&#039;t already exist&lt;br /&gt;
        if (empty( $this-&amp;gt;_data ))&lt;br /&gt;
        {&lt;br /&gt;
            $query = $this-&amp;gt;_buildQuery();&lt;br /&gt;
            $this-&amp;gt;_data = $this-&amp;gt;_getList( $query );&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        return $this-&amp;gt;_data;&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This file is saved as models/hellos.php.&lt;br /&gt;
&lt;br /&gt;
==== The Hellos View ====&lt;br /&gt;
&lt;br /&gt;
Now that we have a model to retrieve our data, we need to display it. This view will be fairly similar to the view from the site section as well.&lt;br /&gt;
&lt;br /&gt;
Just as our model was automatically instantiated in the site, so it is in the administrator. Methods that start with get in the model can be accessed using the get() method of the JView class. So our view has three lines: one to retrieve the data from the model, one to push the data into the template, and one to invoke the display method to display the output. Thus we have:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * Hellos View for Hello World Component&lt;br /&gt;
 * &lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://dev.joomla.org/component/option,com_jd-wiki/Itemid,31/id,tutorials:components/&lt;br /&gt;
 * @license        GNU/GPL&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// Check to ensure this file is included in Joomla!&lt;br /&gt;
defined(&#039;_JEXEC&#039;) or die();&lt;br /&gt;
&lt;br /&gt;
jimport( &#039;joomla.application.component.view&#039; );&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Hellos View&lt;br /&gt;
 *&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 */&lt;br /&gt;
class HellosViewHellos extends JView&lt;br /&gt;
{&lt;br /&gt;
    /**&lt;br /&gt;
     * Hellos view display method&lt;br /&gt;
     * @return void&lt;br /&gt;
     **/&lt;br /&gt;
    function display($tpl = null)&lt;br /&gt;
    {&lt;br /&gt;
        JToolBarHelper::title( JText::_( &#039;Hello Manager&#039; ), &#039;generic.png&#039; );&lt;br /&gt;
        JToolBarHelper::deleteList();&lt;br /&gt;
        JToolBarHelper::editListX();&lt;br /&gt;
        JToolBarHelper::addNewX();&lt;br /&gt;
&lt;br /&gt;
        // Get data from the model&lt;br /&gt;
        $items =&amp;amp; $this-&amp;gt;get( &#039;Data&#039;);&lt;br /&gt;
&lt;br /&gt;
        $this-&amp;gt;assignRef( &#039;items&#039;, $items );&lt;br /&gt;
&lt;br /&gt;
        parent::display($tpl);&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This file is saved as views/hellos/view.html.php.&lt;br /&gt;
&lt;br /&gt;
==== The Hellos Template ====&lt;br /&gt;
&lt;br /&gt;
The template will take the data pushed into it from the view and produce the output. We will display our output in a simple table. While the frontend template was very simple, in the administrator we will need a minimal amount of extra logic to handle looping through the data.&lt;br /&gt;
&lt;br /&gt;
Here is our template:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;); ?&amp;gt;&lt;br /&gt;
&amp;lt;form action=&amp;quot;index.php&amp;quot; method=&amp;quot;post&amp;quot; name=&amp;quot;adminForm&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;div id=&amp;quot;editcell&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;table class=&amp;quot;adminlist&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;thead&amp;gt;&lt;br /&gt;
        &amp;lt;tr&amp;gt;&lt;br /&gt;
            &amp;lt;th width=&amp;quot;5&amp;quot;&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo JText::_( &#039;ID&#039; ); ?&amp;gt;&lt;br /&gt;
            &amp;lt;/th&amp;gt;&lt;br /&gt;
            &amp;lt;th&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo JText::_( &#039;Greeting&#039; ); ?&amp;gt;&lt;br /&gt;
            &amp;lt;/th&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;            &lt;br /&gt;
    &amp;lt;/thead&amp;gt;&lt;br /&gt;
    &amp;lt;?php&lt;br /&gt;
    $k = 0;&lt;br /&gt;
    for ($i=0, $n=count( $this-&amp;gt;items ); $i &amp;lt; $n; $i++)&lt;br /&gt;
    {&lt;br /&gt;
        $row =&amp;amp; $this-&amp;gt;items[$i];&lt;br /&gt;
        ?&amp;gt;&lt;br /&gt;
        &amp;lt;tr class=&amp;quot;&amp;lt;?php echo &amp;quot;row$k&amp;quot;; ?&amp;gt;&amp;quot;&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo $row-&amp;gt;id; ?&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo $row-&amp;gt;greeting; ?&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;&lt;br /&gt;
        &amp;lt;?php&lt;br /&gt;
        $k = 1 - $k;&lt;br /&gt;
    }&lt;br /&gt;
    ?&amp;gt;&lt;br /&gt;
    &amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;option&amp;quot; value=&amp;quot;com_hello&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;task&amp;quot; value=&amp;quot;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;boxchecked&amp;quot; value=&amp;quot;0&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;controller&amp;quot; value=&amp;quot;hello&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/form&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This template is saved as views/hellos/tmpl/default.php.&lt;br /&gt;
&lt;br /&gt;
You will notice that our output is enclosed in a form. Though this is not necessary now, it will be soon.&lt;br /&gt;
&lt;br /&gt;
We have now completed the basic part of the first view. We have added five files to the admin section of our component:&lt;br /&gt;
&lt;br /&gt;
  * admin.hello.php&lt;br /&gt;
&lt;br /&gt;
  * controller.php&lt;br /&gt;
&lt;br /&gt;
  * models/hellos.php&lt;br /&gt;
&lt;br /&gt;
  * views/hellos/view.html.php&lt;br /&gt;
&lt;br /&gt;
  * views/hellos/tmpl/default.php&lt;br /&gt;
&lt;br /&gt;
You can now add these files to the XML install file and give it a try!&lt;br /&gt;
&lt;br /&gt;
== Adding Functionality ==&lt;br /&gt;
&lt;br /&gt;
So far our administrator section is pretty useless. It doesn&#039;t really do anything - all it does is display the entries that we have in our database.&lt;br /&gt;
&lt;br /&gt;
In order to make it useful, we need to add some buttons and links.&lt;br /&gt;
&lt;br /&gt;
==== The Toolbar ====&lt;br /&gt;
&lt;br /&gt;
You may have noticed the toolbar that appears at the top of other Joomla! component administrator panels. Our component needs one as well. Joomla! makes this very easy to do. We will add buttons Delete records, Edit records, and create New records. We will also add a title that will be displayed on our toolbar.&lt;br /&gt;
&lt;br /&gt;
This is done by adding code to the view. To add the buttons, we use static methods from the Joomla! JToolBarHelper class. The code looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;JToolBarHelper::title(   JText::_( &#039;Hello Manager&#039; ), &#039;generic.png&#039; );&lt;br /&gt;
JToolBarHelper::deleteList();&lt;br /&gt;
JToolBarHelper::editListX();&lt;br /&gt;
JToolBarHelper::addNewX();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
These three methods will create the appropriate buttons. The deleteList() method can optionally take up to three parameters - the first parameter is a string to display to the user to confirm that they want to delete the records. The second is the task that should be sent with the query (the default is &#039;remove&#039;), and the third is the text that should be displayed below the button.&lt;br /&gt;
&lt;br /&gt;
The editListX() and addNewX() methods can each take two optional parameters. The first is the task (which are by default edit and add, respectively), and the second is the text that should be displayed below the button.&lt;br /&gt;
&lt;br /&gt;
 &#039;&#039;*You may have noticed the use of the JText::_ method in the template before and here as well. This is a handy function that makes component translation much easier. The JText::_ method will look up the string in your component language file and return the translated string. If not translation is found, it will return the string that you passed it. If you want to translate your component into another language, all you have to do is create a language file that will map the strings within the quotes to the translated version of the string.&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==== Checkboxes and Links ====&lt;br /&gt;
&lt;br /&gt;
We now have buttons. Two of those buttons operate on existing records. But how do we know which records to operate on? We have to let the user tell us. To do this, we need to add checkboxes to our table so that the user can select certain records. This is done in our template.&lt;br /&gt;
&lt;br /&gt;
In order to the add the checkboxes, we need to add an extra column into our table. We will add the column in between the two that we already have.&lt;br /&gt;
&lt;br /&gt;
In the header of the column, we will add a checkbox which can be used to toggle all the boxes below it on or off:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;th width=&amp;quot;20&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;input type=&amp;quot;checkbox&amp;quot; name=&amp;quot;toggle&amp;quot; value=&amp;quot;&amp;quot; onclick=&amp;quot;checkAll(&amp;lt;?php echo count( $this-&amp;gt;items ); ?&amp;gt;);&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/th&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Javascript checkAll function is a function that is built into the Joomla! base Javascript package that provides the functionality that we want here.&lt;br /&gt;
&lt;br /&gt;
Now we need to add the checkboxes into the individual rows. Joomla!&#039;s JHTML class has a method, JHTML::_(), which will generate our checkbox for us. We will add the following line to our loop:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$checked    = JHTML::_( &#039;grid.id&#039;, $i, $row-&amp;gt;id );&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
after the line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$row =&amp;amp; $this-&amp;gt;items[$i];&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then we will add a cell in between the two that we already have:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;td&amp;gt;&lt;br /&gt;
    &amp;lt;?php echo $checked; ?&amp;gt;&lt;br /&gt;
&amp;lt;/td&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It can be cumbersome to have to check the box that we want to edit and then move up and click the edit button. Therefore, we will add a link that it will go straight to the greeting&#039;s edit form. We will add the following line after the call to the JHTML::_() method to generate the link HTML:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$link = JRoute::_( &#039;index.php?option=com_hello&amp;gt;controller=hello&amp;gt;task=edit&amp;gt;cid[]=&#039;. $row-&amp;gt;id );&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And we include the link in the cell showing the greeting text:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;td&amp;gt;&lt;br /&gt;
    &amp;lt;a href=&amp;quot;&amp;lt;?php echo $link; ?&amp;gt;&amp;quot;&amp;gt;&amp;lt;?php echo $row-&amp;gt;greeting; ?&amp;gt;&amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;lt;/td&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You will notice that this link points to the hello controller. This controller will handle the data manipulation of our greetings.&lt;br /&gt;
&lt;br /&gt;
If you recall from above, we had four hidden input fields at the bottom of our form. The first input field was named &#039;option&#039;. This field is necessary so that we stay in our component. The second input field was task. This form property gets set when one of the buttons in the toolbar is clicked. A Javascript error will result and the buttons will not work if this input field is omitted. The third input field is the boxchecked field. This field keeps track of the number of boxes that are checked. The edit and delete buttons will check to ensure that this is greater than zero and will not allow the form to be submitted if it is not. The fourth input field is the controller field. This is used to specify that tasks fired from this form will be handled by the hello controller.&lt;br /&gt;
&lt;br /&gt;
Here is the code for the completed default.php file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;); ?&amp;gt;&lt;br /&gt;
&amp;lt;form action=&amp;quot;index.php&amp;quot; method=&amp;quot;post&amp;quot; name=&amp;quot;adminForm&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;div id=&amp;quot;editcell&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;table class=&amp;quot;adminlist&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;thead&amp;gt;&lt;br /&gt;
        &amp;lt;tr&amp;gt;&lt;br /&gt;
            &amp;lt;th width=&amp;quot;5&amp;quot;&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo JText::_( &#039;ID&#039; ); ?&amp;gt;&lt;br /&gt;
            &amp;lt;/th&amp;gt;&lt;br /&gt;
            &amp;lt;th&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo JText::_( &#039;Greeting&#039; ); ?&amp;gt;&lt;br /&gt;
            &amp;lt;/th&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;            &lt;br /&gt;
    &amp;lt;/thead&amp;gt;&lt;br /&gt;
    &amp;lt;?php&lt;br /&gt;
    $k = 0;&lt;br /&gt;
    for ($i=0, $n=count( $this-&amp;gt;items ); $i &amp;lt; $n; $i++)&lt;br /&gt;
    {&lt;br /&gt;
        $row =&amp;amp; $this-&amp;gt;items[$i];&lt;br /&gt;
        ?&amp;gt;&lt;br /&gt;
        &amp;lt;tr class=&amp;quot;&amp;lt;?php echo &amp;quot;row$k&amp;quot;; ?&amp;gt;&amp;quot;&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo $row-&amp;gt;id; ?&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo $row-&amp;gt;greeting; ?&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;&lt;br /&gt;
        &amp;lt;?php&lt;br /&gt;
        $k = 1 - $k;&lt;br /&gt;
    }&lt;br /&gt;
    ?&amp;gt;&lt;br /&gt;
    &amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;option&amp;quot; value=&amp;quot;com_hello&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;task&amp;quot; value=&amp;quot;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;boxchecked&amp;quot; value=&amp;quot;0&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;controller&amp;quot; value=&amp;quot;hello&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/form&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Our hellos view is now complete. You can try out the component now to see the results.&lt;br /&gt;
&lt;br /&gt;
The component can be downloaded at: [[http://dev.joomla.org/components/com_jd-wiki/data/media/components/com_hello4a.zip|com_hello4a.zip]]&lt;br /&gt;
&lt;br /&gt;
== Getting Down and Dirty: Doing the Real Work ==&lt;br /&gt;
&lt;br /&gt;
Now that the Hellos view is done, it is time to move to the Hello view and model. This is where the real work will get done.&lt;br /&gt;
&lt;br /&gt;
==== The Hello Controller ====&lt;br /&gt;
&lt;br /&gt;
Our default controller just isn&#039;t going to cut it when it comes to doing work - all it is capable of doing is displaying views.&lt;br /&gt;
&lt;br /&gt;
We need to be able to handle the tasks that we are launching from the Hellos view: add, edit and remove.&lt;br /&gt;
&lt;br /&gt;
Add and edit are essentially the same task: they both display a form to the user that allows a greeting to be edited. The only difference is that new displays a blank form, and edit displays a form with data already in it. Since they are similar, we will map the add task onto the edit task handler. This is specified in our constructor:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * constructor (registers additional tasks to methods)&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function __construct()&lt;br /&gt;
{&lt;br /&gt;
    parent::__construct();&lt;br /&gt;
&lt;br /&gt;
    // Register Extra tasks&lt;br /&gt;
    $this-&amp;gt;registerTask( &#039;add&#039;  ,     &#039;edit&#039; );&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first parameter of JController::registerTask is the task to map, and the second is the method to map it to.&lt;br /&gt;
&lt;br /&gt;
We will start with handling the edit task. The controller&#039;s job is fairly simple for the edit task. All it has to do is specify the view and layout to load (the hello view and the form layout). We will also tell Joomla! to disable the mainmenu while we are editing our greeting. This prevents users from leaving unsaved records open.&lt;br /&gt;
&lt;br /&gt;
Our edit task handler looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * display the edit form&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function edit()&lt;br /&gt;
{&lt;br /&gt;
    JRequest::setVar( &#039;view&#039;, &#039;hello&#039; );&lt;br /&gt;
    JRequest::setVar( &#039;layout&#039;, &#039;form&#039;  );&lt;br /&gt;
    JRequest::setVar(&#039;hidemainmenu&#039;, 1);&lt;br /&gt;
&lt;br /&gt;
    parent::display();&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== The Hello View ====&lt;br /&gt;
&lt;br /&gt;
The Hello view will display a form which will allow the user to edit a greeting. The display method if the hello view has to do a few simple tasks:&lt;br /&gt;
&lt;br /&gt;
  * retrieve the data from the model&lt;br /&gt;
&lt;br /&gt;
  * create the toolbar&lt;br /&gt;
&lt;br /&gt;
  * pass the data into the template&lt;br /&gt;
&lt;br /&gt;
  * invoke the display() method to render the template&lt;br /&gt;
&lt;br /&gt;
This becomes a bit more complicated because the one view handles both the edit and add tasks. In our toolbar we want the user to know what whether they are adding or editing, so we have to determine which task was fired.&lt;br /&gt;
&lt;br /&gt;
Since we are already retrieving the record that we want to display from the model, we can use this data to determine what task was fired. If the task was edit, then the id field of our record will have been set. If the task was new, then it will not have been set. This can be used to determine if we have a new record or an existing record.&lt;br /&gt;
&lt;br /&gt;
We will add two buttons to the toolbar: save and cancel. Though the functionality will be the same, we want to display different buttons depending on whether it is a new or existing record. If it is a new record, we will display cancel. If it already exists, we will display close.&lt;br /&gt;
&lt;br /&gt;
Thus our display method looks like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * display method of Hello view&lt;br /&gt;
 * @return void&lt;br /&gt;
 **/&lt;br /&gt;
function display($tpl = null)&lt;br /&gt;
{&lt;br /&gt;
    //get the hello&lt;br /&gt;
    $hello        =&amp;amp; $this-&amp;gt;get(&#039;Data&#039;);&lt;br /&gt;
    $isNew        = ($hello-&amp;gt;id &amp;lt; 1);&lt;br /&gt;
&lt;br /&gt;
    $text = $isNew ? JText::_( &#039;New&#039; ) : JText::_( &#039;Edit&#039; );&lt;br /&gt;
    JToolBarHelper::title(   JText::_( &#039;Hello&#039; ).&#039;: &amp;lt;small&amp;gt;&amp;lt;small&amp;gt;[ &#039; . $text.&#039; ]&amp;lt;/small&amp;gt;&amp;lt;/small&amp;gt;&#039; );&lt;br /&gt;
    JToolBarHelper::save();&lt;br /&gt;
    if ($isNew)  {&lt;br /&gt;
        JToolBarHelper::cancel();&lt;br /&gt;
    } else {&lt;br /&gt;
        // for existing items the button is renamed `close`&lt;br /&gt;
        JToolBarHelper::cancel( &#039;cancel&#039;, &#039;Close&#039; );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    $this-&amp;gt;assignRef(&#039;hello&#039;, $hello);&lt;br /&gt;
    parent::display($tpl);&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== The Hello Model ====&lt;br /&gt;
&lt;br /&gt;
Our view needs data. Therefore, we need to create a model to model a hello.&lt;br /&gt;
&lt;br /&gt;
Our model will have two properties: _id and _data. _id will hold the id of the greeting and data will hold the data.&lt;br /&gt;
&lt;br /&gt;
We will start with a constructor, which will attempt to retrieve the id from the query:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Constructor that retrieves the ID from the request&lt;br /&gt;
 *&lt;br /&gt;
 * @access    public&lt;br /&gt;
 * @return    void&lt;br /&gt;
 */&lt;br /&gt;
function __construct()&lt;br /&gt;
{&lt;br /&gt;
    parent::__construct();&lt;br /&gt;
&lt;br /&gt;
    $array = JRequest::getVar(&#039;cid&#039;,  0, &#039;&#039;, &#039;array&#039;);&lt;br /&gt;
    $this-&amp;gt;setId((int)$array[0]);&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The JRequest::getVar() method is used to retrieve data from the request. The first parameter is the name of the form variable. The second parameter is the default value to assign if there is no value found. The third parameter is the name of the hash to retrieve the value from (get, post, etc), and the last value is the data type that should be forced on the value.&lt;br /&gt;
&lt;br /&gt;
Our constructor will take the first value from the cid array and assign it to the id.&lt;br /&gt;
&lt;br /&gt;
Our setId() method can be used to set our id. Changing the id that our model points to will mean the id points to the wrong data. Therefore, when we set the id, we will clear the data property:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Method to set the hello identifier&lt;br /&gt;
 *&lt;br /&gt;
 * @access    public&lt;br /&gt;
 * @param    int Hello identifier&lt;br /&gt;
 * @return    void&lt;br /&gt;
 */&lt;br /&gt;
function setId($id)&lt;br /&gt;
{&lt;br /&gt;
    // Set id and wipe data&lt;br /&gt;
    $this-&amp;gt;_id        = $id;&lt;br /&gt;
    $this-&amp;gt;_data    = null;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finally, we need a method to retrieve our data: getData()&lt;br /&gt;
&lt;br /&gt;
getData will check if the _data property has already been set. If it has, it will simply return it. Otherwise, it will load the data from the database.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Method to get a hello&lt;br /&gt;
 * @return object with data&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
function &amp;amp;getData()&lt;br /&gt;
{&lt;br /&gt;
    // Load the data&lt;br /&gt;
    if (empty( $this-&amp;gt;_data )) {&lt;br /&gt;
        $query = &#039; SELECT * FROM #__hello &#039;.&lt;br /&gt;
                &#039;  WHERE id = &#039;.$this-&amp;gt;_id;&lt;br /&gt;
        $this-&amp;gt;_db-&amp;gt;setQuery( $query );&lt;br /&gt;
        $this-&amp;gt;_data = $this-&amp;gt;_db-&amp;gt;loadObject();&lt;br /&gt;
    }&lt;br /&gt;
    if (!$this-&amp;gt;_data) {&lt;br /&gt;
        $this-&amp;gt;_data = new stdClass();&lt;br /&gt;
        $this-&amp;gt;_data-&amp;gt;id = 0;&lt;br /&gt;
        $this-&amp;gt;_data-&amp;gt;greeting = null;&lt;br /&gt;
    }&lt;br /&gt;
    return $this-&amp;gt;_data;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== The Form ====&lt;br /&gt;
&lt;br /&gt;
Now all that is left is to create the form that the data will go into. Since we specified our layout as form, the form will go in a file in the tmpl directory of the hello view called form.php:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;); ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;form action=&amp;quot;index.php&amp;quot; method=&amp;quot;post&amp;quot; name=&amp;quot;adminForm&amp;quot; id=&amp;quot;adminForm&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;div class=&amp;quot;col100&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;fieldset class=&amp;quot;adminform&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;legend&amp;gt;&amp;lt;?php echo JText::_( &#039;Details&#039; ); ?&amp;gt;&amp;lt;/legend&amp;gt;&lt;br /&gt;
        &amp;lt;table class=&amp;quot;admintable&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;tr&amp;gt;&lt;br /&gt;
            &amp;lt;td width=&amp;quot;100&amp;quot; align=&amp;quot;right&amp;quot; class=&amp;quot;key&amp;quot;&amp;gt;&lt;br /&gt;
                &amp;lt;label for=&amp;quot;greeting&amp;quot;&amp;gt;&lt;br /&gt;
                    &amp;lt;?php echo JText::_( &#039;Greeting&#039; ); ?&amp;gt;:&lt;br /&gt;
                &amp;lt;/label&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;input class=&amp;quot;text_area&amp;quot; type=&amp;quot;text&amp;quot; name=&amp;quot;greeting&amp;quot; id=&amp;quot;greeting&amp;quot; size=&amp;quot;32&amp;quot; maxlength=&amp;quot;250&amp;quot; value=&amp;quot;&amp;lt;?php echo $this-&amp;gt;hello-&amp;gt;greeting;?&amp;gt;&amp;quot; /&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;&lt;br /&gt;
    &amp;lt;/table&amp;gt;&lt;br /&gt;
    &amp;lt;/fieldset&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;clr&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;option&amp;quot; value=&amp;quot;com_hello&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;id&amp;quot; value=&amp;quot;&amp;lt;?php echo $this-&amp;gt;hello-&amp;gt;id; ?&amp;gt;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;task&amp;quot; value=&amp;quot;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;controller&amp;quot; value=&amp;quot;hello&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/form&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in addition to the input field, there is a hidden field for the id. The user doesn&#039;t need to edit the id (and shouldn&#039;t), so we silently pass it along in the form.&lt;br /&gt;
&lt;br /&gt;
==== Implementing the Functionality ====&lt;br /&gt;
&lt;br /&gt;
So far, our controller only handles two tasks: edit and new. However, we also have buttons to save, delete and cancel records. We need to write code to handle and perform these tasks.&lt;br /&gt;
&lt;br /&gt;
=== Saving a Record ===&lt;br /&gt;
&lt;br /&gt;
The logical next step is to implement the functionality to save a record. Normally, this would require some switches and logic to handle various cases, such as the difference between creating a new record (an INSERT query), and updating an existing query (an UPDATE query). Also, there are complexities involved in getting the data from the form and putting it into the query.&lt;br /&gt;
&lt;br /&gt;
The Joomla! framework takes care of a lot of this for you. The JTable class makes it easy to manipulate records in the database without having to worry about writing the SQL code that lies behind these updates. It also makes it easy to transfer data from an HTML form into the database.&lt;br /&gt;
&lt;br /&gt;
== Creating the Table Class ==&lt;br /&gt;
&lt;br /&gt;
The JTable class is an abstract class from which you can derive child classes to work with specific tables. To use it, you simply create a class that extends the JTable class, add your database fields as properties, and override the constructor to specify the name of the table and the primary key.&lt;br /&gt;
&lt;br /&gt;
Here is our JTable class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * Hello World table class&lt;br /&gt;
 * &lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://dev.joomla.org/component/option,com_jd-wiki/Itemid,31/id,tutorials:components/&lt;br /&gt;
 * @license        GNU/GPL&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// no direct access&lt;br /&gt;
defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;);&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Hello Table class&lt;br /&gt;
 *&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 */&lt;br /&gt;
class TableHello extends JTable&lt;br /&gt;
{&lt;br /&gt;
    /**&lt;br /&gt;
     * Primary Key&lt;br /&gt;
     *&lt;br /&gt;
     * @var int&lt;br /&gt;
     */&lt;br /&gt;
    var $id = null;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * @var string&lt;br /&gt;
     */&lt;br /&gt;
    var $greeting = null;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Constructor&lt;br /&gt;
     *&lt;br /&gt;
     * @param object Database connector object&lt;br /&gt;
     */&lt;br /&gt;
    function TableHello( &amp;amp;$db ) {&lt;br /&gt;
        parent::__construct(&#039;#__hello&#039;, &#039;id&#039;, $db);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You will see here that we have defined our two fields: the id field and the greeting field. Then we have defined a constructor, which will call the constructor of the parent class and pass it the name of the table (hello), the name of the field which is the primary key (id), and the database connector object.&lt;br /&gt;
&lt;br /&gt;
This file should be called hello.php and it will go in a directory called tables in the administrator section of our component.&lt;br /&gt;
&lt;br /&gt;
== Implementing the Function in our Model ==&lt;br /&gt;
&lt;br /&gt;
We are now ready to add the method to the model which will save our record. We will call this method store. Our store() method will do three things: bind the data from the form to the TableHello object, check to ensure that the record is properly formed, and store the record in the database.&lt;br /&gt;
&lt;br /&gt;
Our method looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Method to store a record&lt;br /&gt;
 *&lt;br /&gt;
 * @access    public&lt;br /&gt;
 * @return    boolean    True on success&lt;br /&gt;
 */&lt;br /&gt;
function store()&lt;br /&gt;
{&lt;br /&gt;
    $row =&amp;amp; $this-&amp;gt;getTable();&lt;br /&gt;
&lt;br /&gt;
    $data = JRequest::get( &#039;post&#039; );&lt;br /&gt;
    // Bind the form fields to the hello table&lt;br /&gt;
    if (!$row-&amp;gt;bind($data)) {&lt;br /&gt;
        $this-&amp;gt;setError($this-&amp;gt;_db-&amp;gt;getErrorMsg());&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // Make sure the hello record is valid&lt;br /&gt;
    if (!$row-&amp;gt;check()) {&lt;br /&gt;
        $this-&amp;gt;setError($this-&amp;gt;_db-&amp;gt;getErrorMsg());&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // Store the web link table to the database&lt;br /&gt;
    if (!$row-&amp;gt;store()) {&lt;br /&gt;
        $this-&amp;gt;setError($this-&amp;gt;_db-&amp;gt;getErrorMsg());&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return true;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This method gets added to the hello model.&lt;br /&gt;
&lt;br /&gt;
The method takes one parameter, which is an associative array of data that we want to store in the database. This can easily be retrieved from the request as will be seen later.&lt;br /&gt;
&lt;br /&gt;
You will see that the first line retrieves a reference to our JTable object. If we name our table properly, we don&#039;t have to specify its name - the JModel class knows where to find it. You may recall that we called our table class TableHello and put it in a file called hello.php in the tables directory. If you follow this convention, the JModel class can create your object automatically.&lt;br /&gt;
&lt;br /&gt;
The second line will retrieve the data from the form. The JRequest class makes this very easy. In this case, we are retrieving all of the variables that were submitted using the &#039;POST&#039; method. These will be returned as an associative array.&lt;br /&gt;
&lt;br /&gt;
The rest is easy - we bind, check and store. The bind() method will copy values from the array into the corresponding property of the table object. In this case, it will take the values of id and greeting and copy them to our TableHello object.&lt;br /&gt;
&lt;br /&gt;
The check() method will perform data verification. In the JTable() class, this method simply returns true. While this doesn&#039;t provide any value for us currently, by calling this method we make it possible to do data checking using our TableHello class in the future. This method can be overridden in our TableHello class with a method that performs the appropriate checks.&lt;br /&gt;
&lt;br /&gt;
The store() method will take the data that is in the object and store it in the database. If the id is 0, it will create a new record (INSERT), otherwise, it will update the existing record (UPDATE).&lt;br /&gt;
&lt;br /&gt;
== Adding the Task to the Controller ==&lt;br /&gt;
&lt;br /&gt;
We are now ready to add our task to the controller. Since the task that we are firing is called &#039;save&#039;, we must call our method &#039;save&#039;. This is simple:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * save a record (and redirect to main page)&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function save()&lt;br /&gt;
{&lt;br /&gt;
    $model = $this-&amp;gt;getModel(&#039;hello&#039;);&lt;br /&gt;
&lt;br /&gt;
    if ($model-&amp;gt;store()) {&lt;br /&gt;
        $msg = JText::_( &#039;Greeting Saved!&#039; );&lt;br /&gt;
    } else {&lt;br /&gt;
        $msg = JText::_( &#039;Error Saving Greeting&#039; );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // Check the table in so it can be edited.... we are done with it anyway&lt;br /&gt;
    $link = &#039;index.php?option=com_hello&#039;;&lt;br /&gt;
    $this-&amp;gt;setRedirect($link, $msg);&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All we do is get our model and invoke the store() method. Then we use the setRedirect() method to redirect back to our list of greetings. We also pass a message along, which will be displayed at the top of the page.&lt;br /&gt;
&lt;br /&gt;
=== Deleting a Record ===&lt;br /&gt;
&lt;br /&gt;
=== Implementing the Function in the Model ===&lt;br /&gt;
&lt;br /&gt;
In the model, we will retrieve the list of IDs to delete and call the JTable class to delete them. Here it is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Method to delete record(s)&lt;br /&gt;
 *&lt;br /&gt;
 * @access    public&lt;br /&gt;
 * @return    boolean    True on success&lt;br /&gt;
 */&lt;br /&gt;
function delete()&lt;br /&gt;
{&lt;br /&gt;
    $cids = JRequest::getVar( &#039;cid&#039;, array(0), &#039;post&#039;, &#039;array&#039; );&lt;br /&gt;
    $row =&amp;amp; $this-&amp;gt;getTable();&lt;br /&gt;
&lt;br /&gt;
    foreach($cids as $cid) {&lt;br /&gt;
        if (!$row-&amp;gt;delete( $cid )) {&lt;br /&gt;
            $this-&amp;gt;setError( $row-&amp;gt;getErrorMsg() );&lt;br /&gt;
            return false;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return true;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We invoke the JRequest::getVar() method to get the data from the request, then we invoke the $row-&amp;gt;delete() method to delete each row. By storing errors in the model we make it possible to retrieve them later if we so choose.&lt;br /&gt;
&lt;br /&gt;
=== Handling the Remove Task in the Controller ===&lt;br /&gt;
&lt;br /&gt;
This is similar to the save() method which handled the save task:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * remove record(s)&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function remove()&lt;br /&gt;
{&lt;br /&gt;
    $model = $this-&amp;gt;getModel(&#039;hello&#039;);&lt;br /&gt;
    if(!$model-&amp;gt;delete()) {&lt;br /&gt;
        $msg = JText::_( &#039;Error: One or More Greetings Could not be Deleted&#039; );&lt;br /&gt;
    } else {&lt;br /&gt;
        $msg = JText::_( &#039;Greeting(s) Deleted&#039; );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    $this-&amp;gt;setRedirect( &#039;index.php?option=com_hello&#039;, $msg );&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Cancelling the Edit Operation ====&lt;br /&gt;
&lt;br /&gt;
To cancel the edit operation, all we have to do is redirect back to the main view:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * cancel editing a record&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function cancel()&lt;br /&gt;
{&lt;br /&gt;
    $msg = JText::_( &#039;Operation Cancelled&#039; );&lt;br /&gt;
    $this-&amp;gt;setRedirect( &#039;index.php?option=com_hello&#039;, $msg );&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
We have now implemented a basic backend to our component. We are now able to edit the entries that are viewed in the frontend. We have demonstrated the interaction between models, views and controllers. We have shown how the JTable class can be extended to provide easy access to tables in the database. It can also be seen how the JToolBarHelper class can be used to create button bars in components to present a standardized look between components.&lt;br /&gt;
&lt;br /&gt;
== Contributors ==&lt;br /&gt;
* staalanden&lt;br /&gt;
&lt;br /&gt;
== Download ==&lt;br /&gt;
&lt;br /&gt;
The component can be downloaded at: [http://joomlacode.org/gf/download/frsrelease/8104/29402/com_hello4a_01.zip com_hello4a_01]&lt;/div&gt;</summary>
		<author><name>Tonie</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Using_the_Database&amp;diff=9501</id>
		<title>J1.5:Developing a MVC Component/Using the Database</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Using_the_Database&amp;diff=9501"/>
		<updated>2008-07-27T18:38:16Z</updated>

		<summary type="html">&lt;p&gt;Tonie: /* Retrieving the Data */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
In the first two tutorials, we showed you how to build a simple model-view-controller component. We had one view which retrieved data from a model (which was created in the 2nd tutorial). In this tutorial, we will be working with the model. Instead of the data being hard coded in the model, the model will retrieve the data from a table in the database.&lt;br /&gt;
&lt;br /&gt;
This tutorial will demonstrate how to use the JDatabase class to retrieve data from the database.&lt;br /&gt;
&lt;br /&gt;
== Retrieving the Data ==&lt;br /&gt;
&lt;br /&gt;
Our model currently has one method: getGreeting(). This method is very simple - all it does is return the hard-coded greeting.&lt;br /&gt;
&lt;br /&gt;
To make things more interesting, we will load the greeting from a database table. We will demonstrate later how to create an SQL file and add the appropriate code to the XML manifest file so that the table and some sample data will be created when the component is installed. For now, we will simply replace our return statement with some code that will retrieve the greeting from the database and return it.&lt;br /&gt;
&lt;br /&gt;
The first step is to obtain a reference to a database object. Since Joomla! uses the database for its normal operation, a database connection already exists; therefore, it is not necessary to create your own. A reference to the existing database can be obtained using:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$db =&amp;amp; JFactory::getDBO();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
JFactory is a static class that is used to retrieve references to many of the system objects. More information about this class can be found in the API documentation.&lt;br /&gt;
&lt;br /&gt;
The method name (getDBO) stands for get DataBase Object, and is easy and important to remember.&lt;br /&gt;
&lt;br /&gt;
Now that we have obtained a reference to the database object, we can retrieve our data. We do this in two steps:&lt;br /&gt;
&lt;br /&gt;
* store our query in the database object&lt;br /&gt;
* load the result&lt;br /&gt;
&lt;br /&gt;
Our new getGreeting() method will therefore look like:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;function getGreeting()&lt;br /&gt;
{&lt;br /&gt;
   $db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
&lt;br /&gt;
   $query = &#039;SELECT greeting FROM #__hello&#039;;&lt;br /&gt;
   $db-&amp;gt;setQuery( $query );&lt;br /&gt;
   $greeting = $db-&amp;gt;loadResult();&lt;br /&gt;
&lt;br /&gt;
   return $greeting;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
hello is the name of the table that we will create later, and greeting is the name of the field that stores the greetings. If you are not familiar with SQL, it would be helpful to take a tutorial or a lesson to get yourself up to speed. One such tutorial can be found at [http://www.w3schools.com/sql/default.asp w3schools].&lt;br /&gt;
&lt;br /&gt;
The $db-&amp;gt;loadResult() method will execute the stored database query and return the first field of the first row of the result. See [http://api.joomla.org/Joomla-Framework/Database/JDatabase.html JDatabase API reference] for more information about other load methods in the JDatabase class.&lt;br /&gt;
&lt;br /&gt;
== Creating the Installation SQL File ==&lt;br /&gt;
&lt;br /&gt;
The Joomla! installer has built-in support for executing queries during component installation. These queries are all stored in a standard text file.&lt;br /&gt;
&lt;br /&gt;
We will have three queries in our install file: the first will drop the table in case it already exists, the second will create the table with the appropriate fields, and the third will insert the data.&lt;br /&gt;
&lt;br /&gt;
Here are our queries:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;DROP TABLE IF EXISTS `#__hello`;&lt;br /&gt;
&lt;br /&gt;
CREATE TABLE `#__hello` (&lt;br /&gt;
  `id` int(11) NOT NULL auto_increment,&lt;br /&gt;
  `greeting` varchar(25) NOT NULL,&lt;br /&gt;
  PRIMARY KEY  (`id`)&lt;br /&gt;
) ENGINE=MyISAM AUTO_INCREMENT DEFAULT CHARSET=utf8;&lt;br /&gt;
&lt;br /&gt;
INSERT INTO `#__hello` (`greeting`) VALUES (&#039;Hello, World!&#039;),&lt;br /&gt;
(&#039;Bonjour, Monde!&#039;),&lt;br /&gt;
(&#039;Ciao, Mondo!&#039;);&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You might find the  prefix on the table names rather odd. Joomla! will replace this prefix with the prefix used by the current install. For most installs, this table will become jos_hello. This allows multiple installs of Joomla! to use the same database, and prevents collisions with other applications using the same table names (i.e. two applications might share a database, but might both require a &#039;users&#039; table. This convention avoids problems.)&lt;br /&gt;
&lt;br /&gt;
We have specified two fields in our database. The first field is id, and is called the &#039;primary key&#039;. The primary key of a database table is a field that is used to uniquely identify a record. This is often used to lookup rows in the database. The other field is greeting. This is the field that stores the greeting that is returned from the query that we used above.&lt;br /&gt;
&lt;br /&gt;
We will save our queries in a file called install.utf.sql.&lt;br /&gt;
&lt;br /&gt;
=== Creating the Uninstall SQL File ===&lt;br /&gt;
&lt;br /&gt;
Though we might hope that people will never want to uninstall our component, it is important that if they do, we don&#039;t leave anything behind. Joomla! will look after deleting the files and directories that were created during install, but you must manually include queries that will remove and tables that have been added to the database. Since we have only created one table, we only need one query:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;DROP TABLE IF EXISTS `#__hello`;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will save this query in a file called uninstall.utf.sql.&lt;br /&gt;
&lt;br /&gt;
== Updating our Install File ==&lt;br /&gt;
&lt;br /&gt;
We need to change a few things in our install file. First, we need to add our two new files to the list of files to install. SQL install file have to go in the admin directory. Second, we need to tell the installer to execute the queries in our files on install and uninstall.&lt;br /&gt;
&lt;br /&gt;
Our new file looks like this:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;&lt;br /&gt;
&amp;lt;install type=&amp;quot;component&amp;quot; version=&amp;quot;1.5.0&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;name&amp;gt;Hello&amp;lt;/name&amp;gt;&lt;br /&gt;
 &amp;lt;!-- The following elements are optional and free of formatting conttraints --&amp;gt;&lt;br /&gt;
 &amp;lt;creationDate&amp;gt;2007-02-22&amp;lt;/creationDate&amp;gt;&lt;br /&gt;
 &amp;lt;author&amp;gt;John Doe&amp;lt;/author&amp;gt;&lt;br /&gt;
 &amp;lt;authorEmail&amp;gt;john.doe@example.org&amp;lt;/authorEmail&amp;gt;&lt;br /&gt;
 &amp;lt;authorUrl&amp;gt;http://www.example.org&amp;lt;/authorUrl&amp;gt;&lt;br /&gt;
 &amp;lt;copyright&amp;gt;Copyright Info&amp;lt;/copyright&amp;gt;&lt;br /&gt;
 &amp;lt;license&amp;gt;License Info&amp;lt;/license&amp;gt;&lt;br /&gt;
 &amp;lt;!--  The version string is recorded in the components table --&amp;gt;&lt;br /&gt;
 &amp;lt;version&amp;gt;3.01&amp;lt;/version&amp;gt;&lt;br /&gt;
 &amp;lt;!-- The description is optional and defaults to the name --&amp;gt;&lt;br /&gt;
 &amp;lt;description&amp;gt;Description of the component ...&amp;lt;/description&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;!-- Site Main File Copy Section --&amp;gt;&lt;br /&gt;
 &amp;lt;!-- Note the folder attribute: This attribute describes the folder&lt;br /&gt;
      to copy FROM in the package to install therefore files copied&lt;br /&gt;
      in this section are copied from /site/ in the package --&amp;gt;&lt;br /&gt;
 &amp;lt;files folder=&amp;quot;site&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;controller.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;models/hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;models/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/view.html.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/tmpl/default.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/tmpl/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
 &amp;lt;/files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;install&amp;gt;&lt;br /&gt;
  &amp;lt;sql&amp;gt;&lt;br /&gt;
   &amp;lt;file charset=&amp;quot;utf8&amp;quot; driver=&amp;quot;mysql&amp;quot;&amp;gt;install.sql&amp;lt;/file&amp;gt;&lt;br /&gt;
  &amp;lt;/sql&amp;gt;&lt;br /&gt;
 &amp;lt;/install&amp;gt;&lt;br /&gt;
 &amp;lt;uninstall&amp;gt;&lt;br /&gt;
  &amp;lt;sql&amp;gt;&lt;br /&gt;
   &amp;lt;file charset=&amp;quot;utf8&amp;quot; driver=&amp;quot;mysql&amp;quot;&amp;gt;uninstall.sql&amp;lt;/file&amp;gt;&lt;br /&gt;
  &amp;lt;/sql&amp;gt;&lt;br /&gt;
 &amp;lt;/uninstall&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;administration&amp;gt;&lt;br /&gt;
  &amp;lt;!-- Administration Menu Section --&amp;gt;&lt;br /&gt;
  &amp;lt;menu&amp;gt;Hello World!&amp;lt;/menu&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;!-- Administration Main File Copy Section --&amp;gt;&lt;br /&gt;
  &amp;lt;files folder=&amp;quot;admin&amp;quot;&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;install.sql&amp;lt;/filename&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;uninstall.sql&amp;lt;/filename&amp;gt;&lt;br /&gt;
&amp;lt;/files&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
 &amp;lt;/administration&amp;gt;&lt;br /&gt;
&amp;lt;/install&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You will notice two attributes present on the &amp;lt;file&amp;gt; tags within the &amp;lt;install&amp;gt; and &amp;lt;uninstall&amp;gt; sections: charset and driver. The charset is the type of charset to use. The only valid charset is utf8. If you want to create install files for non-utf8 databases (for older version of MySQL), you should omit this attribute.&lt;br /&gt;
&lt;br /&gt;
The driver attribute specifies which database the queries were written for. Currently, this can only be mysql, but in future versions of Joomla! there may be more database drivers available.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
We now have a component that takes advantage of both the Joomla! MVC framework classes and the JDatabase classes. You are now able to write MVC components that interact with the database and can use the Joomla! installer to create and populate database tables.&lt;br /&gt;
&lt;br /&gt;
== Contributors ==&lt;br /&gt;
* staalanden&lt;br /&gt;
&lt;br /&gt;
== Download ==&lt;br /&gt;
&lt;br /&gt;
The component can be downloaded at: [http://joomlacode.org/gf/download/frsrelease/8104/29402/com_hello3_01.zip com_hello3_01]&lt;/div&gt;</summary>
		<author><name>Tonie</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Using_the_Database&amp;diff=9500</id>
		<title>J1.5:Developing a MVC Component/Using the Database</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Using_the_Database&amp;diff=9500"/>
		<updated>2008-07-27T18:35:50Z</updated>

		<summary type="html">&lt;p&gt;Tonie: /* Retrieving the Data */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
In the first two tutorials, we showed you how to build a simple model-view-controller component. We had one view which retrieved data from a model (which was created in the 2nd tutorial). In this tutorial, we will be working with the model. Instead of the data being hard coded in the model, the model will retrieve the data from a table in the database.&lt;br /&gt;
&lt;br /&gt;
This tutorial will demonstrate how to use the JDatabase class to retrieve data from the database.&lt;br /&gt;
&lt;br /&gt;
== Retrieving the Data ==&lt;br /&gt;
&lt;br /&gt;
Our model currently has one method: getGreeting(). This method is very simple - all it does is return the hard-coded greeting.&lt;br /&gt;
&lt;br /&gt;
To make things more interesting, we will load the greeting from a database table. We will demonstrate later how to create an SQL file and add the appropriate code to the XML manifest file so that the table and some sample data will be created when the component is installed. For now, we will simply replace our return statement with some code that will retrieve the greeting from the database and return it.&lt;br /&gt;
&lt;br /&gt;
The first step is to obtain a reference to a database object. Since Joomla! uses the database for its normal operation, a database connection already exists; therefore, it is not necessary to create your own. A reference to the existing database can be obtained using:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$db =&amp;amp; JFactory::getDBO();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
JFactory is a static class that is used to retrieve references to many of the system objects. More information about this class can be found in the API documentation.&lt;br /&gt;
&lt;br /&gt;
The method name (getDBO) stands for get DataBase Object, and is easy and important to remember.&lt;br /&gt;
&lt;br /&gt;
Now that we have obtained a reference to the database object, we can retrieve our data. We do this in two steps:&lt;br /&gt;
&lt;br /&gt;
* store our query in the database object&lt;br /&gt;
* load the result&lt;br /&gt;
&lt;br /&gt;
Our new getGreeting() method will therefore look like:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;function getGreeting()&lt;br /&gt;
{&lt;br /&gt;
   $db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
&lt;br /&gt;
   $query = &#039;SELECT greeting FROM #__hello&#039;;&lt;br /&gt;
   $db-&amp;gt;setQuery( $query );&lt;br /&gt;
   $greeting = $db-&amp;gt;loadResult();&lt;br /&gt;
&lt;br /&gt;
   return $greeting;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
hello is the name of the table that we will create later, and greeting is the name of the field that stores the greetings. If you are not familiar with SQL, it would be helpful to take a tutorial or a lesson to get yourself up to speed. One such tutorial can be found at [http://www.w3schools.com/sql/default.asp w3schools].&lt;br /&gt;
&lt;br /&gt;
The $db-&amp;gt;loadResult() method will execute the stored database query and return the first field of the first row of the result. See [[http://dev.joomla.org/component/option,com_jd-wiki/Itemid,/id,references:joomla.framework:database:jdatabase/|JDatabase API reference]] for more information about other load methods in the JDatabase class.&lt;br /&gt;
&lt;br /&gt;
== Creating the Installation SQL File ==&lt;br /&gt;
&lt;br /&gt;
The Joomla! installer has built-in support for executing queries during component installation. These queries are all stored in a standard text file.&lt;br /&gt;
&lt;br /&gt;
We will have three queries in our install file: the first will drop the table in case it already exists, the second will create the table with the appropriate fields, and the third will insert the data.&lt;br /&gt;
&lt;br /&gt;
Here are our queries:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;DROP TABLE IF EXISTS `#__hello`;&lt;br /&gt;
&lt;br /&gt;
CREATE TABLE `#__hello` (&lt;br /&gt;
  `id` int(11) NOT NULL auto_increment,&lt;br /&gt;
  `greeting` varchar(25) NOT NULL,&lt;br /&gt;
  PRIMARY KEY  (`id`)&lt;br /&gt;
) ENGINE=MyISAM AUTO_INCREMENT DEFAULT CHARSET=utf8;&lt;br /&gt;
&lt;br /&gt;
INSERT INTO `#__hello` (`greeting`) VALUES (&#039;Hello, World!&#039;),&lt;br /&gt;
(&#039;Bonjour, Monde!&#039;),&lt;br /&gt;
(&#039;Ciao, Mondo!&#039;);&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You might find the  prefix on the table names rather odd. Joomla! will replace this prefix with the prefix used by the current install. For most installs, this table will become jos_hello. This allows multiple installs of Joomla! to use the same database, and prevents collisions with other applications using the same table names (i.e. two applications might share a database, but might both require a &#039;users&#039; table. This convention avoids problems.)&lt;br /&gt;
&lt;br /&gt;
We have specified two fields in our database. The first field is id, and is called the &#039;primary key&#039;. The primary key of a database table is a field that is used to uniquely identify a record. This is often used to lookup rows in the database. The other field is greeting. This is the field that stores the greeting that is returned from the query that we used above.&lt;br /&gt;
&lt;br /&gt;
We will save our queries in a file called install.utf.sql.&lt;br /&gt;
&lt;br /&gt;
=== Creating the Uninstall SQL File ===&lt;br /&gt;
&lt;br /&gt;
Though we might hope that people will never want to uninstall our component, it is important that if they do, we don&#039;t leave anything behind. Joomla! will look after deleting the files and directories that were created during install, but you must manually include queries that will remove and tables that have been added to the database. Since we have only created one table, we only need one query:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;DROP TABLE IF EXISTS `#__hello`;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will save this query in a file called uninstall.utf.sql.&lt;br /&gt;
&lt;br /&gt;
== Updating our Install File ==&lt;br /&gt;
&lt;br /&gt;
We need to change a few things in our install file. First, we need to add our two new files to the list of files to install. SQL install file have to go in the admin directory. Second, we need to tell the installer to execute the queries in our files on install and uninstall.&lt;br /&gt;
&lt;br /&gt;
Our new file looks like this:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;&lt;br /&gt;
&amp;lt;install type=&amp;quot;component&amp;quot; version=&amp;quot;1.5.0&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;name&amp;gt;Hello&amp;lt;/name&amp;gt;&lt;br /&gt;
 &amp;lt;!-- The following elements are optional and free of formatting conttraints --&amp;gt;&lt;br /&gt;
 &amp;lt;creationDate&amp;gt;2007-02-22&amp;lt;/creationDate&amp;gt;&lt;br /&gt;
 &amp;lt;author&amp;gt;John Doe&amp;lt;/author&amp;gt;&lt;br /&gt;
 &amp;lt;authorEmail&amp;gt;john.doe@example.org&amp;lt;/authorEmail&amp;gt;&lt;br /&gt;
 &amp;lt;authorUrl&amp;gt;http://www.example.org&amp;lt;/authorUrl&amp;gt;&lt;br /&gt;
 &amp;lt;copyright&amp;gt;Copyright Info&amp;lt;/copyright&amp;gt;&lt;br /&gt;
 &amp;lt;license&amp;gt;License Info&amp;lt;/license&amp;gt;&lt;br /&gt;
 &amp;lt;!--  The version string is recorded in the components table --&amp;gt;&lt;br /&gt;
 &amp;lt;version&amp;gt;3.01&amp;lt;/version&amp;gt;&lt;br /&gt;
 &amp;lt;!-- The description is optional and defaults to the name --&amp;gt;&lt;br /&gt;
 &amp;lt;description&amp;gt;Description of the component ...&amp;lt;/description&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;!-- Site Main File Copy Section --&amp;gt;&lt;br /&gt;
 &amp;lt;!-- Note the folder attribute: This attribute describes the folder&lt;br /&gt;
      to copy FROM in the package to install therefore files copied&lt;br /&gt;
      in this section are copied from /site/ in the package --&amp;gt;&lt;br /&gt;
 &amp;lt;files folder=&amp;quot;site&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;controller.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;models/hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;models/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/view.html.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/tmpl/default.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/tmpl/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
 &amp;lt;/files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;install&amp;gt;&lt;br /&gt;
  &amp;lt;sql&amp;gt;&lt;br /&gt;
   &amp;lt;file charset=&amp;quot;utf8&amp;quot; driver=&amp;quot;mysql&amp;quot;&amp;gt;install.sql&amp;lt;/file&amp;gt;&lt;br /&gt;
  &amp;lt;/sql&amp;gt;&lt;br /&gt;
 &amp;lt;/install&amp;gt;&lt;br /&gt;
 &amp;lt;uninstall&amp;gt;&lt;br /&gt;
  &amp;lt;sql&amp;gt;&lt;br /&gt;
   &amp;lt;file charset=&amp;quot;utf8&amp;quot; driver=&amp;quot;mysql&amp;quot;&amp;gt;uninstall.sql&amp;lt;/file&amp;gt;&lt;br /&gt;
  &amp;lt;/sql&amp;gt;&lt;br /&gt;
 &amp;lt;/uninstall&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;administration&amp;gt;&lt;br /&gt;
  &amp;lt;!-- Administration Menu Section --&amp;gt;&lt;br /&gt;
  &amp;lt;menu&amp;gt;Hello World!&amp;lt;/menu&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;!-- Administration Main File Copy Section --&amp;gt;&lt;br /&gt;
  &amp;lt;files folder=&amp;quot;admin&amp;quot;&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;install.sql&amp;lt;/filename&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;uninstall.sql&amp;lt;/filename&amp;gt;&lt;br /&gt;
&amp;lt;/files&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
 &amp;lt;/administration&amp;gt;&lt;br /&gt;
&amp;lt;/install&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You will notice two attributes present on the &amp;lt;file&amp;gt; tags within the &amp;lt;install&amp;gt; and &amp;lt;uninstall&amp;gt; sections: charset and driver. The charset is the type of charset to use. The only valid charset is utf8. If you want to create install files for non-utf8 databases (for older version of MySQL), you should omit this attribute.&lt;br /&gt;
&lt;br /&gt;
The driver attribute specifies which database the queries were written for. Currently, this can only be mysql, but in future versions of Joomla! there may be more database drivers available.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
We now have a component that takes advantage of both the Joomla! MVC framework classes and the JDatabase classes. You are now able to write MVC components that interact with the database and can use the Joomla! installer to create and populate database tables.&lt;br /&gt;
&lt;br /&gt;
== Contributors ==&lt;br /&gt;
* staalanden&lt;br /&gt;
&lt;br /&gt;
== Download ==&lt;br /&gt;
&lt;br /&gt;
The component can be downloaded at: [http://joomlacode.org/gf/download/frsrelease/8104/29402/com_hello3_01.zip com_hello3_01]&lt;/div&gt;</summary>
		<author><name>Tonie</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Creating_an_Administrator_Interface&amp;diff=9499</id>
		<title>J1.5:Developing a MVC Component/Creating an Administrator Interface</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Creating_an_Administrator_Interface&amp;diff=9499"/>
		<updated>2008-07-27T18:35:05Z</updated>

		<summary type="html">&lt;p&gt;Tonie: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
In the first three tutorials, we have developed a MVC component that retrieves its data from a table in the database. Currently, there is no way to add data to the database except to do it manually using another tool. In this tutorial, we will develop an administrator section for our component which will make it possible to manage the entries in the database.&lt;br /&gt;
&lt;br /&gt;
== Creating the Basic Framework ==&lt;br /&gt;
&lt;br /&gt;
The basic framework of the administrator panel is very similar to the site portion. The main entry point for the administrator section of the component is admin.hello.php. This file is identical to the hello.php file that was used in the site portion except the name of the controller it loads will be changed to HellosController. The default controller is also called controller.php and this file is identical to the default controller in the site portion, with the exception that the controller is named HellosController instead of HelloController. This difference is so that JController will by default load the hellos view, which will display a list of our greetings.&lt;br /&gt;
&lt;br /&gt;
Here is the listing for admin.hello.php:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://dev.joomla.org/component/option,com_jd-wiki/Itemid,31/id,tutorials:components/&lt;br /&gt;
 * @license    GNU/GPL&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
// no direct access&lt;br /&gt;
&lt;br /&gt;
defined( &#039;_JEXEC&#039; ) or die( &#039;Restricted access&#039; );&lt;br /&gt;
&lt;br /&gt;
// Require the base controller&lt;br /&gt;
&lt;br /&gt;
require_once( JPATH_COMPONENT.DS.&#039;controller.php&#039; );&lt;br /&gt;
&lt;br /&gt;
// Require specific controller if requested&lt;br /&gt;
if($controller = JRequest::getWord(&#039;controller&#039;)) {&lt;br /&gt;
    $path = JPATH_COMPONENT.DS.&#039;controllers&#039;.DS.$controller.&#039;.php&#039;;&lt;br /&gt;
    if (file_exists($path)) {&lt;br /&gt;
        require_once $path;&lt;br /&gt;
    } else {&lt;br /&gt;
        $controller = &#039;&#039;;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Create the controller&lt;br /&gt;
$classname    = &#039;HellosController&#039;.$controller;&lt;br /&gt;
$controller   = new $classname( );&lt;br /&gt;
&lt;br /&gt;
// Perform the Request task&lt;br /&gt;
$controller-&amp;gt;execute( JRequest::getVar( &#039;task&#039; ) );&lt;br /&gt;
&lt;br /&gt;
// Redirect if set by the controller&lt;br /&gt;
$controller-&amp;gt;redirect();&lt;br /&gt;
&lt;br /&gt;
?&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The view and model that we will start with is the hellos view and the hellos model. We will start with the model.&lt;br /&gt;
&lt;br /&gt;
==== The Hellos Model ====&lt;br /&gt;
&lt;br /&gt;
The Hellos Model will be very simple. The only operation that we currently need is the ability to retrieve the list of hellos from the database. This operation will be implemented in a method called getData().&lt;br /&gt;
&lt;br /&gt;
The JModel class has a built in protected method called _getList(). This method can be used to simplify the task of retrieving a list of records from the database. We simply need to pass it the query and it will return the list of records.&lt;br /&gt;
&lt;br /&gt;
At a later point in time, we might want to use our query from within another method. Therefore, we will create a private method called _buildQuery() which will return the query that will be passed to _getList(). This makes it easier to change the query as well since it is localized in one place.&lt;br /&gt;
&lt;br /&gt;
Therefore we need two methods in our class: getData() and _buildQuery().&lt;br /&gt;
&lt;br /&gt;
_buildQuery() simply returns the query. It looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source  lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Returns the query&lt;br /&gt;
 * @return string The query to be used to retrieve the rows from the database&lt;br /&gt;
 */&lt;br /&gt;
function _buildQuery()&lt;br /&gt;
{&lt;br /&gt;
    $query = &#039; SELECT * &#039;&lt;br /&gt;
           . &#039; FROM #__hello &#039;&lt;br /&gt;
    ;&lt;br /&gt;
&lt;br /&gt;
    return $query;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
getData() will obtain the query and retrieve the records from the database. Now it might happen that we need to retrieve this list of data twice in one page load. It would be a waste to have to query the database twice. Therefore, we will have this method store the data in a protected property so that on subsequent requests it can simply return the data it has already retrieved. This property will be called _data.&lt;br /&gt;
&lt;br /&gt;
Here is the getData() method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source  lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Retrieves the hello data&lt;br /&gt;
 * @return array Array of objects containing the data from the database&lt;br /&gt;
 */&lt;br /&gt;
function getData()&lt;br /&gt;
{&lt;br /&gt;
    // Lets load the data if it doesn&#039;t already exist&lt;br /&gt;
    if (empty( $this-&amp;gt;_data ))&lt;br /&gt;
    {&lt;br /&gt;
        $query = $this-&amp;gt;_buildQuery();&lt;br /&gt;
        $this-&amp;gt;_data = $this-&amp;gt;_getList( $query );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return $this-&amp;gt;_data;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The completed model looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * Hellos Model for Hello World Component&lt;br /&gt;
 * &lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://dev.joomla.org/component/option,com_jd-wiki/Itemid,31/id,tutorials:components/&lt;br /&gt;
 * @license        GNU/GPL&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// Check to ensure this file is included in Joomla!&lt;br /&gt;
defined(&#039;_JEXEC&#039;) or die();&lt;br /&gt;
&lt;br /&gt;
jimport( &#039;joomla.application.component.model&#039; );&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Hello Model&lt;br /&gt;
 *&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 */&lt;br /&gt;
class HellosModelHellos extends JModel&lt;br /&gt;
{&lt;br /&gt;
    /**&lt;br /&gt;
     * Hellos data array&lt;br /&gt;
     *&lt;br /&gt;
     * @var array&lt;br /&gt;
     */&lt;br /&gt;
    var $_data;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Returns the query&lt;br /&gt;
     * @return string The query to be used to retrieve the rows from the database&lt;br /&gt;
     */&lt;br /&gt;
    function _buildQuery()&lt;br /&gt;
    {&lt;br /&gt;
        $query = &#039; SELECT * &#039;&lt;br /&gt;
            . &#039; FROM #__hello &#039;&lt;br /&gt;
        ;&lt;br /&gt;
        return $query;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Retrieves the hello data&lt;br /&gt;
     * @return array Array of objects containing the data from the database&lt;br /&gt;
     */&lt;br /&gt;
    function getData()&lt;br /&gt;
    {&lt;br /&gt;
        // Lets load the data if it doesn&#039;t already exist&lt;br /&gt;
        if (empty( $this-&amp;gt;_data ))&lt;br /&gt;
        {&lt;br /&gt;
            $query = $this-&amp;gt;_buildQuery();&lt;br /&gt;
            $this-&amp;gt;_data = $this-&amp;gt;_getList( $query );&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        return $this-&amp;gt;_data;&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This file is saved as models/hellos.php.&lt;br /&gt;
&lt;br /&gt;
==== The Hellos View ====&lt;br /&gt;
&lt;br /&gt;
Now that we have a model to retrieve our data, we need to display it. This view will be fairly similar to the view from the site section as well.&lt;br /&gt;
&lt;br /&gt;
Just as our model was automatically instantiated in the site, so it is in the administrator. Methods that start with get in the model can be accessed using the get() method of the JView class. So our view has three lines: one to retrieve the data from the model, one to push the data into the template, and one to invoke the display method to display the output. Thus we have:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * Hellos View for Hello World Component&lt;br /&gt;
 * &lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://dev.joomla.org/component/option,com_jd-wiki/Itemid,31/id,tutorials:components/&lt;br /&gt;
 * @license        GNU/GPL&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// Check to ensure this file is included in Joomla!&lt;br /&gt;
defined(&#039;_JEXEC&#039;) or die();&lt;br /&gt;
&lt;br /&gt;
jimport( &#039;joomla.application.component.view&#039; );&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Hellos View&lt;br /&gt;
 *&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 */&lt;br /&gt;
class HellosViewHellos extends JView&lt;br /&gt;
{&lt;br /&gt;
    /**&lt;br /&gt;
     * Hellos view display method&lt;br /&gt;
     * @return void&lt;br /&gt;
     **/&lt;br /&gt;
    function display($tpl = null)&lt;br /&gt;
    {&lt;br /&gt;
        JToolBarHelper::title( JText::_( &#039;Hello Manager&#039; ), &#039;generic.png&#039; );&lt;br /&gt;
        JToolBarHelper::deleteList();&lt;br /&gt;
        JToolBarHelper::editListX();&lt;br /&gt;
        JToolBarHelper::addNewX();&lt;br /&gt;
&lt;br /&gt;
        // Get data from the model&lt;br /&gt;
        $items =&amp;amp; $this-&amp;gt;get( &#039;Data&#039;);&lt;br /&gt;
&lt;br /&gt;
        $this-&amp;gt;assignRef( &#039;items&#039;, $items );&lt;br /&gt;
&lt;br /&gt;
        parent::display($tpl);&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This file is saved as views/hellos/view.html.php.&lt;br /&gt;
&lt;br /&gt;
==== The Hellos Template ====&lt;br /&gt;
&lt;br /&gt;
The template will take the data pushed into it from the view and produce the output. We will display our output in a simple table. While the frontend template was very simple, in the administrator we will need a minimal amount of extra logic to handle looping through the data.&lt;br /&gt;
&lt;br /&gt;
Here is our template:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;); ?&amp;gt;&lt;br /&gt;
&amp;lt;form action=&amp;quot;index.php&amp;quot; method=&amp;quot;post&amp;quot; name=&amp;quot;adminForm&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;div id=&amp;quot;editcell&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;table class=&amp;quot;adminlist&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;thead&amp;gt;&lt;br /&gt;
        &amp;lt;tr&amp;gt;&lt;br /&gt;
            &amp;lt;th width=&amp;quot;5&amp;quot;&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo JText::_( &#039;ID&#039; ); ?&amp;gt;&lt;br /&gt;
            &amp;lt;/th&amp;gt;&lt;br /&gt;
            &amp;lt;th&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo JText::_( &#039;Greeting&#039; ); ?&amp;gt;&lt;br /&gt;
            &amp;lt;/th&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;            &lt;br /&gt;
    &amp;lt;/thead&amp;gt;&lt;br /&gt;
    &amp;lt;?php&lt;br /&gt;
    $k = 0;&lt;br /&gt;
    for ($i=0, $n=count( $this-&amp;gt;items ); $i &amp;lt; $n; $i++)&lt;br /&gt;
    {&lt;br /&gt;
        $row =&amp;amp; $this-&amp;gt;items[$i];&lt;br /&gt;
        ?&amp;gt;&lt;br /&gt;
        &amp;lt;tr class=&amp;quot;&amp;lt;?php echo &amp;quot;row$k&amp;quot;; ?&amp;gt;&amp;quot;&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo $row-&amp;gt;id; ?&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo $row-&amp;gt;greeting; ?&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;&lt;br /&gt;
        &amp;lt;?php&lt;br /&gt;
        $k = 1 - $k;&lt;br /&gt;
    }&lt;br /&gt;
    ?&amp;gt;&lt;br /&gt;
    &amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;option&amp;quot; value=&amp;quot;com_hello&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;task&amp;quot; value=&amp;quot;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;boxchecked&amp;quot; value=&amp;quot;0&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;controller&amp;quot; value=&amp;quot;hello&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/form&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This template is saved as views/hellos/tmpl/default.php.&lt;br /&gt;
&lt;br /&gt;
You will notice that our output is enclosed in a form. Though this is not necessary now, it will be soon.&lt;br /&gt;
&lt;br /&gt;
We have now completed the basic part of the first view. We have added five files to the admin section of our component:&lt;br /&gt;
&lt;br /&gt;
  * admin.hello.php&lt;br /&gt;
&lt;br /&gt;
  * controller.php&lt;br /&gt;
&lt;br /&gt;
  * models/hellos.php&lt;br /&gt;
&lt;br /&gt;
  * views/hellos/view.html.php&lt;br /&gt;
&lt;br /&gt;
  * views/hellos/tmpl/default.php&lt;br /&gt;
&lt;br /&gt;
You can now add these files to the XML install file and give it a try!&lt;br /&gt;
&lt;br /&gt;
== Adding Functionality ==&lt;br /&gt;
&lt;br /&gt;
So far our administrator section is pretty useless. It doesn&#039;t really do anything - all it does is display the entries that we have in our database.&lt;br /&gt;
&lt;br /&gt;
In order to make it useful, we need to add some buttons and links.&lt;br /&gt;
&lt;br /&gt;
==== The Toolbar ====&lt;br /&gt;
&lt;br /&gt;
You may have noticed the toolbar that appears at the top of other Joomla! component administrator panels. Our component needs one as well. Joomla! makes this very easy to do. We will add buttons Delete records, Edit records, and create New records. We will also add a title that will be displayed on our toolbar.&lt;br /&gt;
&lt;br /&gt;
This is done by adding code to the view. To add the buttons, we use static methods from the Joomla! JToolBarHelper class. The code looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;JToolBarHelper::title(   JText::_( &#039;Hello Manager&#039; ), &#039;generic.png&#039; );&lt;br /&gt;
JToolBarHelper::deleteList();&lt;br /&gt;
JToolBarHelper::editListX();&lt;br /&gt;
JToolBarHelper::addNewX();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
These three methods will create the appropriate buttons. The deleteList() method can optionally take up to three parameters - the first parameter is a string to display to the user to confirm that they want to delete the records. The second is the task that should be sent with the query (the default is &#039;remove&#039;), and the third is the text that should be displayed below the button.&lt;br /&gt;
&lt;br /&gt;
The editListX() and addNewX() methods can each take two optional parameters. The first is the task (which are by default edit and add, respectively), and the second is the text that should be displayed below the button.&lt;br /&gt;
&lt;br /&gt;
 &#039;&#039;*You may have noticed the use of the JText::_ method in the template before and here as well. This is a handy function that makes component translation much easier. The JText::_ method will look up the string in your component language file and return the translated string. If not translation is found, it will return the string that you passed it. If you want to translate your component into another language, all you have to do is create a language file that will map the strings within the quotes to the translated version of the string.&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==== Checkboxes and Links ====&lt;br /&gt;
&lt;br /&gt;
We now have buttons. Two of those buttons operate on existing records. But how do we know which records to operate on? We have to let the user tell us. To do this, we need to add checkboxes to our table so that the user can select certain records. This is done in our template.&lt;br /&gt;
&lt;br /&gt;
In order to the add the checkboxes, we need to add an extra column into our table. We will add the column in between the two that we already have.&lt;br /&gt;
&lt;br /&gt;
In the header of the column, we will add a checkbox which can be used to toggle all the boxes below it on or off:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;th width=&amp;quot;20&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;input type=&amp;quot;checkbox&amp;quot; name=&amp;quot;toggle&amp;quot; value=&amp;quot;&amp;quot; onclick=&amp;quot;checkAll(&amp;lt;?php echo count( $this-&amp;gt;items ); ?&amp;gt;);&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/th&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Javascript checkAll function is a function that is built into the Joomla! base Javascript package that provides the functionality that we want here.&lt;br /&gt;
&lt;br /&gt;
Now we need to add the checkboxes into the individual rows. Joomla!&#039;s JHTML class has a method, JHTML::_(), which will generate our checkbox for us. We will add the following line to our loop:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$checked    = JHTML::_( &#039;grid.id&#039;, $i, $row-&amp;gt;id );&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
after the line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$row =&amp;amp; $this-&amp;gt;items[$i];&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then we will add a cell in between the two that we already have:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;td&amp;gt;&lt;br /&gt;
    &amp;lt;?php echo $checked; ?&amp;gt;&lt;br /&gt;
&amp;lt;/td&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It can be cumbersome to have to check the box that we want to edit and then move up and click the edit button. Therefore, we will add a link that it will go straight to the greeting&#039;s edit form. We will add the following line after the call to the JHTML::_() method to generate the link HTML:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$link = JRoute::_( &#039;index.php?option=com_hello&amp;gt;controller=hello&amp;gt;task=edit&amp;gt;cid[]=&#039;. $row-&amp;gt;id );&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And we include the link in the cell showing the greeting text:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;td&amp;gt;&lt;br /&gt;
    &amp;lt;a href=&amp;quot;&amp;lt;?php echo $link; ?&amp;gt;&amp;quot;&amp;gt;&amp;lt;?php echo $row-&amp;gt;greeting; ?&amp;gt;&amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;lt;/td&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You will notice that this link points to the hello controller. This controller will handle the data manipulation of our greetings.&lt;br /&gt;
&lt;br /&gt;
If you recall from above, we had four hidden input fields at the bottom of our form. The first input field was named &#039;option&#039;. This field is necessary so that we stay in our component. The second input field was task. This form property gets set when one of the buttons in the toolbar is clicked. A Javascript error will result and the buttons will not work if this input field is omitted. The third input field is the boxchecked field. This field keeps track of the number of boxes that are checked. The edit and delete buttons will check to ensure that this is greater than zero and will not allow the form to be submitted if it is not. The fourth input field is the controller field. This is used to specify that tasks fired from this form will be handled by the hello controller.&lt;br /&gt;
&lt;br /&gt;
Here is the code for the completed default.php file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;); ?&amp;gt;&lt;br /&gt;
&amp;lt;form action=&amp;quot;index.php&amp;quot; method=&amp;quot;post&amp;quot; name=&amp;quot;adminForm&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;div id=&amp;quot;editcell&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;table class=&amp;quot;adminlist&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;thead&amp;gt;&lt;br /&gt;
        &amp;lt;tr&amp;gt;&lt;br /&gt;
            &amp;lt;th width=&amp;quot;5&amp;quot;&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo JText::_( &#039;ID&#039; ); ?&amp;gt;&lt;br /&gt;
            &amp;lt;/th&amp;gt;&lt;br /&gt;
            &amp;lt;th&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo JText::_( &#039;Greeting&#039; ); ?&amp;gt;&lt;br /&gt;
            &amp;lt;/th&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;            &lt;br /&gt;
    &amp;lt;/thead&amp;gt;&lt;br /&gt;
    &amp;lt;?php&lt;br /&gt;
    $k = 0;&lt;br /&gt;
    for ($i=0, $n=count( $this-&amp;gt;items ); $i &amp;lt; $n; $i++)&lt;br /&gt;
    {&lt;br /&gt;
        $row =&amp;amp; $this-&amp;gt;items[$i];&lt;br /&gt;
        ?&amp;gt;&lt;br /&gt;
        &amp;lt;tr class=&amp;quot;&amp;lt;?php echo &amp;quot;row$k&amp;quot;; ?&amp;gt;&amp;quot;&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo $row-&amp;gt;id; ?&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo $row-&amp;gt;greeting; ?&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;&lt;br /&gt;
        &amp;lt;?php&lt;br /&gt;
        $k = 1 - $k;&lt;br /&gt;
    }&lt;br /&gt;
    ?&amp;gt;&lt;br /&gt;
    &amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;option&amp;quot; value=&amp;quot;com_hello&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;task&amp;quot; value=&amp;quot;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;boxchecked&amp;quot; value=&amp;quot;0&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;controller&amp;quot; value=&amp;quot;hello&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/form&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Our hellos view is now complete. You can try out the component now to see the results.&lt;br /&gt;
&lt;br /&gt;
The component can be downloaded at: [[http://dev.joomla.org/components/com_jd-wiki/data/media/components/com_hello4a.zip|com_hello4a.zip]]&lt;br /&gt;
&lt;br /&gt;
== Getting Down and Dirty: Doing the Real Work ==&lt;br /&gt;
&lt;br /&gt;
Now that the Hellos view is done, it is time to move to the Hello view and model. This is where the real work will get done.&lt;br /&gt;
&lt;br /&gt;
==== The Hello Controller ====&lt;br /&gt;
&lt;br /&gt;
Our default controller just isn&#039;t going to cut it when it comes to doing work - all it is capable of doing is displaying views.&lt;br /&gt;
&lt;br /&gt;
We need to be able to handle the tasks that we are launching from the Hellos view: add, edit and remove.&lt;br /&gt;
&lt;br /&gt;
Add and edit are essentially the same task: they both display a form to the user that allows a greeting to be edited. The only difference is that new displays a blank form, and edit displays a form with data already in it. Since they are similar, we will map the add task onto the edit task handler. This is specified in our constructor:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * constructor (registers additional tasks to methods)&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function __construct()&lt;br /&gt;
{&lt;br /&gt;
    parent::__construct();&lt;br /&gt;
&lt;br /&gt;
    // Register Extra tasks&lt;br /&gt;
    $this-&amp;gt;registerTask( &#039;add&#039;  ,     &#039;edit&#039; );&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first parameter of JController::registerTask is the task to map, and the second is the method to map it to.&lt;br /&gt;
&lt;br /&gt;
We will start with handling the edit task. The controller&#039;s job is fairly simple for the edit task. All it has to do is specify the view and layout to load (the hello view and the form layout). We will also tell Joomla! to disable the mainmenu while we are editing our greeting. This prevents users from leaving unsaved records open.&lt;br /&gt;
&lt;br /&gt;
Our edit task handler looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * display the edit form&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function edit()&lt;br /&gt;
{&lt;br /&gt;
    JRequest::setVar( &#039;view&#039;, &#039;hello&#039; );&lt;br /&gt;
    JRequest::setVar( &#039;layout&#039;, &#039;form&#039;  );&lt;br /&gt;
    JRequest::setVar(&#039;hidemainmenu&#039;, 1);&lt;br /&gt;
&lt;br /&gt;
    parent::display();&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== The Hello View ====&lt;br /&gt;
&lt;br /&gt;
The Hello view will display a form which will allow the user to edit a greeting. The display method if the hello view has to do a few simple tasks:&lt;br /&gt;
&lt;br /&gt;
  * retrieve the data from the model&lt;br /&gt;
&lt;br /&gt;
  * create the toolbar&lt;br /&gt;
&lt;br /&gt;
  * pass the data into the template&lt;br /&gt;
&lt;br /&gt;
  * invoke the display() method to render the template&lt;br /&gt;
&lt;br /&gt;
This becomes a bit more complicated because the one view handles both the edit and add tasks. In our toolbar we want the user to know what whether they are adding or editing, so we have to determine which task was fired.&lt;br /&gt;
&lt;br /&gt;
Since we are already retrieving the record that we want to display from the model, we can use this data to determine what task was fired. If the task was edit, then the id field of our record will have been set. If the task was new, then it will not have been set. This can be used to determine if we have a new record or an existing record.&lt;br /&gt;
&lt;br /&gt;
We will add two buttons to the toolbar: save and cancel. Though the functionality will be the same, we want to display different buttons depending on whether it is a new or existing record. If it is a new record, we will display cancel. If it already exists, we will display close.&lt;br /&gt;
&lt;br /&gt;
Thus our display method looks like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * display method of Hello view&lt;br /&gt;
 * @return void&lt;br /&gt;
 **/&lt;br /&gt;
function display($tpl = null)&lt;br /&gt;
{&lt;br /&gt;
    //get the hello&lt;br /&gt;
    $hello        =&amp;amp; $this-&amp;gt;get(&#039;Data&#039;);&lt;br /&gt;
    $isNew        = ($hello-&amp;gt;id &amp;lt; 1);&lt;br /&gt;
&lt;br /&gt;
    $text = $isNew ? JText::_( &#039;New&#039; ) : JText::_( &#039;Edit&#039; );&lt;br /&gt;
    JToolBarHelper::title(   JText::_( &#039;Hello&#039; ).&#039;: &amp;lt;small&amp;gt;&amp;lt;small&amp;gt;[ &#039; . $text.&#039; ]&amp;lt;/small&amp;gt;&amp;lt;/small&amp;gt;&#039; );&lt;br /&gt;
    JToolBarHelper::save();&lt;br /&gt;
    if ($isNew)  {&lt;br /&gt;
        JToolBarHelper::cancel();&lt;br /&gt;
    } else {&lt;br /&gt;
        // for existing items the button is renamed `close`&lt;br /&gt;
        JToolBarHelper::cancel( &#039;cancel&#039;, &#039;Close&#039; );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    $this-&amp;gt;assignRef(&#039;hello&#039;, $hello);&lt;br /&gt;
    parent::display($tpl);&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== The Hello Model ====&lt;br /&gt;
&lt;br /&gt;
Our view needs data. Therefore, we need to create a model to model a hello.&lt;br /&gt;
&lt;br /&gt;
Our model will have two properties: _id and _data. _id will hold the id of the greeting and data will hold the data.&lt;br /&gt;
&lt;br /&gt;
We will start with a constructor, which will attempt to retrieve the id from the query:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Constructor that retrieves the ID from the request&lt;br /&gt;
 *&lt;br /&gt;
 * @access    public&lt;br /&gt;
 * @return    void&lt;br /&gt;
 */&lt;br /&gt;
function __construct()&lt;br /&gt;
{&lt;br /&gt;
    parent::__construct();&lt;br /&gt;
&lt;br /&gt;
    $array = JRequest::getVar(&#039;cid&#039;,  0, &#039;&#039;, &#039;array&#039;);&lt;br /&gt;
    $this-&amp;gt;setId((int)$array[0]);&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The JRequest::getVar() method is used to retrieve data from the request. The first parameter is the name of the form variable. The second parameter is the default value to assign if there is no value found. The third parameter is the name of the hash to retrieve the value from (get, post, etc), and the last value is the data type that should be forced on the value.&lt;br /&gt;
&lt;br /&gt;
Our constructor will take the first value from the cid array and assign it to the id.&lt;br /&gt;
&lt;br /&gt;
Our setId() method can be used to set our id. Changing the id that our model points to will mean the id points to the wrong data. Therefore, when we set the id, we will clear the data property:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Method to set the hello identifier&lt;br /&gt;
 *&lt;br /&gt;
 * @access    public&lt;br /&gt;
 * @param    int Hello identifier&lt;br /&gt;
 * @return    void&lt;br /&gt;
 */&lt;br /&gt;
function setId($id)&lt;br /&gt;
{&lt;br /&gt;
    // Set id and wipe data&lt;br /&gt;
    $this-&amp;gt;_id        = $id;&lt;br /&gt;
    $this-&amp;gt;_data    = null;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finally, we need a method to retrieve our data: getData()&lt;br /&gt;
&lt;br /&gt;
getData will check if the _data property has already been set. If it has, it will simply return it. Otherwise, it will load the data from the database.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Method to get a hello&lt;br /&gt;
 * @return object with data&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
function &amp;amp;getData()&lt;br /&gt;
{&lt;br /&gt;
    // Load the data&lt;br /&gt;
    if (empty( $this-&amp;gt;_data )) {&lt;br /&gt;
        $query = &#039; SELECT * FROM #__hello &#039;.&lt;br /&gt;
                &#039;  WHERE id = &#039;.$this-&amp;gt;_id;&lt;br /&gt;
        $this-&amp;gt;_db-&amp;gt;setQuery( $query );&lt;br /&gt;
        $this-&amp;gt;_data = $this-&amp;gt;_db-&amp;gt;loadObject();&lt;br /&gt;
    }&lt;br /&gt;
    if (!$this-&amp;gt;_data) {&lt;br /&gt;
        $this-&amp;gt;_data = new stdClass();&lt;br /&gt;
        $this-&amp;gt;_data-&amp;gt;id = 0;&lt;br /&gt;
        $this-&amp;gt;_data-&amp;gt;greeting = null;&lt;br /&gt;
    }&lt;br /&gt;
    return $this-&amp;gt;_data;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== The Form ====&lt;br /&gt;
&lt;br /&gt;
Now all that is left is to create the form that the data will go into. Since we specified our layout as form, the form will go in a file in the tmpl directory of the hello view called form.php:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;); ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;form action=&amp;quot;index.php&amp;quot; method=&amp;quot;post&amp;quot; name=&amp;quot;adminForm&amp;quot; id=&amp;quot;adminForm&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;div class=&amp;quot;col100&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;fieldset class=&amp;quot;adminform&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;legend&amp;gt;&amp;lt;?php echo JText::_( &#039;Details&#039; ); ?&amp;gt;&amp;lt;/legend&amp;gt;&lt;br /&gt;
        &amp;lt;table class=&amp;quot;admintable&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;tr&amp;gt;&lt;br /&gt;
            &amp;lt;td width=&amp;quot;100&amp;quot; align=&amp;quot;right&amp;quot; class=&amp;quot;key&amp;quot;&amp;gt;&lt;br /&gt;
                &amp;lt;label for=&amp;quot;greeting&amp;quot;&amp;gt;&lt;br /&gt;
                    &amp;lt;?php echo JText::_( &#039;Greeting&#039; ); ?&amp;gt;:&lt;br /&gt;
                &amp;lt;/label&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;input class=&amp;quot;text_area&amp;quot; type=&amp;quot;text&amp;quot; name=&amp;quot;greeting&amp;quot; id=&amp;quot;greeting&amp;quot; size=&amp;quot;32&amp;quot; maxlength=&amp;quot;250&amp;quot; value=&amp;quot;&amp;lt;?php echo $this-&amp;gt;hello-&amp;gt;greeting;?&amp;gt;&amp;quot; /&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;&lt;br /&gt;
    &amp;lt;/table&amp;gt;&lt;br /&gt;
    &amp;lt;/fieldset&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;clr&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;option&amp;quot; value=&amp;quot;com_hello&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;id&amp;quot; value=&amp;quot;&amp;lt;?php echo $this-&amp;gt;hello-&amp;gt;id; ?&amp;gt;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;task&amp;quot; value=&amp;quot;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;controller&amp;quot; value=&amp;quot;hello&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/form&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in addition to the input field, there is a hidden field for the id. The user doesn&#039;t need to edit the id (and shouldn&#039;t), so we silently pass it along in the form.&lt;br /&gt;
&lt;br /&gt;
==== Implementing the Functionality ====&lt;br /&gt;
&lt;br /&gt;
So far, our controller only handles two tasks: edit and new. However, we also have buttons to save, delete and cancel records. We need to write code to handle and perform these tasks.&lt;br /&gt;
&lt;br /&gt;
=== Saving a Record ===&lt;br /&gt;
&lt;br /&gt;
The logical next step is to implement the functionality to save a record. Normally, this would require some switches and logic to handle various cases, such as the difference between creating a new record (an INSERT query), and updating an existing query (an UPDATE query). Also, there are complexities involved in getting the data from the form and putting it into the query.&lt;br /&gt;
&lt;br /&gt;
The Joomla! framework takes care of a lot of this for you. The JTable class makes it easy to manipulate records in the database without having to worry about writing the SQL code that lies behind these updates. It also makes it easy to transfer data from an HTML form into the database.&lt;br /&gt;
&lt;br /&gt;
== Creating the Table Class ==&lt;br /&gt;
&lt;br /&gt;
The JTable class is an abstract class from which you can derive child classes to work with specific tables. To use it, you simply create a class that extends the JTable class, add your database fields as properties, and override the constructor to specify the name of the table and the primary key.&lt;br /&gt;
&lt;br /&gt;
Here is our JTable class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * Hello World table class&lt;br /&gt;
 * &lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://dev.joomla.org/component/option,com_jd-wiki/Itemid,31/id,tutorials:components/&lt;br /&gt;
 * @license        GNU/GPL&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// no direct access&lt;br /&gt;
defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;);&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Hello Table class&lt;br /&gt;
 *&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 */&lt;br /&gt;
class TableHello extends JTable&lt;br /&gt;
{&lt;br /&gt;
    /**&lt;br /&gt;
     * Primary Key&lt;br /&gt;
     *&lt;br /&gt;
     * @var int&lt;br /&gt;
     */&lt;br /&gt;
    var $id = null;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * @var string&lt;br /&gt;
     */&lt;br /&gt;
    var $greeting = null;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Constructor&lt;br /&gt;
     *&lt;br /&gt;
     * @param object Database connector object&lt;br /&gt;
     */&lt;br /&gt;
    function TableHello( &amp;amp;$db ) {&lt;br /&gt;
        parent::__construct(&#039;#__hello&#039;, &#039;id&#039;, $db);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You will see here that we have defined our two fields: the id field and the greeting field. Then we have defined a constructor, which will call the constructor of the parent class and pass it the name of the table (hello), the name of the field which is the primary key (id), and the database connector object.&lt;br /&gt;
&lt;br /&gt;
This file should be called hello.php and it will go in a directory called tables in the administrator section of our component.&lt;br /&gt;
&lt;br /&gt;
== Implementing the Function in our Model ==&lt;br /&gt;
&lt;br /&gt;
We are now ready to add the method to the model which will save our record. We will call this method store. Our store() method will do three things: bind the data from the form to the TableHello object, check to ensure that the record is properly formed, and store the record in the database.&lt;br /&gt;
&lt;br /&gt;
Our method looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Method to store a record&lt;br /&gt;
 *&lt;br /&gt;
 * @access    public&lt;br /&gt;
 * @return    boolean    True on success&lt;br /&gt;
 */&lt;br /&gt;
function store()&lt;br /&gt;
{&lt;br /&gt;
    $row =&amp;amp; $this-&amp;gt;getTable();&lt;br /&gt;
&lt;br /&gt;
    $data = JRequest::get( &#039;post&#039; );&lt;br /&gt;
    // Bind the form fields to the hello table&lt;br /&gt;
    if (!$row-&amp;gt;bind($data)) {&lt;br /&gt;
        $this-&amp;gt;setError($this-&amp;gt;_db-&amp;gt;getErrorMsg());&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // Make sure the hello record is valid&lt;br /&gt;
    if (!$row-&amp;gt;check()) {&lt;br /&gt;
        $this-&amp;gt;setError($this-&amp;gt;_db-&amp;gt;getErrorMsg());&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // Store the web link table to the database&lt;br /&gt;
    if (!$row-&amp;gt;store()) {&lt;br /&gt;
        $this-&amp;gt;setError($this-&amp;gt;_db-&amp;gt;getErrorMsg());&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return true;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This method gets added to the hello model.&lt;br /&gt;
&lt;br /&gt;
The method takes one parameter, which is an associative array of data that we want to store in the database. This can easily be retrieved from the request as will be seen later.&lt;br /&gt;
&lt;br /&gt;
You will see that the first line retrieves a reference to our JTable object. If we name our table properly, we don&#039;t have to specify its name - the JModel class knows where to find it. You may recall that we called our table class TableHello and put it in a file called hello.php in the tables directory. If you follow this convention, the JModel class can create your object automatically.&lt;br /&gt;
&lt;br /&gt;
The second line will retrieve the data from the form. The JRequest class makes this very easy. In this case, we are retrieving all of the variables that were submitted using the &#039;POST&#039; method. These will be returned as an associative array.&lt;br /&gt;
&lt;br /&gt;
The rest is easy - we bind, check and store. The bind() method will copy values from the array into the corresponding property of the table object. In this case, it will take the values of id and greeting and copy them to our TableHello object.&lt;br /&gt;
&lt;br /&gt;
The check() method will perform data verification. In the JTable() class, this method simply returns true. While this doesn&#039;t provide any value for us currently, by calling this method we make it possible to do data checking using our TableHello class in the future. This method can be overridden in our TableHello class with a method that performs the appropriate checks.&lt;br /&gt;
&lt;br /&gt;
The store() method will take the data that is in the object and store it in the database. If the id is 0, it will create a new record (INSERT), otherwise, it will update the existing record (UPDATE).&lt;br /&gt;
&lt;br /&gt;
== Adding the Task to the Controller ==&lt;br /&gt;
&lt;br /&gt;
We are now ready to add our task to the controller. Since the task that we are firing is called &#039;save&#039;, we must call our method &#039;save&#039;. This is simple:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * save a record (and redirect to main page)&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function save()&lt;br /&gt;
{&lt;br /&gt;
    $model = $this-&amp;gt;getModel(&#039;hello&#039;);&lt;br /&gt;
&lt;br /&gt;
    if ($model-&amp;gt;store()) {&lt;br /&gt;
        $msg = JText::_( &#039;Greeting Saved!&#039; );&lt;br /&gt;
    } else {&lt;br /&gt;
        $msg = JText::_( &#039;Error Saving Greeting&#039; );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // Check the table in so it can be edited.... we are done with it anyway&lt;br /&gt;
    $link = &#039;index.php?option=com_hello&#039;;&lt;br /&gt;
    $this-&amp;gt;setRedirect($link, $msg);&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All we do is get our model and invoke the store() method. Then we use the setRedirect() method to redirect back to our list of greetings. We also pass a message along, which will be displayed at the top of the page.&lt;br /&gt;
&lt;br /&gt;
=== Deleting a Record ===&lt;br /&gt;
&lt;br /&gt;
=== Implementing the Function in the Model ===&lt;br /&gt;
&lt;br /&gt;
In the model, we will retrieve the list of IDs to delete and call the JTable class to delete them. Here it is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Method to delete record(s)&lt;br /&gt;
 *&lt;br /&gt;
 * @access    public&lt;br /&gt;
 * @return    boolean    True on success&lt;br /&gt;
 */&lt;br /&gt;
function delete()&lt;br /&gt;
{&lt;br /&gt;
    $cids = JRequest::getVar( &#039;cid&#039;, array(0), &#039;post&#039;, &#039;array&#039; );&lt;br /&gt;
    $row =&amp;amp; $this-&amp;gt;getTable();&lt;br /&gt;
&lt;br /&gt;
    foreach($cids as $cid) {&lt;br /&gt;
        if (!$row-&amp;gt;delete( $cid )) {&lt;br /&gt;
            $this-&amp;gt;setError( $row-&amp;gt;getErrorMsg() );&lt;br /&gt;
            return false;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return true;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We invoke the JRequest::getVar() method to get the data from the request, then we invoke the $row-&amp;gt;delete() method to delete each row. By storing errors in the model we make it possible to retrieve them later if we so choose.&lt;br /&gt;
&lt;br /&gt;
=== Handling the Remove Task in the Controller ===&lt;br /&gt;
&lt;br /&gt;
This is similar to the save() method which handled the save task:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * remove record(s)&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function remove()&lt;br /&gt;
{&lt;br /&gt;
    $model = $this-&amp;gt;getModel(&#039;hello&#039;);&lt;br /&gt;
    if(!$model-&amp;gt;delete()) {&lt;br /&gt;
        $msg = JText::_( &#039;Error: One or More Greetings Could not be Deleted&#039; );&lt;br /&gt;
    } else {&lt;br /&gt;
        $msg = JText::_( &#039;Greeting(s) Deleted&#039; );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    $this-&amp;gt;setRedirect( &#039;index.php?option=com_hello&#039;, $msg );&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Cancelling the Edit Operation ====&lt;br /&gt;
&lt;br /&gt;
To cancel the edit operation, all we have to do is redirect back to the main view:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * cancel editing a record&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function cancel()&lt;br /&gt;
{&lt;br /&gt;
    $msg = JText::_( &#039;Operation Cancelled&#039; );&lt;br /&gt;
    $this-&amp;gt;setRedirect( &#039;index.php?option=com_hello&#039;, $msg );&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
We have now implemented a basic backend to our component. We are now able to edit the entries that are viewed in the frontend. We have demonstrated the interaction between models, views and controllers. We have shown how the JTable class can be extended to provide easy access to tables in the database. It can also be seen how the JToolBarHelper class can be used to create button bars in components to present a standardized look between components.&lt;br /&gt;
&lt;br /&gt;
== Contributors ==&lt;br /&gt;
* staalanden&lt;br /&gt;
&lt;br /&gt;
== Download ==&lt;br /&gt;
&lt;br /&gt;
The component can be downloaded at: [http://joomlacode.org/gf/download/frsrelease/8104/29402/com_hello4_01.zip com_hello4_01]&lt;/div&gt;</summary>
		<author><name>Tonie</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Creating_an_Administrator_Interface&amp;diff=9498</id>
		<title>J1.5:Developing a MVC Component/Creating an Administrator Interface</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Creating_an_Administrator_Interface&amp;diff=9498"/>
		<updated>2008-07-27T18:32:10Z</updated>

		<summary type="html">&lt;p&gt;Tonie: /* Contributors */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;====== Developing a Model-View-Controller Component - Part 4 - Creating an Administrator Interface ======&lt;br /&gt;
&lt;br /&gt;
===== Introduction =====&lt;br /&gt;
&lt;br /&gt;
In the first three tutorials, we have developed a MVC component that retrieves its data from a table in the database. Currently, there is no way to add data to the database except to do it manually using another tool. In this tutorial, we will develop an administrator section for our component which will make it possible to manage the entries in the database.&lt;br /&gt;
&lt;br /&gt;
===== Creating the Basic Framework =====&lt;br /&gt;
&lt;br /&gt;
The basic framework of the administrator panel is very similar to the site portion. The main entry point for the administrator section of the component is admin.hello.php. This file is identical to the hello.php file that was used in the site portion except the name of the controller it loads will be changed to HellosController. The default controller is also called controller.php and this file is identical to the default controller in the site portion, with the exception that the controller is named HellosController instead of HelloController. This difference is so that JController will by default load the hellos view, which will display a list of our greetings.&lt;br /&gt;
&lt;br /&gt;
Here is the listing for admin.hello.php:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://dev.joomla.org/component/option,com_jd-wiki/Itemid,31/id,tutorials:components/&lt;br /&gt;
 * @license    GNU/GPL&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
// no direct access&lt;br /&gt;
&lt;br /&gt;
defined( &#039;_JEXEC&#039; ) or die( &#039;Restricted access&#039; );&lt;br /&gt;
&lt;br /&gt;
// Require the base controller&lt;br /&gt;
&lt;br /&gt;
require_once( JPATH_COMPONENT.DS.&#039;controller.php&#039; );&lt;br /&gt;
&lt;br /&gt;
// Require specific controller if requested&lt;br /&gt;
if($controller = JRequest::getWord(&#039;controller&#039;)) {&lt;br /&gt;
    $path = JPATH_COMPONENT.DS.&#039;controllers&#039;.DS.$controller.&#039;.php&#039;;&lt;br /&gt;
    if (file_exists($path)) {&lt;br /&gt;
        require_once $path;&lt;br /&gt;
    } else {&lt;br /&gt;
        $controller = &#039;&#039;;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Create the controller&lt;br /&gt;
$classname    = &#039;HellosController&#039;.$controller;&lt;br /&gt;
$controller   = new $classname( );&lt;br /&gt;
&lt;br /&gt;
// Perform the Request task&lt;br /&gt;
$controller-&amp;gt;execute( JRequest::getVar( &#039;task&#039; ) );&lt;br /&gt;
&lt;br /&gt;
// Redirect if set by the controller&lt;br /&gt;
$controller-&amp;gt;redirect();&lt;br /&gt;
&lt;br /&gt;
?&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The view and model that we will start with is the hellos view and the hellos model. We will start with the model.&lt;br /&gt;
&lt;br /&gt;
==== The Hellos Model ====&lt;br /&gt;
&lt;br /&gt;
The Hellos Model will be very simple. The only operation that we currently need is the ability to retrieve the list of hellos from the database. This operation will be implemented in a method called getData().&lt;br /&gt;
&lt;br /&gt;
The JModel class has a built in protected method called _getList(). This method can be used to simplify the task of retrieving a list of records from the database. We simply need to pass it the query and it will return the list of records.&lt;br /&gt;
&lt;br /&gt;
At a later point in time, we might want to use our query from within another method. Therefore, we will create a private method called _buildQuery() which will return the query that will be passed to _getList(). This makes it easier to change the query as well since it is localized in one place.&lt;br /&gt;
&lt;br /&gt;
Therefore we need two methods in our class: getData() and _buildQuery().&lt;br /&gt;
&lt;br /&gt;
_buildQuery() simply returns the query. It looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source  lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Returns the query&lt;br /&gt;
 * @return string The query to be used to retrieve the rows from the database&lt;br /&gt;
 */&lt;br /&gt;
function _buildQuery()&lt;br /&gt;
{&lt;br /&gt;
    $query = &#039; SELECT * &#039;&lt;br /&gt;
           . &#039; FROM #__hello &#039;&lt;br /&gt;
    ;&lt;br /&gt;
&lt;br /&gt;
    return $query;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
getData() will obtain the query and retrieve the records from the database. Now it might happen that we need to retrieve this list of data twice in one page load. It would be a waste to have to query the database twice. Therefore, we will have this method store the data in a protected property so that on subsequent requests it can simply return the data it has already retrieved. This property will be called _data.&lt;br /&gt;
&lt;br /&gt;
Here is the getData() method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source  lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Retrieves the hello data&lt;br /&gt;
 * @return array Array of objects containing the data from the database&lt;br /&gt;
 */&lt;br /&gt;
function getData()&lt;br /&gt;
{&lt;br /&gt;
    // Lets load the data if it doesn&#039;t already exist&lt;br /&gt;
    if (empty( $this-&amp;gt;_data ))&lt;br /&gt;
    {&lt;br /&gt;
        $query = $this-&amp;gt;_buildQuery();&lt;br /&gt;
        $this-&amp;gt;_data = $this-&amp;gt;_getList( $query );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return $this-&amp;gt;_data;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The completed model looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * Hellos Model for Hello World Component&lt;br /&gt;
 * &lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://dev.joomla.org/component/option,com_jd-wiki/Itemid,31/id,tutorials:components/&lt;br /&gt;
 * @license        GNU/GPL&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// Check to ensure this file is included in Joomla!&lt;br /&gt;
defined(&#039;_JEXEC&#039;) or die();&lt;br /&gt;
&lt;br /&gt;
jimport( &#039;joomla.application.component.model&#039; );&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Hello Model&lt;br /&gt;
 *&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 */&lt;br /&gt;
class HellosModelHellos extends JModel&lt;br /&gt;
{&lt;br /&gt;
    /**&lt;br /&gt;
     * Hellos data array&lt;br /&gt;
     *&lt;br /&gt;
     * @var array&lt;br /&gt;
     */&lt;br /&gt;
    var $_data;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Returns the query&lt;br /&gt;
     * @return string The query to be used to retrieve the rows from the database&lt;br /&gt;
     */&lt;br /&gt;
    function _buildQuery()&lt;br /&gt;
    {&lt;br /&gt;
        $query = &#039; SELECT * &#039;&lt;br /&gt;
            . &#039; FROM #__hello &#039;&lt;br /&gt;
        ;&lt;br /&gt;
        return $query;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Retrieves the hello data&lt;br /&gt;
     * @return array Array of objects containing the data from the database&lt;br /&gt;
     */&lt;br /&gt;
    function getData()&lt;br /&gt;
    {&lt;br /&gt;
        // Lets load the data if it doesn&#039;t already exist&lt;br /&gt;
        if (empty( $this-&amp;gt;_data ))&lt;br /&gt;
        {&lt;br /&gt;
            $query = $this-&amp;gt;_buildQuery();&lt;br /&gt;
            $this-&amp;gt;_data = $this-&amp;gt;_getList( $query );&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        return $this-&amp;gt;_data;&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This file is saved as models/hellos.php.&lt;br /&gt;
&lt;br /&gt;
==== The Hellos View ====&lt;br /&gt;
&lt;br /&gt;
Now that we have a model to retrieve our data, we need to display it. This view will be fairly similar to the view from the site section as well.&lt;br /&gt;
&lt;br /&gt;
Just as our model was automatically instantiated in the site, so it is in the administrator. Methods that start with get in the model can be accessed using the get() method of the JView class. So our view has three lines: one to retrieve the data from the model, one to push the data into the template, and one to invoke the display method to display the output. Thus we have:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * Hellos View for Hello World Component&lt;br /&gt;
 * &lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://dev.joomla.org/component/option,com_jd-wiki/Itemid,31/id,tutorials:components/&lt;br /&gt;
 * @license        GNU/GPL&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// Check to ensure this file is included in Joomla!&lt;br /&gt;
defined(&#039;_JEXEC&#039;) or die();&lt;br /&gt;
&lt;br /&gt;
jimport( &#039;joomla.application.component.view&#039; );&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Hellos View&lt;br /&gt;
 *&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 */&lt;br /&gt;
class HellosViewHellos extends JView&lt;br /&gt;
{&lt;br /&gt;
    /**&lt;br /&gt;
     * Hellos view display method&lt;br /&gt;
     * @return void&lt;br /&gt;
     **/&lt;br /&gt;
    function display($tpl = null)&lt;br /&gt;
    {&lt;br /&gt;
        JToolBarHelper::title( JText::_( &#039;Hello Manager&#039; ), &#039;generic.png&#039; );&lt;br /&gt;
        JToolBarHelper::deleteList();&lt;br /&gt;
        JToolBarHelper::editListX();&lt;br /&gt;
        JToolBarHelper::addNewX();&lt;br /&gt;
&lt;br /&gt;
        // Get data from the model&lt;br /&gt;
        $items =&amp;amp; $this-&amp;gt;get( &#039;Data&#039;);&lt;br /&gt;
&lt;br /&gt;
        $this-&amp;gt;assignRef( &#039;items&#039;, $items );&lt;br /&gt;
&lt;br /&gt;
        parent::display($tpl);&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This file is saved as views/hellos/view.html.php.&lt;br /&gt;
&lt;br /&gt;
==== The Hellos Template ====&lt;br /&gt;
&lt;br /&gt;
The template will take the data pushed into it from the view and produce the output. We will display our output in a simple table. While the frontend template was very simple, in the administrator we will need a minimal amount of extra logic to handle looping through the data.&lt;br /&gt;
&lt;br /&gt;
Here is our template:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;); ?&amp;gt;&lt;br /&gt;
&amp;lt;form action=&amp;quot;index.php&amp;quot; method=&amp;quot;post&amp;quot; name=&amp;quot;adminForm&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;div id=&amp;quot;editcell&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;table class=&amp;quot;adminlist&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;thead&amp;gt;&lt;br /&gt;
        &amp;lt;tr&amp;gt;&lt;br /&gt;
            &amp;lt;th width=&amp;quot;5&amp;quot;&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo JText::_( &#039;ID&#039; ); ?&amp;gt;&lt;br /&gt;
            &amp;lt;/th&amp;gt;&lt;br /&gt;
            &amp;lt;th&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo JText::_( &#039;Greeting&#039; ); ?&amp;gt;&lt;br /&gt;
            &amp;lt;/th&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;            &lt;br /&gt;
    &amp;lt;/thead&amp;gt;&lt;br /&gt;
    &amp;lt;?php&lt;br /&gt;
    $k = 0;&lt;br /&gt;
    for ($i=0, $n=count( $this-&amp;gt;items ); $i &amp;lt; $n; $i++)&lt;br /&gt;
    {&lt;br /&gt;
        $row =&amp;amp; $this-&amp;gt;items[$i];&lt;br /&gt;
        ?&amp;gt;&lt;br /&gt;
        &amp;lt;tr class=&amp;quot;&amp;lt;?php echo &amp;quot;row$k&amp;quot;; ?&amp;gt;&amp;quot;&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo $row-&amp;gt;id; ?&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo $row-&amp;gt;greeting; ?&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;&lt;br /&gt;
        &amp;lt;?php&lt;br /&gt;
        $k = 1 - $k;&lt;br /&gt;
    }&lt;br /&gt;
    ?&amp;gt;&lt;br /&gt;
    &amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;option&amp;quot; value=&amp;quot;com_hello&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;task&amp;quot; value=&amp;quot;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;boxchecked&amp;quot; value=&amp;quot;0&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;controller&amp;quot; value=&amp;quot;hello&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/form&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This template is saved as views/hellos/tmpl/default.php.&lt;br /&gt;
&lt;br /&gt;
You will notice that our output is enclosed in a form. Though this is not necessary now, it will be soon.&lt;br /&gt;
&lt;br /&gt;
We have now completed the basic part of the first view. We have added five files to the admin section of our component:&lt;br /&gt;
&lt;br /&gt;
  * admin.hello.php&lt;br /&gt;
&lt;br /&gt;
  * controller.php&lt;br /&gt;
&lt;br /&gt;
  * models/hellos.php&lt;br /&gt;
&lt;br /&gt;
  * views/hellos/view.html.php&lt;br /&gt;
&lt;br /&gt;
  * views/hellos/tmpl/default.php&lt;br /&gt;
&lt;br /&gt;
You can now add these files to the XML install file and give it a try!&lt;br /&gt;
&lt;br /&gt;
===== Adding Functionality =====&lt;br /&gt;
&lt;br /&gt;
So far our administrator section is pretty useless. It doesn&#039;t really do anything - all it does is display the entries that we have in our database.&lt;br /&gt;
&lt;br /&gt;
In order to make it useful, we need to add some buttons and links.&lt;br /&gt;
&lt;br /&gt;
==== The Toolbar ====&lt;br /&gt;
&lt;br /&gt;
You may have noticed the toolbar that appears at the top of other Joomla! component administrator panels. Our component needs one as well. Joomla! makes this very easy to do. We will add buttons Delete records, Edit records, and create New records. We will also add a title that will be displayed on our toolbar.&lt;br /&gt;
&lt;br /&gt;
This is done by adding code to the view. To add the buttons, we use static methods from the Joomla! JToolBarHelper class. The code looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;JToolBarHelper::title(   JText::_( &#039;Hello Manager&#039; ), &#039;generic.png&#039; );&lt;br /&gt;
JToolBarHelper::deleteList();&lt;br /&gt;
JToolBarHelper::editListX();&lt;br /&gt;
JToolBarHelper::addNewX();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
These three methods will create the appropriate buttons. The deleteList() method can optionally take up to three parameters - the first parameter is a string to display to the user to confirm that they want to delete the records. The second is the task that should be sent with the query (the default is &#039;remove&#039;), and the third is the text that should be displayed below the button.&lt;br /&gt;
&lt;br /&gt;
The editListX() and addNewX() methods can each take two optional parameters. The first is the task (which are by default edit and add, respectively), and the second is the text that should be displayed below the button.&lt;br /&gt;
&lt;br /&gt;
 &#039;&#039;*You may have noticed the use of the JText::_ method in the template before and here as well. This is a handy function that makes component translation much easier. The JText::_ method will look up the string in your component language file and return the translated string. If not translation is found, it will return the string that you passed it. If you want to translate your component into another language, all you have to do is create a language file that will map the strings within the quotes to the translated version of the string.&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==== Checkboxes and Links ====&lt;br /&gt;
&lt;br /&gt;
We now have buttons. Two of those buttons operate on existing records. But how do we know which records to operate on? We have to let the user tell us. To do this, we need to add checkboxes to our table so that the user can select certain records. This is done in our template.&lt;br /&gt;
&lt;br /&gt;
In order to the add the checkboxes, we need to add an extra column into our table. We will add the column in between the two that we already have.&lt;br /&gt;
&lt;br /&gt;
In the header of the column, we will add a checkbox which can be used to toggle all the boxes below it on or off:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;th width=&amp;quot;20&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;input type=&amp;quot;checkbox&amp;quot; name=&amp;quot;toggle&amp;quot; value=&amp;quot;&amp;quot; onclick=&amp;quot;checkAll(&amp;lt;?php echo count( $this-&amp;gt;items ); ?&amp;gt;);&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/th&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Javascript checkAll function is a function that is built into the Joomla! base Javascript package that provides the functionality that we want here.&lt;br /&gt;
&lt;br /&gt;
Now we need to add the checkboxes into the individual rows. Joomla!&#039;s JHTML class has a method, JHTML::_(), which will generate our checkbox for us. We will add the following line to our loop:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$checked    = JHTML::_( &#039;grid.id&#039;, $i, $row-&amp;gt;id );&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
after the line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$row =&amp;amp; $this-&amp;gt;items[$i];&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then we will add a cell in between the two that we already have:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;td&amp;gt;&lt;br /&gt;
    &amp;lt;?php echo $checked; ?&amp;gt;&lt;br /&gt;
&amp;lt;/td&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It can be cumbersome to have to check the box that we want to edit and then move up and click the edit button. Therefore, we will add a link that it will go straight to the greeting&#039;s edit form. We will add the following line after the call to the JHTML::_() method to generate the link HTML:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$link = JRoute::_( &#039;index.php?option=com_hello&amp;gt;controller=hello&amp;gt;task=edit&amp;gt;cid[]=&#039;. $row-&amp;gt;id );&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And we include the link in the cell showing the greeting text:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;td&amp;gt;&lt;br /&gt;
    &amp;lt;a href=&amp;quot;&amp;lt;?php echo $link; ?&amp;gt;&amp;quot;&amp;gt;&amp;lt;?php echo $row-&amp;gt;greeting; ?&amp;gt;&amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;lt;/td&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You will notice that this link points to the hello controller. This controller will handle the data manipulation of our greetings.&lt;br /&gt;
&lt;br /&gt;
If you recall from above, we had four hidden input fields at the bottom of our form. The first input field was named &#039;option&#039;. This field is necessary so that we stay in our component. The second input field was task. This form property gets set when one of the buttons in the toolbar is clicked. A Javascript error will result and the buttons will not work if this input field is omitted. The third input field is the boxchecked field. This field keeps track of the number of boxes that are checked. The edit and delete buttons will check to ensure that this is greater than zero and will not allow the form to be submitted if it is not. The fourth input field is the controller field. This is used to specify that tasks fired from this form will be handled by the hello controller.&lt;br /&gt;
&lt;br /&gt;
Here is the code for the completed default.php file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;); ?&amp;gt;&lt;br /&gt;
&amp;lt;form action=&amp;quot;index.php&amp;quot; method=&amp;quot;post&amp;quot; name=&amp;quot;adminForm&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;div id=&amp;quot;editcell&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;table class=&amp;quot;adminlist&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;thead&amp;gt;&lt;br /&gt;
        &amp;lt;tr&amp;gt;&lt;br /&gt;
            &amp;lt;th width=&amp;quot;5&amp;quot;&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo JText::_( &#039;ID&#039; ); ?&amp;gt;&lt;br /&gt;
            &amp;lt;/th&amp;gt;&lt;br /&gt;
            &amp;lt;th&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo JText::_( &#039;Greeting&#039; ); ?&amp;gt;&lt;br /&gt;
            &amp;lt;/th&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;            &lt;br /&gt;
    &amp;lt;/thead&amp;gt;&lt;br /&gt;
    &amp;lt;?php&lt;br /&gt;
    $k = 0;&lt;br /&gt;
    for ($i=0, $n=count( $this-&amp;gt;items ); $i &amp;lt; $n; $i++)&lt;br /&gt;
    {&lt;br /&gt;
        $row =&amp;amp; $this-&amp;gt;items[$i];&lt;br /&gt;
        ?&amp;gt;&lt;br /&gt;
        &amp;lt;tr class=&amp;quot;&amp;lt;?php echo &amp;quot;row$k&amp;quot;; ?&amp;gt;&amp;quot;&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo $row-&amp;gt;id; ?&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo $row-&amp;gt;greeting; ?&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;&lt;br /&gt;
        &amp;lt;?php&lt;br /&gt;
        $k = 1 - $k;&lt;br /&gt;
    }&lt;br /&gt;
    ?&amp;gt;&lt;br /&gt;
    &amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;option&amp;quot; value=&amp;quot;com_hello&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;task&amp;quot; value=&amp;quot;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;boxchecked&amp;quot; value=&amp;quot;0&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;controller&amp;quot; value=&amp;quot;hello&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/form&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Our hellos view is now complete. You can try out the component now to see the results.&lt;br /&gt;
&lt;br /&gt;
The component can be downloaded at: [[http://dev.joomla.org/components/com_jd-wiki/data/media/components/com_hello4a.zip|com_hello4a.zip]]&lt;br /&gt;
&lt;br /&gt;
===== Getting Down and Dirty: Doing the Real Work =====&lt;br /&gt;
&lt;br /&gt;
Now that the Hellos view is done, it is time to move to the Hello view and model. This is where the real work will get done.&lt;br /&gt;
&lt;br /&gt;
==== The Hello Controller ====&lt;br /&gt;
&lt;br /&gt;
Our default controller just isn&#039;t going to cut it when it comes to doing work - all it is capable of doing is displaying views.&lt;br /&gt;
&lt;br /&gt;
We need to be able to handle the tasks that we are launching from the Hellos view: add, edit and remove.&lt;br /&gt;
&lt;br /&gt;
Add and edit are essentially the same task: they both display a form to the user that allows a greeting to be edited. The only difference is that new displays a blank form, and edit displays a form with data already in it. Since they are similar, we will map the add task onto the edit task handler. This is specified in our constructor:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * constructor (registers additional tasks to methods)&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function __construct()&lt;br /&gt;
{&lt;br /&gt;
    parent::__construct();&lt;br /&gt;
&lt;br /&gt;
    // Register Extra tasks&lt;br /&gt;
    $this-&amp;gt;registerTask( &#039;add&#039;  ,     &#039;edit&#039; );&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first parameter of JController::registerTask is the task to map, and the second is the method to map it to.&lt;br /&gt;
&lt;br /&gt;
We will start with handling the edit task. The controller&#039;s job is fairly simple for the edit task. All it has to do is specify the view and layout to load (the hello view and the form layout). We will also tell Joomla! to disable the mainmenu while we are editing our greeting. This prevents users from leaving unsaved records open.&lt;br /&gt;
&lt;br /&gt;
Our edit task handler looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * display the edit form&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function edit()&lt;br /&gt;
{&lt;br /&gt;
    JRequest::setVar( &#039;view&#039;, &#039;hello&#039; );&lt;br /&gt;
    JRequest::setVar( &#039;layout&#039;, &#039;form&#039;  );&lt;br /&gt;
    JRequest::setVar(&#039;hidemainmenu&#039;, 1);&lt;br /&gt;
&lt;br /&gt;
    parent::display();&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== The Hello View ====&lt;br /&gt;
&lt;br /&gt;
The Hello view will display a form which will allow the user to edit a greeting. The display method if the hello view has to do a few simple tasks:&lt;br /&gt;
&lt;br /&gt;
  * retrieve the data from the model&lt;br /&gt;
&lt;br /&gt;
  * create the toolbar&lt;br /&gt;
&lt;br /&gt;
  * pass the data into the template&lt;br /&gt;
&lt;br /&gt;
  * invoke the display() method to render the template&lt;br /&gt;
&lt;br /&gt;
This becomes a bit more complicated because the one view handles both the edit and add tasks. In our toolbar we want the user to know what whether they are adding or editing, so we have to determine which task was fired.&lt;br /&gt;
&lt;br /&gt;
Since we are already retrieving the record that we want to display from the model, we can use this data to determine what task was fired. If the task was edit, then the id field of our record will have been set. If the task was new, then it will not have been set. This can be used to determine if we have a new record or an existing record.&lt;br /&gt;
&lt;br /&gt;
We will add two buttons to the toolbar: save and cancel. Though the functionality will be the same, we want to display different buttons depending on whether it is a new or existing record. If it is a new record, we will display cancel. If it already exists, we will display close.&lt;br /&gt;
&lt;br /&gt;
Thus our display method looks like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * display method of Hello view&lt;br /&gt;
 * @return void&lt;br /&gt;
 **/&lt;br /&gt;
function display($tpl = null)&lt;br /&gt;
{&lt;br /&gt;
    //get the hello&lt;br /&gt;
    $hello        =&amp;amp; $this-&amp;gt;get(&#039;Data&#039;);&lt;br /&gt;
    $isNew        = ($hello-&amp;gt;id &amp;lt; 1);&lt;br /&gt;
&lt;br /&gt;
    $text = $isNew ? JText::_( &#039;New&#039; ) : JText::_( &#039;Edit&#039; );&lt;br /&gt;
    JToolBarHelper::title(   JText::_( &#039;Hello&#039; ).&#039;: &amp;lt;small&amp;gt;&amp;lt;small&amp;gt;[ &#039; . $text.&#039; ]&amp;lt;/small&amp;gt;&amp;lt;/small&amp;gt;&#039; );&lt;br /&gt;
    JToolBarHelper::save();&lt;br /&gt;
    if ($isNew)  {&lt;br /&gt;
        JToolBarHelper::cancel();&lt;br /&gt;
    } else {&lt;br /&gt;
        // for existing items the button is renamed `close`&lt;br /&gt;
        JToolBarHelper::cancel( &#039;cancel&#039;, &#039;Close&#039; );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    $this-&amp;gt;assignRef(&#039;hello&#039;, $hello);&lt;br /&gt;
    parent::display($tpl);&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== The Hello Model ====&lt;br /&gt;
&lt;br /&gt;
Our view needs data. Therefore, we need to create a model to model a hello.&lt;br /&gt;
&lt;br /&gt;
Our model will have two properties: _id and _data. _id will hold the id of the greeting and data will hold the data.&lt;br /&gt;
&lt;br /&gt;
We will start with a constructor, which will attempt to retrieve the id from the query:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Constructor that retrieves the ID from the request&lt;br /&gt;
 *&lt;br /&gt;
 * @access    public&lt;br /&gt;
 * @return    void&lt;br /&gt;
 */&lt;br /&gt;
function __construct()&lt;br /&gt;
{&lt;br /&gt;
    parent::__construct();&lt;br /&gt;
&lt;br /&gt;
    $array = JRequest::getVar(&#039;cid&#039;,  0, &#039;&#039;, &#039;array&#039;);&lt;br /&gt;
    $this-&amp;gt;setId((int)$array[0]);&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The JRequest::getVar() method is used to retrieve data from the request. The first parameter is the name of the form variable. The second parameter is the default value to assign if there is no value found. The third parameter is the name of the hash to retrieve the value from (get, post, etc), and the last value is the data type that should be forced on the value.&lt;br /&gt;
&lt;br /&gt;
Our constructor will take the first value from the cid array and assign it to the id.&lt;br /&gt;
&lt;br /&gt;
Our setId() method can be used to set our id. Changing the id that our model points to will mean the id points to the wrong data. Therefore, when we set the id, we will clear the data property:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Method to set the hello identifier&lt;br /&gt;
 *&lt;br /&gt;
 * @access    public&lt;br /&gt;
 * @param    int Hello identifier&lt;br /&gt;
 * @return    void&lt;br /&gt;
 */&lt;br /&gt;
function setId($id)&lt;br /&gt;
{&lt;br /&gt;
    // Set id and wipe data&lt;br /&gt;
    $this-&amp;gt;_id        = $id;&lt;br /&gt;
    $this-&amp;gt;_data    = null;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finally, we need a method to retrieve our data: getData()&lt;br /&gt;
&lt;br /&gt;
getData will check if the _data property has already been set. If it has, it will simply return it. Otherwise, it will load the data from the database.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Method to get a hello&lt;br /&gt;
 * @return object with data&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
function &amp;amp;getData()&lt;br /&gt;
{&lt;br /&gt;
    // Load the data&lt;br /&gt;
    if (empty( $this-&amp;gt;_data )) {&lt;br /&gt;
        $query = &#039; SELECT * FROM #__hello &#039;.&lt;br /&gt;
                &#039;  WHERE id = &#039;.$this-&amp;gt;_id;&lt;br /&gt;
        $this-&amp;gt;_db-&amp;gt;setQuery( $query );&lt;br /&gt;
        $this-&amp;gt;_data = $this-&amp;gt;_db-&amp;gt;loadObject();&lt;br /&gt;
    }&lt;br /&gt;
    if (!$this-&amp;gt;_data) {&lt;br /&gt;
        $this-&amp;gt;_data = new stdClass();&lt;br /&gt;
        $this-&amp;gt;_data-&amp;gt;id = 0;&lt;br /&gt;
        $this-&amp;gt;_data-&amp;gt;greeting = null;&lt;br /&gt;
    }&lt;br /&gt;
    return $this-&amp;gt;_data;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== The Form ====&lt;br /&gt;
&lt;br /&gt;
Now all that is left is to create the form that the data will go into. Since we specified our layout as form, the form will go in a file in the tmpl directory of the hello view called form.php:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;); ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;form action=&amp;quot;index.php&amp;quot; method=&amp;quot;post&amp;quot; name=&amp;quot;adminForm&amp;quot; id=&amp;quot;adminForm&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;div class=&amp;quot;col100&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;fieldset class=&amp;quot;adminform&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;legend&amp;gt;&amp;lt;?php echo JText::_( &#039;Details&#039; ); ?&amp;gt;&amp;lt;/legend&amp;gt;&lt;br /&gt;
        &amp;lt;table class=&amp;quot;admintable&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;tr&amp;gt;&lt;br /&gt;
            &amp;lt;td width=&amp;quot;100&amp;quot; align=&amp;quot;right&amp;quot; class=&amp;quot;key&amp;quot;&amp;gt;&lt;br /&gt;
                &amp;lt;label for=&amp;quot;greeting&amp;quot;&amp;gt;&lt;br /&gt;
                    &amp;lt;?php echo JText::_( &#039;Greeting&#039; ); ?&amp;gt;:&lt;br /&gt;
                &amp;lt;/label&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;input class=&amp;quot;text_area&amp;quot; type=&amp;quot;text&amp;quot; name=&amp;quot;greeting&amp;quot; id=&amp;quot;greeting&amp;quot; size=&amp;quot;32&amp;quot; maxlength=&amp;quot;250&amp;quot; value=&amp;quot;&amp;lt;?php echo $this-&amp;gt;hello-&amp;gt;greeting;?&amp;gt;&amp;quot; /&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;&lt;br /&gt;
    &amp;lt;/table&amp;gt;&lt;br /&gt;
    &amp;lt;/fieldset&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;clr&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;option&amp;quot; value=&amp;quot;com_hello&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;id&amp;quot; value=&amp;quot;&amp;lt;?php echo $this-&amp;gt;hello-&amp;gt;id; ?&amp;gt;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;task&amp;quot; value=&amp;quot;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;controller&amp;quot; value=&amp;quot;hello&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/form&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in addition to the input field, there is a hidden field for the id. The user doesn&#039;t need to edit the id (and shouldn&#039;t), so we silently pass it along in the form.&lt;br /&gt;
&lt;br /&gt;
==== Implementing the Functionality ====&lt;br /&gt;
&lt;br /&gt;
So far, our controller only handles two tasks: edit and new. However, we also have buttons to save, delete and cancel records. We need to write code to handle and perform these tasks.&lt;br /&gt;
&lt;br /&gt;
=== Saving a Record ===&lt;br /&gt;
&lt;br /&gt;
The logical next step is to implement the functionality to save a record. Normally, this would require some switches and logic to handle various cases, such as the difference between creating a new record (an INSERT query), and updating an existing query (an UPDATE query). Also, there are complexities involved in getting the data from the form and putting it into the query.&lt;br /&gt;
&lt;br /&gt;
The Joomla! framework takes care of a lot of this for you. The JTable class makes it easy to manipulate records in the database without having to worry about writing the SQL code that lies behind these updates. It also makes it easy to transfer data from an HTML form into the database.&lt;br /&gt;
&lt;br /&gt;
== Creating the Table Class ==&lt;br /&gt;
&lt;br /&gt;
The JTable class is an abstract class from which you can derive child classes to work with specific tables. To use it, you simply create a class that extends the JTable class, add your database fields as properties, and override the constructor to specify the name of the table and the primary key.&lt;br /&gt;
&lt;br /&gt;
Here is our JTable class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * Hello World table class&lt;br /&gt;
 * &lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://dev.joomla.org/component/option,com_jd-wiki/Itemid,31/id,tutorials:components/&lt;br /&gt;
 * @license        GNU/GPL&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// no direct access&lt;br /&gt;
defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;);&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Hello Table class&lt;br /&gt;
 *&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 */&lt;br /&gt;
class TableHello extends JTable&lt;br /&gt;
{&lt;br /&gt;
    /**&lt;br /&gt;
     * Primary Key&lt;br /&gt;
     *&lt;br /&gt;
     * @var int&lt;br /&gt;
     */&lt;br /&gt;
    var $id = null;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * @var string&lt;br /&gt;
     */&lt;br /&gt;
    var $greeting = null;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Constructor&lt;br /&gt;
     *&lt;br /&gt;
     * @param object Database connector object&lt;br /&gt;
     */&lt;br /&gt;
    function TableHello( &amp;amp;$db ) {&lt;br /&gt;
        parent::__construct(&#039;#__hello&#039;, &#039;id&#039;, $db);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You will see here that we have defined our two fields: the id field and the greeting field. Then we have defined a constructor, which will call the constructor of the parent class and pass it the name of the table (hello), the name of the field which is the primary key (id), and the database connector object.&lt;br /&gt;
&lt;br /&gt;
This file should be called hello.php and it will go in a directory called tables in the administrator section of our component.&lt;br /&gt;
&lt;br /&gt;
== Implementing the Function in our Model ==&lt;br /&gt;
&lt;br /&gt;
We are now ready to add the method to the model which will save our record. We will call this method store. Our store() method will do three things: bind the data from the form to the TableHello object, check to ensure that the record is properly formed, and store the record in the database.&lt;br /&gt;
&lt;br /&gt;
Our method looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Method to store a record&lt;br /&gt;
 *&lt;br /&gt;
 * @access    public&lt;br /&gt;
 * @return    boolean    True on success&lt;br /&gt;
 */&lt;br /&gt;
function store()&lt;br /&gt;
{&lt;br /&gt;
    $row =&amp;amp; $this-&amp;gt;getTable();&lt;br /&gt;
&lt;br /&gt;
    $data = JRequest::get( &#039;post&#039; );&lt;br /&gt;
    // Bind the form fields to the hello table&lt;br /&gt;
    if (!$row-&amp;gt;bind($data)) {&lt;br /&gt;
        $this-&amp;gt;setError($this-&amp;gt;_db-&amp;gt;getErrorMsg());&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // Make sure the hello record is valid&lt;br /&gt;
    if (!$row-&amp;gt;check()) {&lt;br /&gt;
        $this-&amp;gt;setError($this-&amp;gt;_db-&amp;gt;getErrorMsg());&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // Store the web link table to the database&lt;br /&gt;
    if (!$row-&amp;gt;store()) {&lt;br /&gt;
        $this-&amp;gt;setError($this-&amp;gt;_db-&amp;gt;getErrorMsg());&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return true;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This method gets added to the hello model.&lt;br /&gt;
&lt;br /&gt;
The method takes one parameter, which is an associative array of data that we want to store in the database. This can easily be retrieved from the request as will be seen later.&lt;br /&gt;
&lt;br /&gt;
You will see that the first line retrieves a reference to our JTable object. If we name our table properly, we don&#039;t have to specify its name - the JModel class knows where to find it. You may recall that we called our table class TableHello and put it in a file called hello.php in the tables directory. If you follow this convention, the JModel class can create your object automatically.&lt;br /&gt;
&lt;br /&gt;
The second line will retrieve the data from the form. The JRequest class makes this very easy. In this case, we are retrieving all of the variables that were submitted using the &#039;POST&#039; method. These will be returned as an associative array.&lt;br /&gt;
&lt;br /&gt;
The rest is easy - we bind, check and store. The bind() method will copy values from the array into the corresponding property of the table object. In this case, it will take the values of id and greeting and copy them to our TableHello object.&lt;br /&gt;
&lt;br /&gt;
The check() method will perform data verification. In the JTable() class, this method simply returns true. While this doesn&#039;t provide any value for us currently, by calling this method we make it possible to do data checking using our TableHello class in the future. This method can be overridden in our TableHello class with a method that performs the appropriate checks.&lt;br /&gt;
&lt;br /&gt;
The store() method will take the data that is in the object and store it in the database. If the id is 0, it will create a new record (INSERT), otherwise, it will update the existing record (UPDATE).&lt;br /&gt;
&lt;br /&gt;
== Adding the Task to the Controller ==&lt;br /&gt;
&lt;br /&gt;
We are now ready to add our task to the controller. Since the task that we are firing is called &#039;save&#039;, we must call our method &#039;save&#039;. This is simple:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * save a record (and redirect to main page)&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function save()&lt;br /&gt;
{&lt;br /&gt;
    $model = $this-&amp;gt;getModel(&#039;hello&#039;);&lt;br /&gt;
&lt;br /&gt;
    if ($model-&amp;gt;store()) {&lt;br /&gt;
        $msg = JText::_( &#039;Greeting Saved!&#039; );&lt;br /&gt;
    } else {&lt;br /&gt;
        $msg = JText::_( &#039;Error Saving Greeting&#039; );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // Check the table in so it can be edited.... we are done with it anyway&lt;br /&gt;
    $link = &#039;index.php?option=com_hello&#039;;&lt;br /&gt;
    $this-&amp;gt;setRedirect($link, $msg);&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All we do is get our model and invoke the store() method. Then we use the setRedirect() method to redirect back to our list of greetings. We also pass a message along, which will be displayed at the top of the page.&lt;br /&gt;
&lt;br /&gt;
==== Deleting a Record ====&lt;br /&gt;
&lt;br /&gt;
=== Implementing the Function in the Model ===&lt;br /&gt;
&lt;br /&gt;
In the model, we will retrieve the list of IDs to delete and call the JTable class to delete them. Here it is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Method to delete record(s)&lt;br /&gt;
 *&lt;br /&gt;
 * @access    public&lt;br /&gt;
 * @return    boolean    True on success&lt;br /&gt;
 */&lt;br /&gt;
function delete()&lt;br /&gt;
{&lt;br /&gt;
    $cids = JRequest::getVar( &#039;cid&#039;, array(0), &#039;post&#039;, &#039;array&#039; );&lt;br /&gt;
    $row =&amp;amp; $this-&amp;gt;getTable();&lt;br /&gt;
&lt;br /&gt;
    foreach($cids as $cid) {&lt;br /&gt;
        if (!$row-&amp;gt;delete( $cid )) {&lt;br /&gt;
            $this-&amp;gt;setError( $row-&amp;gt;getErrorMsg() );&lt;br /&gt;
            return false;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return true;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We invoke the JRequest::getVar() method to get the data from the request, then we invoke the $row-&amp;gt;delete() method to delete each row. By storing errors in the model we make it possible to retrieve them later if we so choose.&lt;br /&gt;
&lt;br /&gt;
=== Handling the Remove Task in the Controller ===&lt;br /&gt;
&lt;br /&gt;
This is similar to the save() method which handled the save task:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * remove record(s)&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function remove()&lt;br /&gt;
{&lt;br /&gt;
    $model = $this-&amp;gt;getModel(&#039;hello&#039;);&lt;br /&gt;
    if(!$model-&amp;gt;delete()) {&lt;br /&gt;
        $msg = JText::_( &#039;Error: One or More Greetings Could not be Deleted&#039; );&lt;br /&gt;
    } else {&lt;br /&gt;
        $msg = JText::_( &#039;Greeting(s) Deleted&#039; );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    $this-&amp;gt;setRedirect( &#039;index.php?option=com_hello&#039;, $msg );&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Cancelling the Edit Operation ====&lt;br /&gt;
&lt;br /&gt;
To cancel the edit operation, all we have to do is redirect back to the main view:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * cancel editing a record&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function cancel()&lt;br /&gt;
{&lt;br /&gt;
    $msg = JText::_( &#039;Operation Cancelled&#039; );&lt;br /&gt;
    $this-&amp;gt;setRedirect( &#039;index.php?option=com_hello&#039;, $msg );&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== Conclusion =====&lt;br /&gt;
&lt;br /&gt;
We have now implemented a basic backend to our component. We are now able to edit the entries that are viewed in the frontend. We have demonstrated the interaction between models, views and controllers. We have shown how the JTable class can be extended to provide easy access to tables in the database. It can also be seen how the JToolBarHelper class can be used to create button bars in components to present a standardized look between components.&lt;br /&gt;
&lt;br /&gt;
===== Contributors =====&lt;br /&gt;
* staalanden&lt;br /&gt;
&lt;br /&gt;
===== Download =====&lt;br /&gt;
&lt;br /&gt;
The component can be downloaded at: [http://joomlacode.org/gf/download/frsrelease/8104/29402/com_hello4_01.zip com_hello4_01]&lt;/div&gt;</summary>
		<author><name>Tonie</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Creating_an_Administrator_Interface&amp;diff=9497</id>
		<title>J1.5:Developing a MVC Component/Creating an Administrator Interface</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Creating_an_Administrator_Interface&amp;diff=9497"/>
		<updated>2008-07-27T18:32:00Z</updated>

		<summary type="html">&lt;p&gt;Tonie: /* Download */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;====== Developing a Model-View-Controller Component - Part 4 - Creating an Administrator Interface ======&lt;br /&gt;
&lt;br /&gt;
===== Introduction =====&lt;br /&gt;
&lt;br /&gt;
In the first three tutorials, we have developed a MVC component that retrieves its data from a table in the database. Currently, there is no way to add data to the database except to do it manually using another tool. In this tutorial, we will develop an administrator section for our component which will make it possible to manage the entries in the database.&lt;br /&gt;
&lt;br /&gt;
===== Creating the Basic Framework =====&lt;br /&gt;
&lt;br /&gt;
The basic framework of the administrator panel is very similar to the site portion. The main entry point for the administrator section of the component is admin.hello.php. This file is identical to the hello.php file that was used in the site portion except the name of the controller it loads will be changed to HellosController. The default controller is also called controller.php and this file is identical to the default controller in the site portion, with the exception that the controller is named HellosController instead of HelloController. This difference is so that JController will by default load the hellos view, which will display a list of our greetings.&lt;br /&gt;
&lt;br /&gt;
Here is the listing for admin.hello.php:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://dev.joomla.org/component/option,com_jd-wiki/Itemid,31/id,tutorials:components/&lt;br /&gt;
 * @license    GNU/GPL&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
// no direct access&lt;br /&gt;
&lt;br /&gt;
defined( &#039;_JEXEC&#039; ) or die( &#039;Restricted access&#039; );&lt;br /&gt;
&lt;br /&gt;
// Require the base controller&lt;br /&gt;
&lt;br /&gt;
require_once( JPATH_COMPONENT.DS.&#039;controller.php&#039; );&lt;br /&gt;
&lt;br /&gt;
// Require specific controller if requested&lt;br /&gt;
if($controller = JRequest::getWord(&#039;controller&#039;)) {&lt;br /&gt;
    $path = JPATH_COMPONENT.DS.&#039;controllers&#039;.DS.$controller.&#039;.php&#039;;&lt;br /&gt;
    if (file_exists($path)) {&lt;br /&gt;
        require_once $path;&lt;br /&gt;
    } else {&lt;br /&gt;
        $controller = &#039;&#039;;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Create the controller&lt;br /&gt;
$classname    = &#039;HellosController&#039;.$controller;&lt;br /&gt;
$controller   = new $classname( );&lt;br /&gt;
&lt;br /&gt;
// Perform the Request task&lt;br /&gt;
$controller-&amp;gt;execute( JRequest::getVar( &#039;task&#039; ) );&lt;br /&gt;
&lt;br /&gt;
// Redirect if set by the controller&lt;br /&gt;
$controller-&amp;gt;redirect();&lt;br /&gt;
&lt;br /&gt;
?&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The view and model that we will start with is the hellos view and the hellos model. We will start with the model.&lt;br /&gt;
&lt;br /&gt;
==== The Hellos Model ====&lt;br /&gt;
&lt;br /&gt;
The Hellos Model will be very simple. The only operation that we currently need is the ability to retrieve the list of hellos from the database. This operation will be implemented in a method called getData().&lt;br /&gt;
&lt;br /&gt;
The JModel class has a built in protected method called _getList(). This method can be used to simplify the task of retrieving a list of records from the database. We simply need to pass it the query and it will return the list of records.&lt;br /&gt;
&lt;br /&gt;
At a later point in time, we might want to use our query from within another method. Therefore, we will create a private method called _buildQuery() which will return the query that will be passed to _getList(). This makes it easier to change the query as well since it is localized in one place.&lt;br /&gt;
&lt;br /&gt;
Therefore we need two methods in our class: getData() and _buildQuery().&lt;br /&gt;
&lt;br /&gt;
_buildQuery() simply returns the query. It looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source  lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Returns the query&lt;br /&gt;
 * @return string The query to be used to retrieve the rows from the database&lt;br /&gt;
 */&lt;br /&gt;
function _buildQuery()&lt;br /&gt;
{&lt;br /&gt;
    $query = &#039; SELECT * &#039;&lt;br /&gt;
           . &#039; FROM #__hello &#039;&lt;br /&gt;
    ;&lt;br /&gt;
&lt;br /&gt;
    return $query;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
getData() will obtain the query and retrieve the records from the database. Now it might happen that we need to retrieve this list of data twice in one page load. It would be a waste to have to query the database twice. Therefore, we will have this method store the data in a protected property so that on subsequent requests it can simply return the data it has already retrieved. This property will be called _data.&lt;br /&gt;
&lt;br /&gt;
Here is the getData() method:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source  lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Retrieves the hello data&lt;br /&gt;
 * @return array Array of objects containing the data from the database&lt;br /&gt;
 */&lt;br /&gt;
function getData()&lt;br /&gt;
{&lt;br /&gt;
    // Lets load the data if it doesn&#039;t already exist&lt;br /&gt;
    if (empty( $this-&amp;gt;_data ))&lt;br /&gt;
    {&lt;br /&gt;
        $query = $this-&amp;gt;_buildQuery();&lt;br /&gt;
        $this-&amp;gt;_data = $this-&amp;gt;_getList( $query );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return $this-&amp;gt;_data;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The completed model looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * Hellos Model for Hello World Component&lt;br /&gt;
 * &lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://dev.joomla.org/component/option,com_jd-wiki/Itemid,31/id,tutorials:components/&lt;br /&gt;
 * @license        GNU/GPL&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// Check to ensure this file is included in Joomla!&lt;br /&gt;
defined(&#039;_JEXEC&#039;) or die();&lt;br /&gt;
&lt;br /&gt;
jimport( &#039;joomla.application.component.model&#039; );&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Hello Model&lt;br /&gt;
 *&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 */&lt;br /&gt;
class HellosModelHellos extends JModel&lt;br /&gt;
{&lt;br /&gt;
    /**&lt;br /&gt;
     * Hellos data array&lt;br /&gt;
     *&lt;br /&gt;
     * @var array&lt;br /&gt;
     */&lt;br /&gt;
    var $_data;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Returns the query&lt;br /&gt;
     * @return string The query to be used to retrieve the rows from the database&lt;br /&gt;
     */&lt;br /&gt;
    function _buildQuery()&lt;br /&gt;
    {&lt;br /&gt;
        $query = &#039; SELECT * &#039;&lt;br /&gt;
            . &#039; FROM #__hello &#039;&lt;br /&gt;
        ;&lt;br /&gt;
        return $query;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Retrieves the hello data&lt;br /&gt;
     * @return array Array of objects containing the data from the database&lt;br /&gt;
     */&lt;br /&gt;
    function getData()&lt;br /&gt;
    {&lt;br /&gt;
        // Lets load the data if it doesn&#039;t already exist&lt;br /&gt;
        if (empty( $this-&amp;gt;_data ))&lt;br /&gt;
        {&lt;br /&gt;
            $query = $this-&amp;gt;_buildQuery();&lt;br /&gt;
            $this-&amp;gt;_data = $this-&amp;gt;_getList( $query );&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        return $this-&amp;gt;_data;&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This file is saved as models/hellos.php.&lt;br /&gt;
&lt;br /&gt;
==== The Hellos View ====&lt;br /&gt;
&lt;br /&gt;
Now that we have a model to retrieve our data, we need to display it. This view will be fairly similar to the view from the site section as well.&lt;br /&gt;
&lt;br /&gt;
Just as our model was automatically instantiated in the site, so it is in the administrator. Methods that start with get in the model can be accessed using the get() method of the JView class. So our view has three lines: one to retrieve the data from the model, one to push the data into the template, and one to invoke the display method to display the output. Thus we have:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * Hellos View for Hello World Component&lt;br /&gt;
 * &lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://dev.joomla.org/component/option,com_jd-wiki/Itemid,31/id,tutorials:components/&lt;br /&gt;
 * @license        GNU/GPL&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// Check to ensure this file is included in Joomla!&lt;br /&gt;
defined(&#039;_JEXEC&#039;) or die();&lt;br /&gt;
&lt;br /&gt;
jimport( &#039;joomla.application.component.view&#039; );&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Hellos View&lt;br /&gt;
 *&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 */&lt;br /&gt;
class HellosViewHellos extends JView&lt;br /&gt;
{&lt;br /&gt;
    /**&lt;br /&gt;
     * Hellos view display method&lt;br /&gt;
     * @return void&lt;br /&gt;
     **/&lt;br /&gt;
    function display($tpl = null)&lt;br /&gt;
    {&lt;br /&gt;
        JToolBarHelper::title( JText::_( &#039;Hello Manager&#039; ), &#039;generic.png&#039; );&lt;br /&gt;
        JToolBarHelper::deleteList();&lt;br /&gt;
        JToolBarHelper::editListX();&lt;br /&gt;
        JToolBarHelper::addNewX();&lt;br /&gt;
&lt;br /&gt;
        // Get data from the model&lt;br /&gt;
        $items =&amp;amp; $this-&amp;gt;get( &#039;Data&#039;);&lt;br /&gt;
&lt;br /&gt;
        $this-&amp;gt;assignRef( &#039;items&#039;, $items );&lt;br /&gt;
&lt;br /&gt;
        parent::display($tpl);&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This file is saved as views/hellos/view.html.php.&lt;br /&gt;
&lt;br /&gt;
==== The Hellos Template ====&lt;br /&gt;
&lt;br /&gt;
The template will take the data pushed into it from the view and produce the output. We will display our output in a simple table. While the frontend template was very simple, in the administrator we will need a minimal amount of extra logic to handle looping through the data.&lt;br /&gt;
&lt;br /&gt;
Here is our template:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;); ?&amp;gt;&lt;br /&gt;
&amp;lt;form action=&amp;quot;index.php&amp;quot; method=&amp;quot;post&amp;quot; name=&amp;quot;adminForm&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;div id=&amp;quot;editcell&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;table class=&amp;quot;adminlist&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;thead&amp;gt;&lt;br /&gt;
        &amp;lt;tr&amp;gt;&lt;br /&gt;
            &amp;lt;th width=&amp;quot;5&amp;quot;&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo JText::_( &#039;ID&#039; ); ?&amp;gt;&lt;br /&gt;
            &amp;lt;/th&amp;gt;&lt;br /&gt;
            &amp;lt;th&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo JText::_( &#039;Greeting&#039; ); ?&amp;gt;&lt;br /&gt;
            &amp;lt;/th&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;            &lt;br /&gt;
    &amp;lt;/thead&amp;gt;&lt;br /&gt;
    &amp;lt;?php&lt;br /&gt;
    $k = 0;&lt;br /&gt;
    for ($i=0, $n=count( $this-&amp;gt;items ); $i &amp;lt; $n; $i++)&lt;br /&gt;
    {&lt;br /&gt;
        $row =&amp;amp; $this-&amp;gt;items[$i];&lt;br /&gt;
        ?&amp;gt;&lt;br /&gt;
        &amp;lt;tr class=&amp;quot;&amp;lt;?php echo &amp;quot;row$k&amp;quot;; ?&amp;gt;&amp;quot;&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo $row-&amp;gt;id; ?&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo $row-&amp;gt;greeting; ?&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;&lt;br /&gt;
        &amp;lt;?php&lt;br /&gt;
        $k = 1 - $k;&lt;br /&gt;
    }&lt;br /&gt;
    ?&amp;gt;&lt;br /&gt;
    &amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;option&amp;quot; value=&amp;quot;com_hello&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;task&amp;quot; value=&amp;quot;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;boxchecked&amp;quot; value=&amp;quot;0&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;controller&amp;quot; value=&amp;quot;hello&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/form&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This template is saved as views/hellos/tmpl/default.php.&lt;br /&gt;
&lt;br /&gt;
You will notice that our output is enclosed in a form. Though this is not necessary now, it will be soon.&lt;br /&gt;
&lt;br /&gt;
We have now completed the basic part of the first view. We have added five files to the admin section of our component:&lt;br /&gt;
&lt;br /&gt;
  * admin.hello.php&lt;br /&gt;
&lt;br /&gt;
  * controller.php&lt;br /&gt;
&lt;br /&gt;
  * models/hellos.php&lt;br /&gt;
&lt;br /&gt;
  * views/hellos/view.html.php&lt;br /&gt;
&lt;br /&gt;
  * views/hellos/tmpl/default.php&lt;br /&gt;
&lt;br /&gt;
You can now add these files to the XML install file and give it a try!&lt;br /&gt;
&lt;br /&gt;
===== Adding Functionality =====&lt;br /&gt;
&lt;br /&gt;
So far our administrator section is pretty useless. It doesn&#039;t really do anything - all it does is display the entries that we have in our database.&lt;br /&gt;
&lt;br /&gt;
In order to make it useful, we need to add some buttons and links.&lt;br /&gt;
&lt;br /&gt;
==== The Toolbar ====&lt;br /&gt;
&lt;br /&gt;
You may have noticed the toolbar that appears at the top of other Joomla! component administrator panels. Our component needs one as well. Joomla! makes this very easy to do. We will add buttons Delete records, Edit records, and create New records. We will also add a title that will be displayed on our toolbar.&lt;br /&gt;
&lt;br /&gt;
This is done by adding code to the view. To add the buttons, we use static methods from the Joomla! JToolBarHelper class. The code looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;JToolBarHelper::title(   JText::_( &#039;Hello Manager&#039; ), &#039;generic.png&#039; );&lt;br /&gt;
JToolBarHelper::deleteList();&lt;br /&gt;
JToolBarHelper::editListX();&lt;br /&gt;
JToolBarHelper::addNewX();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
These three methods will create the appropriate buttons. The deleteList() method can optionally take up to three parameters - the first parameter is a string to display to the user to confirm that they want to delete the records. The second is the task that should be sent with the query (the default is &#039;remove&#039;), and the third is the text that should be displayed below the button.&lt;br /&gt;
&lt;br /&gt;
The editListX() and addNewX() methods can each take two optional parameters. The first is the task (which are by default edit and add, respectively), and the second is the text that should be displayed below the button.&lt;br /&gt;
&lt;br /&gt;
 &#039;&#039;*You may have noticed the use of the JText::_ method in the template before and here as well. This is a handy function that makes component translation much easier. The JText::_ method will look up the string in your component language file and return the translated string. If not translation is found, it will return the string that you passed it. If you want to translate your component into another language, all you have to do is create a language file that will map the strings within the quotes to the translated version of the string.&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==== Checkboxes and Links ====&lt;br /&gt;
&lt;br /&gt;
We now have buttons. Two of those buttons operate on existing records. But how do we know which records to operate on? We have to let the user tell us. To do this, we need to add checkboxes to our table so that the user can select certain records. This is done in our template.&lt;br /&gt;
&lt;br /&gt;
In order to the add the checkboxes, we need to add an extra column into our table. We will add the column in between the two that we already have.&lt;br /&gt;
&lt;br /&gt;
In the header of the column, we will add a checkbox which can be used to toggle all the boxes below it on or off:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;th width=&amp;quot;20&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;input type=&amp;quot;checkbox&amp;quot; name=&amp;quot;toggle&amp;quot; value=&amp;quot;&amp;quot; onclick=&amp;quot;checkAll(&amp;lt;?php echo count( $this-&amp;gt;items ); ?&amp;gt;);&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/th&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Javascript checkAll function is a function that is built into the Joomla! base Javascript package that provides the functionality that we want here.&lt;br /&gt;
&lt;br /&gt;
Now we need to add the checkboxes into the individual rows. Joomla!&#039;s JHTML class has a method, JHTML::_(), which will generate our checkbox for us. We will add the following line to our loop:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$checked    = JHTML::_( &#039;grid.id&#039;, $i, $row-&amp;gt;id );&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
after the line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$row =&amp;amp; $this-&amp;gt;items[$i];&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then we will add a cell in between the two that we already have:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;td&amp;gt;&lt;br /&gt;
    &amp;lt;?php echo $checked; ?&amp;gt;&lt;br /&gt;
&amp;lt;/td&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It can be cumbersome to have to check the box that we want to edit and then move up and click the edit button. Therefore, we will add a link that it will go straight to the greeting&#039;s edit form. We will add the following line after the call to the JHTML::_() method to generate the link HTML:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$link = JRoute::_( &#039;index.php?option=com_hello&amp;gt;controller=hello&amp;gt;task=edit&amp;gt;cid[]=&#039;. $row-&amp;gt;id );&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And we include the link in the cell showing the greeting text:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;td&amp;gt;&lt;br /&gt;
    &amp;lt;a href=&amp;quot;&amp;lt;?php echo $link; ?&amp;gt;&amp;quot;&amp;gt;&amp;lt;?php echo $row-&amp;gt;greeting; ?&amp;gt;&amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;lt;/td&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You will notice that this link points to the hello controller. This controller will handle the data manipulation of our greetings.&lt;br /&gt;
&lt;br /&gt;
If you recall from above, we had four hidden input fields at the bottom of our form. The first input field was named &#039;option&#039;. This field is necessary so that we stay in our component. The second input field was task. This form property gets set when one of the buttons in the toolbar is clicked. A Javascript error will result and the buttons will not work if this input field is omitted. The third input field is the boxchecked field. This field keeps track of the number of boxes that are checked. The edit and delete buttons will check to ensure that this is greater than zero and will not allow the form to be submitted if it is not. The fourth input field is the controller field. This is used to specify that tasks fired from this form will be handled by the hello controller.&lt;br /&gt;
&lt;br /&gt;
Here is the code for the completed default.php file:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;); ?&amp;gt;&lt;br /&gt;
&amp;lt;form action=&amp;quot;index.php&amp;quot; method=&amp;quot;post&amp;quot; name=&amp;quot;adminForm&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;div id=&amp;quot;editcell&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;table class=&amp;quot;adminlist&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;thead&amp;gt;&lt;br /&gt;
        &amp;lt;tr&amp;gt;&lt;br /&gt;
            &amp;lt;th width=&amp;quot;5&amp;quot;&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo JText::_( &#039;ID&#039; ); ?&amp;gt;&lt;br /&gt;
            &amp;lt;/th&amp;gt;&lt;br /&gt;
            &amp;lt;th&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo JText::_( &#039;Greeting&#039; ); ?&amp;gt;&lt;br /&gt;
            &amp;lt;/th&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;            &lt;br /&gt;
    &amp;lt;/thead&amp;gt;&lt;br /&gt;
    &amp;lt;?php&lt;br /&gt;
    $k = 0;&lt;br /&gt;
    for ($i=0, $n=count( $this-&amp;gt;items ); $i &amp;lt; $n; $i++)&lt;br /&gt;
    {&lt;br /&gt;
        $row =&amp;amp; $this-&amp;gt;items[$i];&lt;br /&gt;
        ?&amp;gt;&lt;br /&gt;
        &amp;lt;tr class=&amp;quot;&amp;lt;?php echo &amp;quot;row$k&amp;quot;; ?&amp;gt;&amp;quot;&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo $row-&amp;gt;id; ?&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;?php echo $row-&amp;gt;greeting; ?&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;&lt;br /&gt;
        &amp;lt;?php&lt;br /&gt;
        $k = 1 - $k;&lt;br /&gt;
    }&lt;br /&gt;
    ?&amp;gt;&lt;br /&gt;
    &amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;option&amp;quot; value=&amp;quot;com_hello&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;task&amp;quot; value=&amp;quot;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;boxchecked&amp;quot; value=&amp;quot;0&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;controller&amp;quot; value=&amp;quot;hello&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/form&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Our hellos view is now complete. You can try out the component now to see the results.&lt;br /&gt;
&lt;br /&gt;
The component can be downloaded at: [[http://dev.joomla.org/components/com_jd-wiki/data/media/components/com_hello4a.zip|com_hello4a.zip]]&lt;br /&gt;
&lt;br /&gt;
===== Getting Down and Dirty: Doing the Real Work =====&lt;br /&gt;
&lt;br /&gt;
Now that the Hellos view is done, it is time to move to the Hello view and model. This is where the real work will get done.&lt;br /&gt;
&lt;br /&gt;
==== The Hello Controller ====&lt;br /&gt;
&lt;br /&gt;
Our default controller just isn&#039;t going to cut it when it comes to doing work - all it is capable of doing is displaying views.&lt;br /&gt;
&lt;br /&gt;
We need to be able to handle the tasks that we are launching from the Hellos view: add, edit and remove.&lt;br /&gt;
&lt;br /&gt;
Add and edit are essentially the same task: they both display a form to the user that allows a greeting to be edited. The only difference is that new displays a blank form, and edit displays a form with data already in it. Since they are similar, we will map the add task onto the edit task handler. This is specified in our constructor:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * constructor (registers additional tasks to methods)&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function __construct()&lt;br /&gt;
{&lt;br /&gt;
    parent::__construct();&lt;br /&gt;
&lt;br /&gt;
    // Register Extra tasks&lt;br /&gt;
    $this-&amp;gt;registerTask( &#039;add&#039;  ,     &#039;edit&#039; );&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first parameter of JController::registerTask is the task to map, and the second is the method to map it to.&lt;br /&gt;
&lt;br /&gt;
We will start with handling the edit task. The controller&#039;s job is fairly simple for the edit task. All it has to do is specify the view and layout to load (the hello view and the form layout). We will also tell Joomla! to disable the mainmenu while we are editing our greeting. This prevents users from leaving unsaved records open.&lt;br /&gt;
&lt;br /&gt;
Our edit task handler looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * display the edit form&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function edit()&lt;br /&gt;
{&lt;br /&gt;
    JRequest::setVar( &#039;view&#039;, &#039;hello&#039; );&lt;br /&gt;
    JRequest::setVar( &#039;layout&#039;, &#039;form&#039;  );&lt;br /&gt;
    JRequest::setVar(&#039;hidemainmenu&#039;, 1);&lt;br /&gt;
&lt;br /&gt;
    parent::display();&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== The Hello View ====&lt;br /&gt;
&lt;br /&gt;
The Hello view will display a form which will allow the user to edit a greeting. The display method if the hello view has to do a few simple tasks:&lt;br /&gt;
&lt;br /&gt;
  * retrieve the data from the model&lt;br /&gt;
&lt;br /&gt;
  * create the toolbar&lt;br /&gt;
&lt;br /&gt;
  * pass the data into the template&lt;br /&gt;
&lt;br /&gt;
  * invoke the display() method to render the template&lt;br /&gt;
&lt;br /&gt;
This becomes a bit more complicated because the one view handles both the edit and add tasks. In our toolbar we want the user to know what whether they are adding or editing, so we have to determine which task was fired.&lt;br /&gt;
&lt;br /&gt;
Since we are already retrieving the record that we want to display from the model, we can use this data to determine what task was fired. If the task was edit, then the id field of our record will have been set. If the task was new, then it will not have been set. This can be used to determine if we have a new record or an existing record.&lt;br /&gt;
&lt;br /&gt;
We will add two buttons to the toolbar: save and cancel. Though the functionality will be the same, we want to display different buttons depending on whether it is a new or existing record. If it is a new record, we will display cancel. If it already exists, we will display close.&lt;br /&gt;
&lt;br /&gt;
Thus our display method looks like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * display method of Hello view&lt;br /&gt;
 * @return void&lt;br /&gt;
 **/&lt;br /&gt;
function display($tpl = null)&lt;br /&gt;
{&lt;br /&gt;
    //get the hello&lt;br /&gt;
    $hello        =&amp;amp; $this-&amp;gt;get(&#039;Data&#039;);&lt;br /&gt;
    $isNew        = ($hello-&amp;gt;id &amp;lt; 1);&lt;br /&gt;
&lt;br /&gt;
    $text = $isNew ? JText::_( &#039;New&#039; ) : JText::_( &#039;Edit&#039; );&lt;br /&gt;
    JToolBarHelper::title(   JText::_( &#039;Hello&#039; ).&#039;: &amp;lt;small&amp;gt;&amp;lt;small&amp;gt;[ &#039; . $text.&#039; ]&amp;lt;/small&amp;gt;&amp;lt;/small&amp;gt;&#039; );&lt;br /&gt;
    JToolBarHelper::save();&lt;br /&gt;
    if ($isNew)  {&lt;br /&gt;
        JToolBarHelper::cancel();&lt;br /&gt;
    } else {&lt;br /&gt;
        // for existing items the button is renamed `close`&lt;br /&gt;
        JToolBarHelper::cancel( &#039;cancel&#039;, &#039;Close&#039; );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    $this-&amp;gt;assignRef(&#039;hello&#039;, $hello);&lt;br /&gt;
    parent::display($tpl);&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== The Hello Model ====&lt;br /&gt;
&lt;br /&gt;
Our view needs data. Therefore, we need to create a model to model a hello.&lt;br /&gt;
&lt;br /&gt;
Our model will have two properties: _id and _data. _id will hold the id of the greeting and data will hold the data.&lt;br /&gt;
&lt;br /&gt;
We will start with a constructor, which will attempt to retrieve the id from the query:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Constructor that retrieves the ID from the request&lt;br /&gt;
 *&lt;br /&gt;
 * @access    public&lt;br /&gt;
 * @return    void&lt;br /&gt;
 */&lt;br /&gt;
function __construct()&lt;br /&gt;
{&lt;br /&gt;
    parent::__construct();&lt;br /&gt;
&lt;br /&gt;
    $array = JRequest::getVar(&#039;cid&#039;,  0, &#039;&#039;, &#039;array&#039;);&lt;br /&gt;
    $this-&amp;gt;setId((int)$array[0]);&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The JRequest::getVar() method is used to retrieve data from the request. The first parameter is the name of the form variable. The second parameter is the default value to assign if there is no value found. The third parameter is the name of the hash to retrieve the value from (get, post, etc), and the last value is the data type that should be forced on the value.&lt;br /&gt;
&lt;br /&gt;
Our constructor will take the first value from the cid array and assign it to the id.&lt;br /&gt;
&lt;br /&gt;
Our setId() method can be used to set our id. Changing the id that our model points to will mean the id points to the wrong data. Therefore, when we set the id, we will clear the data property:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Method to set the hello identifier&lt;br /&gt;
 *&lt;br /&gt;
 * @access    public&lt;br /&gt;
 * @param    int Hello identifier&lt;br /&gt;
 * @return    void&lt;br /&gt;
 */&lt;br /&gt;
function setId($id)&lt;br /&gt;
{&lt;br /&gt;
    // Set id and wipe data&lt;br /&gt;
    $this-&amp;gt;_id        = $id;&lt;br /&gt;
    $this-&amp;gt;_data    = null;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finally, we need a method to retrieve our data: getData()&lt;br /&gt;
&lt;br /&gt;
getData will check if the _data property has already been set. If it has, it will simply return it. Otherwise, it will load the data from the database.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Method to get a hello&lt;br /&gt;
 * @return object with data&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
function &amp;amp;getData()&lt;br /&gt;
{&lt;br /&gt;
    // Load the data&lt;br /&gt;
    if (empty( $this-&amp;gt;_data )) {&lt;br /&gt;
        $query = &#039; SELECT * FROM #__hello &#039;.&lt;br /&gt;
                &#039;  WHERE id = &#039;.$this-&amp;gt;_id;&lt;br /&gt;
        $this-&amp;gt;_db-&amp;gt;setQuery( $query );&lt;br /&gt;
        $this-&amp;gt;_data = $this-&amp;gt;_db-&amp;gt;loadObject();&lt;br /&gt;
    }&lt;br /&gt;
    if (!$this-&amp;gt;_data) {&lt;br /&gt;
        $this-&amp;gt;_data = new stdClass();&lt;br /&gt;
        $this-&amp;gt;_data-&amp;gt;id = 0;&lt;br /&gt;
        $this-&amp;gt;_data-&amp;gt;greeting = null;&lt;br /&gt;
    }&lt;br /&gt;
    return $this-&amp;gt;_data;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== The Form ====&lt;br /&gt;
&lt;br /&gt;
Now all that is left is to create the form that the data will go into. Since we specified our layout as form, the form will go in a file in the tmpl directory of the hello view called form.php:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;); ?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;form action=&amp;quot;index.php&amp;quot; method=&amp;quot;post&amp;quot; name=&amp;quot;adminForm&amp;quot; id=&amp;quot;adminForm&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;div class=&amp;quot;col100&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;fieldset class=&amp;quot;adminform&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;legend&amp;gt;&amp;lt;?php echo JText::_( &#039;Details&#039; ); ?&amp;gt;&amp;lt;/legend&amp;gt;&lt;br /&gt;
        &amp;lt;table class=&amp;quot;admintable&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;tr&amp;gt;&lt;br /&gt;
            &amp;lt;td width=&amp;quot;100&amp;quot; align=&amp;quot;right&amp;quot; class=&amp;quot;key&amp;quot;&amp;gt;&lt;br /&gt;
                &amp;lt;label for=&amp;quot;greeting&amp;quot;&amp;gt;&lt;br /&gt;
                    &amp;lt;?php echo JText::_( &#039;Greeting&#039; ); ?&amp;gt;:&lt;br /&gt;
                &amp;lt;/label&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
            &amp;lt;td&amp;gt;&lt;br /&gt;
                &amp;lt;input class=&amp;quot;text_area&amp;quot; type=&amp;quot;text&amp;quot; name=&amp;quot;greeting&amp;quot; id=&amp;quot;greeting&amp;quot; size=&amp;quot;32&amp;quot; maxlength=&amp;quot;250&amp;quot; value=&amp;quot;&amp;lt;?php echo $this-&amp;gt;hello-&amp;gt;greeting;?&amp;gt;&amp;quot; /&amp;gt;&lt;br /&gt;
            &amp;lt;/td&amp;gt;&lt;br /&gt;
        &amp;lt;/tr&amp;gt;&lt;br /&gt;
    &amp;lt;/table&amp;gt;&lt;br /&gt;
    &amp;lt;/fieldset&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;clr&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;option&amp;quot; value=&amp;quot;com_hello&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;id&amp;quot; value=&amp;quot;&amp;lt;?php echo $this-&amp;gt;hello-&amp;gt;id; ?&amp;gt;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;task&amp;quot; value=&amp;quot;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;controller&amp;quot; value=&amp;quot;hello&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/form&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in addition to the input field, there is a hidden field for the id. The user doesn&#039;t need to edit the id (and shouldn&#039;t), so we silently pass it along in the form.&lt;br /&gt;
&lt;br /&gt;
==== Implementing the Functionality ====&lt;br /&gt;
&lt;br /&gt;
So far, our controller only handles two tasks: edit and new. However, we also have buttons to save, delete and cancel records. We need to write code to handle and perform these tasks.&lt;br /&gt;
&lt;br /&gt;
=== Saving a Record ===&lt;br /&gt;
&lt;br /&gt;
The logical next step is to implement the functionality to save a record. Normally, this would require some switches and logic to handle various cases, such as the difference between creating a new record (an INSERT query), and updating an existing query (an UPDATE query). Also, there are complexities involved in getting the data from the form and putting it into the query.&lt;br /&gt;
&lt;br /&gt;
The Joomla! framework takes care of a lot of this for you. The JTable class makes it easy to manipulate records in the database without having to worry about writing the SQL code that lies behind these updates. It also makes it easy to transfer data from an HTML form into the database.&lt;br /&gt;
&lt;br /&gt;
== Creating the Table Class ==&lt;br /&gt;
&lt;br /&gt;
The JTable class is an abstract class from which you can derive child classes to work with specific tables. To use it, you simply create a class that extends the JTable class, add your database fields as properties, and override the constructor to specify the name of the table and the primary key.&lt;br /&gt;
&lt;br /&gt;
Here is our JTable class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * Hello World table class&lt;br /&gt;
 * &lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://dev.joomla.org/component/option,com_jd-wiki/Itemid,31/id,tutorials:components/&lt;br /&gt;
 * @license        GNU/GPL&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// no direct access&lt;br /&gt;
defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;);&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Hello Table class&lt;br /&gt;
 *&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 */&lt;br /&gt;
class TableHello extends JTable&lt;br /&gt;
{&lt;br /&gt;
    /**&lt;br /&gt;
     * Primary Key&lt;br /&gt;
     *&lt;br /&gt;
     * @var int&lt;br /&gt;
     */&lt;br /&gt;
    var $id = null;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * @var string&lt;br /&gt;
     */&lt;br /&gt;
    var $greeting = null;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Constructor&lt;br /&gt;
     *&lt;br /&gt;
     * @param object Database connector object&lt;br /&gt;
     */&lt;br /&gt;
    function TableHello( &amp;amp;$db ) {&lt;br /&gt;
        parent::__construct(&#039;#__hello&#039;, &#039;id&#039;, $db);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You will see here that we have defined our two fields: the id field and the greeting field. Then we have defined a constructor, which will call the constructor of the parent class and pass it the name of the table (hello), the name of the field which is the primary key (id), and the database connector object.&lt;br /&gt;
&lt;br /&gt;
This file should be called hello.php and it will go in a directory called tables in the administrator section of our component.&lt;br /&gt;
&lt;br /&gt;
== Implementing the Function in our Model ==&lt;br /&gt;
&lt;br /&gt;
We are now ready to add the method to the model which will save our record. We will call this method store. Our store() method will do three things: bind the data from the form to the TableHello object, check to ensure that the record is properly formed, and store the record in the database.&lt;br /&gt;
&lt;br /&gt;
Our method looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Method to store a record&lt;br /&gt;
 *&lt;br /&gt;
 * @access    public&lt;br /&gt;
 * @return    boolean    True on success&lt;br /&gt;
 */&lt;br /&gt;
function store()&lt;br /&gt;
{&lt;br /&gt;
    $row =&amp;amp; $this-&amp;gt;getTable();&lt;br /&gt;
&lt;br /&gt;
    $data = JRequest::get( &#039;post&#039; );&lt;br /&gt;
    // Bind the form fields to the hello table&lt;br /&gt;
    if (!$row-&amp;gt;bind($data)) {&lt;br /&gt;
        $this-&amp;gt;setError($this-&amp;gt;_db-&amp;gt;getErrorMsg());&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // Make sure the hello record is valid&lt;br /&gt;
    if (!$row-&amp;gt;check()) {&lt;br /&gt;
        $this-&amp;gt;setError($this-&amp;gt;_db-&amp;gt;getErrorMsg());&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // Store the web link table to the database&lt;br /&gt;
    if (!$row-&amp;gt;store()) {&lt;br /&gt;
        $this-&amp;gt;setError($this-&amp;gt;_db-&amp;gt;getErrorMsg());&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return true;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This method gets added to the hello model.&lt;br /&gt;
&lt;br /&gt;
The method takes one parameter, which is an associative array of data that we want to store in the database. This can easily be retrieved from the request as will be seen later.&lt;br /&gt;
&lt;br /&gt;
You will see that the first line retrieves a reference to our JTable object. If we name our table properly, we don&#039;t have to specify its name - the JModel class knows where to find it. You may recall that we called our table class TableHello and put it in a file called hello.php in the tables directory. If you follow this convention, the JModel class can create your object automatically.&lt;br /&gt;
&lt;br /&gt;
The second line will retrieve the data from the form. The JRequest class makes this very easy. In this case, we are retrieving all of the variables that were submitted using the &#039;POST&#039; method. These will be returned as an associative array.&lt;br /&gt;
&lt;br /&gt;
The rest is easy - we bind, check and store. The bind() method will copy values from the array into the corresponding property of the table object. In this case, it will take the values of id and greeting and copy them to our TableHello object.&lt;br /&gt;
&lt;br /&gt;
The check() method will perform data verification. In the JTable() class, this method simply returns true. While this doesn&#039;t provide any value for us currently, by calling this method we make it possible to do data checking using our TableHello class in the future. This method can be overridden in our TableHello class with a method that performs the appropriate checks.&lt;br /&gt;
&lt;br /&gt;
The store() method will take the data that is in the object and store it in the database. If the id is 0, it will create a new record (INSERT), otherwise, it will update the existing record (UPDATE).&lt;br /&gt;
&lt;br /&gt;
== Adding the Task to the Controller ==&lt;br /&gt;
&lt;br /&gt;
We are now ready to add our task to the controller. Since the task that we are firing is called &#039;save&#039;, we must call our method &#039;save&#039;. This is simple:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * save a record (and redirect to main page)&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function save()&lt;br /&gt;
{&lt;br /&gt;
    $model = $this-&amp;gt;getModel(&#039;hello&#039;);&lt;br /&gt;
&lt;br /&gt;
    if ($model-&amp;gt;store()) {&lt;br /&gt;
        $msg = JText::_( &#039;Greeting Saved!&#039; );&lt;br /&gt;
    } else {&lt;br /&gt;
        $msg = JText::_( &#039;Error Saving Greeting&#039; );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // Check the table in so it can be edited.... we are done with it anyway&lt;br /&gt;
    $link = &#039;index.php?option=com_hello&#039;;&lt;br /&gt;
    $this-&amp;gt;setRedirect($link, $msg);&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All we do is get our model and invoke the store() method. Then we use the setRedirect() method to redirect back to our list of greetings. We also pass a message along, which will be displayed at the top of the page.&lt;br /&gt;
&lt;br /&gt;
==== Deleting a Record ====&lt;br /&gt;
&lt;br /&gt;
=== Implementing the Function in the Model ===&lt;br /&gt;
&lt;br /&gt;
In the model, we will retrieve the list of IDs to delete and call the JTable class to delete them. Here it is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * Method to delete record(s)&lt;br /&gt;
 *&lt;br /&gt;
 * @access    public&lt;br /&gt;
 * @return    boolean    True on success&lt;br /&gt;
 */&lt;br /&gt;
function delete()&lt;br /&gt;
{&lt;br /&gt;
    $cids = JRequest::getVar( &#039;cid&#039;, array(0), &#039;post&#039;, &#039;array&#039; );&lt;br /&gt;
    $row =&amp;amp; $this-&amp;gt;getTable();&lt;br /&gt;
&lt;br /&gt;
    foreach($cids as $cid) {&lt;br /&gt;
        if (!$row-&amp;gt;delete( $cid )) {&lt;br /&gt;
            $this-&amp;gt;setError( $row-&amp;gt;getErrorMsg() );&lt;br /&gt;
            return false;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return true;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We invoke the JRequest::getVar() method to get the data from the request, then we invoke the $row-&amp;gt;delete() method to delete each row. By storing errors in the model we make it possible to retrieve them later if we so choose.&lt;br /&gt;
&lt;br /&gt;
=== Handling the Remove Task in the Controller ===&lt;br /&gt;
&lt;br /&gt;
This is similar to the save() method which handled the save task:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * remove record(s)&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function remove()&lt;br /&gt;
{&lt;br /&gt;
    $model = $this-&amp;gt;getModel(&#039;hello&#039;);&lt;br /&gt;
    if(!$model-&amp;gt;delete()) {&lt;br /&gt;
        $msg = JText::_( &#039;Error: One or More Greetings Could not be Deleted&#039; );&lt;br /&gt;
    } else {&lt;br /&gt;
        $msg = JText::_( &#039;Greeting(s) Deleted&#039; );&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    $this-&amp;gt;setRedirect( &#039;index.php?option=com_hello&#039;, $msg );&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Cancelling the Edit Operation ====&lt;br /&gt;
&lt;br /&gt;
To cancel the edit operation, all we have to do is redirect back to the main view:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;/**&lt;br /&gt;
 * cancel editing a record&lt;br /&gt;
 * @return void&lt;br /&gt;
 */&lt;br /&gt;
function cancel()&lt;br /&gt;
{&lt;br /&gt;
    $msg = JText::_( &#039;Operation Cancelled&#039; );&lt;br /&gt;
    $this-&amp;gt;setRedirect( &#039;index.php?option=com_hello&#039;, $msg );&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== Conclusion =====&lt;br /&gt;
&lt;br /&gt;
We have now implemented a basic backend to our component. We are now able to edit the entries that are viewed in the frontend. We have demonstrated the interaction between models, views and controllers. We have shown how the JTable class can be extended to provide easy access to tables in the database. It can also be seen how the JToolBarHelper class can be used to create button bars in components to present a standardized look between components.&lt;br /&gt;
&lt;br /&gt;
===== Contributors =====&lt;br /&gt;
&lt;br /&gt;
  * staalanden&lt;br /&gt;
&lt;br /&gt;
===== Download =====&lt;br /&gt;
&lt;br /&gt;
The component can be downloaded at: [http://joomlacode.org/gf/download/frsrelease/8104/29402/com_hello4_01.zip com_hello4_01]&lt;/div&gt;</summary>
		<author><name>Tonie</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Using_the_Database&amp;diff=9496</id>
		<title>J1.5:Developing a MVC Component/Using the Database</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Using_the_Database&amp;diff=9496"/>
		<updated>2008-07-27T18:22:08Z</updated>

		<summary type="html">&lt;p&gt;Tonie: /* Retrieving the Data */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
In the first two tutorials, we showed you how to build a simple model-view-controller component. We had one view which retrieved data from a model (which was created in the 2nd tutorial). In this tutorial, we will be working with the model. Instead of the data being hard coded in the model, the model will retrieve the data from a table in the database.&lt;br /&gt;
&lt;br /&gt;
This tutorial will demonstrate how to use the JDatabase class to retrieve data from the database.&lt;br /&gt;
&lt;br /&gt;
== Retrieving the Data ==&lt;br /&gt;
&lt;br /&gt;
Our model currently has one method: getGreeting(). This method is very simple - all it does is return the hard-coded greeting.&lt;br /&gt;
&lt;br /&gt;
To make things more interesting, we will load the greeting from a database table. We will demonstrate later how to create an SQL file and add the appropriate code to the XML manifest file so that the table and some sample data will be created when the component is installed. For now, we will simply replace our return statement with some code that will retrieve the greeting from the database and return it.&lt;br /&gt;
&lt;br /&gt;
The first step is to obtain a reference to a database object. Since Joomla! uses the database for its normal operation, a database connection already exists; therefore, it is not necessary to create your own. A reference to the existing database can be obtained using:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$db =&amp;amp; JFactory::getDBO();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
JFactory is a static class that is used to retrieve references to many of the system objects. More information about this class can be found in the API documentation.&lt;br /&gt;
&lt;br /&gt;
The method name (getDBO) stands for get DataBase Object, and is easy and important to remember.&lt;br /&gt;
&lt;br /&gt;
Now that we have obtained a reference to the database object, we can retrieve our data. We do this in two steps:&lt;br /&gt;
&lt;br /&gt;
* store our query in the database object&lt;br /&gt;
* load the result&lt;br /&gt;
&lt;br /&gt;
Our new getGreeting() method will therefore look like:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;function getGreeting()&lt;br /&gt;
{&lt;br /&gt;
   $db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
&lt;br /&gt;
   $query = &#039;SELECT greeting FROM #__hello&#039;;&lt;br /&gt;
   $db-&amp;gt;setQuery( $query );&lt;br /&gt;
   $greeting = $db-&amp;gt;loadResult();&lt;br /&gt;
&lt;br /&gt;
   return $greeting;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
hello is the name of the table that we will create later, and greeting is the name of the field that stores the greetings. If you are not familiar with SQL, it would be helpful to take a tutorial or a lesson to get yourself up to speed. One such tutorial can be found at [[http://www.w3schools.com/sql/default.asp|w3schools]].&lt;br /&gt;
&lt;br /&gt;
The $db-&amp;gt;loadResult() method will execute the stored database query and return the first field of the first row of the result. See [[http://dev.joomla.org/component/option,com_jd-wiki/Itemid,/id,references:joomla.framework:database:jdatabase/|JDatabase API reference]] for more information about other load methods in the JDatabase class.&lt;br /&gt;
&lt;br /&gt;
== Creating the Installation SQL File ==&lt;br /&gt;
&lt;br /&gt;
The Joomla! installer has built-in support for executing queries during component installation. These queries are all stored in a standard text file.&lt;br /&gt;
&lt;br /&gt;
We will have three queries in our install file: the first will drop the table in case it already exists, the second will create the table with the appropriate fields, and the third will insert the data.&lt;br /&gt;
&lt;br /&gt;
Here are our queries:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;DROP TABLE IF EXISTS `#__hello`;&lt;br /&gt;
&lt;br /&gt;
CREATE TABLE `#__hello` (&lt;br /&gt;
  `id` int(11) NOT NULL auto_increment,&lt;br /&gt;
  `greeting` varchar(25) NOT NULL,&lt;br /&gt;
  PRIMARY KEY  (`id`)&lt;br /&gt;
) ENGINE=MyISAM AUTO_INCREMENT DEFAULT CHARSET=utf8;&lt;br /&gt;
&lt;br /&gt;
INSERT INTO `#__hello` (`greeting`) VALUES (&#039;Hello, World!&#039;),&lt;br /&gt;
(&#039;Bonjour, Monde!&#039;),&lt;br /&gt;
(&#039;Ciao, Mondo!&#039;);&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You might find the  prefix on the table names rather odd. Joomla! will replace this prefix with the prefix used by the current install. For most installs, this table will become jos_hello. This allows multiple installs of Joomla! to use the same database, and prevents collisions with other applications using the same table names (i.e. two applications might share a database, but might both require a &#039;users&#039; table. This convention avoids problems.)&lt;br /&gt;
&lt;br /&gt;
We have specified two fields in our database. The first field is id, and is called the &#039;primary key&#039;. The primary key of a database table is a field that is used to uniquely identify a record. This is often used to lookup rows in the database. The other field is greeting. This is the field that stores the greeting that is returned from the query that we used above.&lt;br /&gt;
&lt;br /&gt;
We will save our queries in a file called install.utf.sql.&lt;br /&gt;
&lt;br /&gt;
=== Creating the Uninstall SQL File ===&lt;br /&gt;
&lt;br /&gt;
Though we might hope that people will never want to uninstall our component, it is important that if they do, we don&#039;t leave anything behind. Joomla! will look after deleting the files and directories that were created during install, but you must manually include queries that will remove and tables that have been added to the database. Since we have only created one table, we only need one query:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;DROP TABLE IF EXISTS `#__hello`;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will save this query in a file called uninstall.utf.sql.&lt;br /&gt;
&lt;br /&gt;
== Updating our Install File ==&lt;br /&gt;
&lt;br /&gt;
We need to change a few things in our install file. First, we need to add our two new files to the list of files to install. SQL install file have to go in the admin directory. Second, we need to tell the installer to execute the queries in our files on install and uninstall.&lt;br /&gt;
&lt;br /&gt;
Our new file looks like this:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;&lt;br /&gt;
&amp;lt;install type=&amp;quot;component&amp;quot; version=&amp;quot;1.5.0&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;name&amp;gt;Hello&amp;lt;/name&amp;gt;&lt;br /&gt;
 &amp;lt;!-- The following elements are optional and free of formatting conttraints --&amp;gt;&lt;br /&gt;
 &amp;lt;creationDate&amp;gt;2007-02-22&amp;lt;/creationDate&amp;gt;&lt;br /&gt;
 &amp;lt;author&amp;gt;John Doe&amp;lt;/author&amp;gt;&lt;br /&gt;
 &amp;lt;authorEmail&amp;gt;john.doe@example.org&amp;lt;/authorEmail&amp;gt;&lt;br /&gt;
 &amp;lt;authorUrl&amp;gt;http://www.example.org&amp;lt;/authorUrl&amp;gt;&lt;br /&gt;
 &amp;lt;copyright&amp;gt;Copyright Info&amp;lt;/copyright&amp;gt;&lt;br /&gt;
 &amp;lt;license&amp;gt;License Info&amp;lt;/license&amp;gt;&lt;br /&gt;
 &amp;lt;!--  The version string is recorded in the components table --&amp;gt;&lt;br /&gt;
 &amp;lt;version&amp;gt;3.01&amp;lt;/version&amp;gt;&lt;br /&gt;
 &amp;lt;!-- The description is optional and defaults to the name --&amp;gt;&lt;br /&gt;
 &amp;lt;description&amp;gt;Description of the component ...&amp;lt;/description&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;!-- Site Main File Copy Section --&amp;gt;&lt;br /&gt;
 &amp;lt;!-- Note the folder attribute: This attribute describes the folder&lt;br /&gt;
      to copy FROM in the package to install therefore files copied&lt;br /&gt;
      in this section are copied from /site/ in the package --&amp;gt;&lt;br /&gt;
 &amp;lt;files folder=&amp;quot;site&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;controller.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;models/hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;models/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/view.html.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/tmpl/default.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/tmpl/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
 &amp;lt;/files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;install&amp;gt;&lt;br /&gt;
  &amp;lt;sql&amp;gt;&lt;br /&gt;
   &amp;lt;file charset=&amp;quot;utf8&amp;quot; driver=&amp;quot;mysql&amp;quot;&amp;gt;install.sql&amp;lt;/file&amp;gt;&lt;br /&gt;
  &amp;lt;/sql&amp;gt;&lt;br /&gt;
 &amp;lt;/install&amp;gt;&lt;br /&gt;
 &amp;lt;uninstall&amp;gt;&lt;br /&gt;
  &amp;lt;sql&amp;gt;&lt;br /&gt;
   &amp;lt;file charset=&amp;quot;utf8&amp;quot; driver=&amp;quot;mysql&amp;quot;&amp;gt;uninstall.sql&amp;lt;/file&amp;gt;&lt;br /&gt;
  &amp;lt;/sql&amp;gt;&lt;br /&gt;
 &amp;lt;/uninstall&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;administration&amp;gt;&lt;br /&gt;
  &amp;lt;!-- Administration Menu Section --&amp;gt;&lt;br /&gt;
  &amp;lt;menu&amp;gt;Hello World!&amp;lt;/menu&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;!-- Administration Main File Copy Section --&amp;gt;&lt;br /&gt;
  &amp;lt;files folder=&amp;quot;admin&amp;quot;&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;install.sql&amp;lt;/filename&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;uninstall.sql&amp;lt;/filename&amp;gt;&lt;br /&gt;
&amp;lt;/files&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
 &amp;lt;/administration&amp;gt;&lt;br /&gt;
&amp;lt;/install&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You will notice two attributes present on the &amp;lt;file&amp;gt; tags within the &amp;lt;install&amp;gt; and &amp;lt;uninstall&amp;gt; sections: charset and driver. The charset is the type of charset to use. The only valid charset is utf8. If you want to create install files for non-utf8 databases (for older version of MySQL), you should omit this attribute.&lt;br /&gt;
&lt;br /&gt;
The driver attribute specifies which database the queries were written for. Currently, this can only be mysql, but in future versions of Joomla! there may be more database drivers available.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
We now have a component that takes advantage of both the Joomla! MVC framework classes and the JDatabase classes. You are now able to write MVC components that interact with the database and can use the Joomla! installer to create and populate database tables.&lt;br /&gt;
&lt;br /&gt;
== Contributors ==&lt;br /&gt;
* staalanden&lt;br /&gt;
&lt;br /&gt;
== Download ==&lt;br /&gt;
&lt;br /&gt;
The component can be downloaded at: [http://joomlacode.org/gf/download/frsrelease/8104/29402/com_hello3_01.zip com_hello3_01]&lt;/div&gt;</summary>
		<author><name>Tonie</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Using_the_Database&amp;diff=9495</id>
		<title>J1.5:Developing a MVC Component/Using the Database</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Using_the_Database&amp;diff=9495"/>
		<updated>2008-07-27T18:19:56Z</updated>

		<summary type="html">&lt;p&gt;Tonie: /* Download */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
In the first two tutorials, we showed you how to build a simple model-view-controller component. We had one view which retrieved data from a model (which was created in the 2nd tutorial). In this tutorial, we will be working with the model. Instead of the data being hard coded in the model, the model will retrieve the data from a table in the database.&lt;br /&gt;
&lt;br /&gt;
This tutorial will demonstrate how to use the JDatabase class to retrieve data from the database.&lt;br /&gt;
&lt;br /&gt;
== Retrieving the Data ==&lt;br /&gt;
&lt;br /&gt;
Our model currently has one method: getGreeting(). This method is very simple - all it does is return the hard-coded greeting.&lt;br /&gt;
&lt;br /&gt;
To make things more interesting, we will load the greeting from a database table. We will demonstrate later how to create an SQL file and add the appropriate code to the XML manifest file so that the table and some sample data will be created when the component is installed. For now, we will simply replace our return statement with some code that will retrieve the greeting from the database and return it.&lt;br /&gt;
&lt;br /&gt;
The first step is to obtain a reference to a database object. Since Joomla! uses the database for its normal operation, a database connection already exists; therefore, it is not necessary to create your own. A reference to the existing database can be obtained using:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$db =&amp;amp; JFactory::getDBO();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
JFactory is a static class that is used to retrieve references to many of the system objects. More information about this class can be found in the API documentation.&lt;br /&gt;
&lt;br /&gt;
The method name (getDBO) stands for get DataBase Object, and is easy and important to remember.&lt;br /&gt;
&lt;br /&gt;
Now that we have obtained a reference to the database object, we can retrieve our data. We do this in two steps:&lt;br /&gt;
&lt;br /&gt;
  * store our query in the database object&lt;br /&gt;
&lt;br /&gt;
  * load the result&lt;br /&gt;
&lt;br /&gt;
Our new getGreeting() method will therefore look like:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;function getGreeting()&lt;br /&gt;
{&lt;br /&gt;
   $db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
&lt;br /&gt;
   $query = &#039;SELECT greeting FROM #__hello&#039;;&lt;br /&gt;
   $db-&amp;gt;setQuery( $query );&lt;br /&gt;
   $greeting = $db-&amp;gt;loadResult();&lt;br /&gt;
&lt;br /&gt;
   return $greeting;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
hello is the name of the table that we will create later, and greeting is the name of the field that stores the greetings. If you are not familiar with SQL, it would be helpful to take a tutorial or a lesson to get yourself up to speed. One such tutorial can be found at [[http://www.w3schools.com/sql/default.asp|w3schools]].&lt;br /&gt;
&lt;br /&gt;
The $db-&amp;gt;loadResult() method will execute the stored database query and return the first field of the first row of the result. See [[http://dev.joomla.org/component/option,com_jd-wiki/Itemid,/id,references:joomla.framework:database:jdatabase/|JDatabase API reference]] for more information about other load methods in the JDatabase class.&lt;br /&gt;
&lt;br /&gt;
== Creating the Installation SQL File ==&lt;br /&gt;
&lt;br /&gt;
The Joomla! installer has built-in support for executing queries during component installation. These queries are all stored in a standard text file.&lt;br /&gt;
&lt;br /&gt;
We will have three queries in our install file: the first will drop the table in case it already exists, the second will create the table with the appropriate fields, and the third will insert the data.&lt;br /&gt;
&lt;br /&gt;
Here are our queries:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;DROP TABLE IF EXISTS `#__hello`;&lt;br /&gt;
&lt;br /&gt;
CREATE TABLE `#__hello` (&lt;br /&gt;
  `id` int(11) NOT NULL auto_increment,&lt;br /&gt;
  `greeting` varchar(25) NOT NULL,&lt;br /&gt;
  PRIMARY KEY  (`id`)&lt;br /&gt;
) ENGINE=MyISAM AUTO_INCREMENT DEFAULT CHARSET=utf8;&lt;br /&gt;
&lt;br /&gt;
INSERT INTO `#__hello` (`greeting`) VALUES (&#039;Hello, World!&#039;),&lt;br /&gt;
(&#039;Bonjour, Monde!&#039;),&lt;br /&gt;
(&#039;Ciao, Mondo!&#039;);&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You might find the  prefix on the table names rather odd. Joomla! will replace this prefix with the prefix used by the current install. For most installs, this table will become jos_hello. This allows multiple installs of Joomla! to use the same database, and prevents collisions with other applications using the same table names (i.e. two applications might share a database, but might both require a &#039;users&#039; table. This convention avoids problems.)&lt;br /&gt;
&lt;br /&gt;
We have specified two fields in our database. The first field is id, and is called the &#039;primary key&#039;. The primary key of a database table is a field that is used to uniquely identify a record. This is often used to lookup rows in the database. The other field is greeting. This is the field that stores the greeting that is returned from the query that we used above.&lt;br /&gt;
&lt;br /&gt;
We will save our queries in a file called install.utf.sql.&lt;br /&gt;
&lt;br /&gt;
=== Creating the Uninstall SQL File ===&lt;br /&gt;
&lt;br /&gt;
Though we might hope that people will never want to uninstall our component, it is important that if they do, we don&#039;t leave anything behind. Joomla! will look after deleting the files and directories that were created during install, but you must manually include queries that will remove and tables that have been added to the database. Since we have only created one table, we only need one query:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;DROP TABLE IF EXISTS `#__hello`;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will save this query in a file called uninstall.utf.sql.&lt;br /&gt;
&lt;br /&gt;
== Updating our Install File ==&lt;br /&gt;
&lt;br /&gt;
We need to change a few things in our install file. First, we need to add our two new files to the list of files to install. SQL install file have to go in the admin directory. Second, we need to tell the installer to execute the queries in our files on install and uninstall.&lt;br /&gt;
&lt;br /&gt;
Our new file looks like this:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;&lt;br /&gt;
&amp;lt;install type=&amp;quot;component&amp;quot; version=&amp;quot;1.5.0&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;name&amp;gt;Hello&amp;lt;/name&amp;gt;&lt;br /&gt;
 &amp;lt;!-- The following elements are optional and free of formatting conttraints --&amp;gt;&lt;br /&gt;
 &amp;lt;creationDate&amp;gt;2007-02-22&amp;lt;/creationDate&amp;gt;&lt;br /&gt;
 &amp;lt;author&amp;gt;John Doe&amp;lt;/author&amp;gt;&lt;br /&gt;
 &amp;lt;authorEmail&amp;gt;john.doe@example.org&amp;lt;/authorEmail&amp;gt;&lt;br /&gt;
 &amp;lt;authorUrl&amp;gt;http://www.example.org&amp;lt;/authorUrl&amp;gt;&lt;br /&gt;
 &amp;lt;copyright&amp;gt;Copyright Info&amp;lt;/copyright&amp;gt;&lt;br /&gt;
 &amp;lt;license&amp;gt;License Info&amp;lt;/license&amp;gt;&lt;br /&gt;
 &amp;lt;!--  The version string is recorded in the components table --&amp;gt;&lt;br /&gt;
 &amp;lt;version&amp;gt;3.01&amp;lt;/version&amp;gt;&lt;br /&gt;
 &amp;lt;!-- The description is optional and defaults to the name --&amp;gt;&lt;br /&gt;
 &amp;lt;description&amp;gt;Description of the component ...&amp;lt;/description&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;!-- Site Main File Copy Section --&amp;gt;&lt;br /&gt;
 &amp;lt;!-- Note the folder attribute: This attribute describes the folder&lt;br /&gt;
      to copy FROM in the package to install therefore files copied&lt;br /&gt;
      in this section are copied from /site/ in the package --&amp;gt;&lt;br /&gt;
 &amp;lt;files folder=&amp;quot;site&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;controller.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;models/hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;models/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/view.html.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/tmpl/default.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/tmpl/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
 &amp;lt;/files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;install&amp;gt;&lt;br /&gt;
  &amp;lt;sql&amp;gt;&lt;br /&gt;
   &amp;lt;file charset=&amp;quot;utf8&amp;quot; driver=&amp;quot;mysql&amp;quot;&amp;gt;install.sql&amp;lt;/file&amp;gt;&lt;br /&gt;
  &amp;lt;/sql&amp;gt;&lt;br /&gt;
 &amp;lt;/install&amp;gt;&lt;br /&gt;
 &amp;lt;uninstall&amp;gt;&lt;br /&gt;
  &amp;lt;sql&amp;gt;&lt;br /&gt;
   &amp;lt;file charset=&amp;quot;utf8&amp;quot; driver=&amp;quot;mysql&amp;quot;&amp;gt;uninstall.sql&amp;lt;/file&amp;gt;&lt;br /&gt;
  &amp;lt;/sql&amp;gt;&lt;br /&gt;
 &amp;lt;/uninstall&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;administration&amp;gt;&lt;br /&gt;
  &amp;lt;!-- Administration Menu Section --&amp;gt;&lt;br /&gt;
  &amp;lt;menu&amp;gt;Hello World!&amp;lt;/menu&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;!-- Administration Main File Copy Section --&amp;gt;&lt;br /&gt;
  &amp;lt;files folder=&amp;quot;admin&amp;quot;&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;install.sql&amp;lt;/filename&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;uninstall.sql&amp;lt;/filename&amp;gt;&lt;br /&gt;
&amp;lt;/files&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
 &amp;lt;/administration&amp;gt;&lt;br /&gt;
&amp;lt;/install&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You will notice two attributes present on the &amp;lt;file&amp;gt; tags within the &amp;lt;install&amp;gt; and &amp;lt;uninstall&amp;gt; sections: charset and driver. The charset is the type of charset to use. The only valid charset is utf8. If you want to create install files for non-utf8 databases (for older version of MySQL), you should omit this attribute.&lt;br /&gt;
&lt;br /&gt;
The driver attribute specifies which database the queries were written for. Currently, this can only be mysql, but in future versions of Joomla! there may be more database drivers available.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
We now have a component that takes advantage of both the Joomla! MVC framework classes and the JDatabase classes. You are now able to write MVC components that interact with the database and can use the Joomla! installer to create and populate database tables.&lt;br /&gt;
&lt;br /&gt;
== Contributors ==&lt;br /&gt;
* staalanden&lt;br /&gt;
&lt;br /&gt;
== Download ==&lt;br /&gt;
&lt;br /&gt;
The component can be downloaded at: [http://joomlacode.org/gf/download/frsrelease/8104/29402/com_hello3_01.zip com_hello3_01]&lt;/div&gt;</summary>
		<author><name>Tonie</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Adding_a_Model&amp;diff=9494</id>
		<title>J1.5:Developing a MVC Component/Adding a Model</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Adding_a_Model&amp;diff=9494"/>
		<updated>2008-07-27T18:19:40Z</updated>

		<summary type="html">&lt;p&gt;Tonie: /* Download */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
In the first tutorial of this series, creating a simple view-controller component using the Joomla! 1.5 CMS framework was demonstrated.&lt;br /&gt;
&lt;br /&gt;
In the first tutorial, the greeting was hardcoded into the view. This doesn&#039;t follow the MVC pattern exactly because the view is intended to only display the data, and not contain it.&lt;br /&gt;
&lt;br /&gt;
In this second part of the tutorial we will demonstrate how to move this out of the view and into a model. In future tutorials we will demonstrate the power and flexibility that this design pattern provides.&lt;br /&gt;
&lt;br /&gt;
== Creating the Model ==&lt;br /&gt;
&lt;br /&gt;
The concept of model gets its name because this class is intended to represent (or &#039;model&#039;) some entity. In our case, our first model will represent a &#039;hello&#039;, or a greeting. This is in line with our design thus far, because we have one view (&#039;hello&#039;), which is a view of our greeting.&lt;br /&gt;
&lt;br /&gt;
The naming convention for models in the Joomla! framework is that the class name starts with the name of the component (in our case &#039;hello&#039;, followed by &#039;model&#039;, followed by the model name. Therefore, our model class is called HelloModelHello.&lt;br /&gt;
&lt;br /&gt;
At this point, we will only model one behaviour of our hello, and that is retrieving the greeting. We will thus have one method, called getGreeting(). It will simply return the string &#039;Hello, World!&#039;.&lt;br /&gt;
&lt;br /&gt;
Here is the code for our model class:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * Hello Model for Hello World Component&lt;br /&gt;
 * &lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_2&lt;br /&gt;
 * @license    GNU/GPL&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// No direct access&lt;br /&gt;
&lt;br /&gt;
defined( &#039;_JEXEC&#039; ) or die( &#039;Restricted access&#039; );&lt;br /&gt;
&lt;br /&gt;
jimport( &#039;joomla.application.component.model&#039; );&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Hello Model&lt;br /&gt;
 *&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 */&lt;br /&gt;
class HelloModelHello extends JModel&lt;br /&gt;
{&lt;br /&gt;
    /**&lt;br /&gt;
    * Gets the greeting&lt;br /&gt;
    * @return string The greeting to be displayed to the user&lt;br /&gt;
    */&lt;br /&gt;
    function getGreeting()&lt;br /&gt;
    {&lt;br /&gt;
        return &#039;Hello, World!&#039;;&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You will notice a line that starts with jimport. The jimport function is used to load files from the Joomla! framework that are required for our component. This particular statement will load the file /libraries/joomla/application/component/model.php. The &#039;.&#039;s are used as directory separators and the last part is the name of the file to load. All files are loaded relative to the libraries directory. This particular file contains the class definition for the JModel class, which is necessary because our model extends this class.&lt;br /&gt;
&lt;br /&gt;
Now that we have created our model, we must modify our view so that it uses it to obtain the greeting.&lt;br /&gt;
&lt;br /&gt;
== Using the Model ==&lt;br /&gt;
&lt;br /&gt;
The Joomla! framework is setup in such a way that the controller will automatically load the model that has the same name as the view and will push it into the view. Since our view is called &#039;Hello&#039;, our &#039;Hello&#039; model will automatically be loaded and pushed into the view. Therefore, we can easily retrieve a reference to our model using the JView::getModel() method.&lt;br /&gt;
&lt;br /&gt;
Our previous view code contained the lines:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$greeting = &amp;quot;Hello World!&amp;quot;;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To take advantage of our model, we change this line to:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$model =&amp;amp; $this-&amp;gt;getModel();&lt;br /&gt;
$greeting = $model-&amp;gt;getGreeting();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The complete view now looks like:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_2&lt;br /&gt;
 * @license    GNU/GPL&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
// No direct access&lt;br /&gt;
&lt;br /&gt;
defined( &#039;_JEXEC&#039; ) or die( &#039;Restricted access&#039; );&lt;br /&gt;
&lt;br /&gt;
jimport( &#039;joomla.application.component.view&#039;);&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * HTML View class for the HelloWorld Component&lt;br /&gt;
 *&lt;br /&gt;
 * @package    HelloWorld&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
class HelloViewHello extends JView&lt;br /&gt;
{&lt;br /&gt;
    function display($tpl = null)&lt;br /&gt;
    {&lt;br /&gt;
        $model =&amp;amp; $this-&amp;gt;getModel();&lt;br /&gt;
        $greeting = $model-&amp;gt;getGreeting();&lt;br /&gt;
        $this-&amp;gt;assignRef( &#039;greeting&#039;,	$greeting );&lt;br /&gt;
&lt;br /&gt;
        parent::display($tpl);&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Adding the File to the Package ===&lt;br /&gt;
&lt;br /&gt;
All that remains is to add an entry to the XML file so that our new model will be copied. The Joomla! framework will look for our model in the models directory, so the entry for this file will look like (it should be added to the site section):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;filename&amp;gt;models/hello.php&amp;lt;/filename&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Our new hello.xml file will look like:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;&lt;br /&gt;
&amp;lt;install type=&amp;quot;component&amp;quot; version=&amp;quot;1.5.0&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;name&amp;gt;Hello&amp;lt;/name&amp;gt;&lt;br /&gt;
 &amp;lt;!-- The following elements are optional and free of formatting conttraints --&amp;gt;&lt;br /&gt;
 &amp;lt;creationDate&amp;gt;2007-02-22&amp;lt;/creationDate&amp;gt;&lt;br /&gt;
 &amp;lt;author&amp;gt;John Doe&amp;lt;/author&amp;gt;&lt;br /&gt;
 &amp;lt;authorEmail&amp;gt;john.doe@example.org&amp;lt;/authorEmail&amp;gt;&lt;br /&gt;
 &amp;lt;authorUrl&amp;gt;http://www.example.org&amp;lt;/authorUrl&amp;gt;&lt;br /&gt;
 &amp;lt;copyright&amp;gt;Copyright Info&amp;lt;/copyright&amp;gt;&lt;br /&gt;
 &amp;lt;license&amp;gt;License Info&amp;lt;/license&amp;gt;&lt;br /&gt;
 &amp;lt;!--  The version string is recorded in the components table --&amp;gt;&lt;br /&gt;
 &amp;lt;version&amp;gt;1.01&amp;lt;/version&amp;gt;&lt;br /&gt;
 &amp;lt;!-- The description is optional and defaults to the name --&amp;gt;&lt;br /&gt;
 &amp;lt;description&amp;gt;Description of the component ...&amp;lt;/description&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;!-- Site Main File Copy Section --&amp;gt;&lt;br /&gt;
 &amp;lt;!-- Note the folder attribute: This attribute describes the folder&lt;br /&gt;
      to copy FROM in the package to install therefore files copied&lt;br /&gt;
      in this section are copied from /site/ in the package --&amp;gt;&lt;br /&gt;
 &amp;lt;files folder=&amp;quot;site&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;controller.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;models/hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;models/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/view.html.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/tmpl/default.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/tmpl/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
 &amp;lt;/files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;administration&amp;gt;&lt;br /&gt;
  &amp;lt;!-- Administration Menu Section --&amp;gt;&lt;br /&gt;
  &amp;lt;menu&amp;gt;Hello World!&amp;lt;/menu&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;!-- Administration Main File Copy Section --&amp;gt;&lt;br /&gt;
  &amp;lt;files folder=&amp;quot;admin&amp;quot;&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;/files&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
 &amp;lt;/administration&amp;gt;&lt;br /&gt;
&amp;lt;/install&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
We now have a simple MVC component. Each element is very simple at this point, but provides a great deal of flexibility and power.&lt;br /&gt;
&lt;br /&gt;
== Contributors ==&lt;br /&gt;
* staalanden&lt;br /&gt;
&lt;br /&gt;
== Download ==&lt;br /&gt;
&lt;br /&gt;
The component can be downloaded at: [http://joomlacode.org/gf/download/frsrelease/8104/29402/com_hello2_01.zip com_hello2_01]&lt;/div&gt;</summary>
		<author><name>Tonie</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Introduction&amp;diff=9493</id>
		<title>J1.5:Developing a MVC Component/Introduction</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Introduction&amp;diff=9493"/>
		<updated>2008-07-27T18:19:17Z</updated>

		<summary type="html">&lt;p&gt;Tonie: /* Download */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
A software framework is the base of an application that can be used by a developer. The framework in Joomla! 1.5 unleashes a great deal of power for them. The Joomla! code has been completely overhauled and cleaned up. This tutorial will guide you through the process of developing a component using the framework.&lt;br /&gt;
&lt;br /&gt;
The scope of this project will be to develop a simple Hello World! component. In future tutorials, this simple framework will be built upon to show the full power and versatility of the MVC design pattern in Joomla!&lt;br /&gt;
&lt;br /&gt;
== Requirements ==&lt;br /&gt;
You need Joomla! 1.5 or greater for this tutorial.&lt;br /&gt;
&lt;br /&gt;
== Introduction to Model-View-Controller ==&lt;br /&gt;
While the idea behind a component may seem extremely simple, code can quickly become very complex as additional features are added or the interface is customized.&lt;br /&gt;
&lt;br /&gt;
Model-View-Controller (herein referred to as MVC) is a software design pattern that can be used to organize code in such a way that the business logic and data presentation are separate. The premise behind this approach is that if the business logic is grouped into one section, then the interface and user interaction that surrounds the data can revised and customized without having to reprogram the business logic.&lt;br /&gt;
&lt;br /&gt;
There are three main parts of an MVC component. They are described here in brief, but for a more thorough explanation, please refer to the links provided at the end of this tutorial.&lt;br /&gt;
&lt;br /&gt;
=== Model ===&lt;br /&gt;
The model is the part of the component that encapsulates the application&#039;s data. It will often provide routines to manage and manipulate this data in a meaningful way in addition to routines that retrieve the data from the model. In our case, the model will contain methods to add, remove and update information about the greetings in the database. It will also contain methods to retrieve the list of greetings from the database. In general, the underlying data access technique should be encapsulated in the model. In this way, if an application is to be moved from a system that utilizes a flat file to store its information to a system that uses a database, the model is the only element that needs to be changed, not the view or the controller.&lt;br /&gt;
&lt;br /&gt;
=== View ===&lt;br /&gt;
The view is the part of the component that is used to render the data from the model in a manner that is suitable for interaction. For a web-based application, the view would generally be an HTML page that is returned to the data. The view pulls data from the model (which is passed to it from the controller) and feeds the data into a template which is populated and presented to the user. The view does not cause the data to be modified in any way, it only displays data retrieved from the model.&lt;br /&gt;
&lt;br /&gt;
=== Controller ===&lt;br /&gt;
The controller is responsible for responding to user actions. In the case of a web application, a user action is (generally) a page request. The controller will determine what request is being made by the user and respond appropriately by triggering the model to manipulate the data appropriately and passing the model into the view. The controller does not display the data in the model, it only triggers methods in the model which modify the data, and then pass the model into the view which displays the data.&lt;br /&gt;
&lt;br /&gt;
== Joomla! MVC Implementation ==&lt;br /&gt;
In Joomla!, the MVC pattern is implemented using three classes: [http://api.joomla.org/Joomla-Framework/Application/JModel.html JModel], [http://api.joomla.org/Joomla-Framework/Application/JView.html JView] and [http://api.joomla.org/Joomla-Framework/Application/JController.html JController]. For more detailed information about these classes, please refer to the API reference documentation (WIP).&lt;br /&gt;
&lt;br /&gt;
== Creating a Component ==&lt;br /&gt;
For our basic component, we only require five files:&lt;br /&gt;
&lt;br /&gt;
* hello.php - this is the entry point to our component&lt;br /&gt;
* controller.php - this file contains our base controller&lt;br /&gt;
* views/hello/view.html.php - this file retrieves the necessary data and pushes it into the template&lt;br /&gt;
* views/hello/tmpl/default.php - this is the template for our output&lt;br /&gt;
* hello.xml - this is an XML file that tells Joomla! how to install our component.&lt;br /&gt;
&lt;br /&gt;
=== Creating the Entry Point ===&lt;br /&gt;
Joomla! is always accessed through a single point of entry: index.php for the Site Application or administrator/index.php for the Administrator Application. The application will then load the required component, based on the value of &#039;option&#039; in the URL or in the POST data. For our component, the URL would be:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;index.php?option=com_hello&amp;amp;view=hello&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will load our main file, which can be seen as the single point of entry for our component: components/com_hello/hello.php.&lt;br /&gt;
&lt;br /&gt;
The code for this file is fairly typical across components.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * components/com_hello/hello.php&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_1&lt;br /&gt;
 * @license    GNU/GPL&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
// No direct access&lt;br /&gt;
&lt;br /&gt;
defined( &#039;_JEXEC&#039; ) or die( &#039;Restricted access&#039; );&lt;br /&gt;
&lt;br /&gt;
// Require the base controller&lt;br /&gt;
&lt;br /&gt;
require_once( JPATH_COMPONENT.DS.&#039;controller.php&#039; );&lt;br /&gt;
&lt;br /&gt;
// Require specific controller if requested&lt;br /&gt;
if($controller = JRequest::getWord(&#039;controller&#039;)) {&lt;br /&gt;
    $path = JPATH_COMPONENT.DS.&#039;controllers&#039;.DS.$controller.&#039;.php&#039;;&lt;br /&gt;
    if (file_exists($path)) {&lt;br /&gt;
        require_once $path;&lt;br /&gt;
    } else {&lt;br /&gt;
        $controller = &#039;&#039;;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Create the controller&lt;br /&gt;
$classname    = &#039;HelloController&#039;.$controller;&lt;br /&gt;
$controller   = new $classname( );&lt;br /&gt;
&lt;br /&gt;
// Perform the Request task&lt;br /&gt;
$controller-&amp;gt;execute( JRequest::getVar( &#039;task&#039; ) );&lt;br /&gt;
&lt;br /&gt;
// Redirect if set by the controller&lt;br /&gt;
$controller-&amp;gt;redirect();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first statement is a security check.&lt;br /&gt;
&lt;br /&gt;
JPATH_COMPONENT is the absolute path to the current component, in our case components/com_hello. If you specifically need either the Site component or the Administrator component, you can use JPATH_COMPONENT_SITE or JPATH_COMPONENT_ADMINISTRATOR.&lt;br /&gt;
&lt;br /&gt;
DS is the directory separator of your system: either &#039;/&#039; or &#039;\&#039;. This is automatically set by the framework so the developer doesn&#039;t have to worry about developing different versions for different server OSs. DS should always be used when referring to files on the local server.&lt;br /&gt;
&lt;br /&gt;
After loading the base controller, we check if a specific controller is needed. In this component, the base controller is the only controller, but we will leave this here for future use.&lt;br /&gt;
&lt;br /&gt;
JRequest:getVar() finds a variable in the URL or the POST data. So if our URL is index.php?option=com_hello&amp;gt;controller=controller_name, then we can retrieve our controller name in our component using: echo JRequest::getVar(&#039;controller&#039;);&lt;br /&gt;
&lt;br /&gt;
Now we have our base controller &#039;HelloController&#039; in com_hello/controller.php, and, if needed, additional controllers like &#039;HelloControllerController1&#039; in com_hello/controllers/controller1.php. Using this standard naming scheme will make things easy later on: &#039;{Componentname}{Controller}{Controllername}&#039;&lt;br /&gt;
&lt;br /&gt;
After the controller is created, we instruct the controller to execute the task, as defined in the URL: index.php?option=com_hello&amp;amp;task=sometask. If no task is set, the default task &#039;display&#039; will be assumed. When display is used, the &#039;view&#039; variable will decide what will be displayed. Other common tasks are save, edit, new...&lt;br /&gt;
&lt;br /&gt;
The controller might decide to redirect the page, usually after a task like &#039;save&#039; has been completed. This last statement takes care of the actual redirection.&lt;br /&gt;
&lt;br /&gt;
The main entry point (hello.php) essentially passes control to the controller, which handles performing the task that was specified in the request.&lt;br /&gt;
&lt;br /&gt;
Note that we don&#039;t use a closing php tag in this file: ?&amp;gt;. The reason for this is that we will not have any unwanted whitespace in the output code. This is default practice since Joomla! 1.5, and will be used for all php-only files.&lt;br /&gt;
&lt;br /&gt;
=== Creating the Controller ===&lt;br /&gt;
Our component only has one task - greet the world. Therefore, the controller will be very simple. No data manipulation is required. All that needs to be done is the appropriate view loaded. We will have only one method in our controller: display(). Most of the required functionality is built into the JController class, so all that we need to do is invoke the JController::display() method.&lt;br /&gt;
&lt;br /&gt;
The code for the base controller is:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_1&lt;br /&gt;
 * @license    GNU/GPL&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// No direct access&lt;br /&gt;
&lt;br /&gt;
defined( &#039;_JEXEC&#039; ) or die( &#039;Restricted access&#039; );&lt;br /&gt;
&lt;br /&gt;
jimport(&#039;joomla.application.component.controller&#039;);&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Hello World Component Controller&lt;br /&gt;
 *&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 */&lt;br /&gt;
class HelloController extends JController&lt;br /&gt;
{&lt;br /&gt;
    /**&lt;br /&gt;
     * Method to display the view&lt;br /&gt;
     *&lt;br /&gt;
     * @access    public&lt;br /&gt;
     */&lt;br /&gt;
    function display()&lt;br /&gt;
    {&lt;br /&gt;
        parent::display();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The JController constructor will always register a display() task and unless otherwise specified (using the registerDefaultTask() method), it will set it as the default task.&lt;br /&gt;
&lt;br /&gt;
This barebones display() method isn&#039;t really even necessary since all it does is invoke the parent constructor. However, it is a good visual clue to indicate what is happening in the controller.&lt;br /&gt;
&lt;br /&gt;
The JController::display() method will determine the name of the view and layout from the request and load that view and set the layout. When you create a menu item for your component, the menu manager will allow the administrator to select the view that they would like the menu link to display and to specify the layout. A view usually refers to a view of a certain set of data (i.e. a list of cars, a list of events, a single car, a single event). A layout is a way that that view is organized.&lt;br /&gt;
&lt;br /&gt;
In our component, we will have a single view called hello, and a single layout (default).&lt;br /&gt;
&lt;br /&gt;
=== Creating the View ===&lt;br /&gt;
The task of the view is very simple: It retrieves the data to be displayed and pushes it into the template. Data is pushed into the template using the JView::assignRef method.&lt;br /&gt;
&lt;br /&gt;
The code for the view is:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_1&lt;br /&gt;
 * @license    GNU/GPL&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
// no direct access&lt;br /&gt;
&lt;br /&gt;
defined( &#039;_JEXEC&#039; ) or die( &#039;Restricted access&#039; );&lt;br /&gt;
&lt;br /&gt;
jimport( &#039;joomla.application.component.view&#039;);&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * HTML View class for the HelloWorld Component&lt;br /&gt;
 *&lt;br /&gt;
 * @package    HelloWorld&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
class HelloViewHello extends JView&lt;br /&gt;
{&lt;br /&gt;
    function display($tpl = null)&lt;br /&gt;
    {&lt;br /&gt;
        $greeting = &amp;quot;Hello World!&amp;quot;;&lt;br /&gt;
        $this-&amp;gt;assignRef( &#039;greeting&#039;, $greeting );&lt;br /&gt;
&lt;br /&gt;
        parent::display($tpl);&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Creating the Template ====&lt;br /&gt;
&lt;br /&gt;
Joomla! templates/layouts are regular PHP files that are used to layout the data from the view in a particular manner. The variables assigned by the JView::assignRef method can be accessed from the template using $this-&amp;gt;{propertyname} (see the template code below for an example).&lt;br /&gt;
&lt;br /&gt;
Our template is very simple: we only want to display the greeting that was passed in from the view:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
&lt;br /&gt;
// No direct access&lt;br /&gt;
&lt;br /&gt;
defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;); ?&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;&amp;lt;?php echo $this-&amp;gt;greeting; ?&amp;gt;&amp;lt;/h1&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Wrapping It All Up - Creating the hello.xml File ===&lt;br /&gt;
It is possible to install a component manually by copying the files using an FTP client and modifying the database tables. It is more efficient to create a package file that will allow the Joomla! Installer to do this for you. This package file contains a variety of information:&lt;br /&gt;
&lt;br /&gt;
* basic descriptive details about your component (i.e. name), and optionally, a description, copyright and license information.&lt;br /&gt;
* a list of files that need to be copied.&lt;br /&gt;
* optionally, a PHP file that performs additional install and uninstall operations.&lt;br /&gt;
* optionally, an SQL file which contains database queries that should be executed upon install/uninstall&lt;br /&gt;
&lt;br /&gt;
The format of the XML file is as follows:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;&lt;br /&gt;
&amp;lt;install type=&amp;quot;component&amp;quot; version=&amp;quot;1.5.0&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;name&amp;gt;Hello&amp;lt;/name&amp;gt;&lt;br /&gt;
 &amp;lt;!-- The following elements are optional and free of formatting conttraints --&amp;gt;&lt;br /&gt;
 &amp;lt;creationDate&amp;gt;2007-02-22&amp;lt;/creationDate&amp;gt;&lt;br /&gt;
 &amp;lt;author&amp;gt;John Doe&amp;lt;/author&amp;gt;&lt;br /&gt;
 &amp;lt;authorEmail&amp;gt;john.doe@example.org&amp;lt;/authorEmail&amp;gt;&lt;br /&gt;
 &amp;lt;authorUrl&amp;gt;http://www.example.org&amp;lt;/authorUrl&amp;gt;&lt;br /&gt;
 &amp;lt;copyright&amp;gt;Copyright Info&amp;lt;/copyright&amp;gt;&lt;br /&gt;
 &amp;lt;license&amp;gt;License Info&amp;lt;/license&amp;gt;&lt;br /&gt;
 &amp;lt;!--  The version string is recorded in the components table --&amp;gt;&lt;br /&gt;
 &amp;lt;version&amp;gt;1.01&amp;lt;/version&amp;gt;&lt;br /&gt;
 &amp;lt;!-- The description is optional and defaults to the name --&amp;gt;&lt;br /&gt;
 &amp;lt;description&amp;gt;Description of the component ...&amp;lt;/description&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;!-- Site Main File Copy Section --&amp;gt;&lt;br /&gt;
 &amp;lt;!-- Note the folder attribute: This attribute describes the folder&lt;br /&gt;
      to copy FROM in the package to install therefore files copied&lt;br /&gt;
      in this section are copied from /site/ in the package --&amp;gt;&lt;br /&gt;
 &amp;lt;files folder=&amp;quot;site&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;controller.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/view.html.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/tmpl/default.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/tmpl/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
 &amp;lt;/files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;administration&amp;gt;&lt;br /&gt;
  &amp;lt;!-- Administration Menu Section --&amp;gt;&lt;br /&gt;
  &amp;lt;menu&amp;gt;Hello World!&amp;lt;/menu&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;!-- Administration Main File Copy Section --&amp;gt;&lt;br /&gt;
  &amp;lt;files folder=&amp;quot;admin&amp;quot;&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;/files&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
 &amp;lt;/administration&amp;gt;&lt;br /&gt;
&amp;lt;/install&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you look closely you will notice that there are some files that will be copied that we have not discussed. These are the index.html files. An index.html file is placed in each directory to prevent prying users from getting a directory listing. If there is no index.html file, some web servers will list the directory contents. This is often undesirable. These files have the simple line:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;html&amp;gt;&amp;lt;body bgcolor=&amp;quot;#FFFFFF&amp;quot;&amp;gt;&amp;lt;/body&amp;gt;&amp;lt;/html&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It will simply display a blank page.&lt;br /&gt;
&lt;br /&gt;
The other file is the admin.hello.php file. This is the entry point for the admin section of our component. Since we don&#039;t have an admin section of our component, it will have the same content as the index.html files at this point in time.&lt;br /&gt;
&lt;br /&gt;
== Contributors ==&lt;br /&gt;
* mjaz&lt;br /&gt;
* staalanden&lt;br /&gt;
&lt;br /&gt;
== Download ==&lt;br /&gt;
The component can be downloaded at: [http://joomlacode.org/gf/download/frsrelease/8104/29402/com_hello1_01.zip com_hello1_01]&lt;/div&gt;</summary>
		<author><name>Tonie</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Using_the_Database&amp;diff=9492</id>
		<title>J1.5:Developing a MVC Component/Using the Database</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Using_the_Database&amp;diff=9492"/>
		<updated>2008-07-27T18:11:09Z</updated>

		<summary type="html">&lt;p&gt;Tonie: /* Updating our Install File */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
In the first two tutorials, we showed you how to build a simple model-view-controller component. We had one view which retrieved data from a model (which was created in the 2nd tutorial). In this tutorial, we will be working with the model. Instead of the data being hard coded in the model, the model will retrieve the data from a table in the database.&lt;br /&gt;
&lt;br /&gt;
This tutorial will demonstrate how to use the JDatabase class to retrieve data from the database.&lt;br /&gt;
&lt;br /&gt;
== Retrieving the Data ==&lt;br /&gt;
&lt;br /&gt;
Our model currently has one method: getGreeting(). This method is very simple - all it does is return the hard-coded greeting.&lt;br /&gt;
&lt;br /&gt;
To make things more interesting, we will load the greeting from a database table. We will demonstrate later how to create an SQL file and add the appropriate code to the XML manifest file so that the table and some sample data will be created when the component is installed. For now, we will simply replace our return statement with some code that will retrieve the greeting from the database and return it.&lt;br /&gt;
&lt;br /&gt;
The first step is to obtain a reference to a database object. Since Joomla! uses the database for its normal operation, a database connection already exists; therefore, it is not necessary to create your own. A reference to the existing database can be obtained using:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$db =&amp;amp; JFactory::getDBO();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
JFactory is a static class that is used to retrieve references to many of the system objects. More information about this class can be found in the API documentation.&lt;br /&gt;
&lt;br /&gt;
The method name (getDBO) stands for get DataBase Object, and is easy and important to remember.&lt;br /&gt;
&lt;br /&gt;
Now that we have obtained a reference to the database object, we can retrieve our data. We do this in two steps:&lt;br /&gt;
&lt;br /&gt;
  * store our query in the database object&lt;br /&gt;
&lt;br /&gt;
  * load the result&lt;br /&gt;
&lt;br /&gt;
Our new getGreeting() method will therefore look like:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;function getGreeting()&lt;br /&gt;
{&lt;br /&gt;
   $db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
&lt;br /&gt;
   $query = &#039;SELECT greeting FROM #__hello&#039;;&lt;br /&gt;
   $db-&amp;gt;setQuery( $query );&lt;br /&gt;
   $greeting = $db-&amp;gt;loadResult();&lt;br /&gt;
&lt;br /&gt;
   return $greeting;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
hello is the name of the table that we will create later, and greeting is the name of the field that stores the greetings. If you are not familiar with SQL, it would be helpful to take a tutorial or a lesson to get yourself up to speed. One such tutorial can be found at [[http://www.w3schools.com/sql/default.asp|w3schools]].&lt;br /&gt;
&lt;br /&gt;
The $db-&amp;gt;loadResult() method will execute the stored database query and return the first field of the first row of the result. See [[http://dev.joomla.org/component/option,com_jd-wiki/Itemid,/id,references:joomla.framework:database:jdatabase/|JDatabase API reference]] for more information about other load methods in the JDatabase class.&lt;br /&gt;
&lt;br /&gt;
== Creating the Installation SQL File ==&lt;br /&gt;
&lt;br /&gt;
The Joomla! installer has built-in support for executing queries during component installation. These queries are all stored in a standard text file.&lt;br /&gt;
&lt;br /&gt;
We will have three queries in our install file: the first will drop the table in case it already exists, the second will create the table with the appropriate fields, and the third will insert the data.&lt;br /&gt;
&lt;br /&gt;
Here are our queries:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;DROP TABLE IF EXISTS `#__hello`;&lt;br /&gt;
&lt;br /&gt;
CREATE TABLE `#__hello` (&lt;br /&gt;
  `id` int(11) NOT NULL auto_increment,&lt;br /&gt;
  `greeting` varchar(25) NOT NULL,&lt;br /&gt;
  PRIMARY KEY  (`id`)&lt;br /&gt;
) ENGINE=MyISAM AUTO_INCREMENT DEFAULT CHARSET=utf8;&lt;br /&gt;
&lt;br /&gt;
INSERT INTO `#__hello` (`greeting`) VALUES (&#039;Hello, World!&#039;),&lt;br /&gt;
(&#039;Bonjour, Monde!&#039;),&lt;br /&gt;
(&#039;Ciao, Mondo!&#039;);&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You might find the  prefix on the table names rather odd. Joomla! will replace this prefix with the prefix used by the current install. For most installs, this table will become jos_hello. This allows multiple installs of Joomla! to use the same database, and prevents collisions with other applications using the same table names (i.e. two applications might share a database, but might both require a &#039;users&#039; table. This convention avoids problems.)&lt;br /&gt;
&lt;br /&gt;
We have specified two fields in our database. The first field is id, and is called the &#039;primary key&#039;. The primary key of a database table is a field that is used to uniquely identify a record. This is often used to lookup rows in the database. The other field is greeting. This is the field that stores the greeting that is returned from the query that we used above.&lt;br /&gt;
&lt;br /&gt;
We will save our queries in a file called install.utf.sql.&lt;br /&gt;
&lt;br /&gt;
=== Creating the Uninstall SQL File ===&lt;br /&gt;
&lt;br /&gt;
Though we might hope that people will never want to uninstall our component, it is important that if they do, we don&#039;t leave anything behind. Joomla! will look after deleting the files and directories that were created during install, but you must manually include queries that will remove and tables that have been added to the database. Since we have only created one table, we only need one query:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;DROP TABLE IF EXISTS `#__hello`;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will save this query in a file called uninstall.utf.sql.&lt;br /&gt;
&lt;br /&gt;
== Updating our Install File ==&lt;br /&gt;
&lt;br /&gt;
We need to change a few things in our install file. First, we need to add our two new files to the list of files to install. SQL install file have to go in the admin directory. Second, we need to tell the installer to execute the queries in our files on install and uninstall.&lt;br /&gt;
&lt;br /&gt;
Our new file looks like this:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;&lt;br /&gt;
&amp;lt;install type=&amp;quot;component&amp;quot; version=&amp;quot;1.5.0&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;name&amp;gt;Hello&amp;lt;/name&amp;gt;&lt;br /&gt;
 &amp;lt;!-- The following elements are optional and free of formatting conttraints --&amp;gt;&lt;br /&gt;
 &amp;lt;creationDate&amp;gt;2007-02-22&amp;lt;/creationDate&amp;gt;&lt;br /&gt;
 &amp;lt;author&amp;gt;John Doe&amp;lt;/author&amp;gt;&lt;br /&gt;
 &amp;lt;authorEmail&amp;gt;john.doe@example.org&amp;lt;/authorEmail&amp;gt;&lt;br /&gt;
 &amp;lt;authorUrl&amp;gt;http://www.example.org&amp;lt;/authorUrl&amp;gt;&lt;br /&gt;
 &amp;lt;copyright&amp;gt;Copyright Info&amp;lt;/copyright&amp;gt;&lt;br /&gt;
 &amp;lt;license&amp;gt;License Info&amp;lt;/license&amp;gt;&lt;br /&gt;
 &amp;lt;!--  The version string is recorded in the components table --&amp;gt;&lt;br /&gt;
 &amp;lt;version&amp;gt;3.01&amp;lt;/version&amp;gt;&lt;br /&gt;
 &amp;lt;!-- The description is optional and defaults to the name --&amp;gt;&lt;br /&gt;
 &amp;lt;description&amp;gt;Description of the component ...&amp;lt;/description&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;!-- Site Main File Copy Section --&amp;gt;&lt;br /&gt;
 &amp;lt;!-- Note the folder attribute: This attribute describes the folder&lt;br /&gt;
      to copy FROM in the package to install therefore files copied&lt;br /&gt;
      in this section are copied from /site/ in the package --&amp;gt;&lt;br /&gt;
 &amp;lt;files folder=&amp;quot;site&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;controller.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;models/hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;models/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/view.html.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/tmpl/default.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/tmpl/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
 &amp;lt;/files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;install&amp;gt;&lt;br /&gt;
  &amp;lt;sql&amp;gt;&lt;br /&gt;
   &amp;lt;file charset=&amp;quot;utf8&amp;quot; driver=&amp;quot;mysql&amp;quot;&amp;gt;install.sql&amp;lt;/file&amp;gt;&lt;br /&gt;
  &amp;lt;/sql&amp;gt;&lt;br /&gt;
 &amp;lt;/install&amp;gt;&lt;br /&gt;
 &amp;lt;uninstall&amp;gt;&lt;br /&gt;
  &amp;lt;sql&amp;gt;&lt;br /&gt;
   &amp;lt;file charset=&amp;quot;utf8&amp;quot; driver=&amp;quot;mysql&amp;quot;&amp;gt;uninstall.sql&amp;lt;/file&amp;gt;&lt;br /&gt;
  &amp;lt;/sql&amp;gt;&lt;br /&gt;
 &amp;lt;/uninstall&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;administration&amp;gt;&lt;br /&gt;
  &amp;lt;!-- Administration Menu Section --&amp;gt;&lt;br /&gt;
  &amp;lt;menu&amp;gt;Hello World!&amp;lt;/menu&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;!-- Administration Main File Copy Section --&amp;gt;&lt;br /&gt;
  &amp;lt;files folder=&amp;quot;admin&amp;quot;&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;install.sql&amp;lt;/filename&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;uninstall.sql&amp;lt;/filename&amp;gt;&lt;br /&gt;
&amp;lt;/files&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
 &amp;lt;/administration&amp;gt;&lt;br /&gt;
&amp;lt;/install&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You will notice two attributes present on the &amp;lt;file&amp;gt; tags within the &amp;lt;install&amp;gt; and &amp;lt;uninstall&amp;gt; sections: charset and driver. The charset is the type of charset to use. The only valid charset is utf8. If you want to create install files for non-utf8 databases (for older version of MySQL), you should omit this attribute.&lt;br /&gt;
&lt;br /&gt;
The driver attribute specifies which database the queries were written for. Currently, this can only be mysql, but in future versions of Joomla! there may be more database drivers available.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
We now have a component that takes advantage of both the Joomla! MVC framework classes and the JDatabase classes. You are now able to write MVC components that interact with the database and can use the Joomla! installer to create and populate database tables.&lt;br /&gt;
&lt;br /&gt;
== Contributors ==&lt;br /&gt;
* staalanden&lt;br /&gt;
&lt;br /&gt;
== Download ==&lt;br /&gt;
&lt;br /&gt;
The component can be downloaded at: [http://joomlacode.org/gf/download/frsrelease/8104/29402/com_hello3_01.zip]&lt;/div&gt;</summary>
		<author><name>Tonie</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Using_the_Database&amp;diff=9491</id>
		<title>J1.5:Developing a MVC Component/Using the Database</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Using_the_Database&amp;diff=9491"/>
		<updated>2008-07-27T18:08:39Z</updated>

		<summary type="html">&lt;p&gt;Tonie: /* Download */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
In the first two tutorials, we showed you how to build a simple model-view-controller component. We had one view which retrieved data from a model (which was created in the 2nd tutorial). In this tutorial, we will be working with the model. Instead of the data being hard coded in the model, the model will retrieve the data from a table in the database.&lt;br /&gt;
&lt;br /&gt;
This tutorial will demonstrate how to use the JDatabase class to retrieve data from the database.&lt;br /&gt;
&lt;br /&gt;
== Retrieving the Data ==&lt;br /&gt;
&lt;br /&gt;
Our model currently has one method: getGreeting(). This method is very simple - all it does is return the hard-coded greeting.&lt;br /&gt;
&lt;br /&gt;
To make things more interesting, we will load the greeting from a database table. We will demonstrate later how to create an SQL file and add the appropriate code to the XML manifest file so that the table and some sample data will be created when the component is installed. For now, we will simply replace our return statement with some code that will retrieve the greeting from the database and return it.&lt;br /&gt;
&lt;br /&gt;
The first step is to obtain a reference to a database object. Since Joomla! uses the database for its normal operation, a database connection already exists; therefore, it is not necessary to create your own. A reference to the existing database can be obtained using:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$db =&amp;amp; JFactory::getDBO();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
JFactory is a static class that is used to retrieve references to many of the system objects. More information about this class can be found in the API documentation.&lt;br /&gt;
&lt;br /&gt;
The method name (getDBO) stands for get DataBase Object, and is easy and important to remember.&lt;br /&gt;
&lt;br /&gt;
Now that we have obtained a reference to the database object, we can retrieve our data. We do this in two steps:&lt;br /&gt;
&lt;br /&gt;
  * store our query in the database object&lt;br /&gt;
&lt;br /&gt;
  * load the result&lt;br /&gt;
&lt;br /&gt;
Our new getGreeting() method will therefore look like:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;function getGreeting()&lt;br /&gt;
{&lt;br /&gt;
   $db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
&lt;br /&gt;
   $query = &#039;SELECT greeting FROM #__hello&#039;;&lt;br /&gt;
   $db-&amp;gt;setQuery( $query );&lt;br /&gt;
   $greeting = $db-&amp;gt;loadResult();&lt;br /&gt;
&lt;br /&gt;
   return $greeting;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
hello is the name of the table that we will create later, and greeting is the name of the field that stores the greetings. If you are not familiar with SQL, it would be helpful to take a tutorial or a lesson to get yourself up to speed. One such tutorial can be found at [[http://www.w3schools.com/sql/default.asp|w3schools]].&lt;br /&gt;
&lt;br /&gt;
The $db-&amp;gt;loadResult() method will execute the stored database query and return the first field of the first row of the result. See [[http://dev.joomla.org/component/option,com_jd-wiki/Itemid,/id,references:joomla.framework:database:jdatabase/|JDatabase API reference]] for more information about other load methods in the JDatabase class.&lt;br /&gt;
&lt;br /&gt;
== Creating the Installation SQL File ==&lt;br /&gt;
&lt;br /&gt;
The Joomla! installer has built-in support for executing queries during component installation. These queries are all stored in a standard text file.&lt;br /&gt;
&lt;br /&gt;
We will have three queries in our install file: the first will drop the table in case it already exists, the second will create the table with the appropriate fields, and the third will insert the data.&lt;br /&gt;
&lt;br /&gt;
Here are our queries:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;DROP TABLE IF EXISTS `#__hello`;&lt;br /&gt;
&lt;br /&gt;
CREATE TABLE `#__hello` (&lt;br /&gt;
  `id` int(11) NOT NULL auto_increment,&lt;br /&gt;
  `greeting` varchar(25) NOT NULL,&lt;br /&gt;
  PRIMARY KEY  (`id`)&lt;br /&gt;
) ENGINE=MyISAM AUTO_INCREMENT DEFAULT CHARSET=utf8;&lt;br /&gt;
&lt;br /&gt;
INSERT INTO `#__hello` (`greeting`) VALUES (&#039;Hello, World!&#039;),&lt;br /&gt;
(&#039;Bonjour, Monde!&#039;),&lt;br /&gt;
(&#039;Ciao, Mondo!&#039;);&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You might find the  prefix on the table names rather odd. Joomla! will replace this prefix with the prefix used by the current install. For most installs, this table will become jos_hello. This allows multiple installs of Joomla! to use the same database, and prevents collisions with other applications using the same table names (i.e. two applications might share a database, but might both require a &#039;users&#039; table. This convention avoids problems.)&lt;br /&gt;
&lt;br /&gt;
We have specified two fields in our database. The first field is id, and is called the &#039;primary key&#039;. The primary key of a database table is a field that is used to uniquely identify a record. This is often used to lookup rows in the database. The other field is greeting. This is the field that stores the greeting that is returned from the query that we used above.&lt;br /&gt;
&lt;br /&gt;
We will save our queries in a file called install.utf.sql.&lt;br /&gt;
&lt;br /&gt;
=== Creating the Uninstall SQL File ===&lt;br /&gt;
&lt;br /&gt;
Though we might hope that people will never want to uninstall our component, it is important that if they do, we don&#039;t leave anything behind. Joomla! will look after deleting the files and directories that were created during install, but you must manually include queries that will remove and tables that have been added to the database. Since we have only created one table, we only need one query:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;DROP TABLE IF EXISTS `#__hello`;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will save this query in a file called uninstall.utf.sql.&lt;br /&gt;
&lt;br /&gt;
== Updating our Install File ==&lt;br /&gt;
&lt;br /&gt;
We need to change a few things in our install file. First, we need to add our two new files to the list of files to install. SQL install file have to go in the admin directory. Second, we need to tell the installer to execute the queries in our files on install and uninstall.&lt;br /&gt;
&lt;br /&gt;
Our new file looks like this:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;&lt;br /&gt;
&amp;lt;!DOCTYPE install SYSTEM &amp;quot;http://dev.joomla.org/xml/1.5/component-install.dtd&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;install type=&amp;quot;component&amp;quot; version=&amp;quot;1.5.0&amp;quot;&amp;gt;&lt;br /&gt;
   &amp;lt;name&amp;gt;Hello&amp;lt;/name&amp;gt;&lt;br /&gt;
   &amp;lt;!-- The following elements are optional and free of formatting conttraints --&amp;gt;&lt;br /&gt;
   &amp;lt;creationDate&amp;gt;2007 02 22&amp;lt;/creationDate&amp;gt;&lt;br /&gt;
   &amp;lt;author&amp;gt;John Doe&amp;lt;/author&amp;gt;&lt;br /&gt;
   &amp;lt;authorEmail&amp;gt;john.doe@example.org&amp;lt;/authorEmail&amp;gt;&lt;br /&gt;
   &amp;lt;authorUrl&amp;gt;http://www.example.org&amp;lt;/authorUrl&amp;gt;&lt;br /&gt;
   &amp;lt;copyright&amp;gt;Copyright Info&amp;lt;/copyright&amp;gt;&lt;br /&gt;
   &amp;lt;license&amp;gt;License Info&amp;lt;/license&amp;gt;&lt;br /&gt;
   &amp;lt;!--  The version string is recorded in the components table --&amp;gt;&lt;br /&gt;
   &amp;lt;version&amp;gt;Component Version String&amp;lt;/version&amp;gt;&lt;br /&gt;
   &amp;lt;!-- The description is optional and defaults to the name --&amp;gt;&lt;br /&gt;
   &amp;lt;description&amp;gt;Description of the component ...&amp;lt;/description&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   &amp;lt;!-- Site Main File Copy Section --&amp;gt;&lt;br /&gt;
   &amp;lt;files folder=&amp;quot;site&amp;quot;&amp;gt;&lt;br /&gt;
      &amp;lt;filename&amp;gt;index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
      &amp;lt;filename&amp;gt;hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
      &amp;lt;filename&amp;gt;controller.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
      &amp;lt;filename&amp;gt;views/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
      &amp;lt;filename&amp;gt;views/hello/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
      &amp;lt;filename&amp;gt;views/hello/view.html.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
      &amp;lt;filename&amp;gt;views/hello/tmpl/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
      &amp;lt;filename&amp;gt;views/hello/tmpl/default.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
      &amp;lt;filename&amp;gt;models/hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
   &amp;lt;/files&amp;gt;&lt;br /&gt;
   &amp;lt;install&amp;gt;&lt;br /&gt;
      &amp;lt;sql&amp;gt;&lt;br /&gt;
         &amp;lt;file charset=&amp;quot;utf8&amp;quot; driver=&amp;quot;mysql&amp;quot;&amp;gt;install.sql&amp;lt;/file&amp;gt;&lt;br /&gt;
      &amp;lt;/sql&amp;gt;&lt;br /&gt;
   &amp;lt;/install&amp;gt;&lt;br /&gt;
   &amp;lt;uninstall&amp;gt;&lt;br /&gt;
      &amp;lt;sql&amp;gt;&lt;br /&gt;
         &amp;lt;file charset=&amp;quot;utf8&amp;quot; driver=&amp;quot;mysql&amp;quot;&amp;gt;uninstall.sql&amp;lt;/file&amp;gt;&lt;br /&gt;
      &amp;lt;/sql&amp;gt;&lt;br /&gt;
   &amp;lt;/uninstall&amp;gt;&lt;br /&gt;
   &amp;lt;administration&amp;gt;&lt;br /&gt;
      &amp;lt;!-- Administration Menu Section --&amp;gt;&lt;br /&gt;
      &amp;lt;menu&amp;gt;Hello World!&amp;lt;/menu&amp;gt;&lt;br /&gt;
      &lt;br /&gt;
      &amp;lt;!-- Administration Main File Copy Section --&amp;gt;&lt;br /&gt;
      &amp;lt;!-- Note the folder attribute: This attribute describes the folder&lt;br /&gt;
         to copy FROM in the package to install therefore files copied&lt;br /&gt;
         in this section are copied from /admin/ in the package --&amp;gt;&lt;br /&gt;
      &amp;lt;files folder=&amp;quot;admin&amp;quot;&amp;gt;&lt;br /&gt;
   &amp;lt;!-- Site Main File Copy Section --&amp;gt;&lt;br /&gt;
         &amp;lt;filename&amp;gt;index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
         &amp;lt;filename&amp;gt;admin.hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
         &amp;lt;filename&amp;gt;install.sql&amp;lt;/filename&amp;gt;&lt;br /&gt;
         &amp;lt;filename&amp;gt;uninstall.sql&amp;lt;/filename&amp;gt;&lt;br /&gt;
      &amp;lt;/files&amp;gt;      &lt;br /&gt;
   &amp;lt;/administration&amp;gt;&lt;br /&gt;
&amp;lt;/install&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You will notice two attributes present on the &amp;lt;file&amp;gt; tags within the &amp;lt;install&amp;gt; and &amp;lt;uninstall&amp;gt; sections: charset and driver. The charset is the type of charset to use. The only valid charset is utf8. If you want to create install files for non-utf8 databases (for older version of MySQL), you should omit this attribute.&lt;br /&gt;
&lt;br /&gt;
The driver attribute specifies which database the queries were written for. Currently, this can only be mysql, but in future versions of Joomla! there may be more database drivers available.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
We now have a component that takes advantage of both the Joomla! MVC framework classes and the JDatabase classes. You are now able to write MVC components that interact with the database and can use the Joomla! installer to create and populate database tables.&lt;br /&gt;
&lt;br /&gt;
== Contributors ==&lt;br /&gt;
* staalanden&lt;br /&gt;
&lt;br /&gt;
== Download ==&lt;br /&gt;
&lt;br /&gt;
The component can be downloaded at: [http://joomlacode.org/gf/download/frsrelease/8104/29402/com_hello3_01.zip]&lt;/div&gt;</summary>
		<author><name>Tonie</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Using_the_Database&amp;diff=9490</id>
		<title>J1.5:Developing a MVC Component/Using the Database</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Using_the_Database&amp;diff=9490"/>
		<updated>2008-07-27T18:07:58Z</updated>

		<summary type="html">&lt;p&gt;Tonie: /* Contributors */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
In the first two tutorials, we showed you how to build a simple model-view-controller component. We had one view which retrieved data from a model (which was created in the 2nd tutorial). In this tutorial, we will be working with the model. Instead of the data being hard coded in the model, the model will retrieve the data from a table in the database.&lt;br /&gt;
&lt;br /&gt;
This tutorial will demonstrate how to use the JDatabase class to retrieve data from the database.&lt;br /&gt;
&lt;br /&gt;
== Retrieving the Data ==&lt;br /&gt;
&lt;br /&gt;
Our model currently has one method: getGreeting(). This method is very simple - all it does is return the hard-coded greeting.&lt;br /&gt;
&lt;br /&gt;
To make things more interesting, we will load the greeting from a database table. We will demonstrate later how to create an SQL file and add the appropriate code to the XML manifest file so that the table and some sample data will be created when the component is installed. For now, we will simply replace our return statement with some code that will retrieve the greeting from the database and return it.&lt;br /&gt;
&lt;br /&gt;
The first step is to obtain a reference to a database object. Since Joomla! uses the database for its normal operation, a database connection already exists; therefore, it is not necessary to create your own. A reference to the existing database can be obtained using:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$db =&amp;amp; JFactory::getDBO();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
JFactory is a static class that is used to retrieve references to many of the system objects. More information about this class can be found in the API documentation.&lt;br /&gt;
&lt;br /&gt;
The method name (getDBO) stands for get DataBase Object, and is easy and important to remember.&lt;br /&gt;
&lt;br /&gt;
Now that we have obtained a reference to the database object, we can retrieve our data. We do this in two steps:&lt;br /&gt;
&lt;br /&gt;
  * store our query in the database object&lt;br /&gt;
&lt;br /&gt;
  * load the result&lt;br /&gt;
&lt;br /&gt;
Our new getGreeting() method will therefore look like:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;function getGreeting()&lt;br /&gt;
{&lt;br /&gt;
   $db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
&lt;br /&gt;
   $query = &#039;SELECT greeting FROM #__hello&#039;;&lt;br /&gt;
   $db-&amp;gt;setQuery( $query );&lt;br /&gt;
   $greeting = $db-&amp;gt;loadResult();&lt;br /&gt;
&lt;br /&gt;
   return $greeting;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
hello is the name of the table that we will create later, and greeting is the name of the field that stores the greetings. If you are not familiar with SQL, it would be helpful to take a tutorial or a lesson to get yourself up to speed. One such tutorial can be found at [[http://www.w3schools.com/sql/default.asp|w3schools]].&lt;br /&gt;
&lt;br /&gt;
The $db-&amp;gt;loadResult() method will execute the stored database query and return the first field of the first row of the result. See [[http://dev.joomla.org/component/option,com_jd-wiki/Itemid,/id,references:joomla.framework:database:jdatabase/|JDatabase API reference]] for more information about other load methods in the JDatabase class.&lt;br /&gt;
&lt;br /&gt;
== Creating the Installation SQL File ==&lt;br /&gt;
&lt;br /&gt;
The Joomla! installer has built-in support for executing queries during component installation. These queries are all stored in a standard text file.&lt;br /&gt;
&lt;br /&gt;
We will have three queries in our install file: the first will drop the table in case it already exists, the second will create the table with the appropriate fields, and the third will insert the data.&lt;br /&gt;
&lt;br /&gt;
Here are our queries:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;DROP TABLE IF EXISTS `#__hello`;&lt;br /&gt;
&lt;br /&gt;
CREATE TABLE `#__hello` (&lt;br /&gt;
  `id` int(11) NOT NULL auto_increment,&lt;br /&gt;
  `greeting` varchar(25) NOT NULL,&lt;br /&gt;
  PRIMARY KEY  (`id`)&lt;br /&gt;
) ENGINE=MyISAM AUTO_INCREMENT DEFAULT CHARSET=utf8;&lt;br /&gt;
&lt;br /&gt;
INSERT INTO `#__hello` (`greeting`) VALUES (&#039;Hello, World!&#039;),&lt;br /&gt;
(&#039;Bonjour, Monde!&#039;),&lt;br /&gt;
(&#039;Ciao, Mondo!&#039;);&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You might find the  prefix on the table names rather odd. Joomla! will replace this prefix with the prefix used by the current install. For most installs, this table will become jos_hello. This allows multiple installs of Joomla! to use the same database, and prevents collisions with other applications using the same table names (i.e. two applications might share a database, but might both require a &#039;users&#039; table. This convention avoids problems.)&lt;br /&gt;
&lt;br /&gt;
We have specified two fields in our database. The first field is id, and is called the &#039;primary key&#039;. The primary key of a database table is a field that is used to uniquely identify a record. This is often used to lookup rows in the database. The other field is greeting. This is the field that stores the greeting that is returned from the query that we used above.&lt;br /&gt;
&lt;br /&gt;
We will save our queries in a file called install.utf.sql.&lt;br /&gt;
&lt;br /&gt;
=== Creating the Uninstall SQL File ===&lt;br /&gt;
&lt;br /&gt;
Though we might hope that people will never want to uninstall our component, it is important that if they do, we don&#039;t leave anything behind. Joomla! will look after deleting the files and directories that were created during install, but you must manually include queries that will remove and tables that have been added to the database. Since we have only created one table, we only need one query:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;DROP TABLE IF EXISTS `#__hello`;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will save this query in a file called uninstall.utf.sql.&lt;br /&gt;
&lt;br /&gt;
== Updating our Install File ==&lt;br /&gt;
&lt;br /&gt;
We need to change a few things in our install file. First, we need to add our two new files to the list of files to install. SQL install file have to go in the admin directory. Second, we need to tell the installer to execute the queries in our files on install and uninstall.&lt;br /&gt;
&lt;br /&gt;
Our new file looks like this:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;&lt;br /&gt;
&amp;lt;!DOCTYPE install SYSTEM &amp;quot;http://dev.joomla.org/xml/1.5/component-install.dtd&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;install type=&amp;quot;component&amp;quot; version=&amp;quot;1.5.0&amp;quot;&amp;gt;&lt;br /&gt;
   &amp;lt;name&amp;gt;Hello&amp;lt;/name&amp;gt;&lt;br /&gt;
   &amp;lt;!-- The following elements are optional and free of formatting conttraints --&amp;gt;&lt;br /&gt;
   &amp;lt;creationDate&amp;gt;2007 02 22&amp;lt;/creationDate&amp;gt;&lt;br /&gt;
   &amp;lt;author&amp;gt;John Doe&amp;lt;/author&amp;gt;&lt;br /&gt;
   &amp;lt;authorEmail&amp;gt;john.doe@example.org&amp;lt;/authorEmail&amp;gt;&lt;br /&gt;
   &amp;lt;authorUrl&amp;gt;http://www.example.org&amp;lt;/authorUrl&amp;gt;&lt;br /&gt;
   &amp;lt;copyright&amp;gt;Copyright Info&amp;lt;/copyright&amp;gt;&lt;br /&gt;
   &amp;lt;license&amp;gt;License Info&amp;lt;/license&amp;gt;&lt;br /&gt;
   &amp;lt;!--  The version string is recorded in the components table --&amp;gt;&lt;br /&gt;
   &amp;lt;version&amp;gt;Component Version String&amp;lt;/version&amp;gt;&lt;br /&gt;
   &amp;lt;!-- The description is optional and defaults to the name --&amp;gt;&lt;br /&gt;
   &amp;lt;description&amp;gt;Description of the component ...&amp;lt;/description&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   &amp;lt;!-- Site Main File Copy Section --&amp;gt;&lt;br /&gt;
   &amp;lt;files folder=&amp;quot;site&amp;quot;&amp;gt;&lt;br /&gt;
      &amp;lt;filename&amp;gt;index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
      &amp;lt;filename&amp;gt;hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
      &amp;lt;filename&amp;gt;controller.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
      &amp;lt;filename&amp;gt;views/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
      &amp;lt;filename&amp;gt;views/hello/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
      &amp;lt;filename&amp;gt;views/hello/view.html.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
      &amp;lt;filename&amp;gt;views/hello/tmpl/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
      &amp;lt;filename&amp;gt;views/hello/tmpl/default.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
      &amp;lt;filename&amp;gt;models/hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
   &amp;lt;/files&amp;gt;&lt;br /&gt;
   &amp;lt;install&amp;gt;&lt;br /&gt;
      &amp;lt;sql&amp;gt;&lt;br /&gt;
         &amp;lt;file charset=&amp;quot;utf8&amp;quot; driver=&amp;quot;mysql&amp;quot;&amp;gt;install.sql&amp;lt;/file&amp;gt;&lt;br /&gt;
      &amp;lt;/sql&amp;gt;&lt;br /&gt;
   &amp;lt;/install&amp;gt;&lt;br /&gt;
   &amp;lt;uninstall&amp;gt;&lt;br /&gt;
      &amp;lt;sql&amp;gt;&lt;br /&gt;
         &amp;lt;file charset=&amp;quot;utf8&amp;quot; driver=&amp;quot;mysql&amp;quot;&amp;gt;uninstall.sql&amp;lt;/file&amp;gt;&lt;br /&gt;
      &amp;lt;/sql&amp;gt;&lt;br /&gt;
   &amp;lt;/uninstall&amp;gt;&lt;br /&gt;
   &amp;lt;administration&amp;gt;&lt;br /&gt;
      &amp;lt;!-- Administration Menu Section --&amp;gt;&lt;br /&gt;
      &amp;lt;menu&amp;gt;Hello World!&amp;lt;/menu&amp;gt;&lt;br /&gt;
      &lt;br /&gt;
      &amp;lt;!-- Administration Main File Copy Section --&amp;gt;&lt;br /&gt;
      &amp;lt;!-- Note the folder attribute: This attribute describes the folder&lt;br /&gt;
         to copy FROM in the package to install therefore files copied&lt;br /&gt;
         in this section are copied from /admin/ in the package --&amp;gt;&lt;br /&gt;
      &amp;lt;files folder=&amp;quot;admin&amp;quot;&amp;gt;&lt;br /&gt;
   &amp;lt;!-- Site Main File Copy Section --&amp;gt;&lt;br /&gt;
         &amp;lt;filename&amp;gt;index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
         &amp;lt;filename&amp;gt;admin.hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
         &amp;lt;filename&amp;gt;install.sql&amp;lt;/filename&amp;gt;&lt;br /&gt;
         &amp;lt;filename&amp;gt;uninstall.sql&amp;lt;/filename&amp;gt;&lt;br /&gt;
      &amp;lt;/files&amp;gt;      &lt;br /&gt;
   &amp;lt;/administration&amp;gt;&lt;br /&gt;
&amp;lt;/install&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You will notice two attributes present on the &amp;lt;file&amp;gt; tags within the &amp;lt;install&amp;gt; and &amp;lt;uninstall&amp;gt; sections: charset and driver. The charset is the type of charset to use. The only valid charset is utf8. If you want to create install files for non-utf8 databases (for older version of MySQL), you should omit this attribute.&lt;br /&gt;
&lt;br /&gt;
The driver attribute specifies which database the queries were written for. Currently, this can only be mysql, but in future versions of Joomla! there may be more database drivers available.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
We now have a component that takes advantage of both the Joomla! MVC framework classes and the JDatabase classes. You are now able to write MVC components that interact with the database and can use the Joomla! installer to create and populate database tables.&lt;br /&gt;
&lt;br /&gt;
== Contributors ==&lt;br /&gt;
* staalanden&lt;br /&gt;
&lt;br /&gt;
== Download ==&lt;br /&gt;
&lt;br /&gt;
The component can be downloaded at: [[http://dev.joomla.org/components/com_jd-wiki/data/media/components/com_hello3.zip|com_hello3.zip]]&lt;/div&gt;</summary>
		<author><name>Tonie</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Adding_a_Model&amp;diff=9489</id>
		<title>J1.5:Developing a MVC Component/Adding a Model</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Adding_a_Model&amp;diff=9489"/>
		<updated>2008-07-27T18:05:59Z</updated>

		<summary type="html">&lt;p&gt;Tonie: /* Adding the File to the Package */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
In the first tutorial of this series, creating a simple view-controller component using the Joomla! 1.5 CMS framework was demonstrated.&lt;br /&gt;
&lt;br /&gt;
In the first tutorial, the greeting was hardcoded into the view. This doesn&#039;t follow the MVC pattern exactly because the view is intended to only display the data, and not contain it.&lt;br /&gt;
&lt;br /&gt;
In this second part of the tutorial we will demonstrate how to move this out of the view and into a model. In future tutorials we will demonstrate the power and flexibility that this design pattern provides.&lt;br /&gt;
&lt;br /&gt;
== Creating the Model ==&lt;br /&gt;
&lt;br /&gt;
The concept of model gets its name because this class is intended to represent (or &#039;model&#039;) some entity. In our case, our first model will represent a &#039;hello&#039;, or a greeting. This is in line with our design thus far, because we have one view (&#039;hello&#039;), which is a view of our greeting.&lt;br /&gt;
&lt;br /&gt;
The naming convention for models in the Joomla! framework is that the class name starts with the name of the component (in our case &#039;hello&#039;, followed by &#039;model&#039;, followed by the model name. Therefore, our model class is called HelloModelHello.&lt;br /&gt;
&lt;br /&gt;
At this point, we will only model one behaviour of our hello, and that is retrieving the greeting. We will thus have one method, called getGreeting(). It will simply return the string &#039;Hello, World!&#039;.&lt;br /&gt;
&lt;br /&gt;
Here is the code for our model class:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * Hello Model for Hello World Component&lt;br /&gt;
 * &lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_2&lt;br /&gt;
 * @license    GNU/GPL&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// No direct access&lt;br /&gt;
&lt;br /&gt;
defined( &#039;_JEXEC&#039; ) or die( &#039;Restricted access&#039; );&lt;br /&gt;
&lt;br /&gt;
jimport( &#039;joomla.application.component.model&#039; );&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Hello Model&lt;br /&gt;
 *&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 */&lt;br /&gt;
class HelloModelHello extends JModel&lt;br /&gt;
{&lt;br /&gt;
    /**&lt;br /&gt;
    * Gets the greeting&lt;br /&gt;
    * @return string The greeting to be displayed to the user&lt;br /&gt;
    */&lt;br /&gt;
    function getGreeting()&lt;br /&gt;
    {&lt;br /&gt;
        return &#039;Hello, World!&#039;;&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You will notice a line that starts with jimport. The jimport function is used to load files from the Joomla! framework that are required for our component. This particular statement will load the file /libraries/joomla/application/component/model.php. The &#039;.&#039;s are used as directory separators and the last part is the name of the file to load. All files are loaded relative to the libraries directory. This particular file contains the class definition for the JModel class, which is necessary because our model extends this class.&lt;br /&gt;
&lt;br /&gt;
Now that we have created our model, we must modify our view so that it uses it to obtain the greeting.&lt;br /&gt;
&lt;br /&gt;
== Using the Model ==&lt;br /&gt;
&lt;br /&gt;
The Joomla! framework is setup in such a way that the controller will automatically load the model that has the same name as the view and will push it into the view. Since our view is called &#039;Hello&#039;, our &#039;Hello&#039; model will automatically be loaded and pushed into the view. Therefore, we can easily retrieve a reference to our model using the JView::getModel() method.&lt;br /&gt;
&lt;br /&gt;
Our previous view code contained the lines:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$greeting = &amp;quot;Hello World!&amp;quot;;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To take advantage of our model, we change this line to:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$model =&amp;amp; $this-&amp;gt;getModel();&lt;br /&gt;
$greeting = $model-&amp;gt;getGreeting();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The complete view now looks like:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_2&lt;br /&gt;
 * @license    GNU/GPL&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
// No direct access&lt;br /&gt;
&lt;br /&gt;
defined( &#039;_JEXEC&#039; ) or die( &#039;Restricted access&#039; );&lt;br /&gt;
&lt;br /&gt;
jimport( &#039;joomla.application.component.view&#039;);&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * HTML View class for the HelloWorld Component&lt;br /&gt;
 *&lt;br /&gt;
 * @package    HelloWorld&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
class HelloViewHello extends JView&lt;br /&gt;
{&lt;br /&gt;
    function display($tpl = null)&lt;br /&gt;
    {&lt;br /&gt;
        $model =&amp;amp; $this-&amp;gt;getModel();&lt;br /&gt;
        $greeting = $model-&amp;gt;getGreeting();&lt;br /&gt;
        $this-&amp;gt;assignRef( &#039;greeting&#039;,	$greeting );&lt;br /&gt;
&lt;br /&gt;
        parent::display($tpl);&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Adding the File to the Package ===&lt;br /&gt;
&lt;br /&gt;
All that remains is to add an entry to the XML file so that our new model will be copied. The Joomla! framework will look for our model in the models directory, so the entry for this file will look like (it should be added to the site section):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;filename&amp;gt;models/hello.php&amp;lt;/filename&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Our new hello.xml file will look like:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;&lt;br /&gt;
&amp;lt;install type=&amp;quot;component&amp;quot; version=&amp;quot;1.5.0&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;name&amp;gt;Hello&amp;lt;/name&amp;gt;&lt;br /&gt;
 &amp;lt;!-- The following elements are optional and free of formatting conttraints --&amp;gt;&lt;br /&gt;
 &amp;lt;creationDate&amp;gt;2007-02-22&amp;lt;/creationDate&amp;gt;&lt;br /&gt;
 &amp;lt;author&amp;gt;John Doe&amp;lt;/author&amp;gt;&lt;br /&gt;
 &amp;lt;authorEmail&amp;gt;john.doe@example.org&amp;lt;/authorEmail&amp;gt;&lt;br /&gt;
 &amp;lt;authorUrl&amp;gt;http://www.example.org&amp;lt;/authorUrl&amp;gt;&lt;br /&gt;
 &amp;lt;copyright&amp;gt;Copyright Info&amp;lt;/copyright&amp;gt;&lt;br /&gt;
 &amp;lt;license&amp;gt;License Info&amp;lt;/license&amp;gt;&lt;br /&gt;
 &amp;lt;!--  The version string is recorded in the components table --&amp;gt;&lt;br /&gt;
 &amp;lt;version&amp;gt;1.01&amp;lt;/version&amp;gt;&lt;br /&gt;
 &amp;lt;!-- The description is optional and defaults to the name --&amp;gt;&lt;br /&gt;
 &amp;lt;description&amp;gt;Description of the component ...&amp;lt;/description&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;!-- Site Main File Copy Section --&amp;gt;&lt;br /&gt;
 &amp;lt;!-- Note the folder attribute: This attribute describes the folder&lt;br /&gt;
      to copy FROM in the package to install therefore files copied&lt;br /&gt;
      in this section are copied from /site/ in the package --&amp;gt;&lt;br /&gt;
 &amp;lt;files folder=&amp;quot;site&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;controller.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;models/hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;models/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/view.html.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/tmpl/default.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/tmpl/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
 &amp;lt;/files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;administration&amp;gt;&lt;br /&gt;
  &amp;lt;!-- Administration Menu Section --&amp;gt;&lt;br /&gt;
  &amp;lt;menu&amp;gt;Hello World!&amp;lt;/menu&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;!-- Administration Main File Copy Section --&amp;gt;&lt;br /&gt;
  &amp;lt;files folder=&amp;quot;admin&amp;quot;&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;/files&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
 &amp;lt;/administration&amp;gt;&lt;br /&gt;
&amp;lt;/install&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
We now have a simple MVC component. Each element is very simple at this point, but provides a great deal of flexibility and power.&lt;br /&gt;
&lt;br /&gt;
== Contributors ==&lt;br /&gt;
* staalanden&lt;br /&gt;
&lt;br /&gt;
== Download ==&lt;br /&gt;
&lt;br /&gt;
The component can be downloaded at: [http://joomlacode.org/gf/download/frsrelease/8104/29402/com_hello2_01.zip]&lt;/div&gt;</summary>
		<author><name>Tonie</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Introduction&amp;diff=9488</id>
		<title>J1.5:Developing a MVC Component/Introduction</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Introduction&amp;diff=9488"/>
		<updated>2008-07-27T17:59:28Z</updated>

		<summary type="html">&lt;p&gt;Tonie: /* Wrapping It All Up - Creating the hello.xml File */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
A software framework is the base of an application that can be used by a developer. The framework in Joomla! 1.5 unleashes a great deal of power for them. The Joomla! code has been completely overhauled and cleaned up. This tutorial will guide you through the process of developing a component using the framework.&lt;br /&gt;
&lt;br /&gt;
The scope of this project will be to develop a simple Hello World! component. In future tutorials, this simple framework will be built upon to show the full power and versatility of the MVC design pattern in Joomla!&lt;br /&gt;
&lt;br /&gt;
== Requirements ==&lt;br /&gt;
You need Joomla! 1.5 or greater for this tutorial.&lt;br /&gt;
&lt;br /&gt;
== Introduction to Model-View-Controller ==&lt;br /&gt;
While the idea behind a component may seem extremely simple, code can quickly become very complex as additional features are added or the interface is customized.&lt;br /&gt;
&lt;br /&gt;
Model-View-Controller (herein referred to as MVC) is a software design pattern that can be used to organize code in such a way that the business logic and data presentation are separate. The premise behind this approach is that if the business logic is grouped into one section, then the interface and user interaction that surrounds the data can revised and customized without having to reprogram the business logic.&lt;br /&gt;
&lt;br /&gt;
There are three main parts of an MVC component. They are described here in brief, but for a more thorough explanation, please refer to the links provided at the end of this tutorial.&lt;br /&gt;
&lt;br /&gt;
=== Model ===&lt;br /&gt;
The model is the part of the component that encapsulates the application&#039;s data. It will often provide routines to manage and manipulate this data in a meaningful way in addition to routines that retrieve the data from the model. In our case, the model will contain methods to add, remove and update information about the greetings in the database. It will also contain methods to retrieve the list of greetings from the database. In general, the underlying data access technique should be encapsulated in the model. In this way, if an application is to be moved from a system that utilizes a flat file to store its information to a system that uses a database, the model is the only element that needs to be changed, not the view or the controller.&lt;br /&gt;
&lt;br /&gt;
=== View ===&lt;br /&gt;
The view is the part of the component that is used to render the data from the model in a manner that is suitable for interaction. For a web-based application, the view would generally be an HTML page that is returned to the data. The view pulls data from the model (which is passed to it from the controller) and feeds the data into a template which is populated and presented to the user. The view does not cause the data to be modified in any way, it only displays data retrieved from the model.&lt;br /&gt;
&lt;br /&gt;
=== Controller ===&lt;br /&gt;
The controller is responsible for responding to user actions. In the case of a web application, a user action is (generally) a page request. The controller will determine what request is being made by the user and respond appropriately by triggering the model to manipulate the data appropriately and passing the model into the view. The controller does not display the data in the model, it only triggers methods in the model which modify the data, and then pass the model into the view which displays the data.&lt;br /&gt;
&lt;br /&gt;
== Joomla! MVC Implementation ==&lt;br /&gt;
In Joomla!, the MVC pattern is implemented using three classes: [http://api.joomla.org/Joomla-Framework/Application/JModel.html JModel], [http://api.joomla.org/Joomla-Framework/Application/JView.html JView] and [http://api.joomla.org/Joomla-Framework/Application/JController.html JController]. For more detailed information about these classes, please refer to the API reference documentation (WIP).&lt;br /&gt;
&lt;br /&gt;
== Creating a Component ==&lt;br /&gt;
For our basic component, we only require five files:&lt;br /&gt;
&lt;br /&gt;
* hello.php - this is the entry point to our component&lt;br /&gt;
* controller.php - this file contains our base controller&lt;br /&gt;
* views/hello/view.html.php - this file retrieves the necessary data and pushes it into the template&lt;br /&gt;
* views/hello/tmpl/default.php - this is the template for our output&lt;br /&gt;
* hello.xml - this is an XML file that tells Joomla! how to install our component.&lt;br /&gt;
&lt;br /&gt;
=== Creating the Entry Point ===&lt;br /&gt;
Joomla! is always accessed through a single point of entry: index.php for the Site Application or administrator/index.php for the Administrator Application. The application will then load the required component, based on the value of &#039;option&#039; in the URL or in the POST data. For our component, the URL would be:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;index.php?option=com_hello&amp;amp;view=hello&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will load our main file, which can be seen as the single point of entry for our component: components/com_hello/hello.php.&lt;br /&gt;
&lt;br /&gt;
The code for this file is fairly typical across components.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * components/com_hello/hello.php&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_1&lt;br /&gt;
 * @license    GNU/GPL&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
// No direct access&lt;br /&gt;
&lt;br /&gt;
defined( &#039;_JEXEC&#039; ) or die( &#039;Restricted access&#039; );&lt;br /&gt;
&lt;br /&gt;
// Require the base controller&lt;br /&gt;
&lt;br /&gt;
require_once( JPATH_COMPONENT.DS.&#039;controller.php&#039; );&lt;br /&gt;
&lt;br /&gt;
// Require specific controller if requested&lt;br /&gt;
if($controller = JRequest::getWord(&#039;controller&#039;)) {&lt;br /&gt;
    $path = JPATH_COMPONENT.DS.&#039;controllers&#039;.DS.$controller.&#039;.php&#039;;&lt;br /&gt;
    if (file_exists($path)) {&lt;br /&gt;
        require_once $path;&lt;br /&gt;
    } else {&lt;br /&gt;
        $controller = &#039;&#039;;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Create the controller&lt;br /&gt;
$classname    = &#039;HelloController&#039;.$controller;&lt;br /&gt;
$controller   = new $classname( );&lt;br /&gt;
&lt;br /&gt;
// Perform the Request task&lt;br /&gt;
$controller-&amp;gt;execute( JRequest::getVar( &#039;task&#039; ) );&lt;br /&gt;
&lt;br /&gt;
// Redirect if set by the controller&lt;br /&gt;
$controller-&amp;gt;redirect();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first statement is a security check.&lt;br /&gt;
&lt;br /&gt;
JPATH_COMPONENT is the absolute path to the current component, in our case components/com_hello. If you specifically need either the Site component or the Administrator component, you can use JPATH_COMPONENT_SITE or JPATH_COMPONENT_ADMINISTRATOR.&lt;br /&gt;
&lt;br /&gt;
DS is the directory separator of your system: either &#039;/&#039; or &#039;\&#039;. This is automatically set by the framework so the developer doesn&#039;t have to worry about developing different versions for different server OSs. DS should always be used when referring to files on the local server.&lt;br /&gt;
&lt;br /&gt;
After loading the base controller, we check if a specific controller is needed. In this component, the base controller is the only controller, but we will leave this here for future use.&lt;br /&gt;
&lt;br /&gt;
JRequest:getVar() finds a variable in the URL or the POST data. So if our URL is index.php?option=com_hello&amp;gt;controller=controller_name, then we can retrieve our controller name in our component using: echo JRequest::getVar(&#039;controller&#039;);&lt;br /&gt;
&lt;br /&gt;
Now we have our base controller &#039;HelloController&#039; in com_hello/controller.php, and, if needed, additional controllers like &#039;HelloControllerController1&#039; in com_hello/controllers/controller1.php. Using this standard naming scheme will make things easy later on: &#039;{Componentname}{Controller}{Controllername}&#039;&lt;br /&gt;
&lt;br /&gt;
After the controller is created, we instruct the controller to execute the task, as defined in the URL: index.php?option=com_hello&amp;amp;task=sometask. If no task is set, the default task &#039;display&#039; will be assumed. When display is used, the &#039;view&#039; variable will decide what will be displayed. Other common tasks are save, edit, new...&lt;br /&gt;
&lt;br /&gt;
The controller might decide to redirect the page, usually after a task like &#039;save&#039; has been completed. This last statement takes care of the actual redirection.&lt;br /&gt;
&lt;br /&gt;
The main entry point (hello.php) essentially passes control to the controller, which handles performing the task that was specified in the request.&lt;br /&gt;
&lt;br /&gt;
Note that we don&#039;t use a closing php tag in this file: ?&amp;gt;. The reason for this is that we will not have any unwanted whitespace in the output code. This is default practice since Joomla! 1.5, and will be used for all php-only files.&lt;br /&gt;
&lt;br /&gt;
=== Creating the Controller ===&lt;br /&gt;
Our component only has one task - greet the world. Therefore, the controller will be very simple. No data manipulation is required. All that needs to be done is the appropriate view loaded. We will have only one method in our controller: display(). Most of the required functionality is built into the JController class, so all that we need to do is invoke the JController::display() method.&lt;br /&gt;
&lt;br /&gt;
The code for the base controller is:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_1&lt;br /&gt;
 * @license    GNU/GPL&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// No direct access&lt;br /&gt;
&lt;br /&gt;
defined( &#039;_JEXEC&#039; ) or die( &#039;Restricted access&#039; );&lt;br /&gt;
&lt;br /&gt;
jimport(&#039;joomla.application.component.controller&#039;);&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Hello World Component Controller&lt;br /&gt;
 *&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 */&lt;br /&gt;
class HelloController extends JController&lt;br /&gt;
{&lt;br /&gt;
    /**&lt;br /&gt;
     * Method to display the view&lt;br /&gt;
     *&lt;br /&gt;
     * @access    public&lt;br /&gt;
     */&lt;br /&gt;
    function display()&lt;br /&gt;
    {&lt;br /&gt;
        parent::display();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The JController constructor will always register a display() task and unless otherwise specified (using the registerDefaultTask() method), it will set it as the default task.&lt;br /&gt;
&lt;br /&gt;
This barebones display() method isn&#039;t really even necessary since all it does is invoke the parent constructor. However, it is a good visual clue to indicate what is happening in the controller.&lt;br /&gt;
&lt;br /&gt;
The JController::display() method will determine the name of the view and layout from the request and load that view and set the layout. When you create a menu item for your component, the menu manager will allow the administrator to select the view that they would like the menu link to display and to specify the layout. A view usually refers to a view of a certain set of data (i.e. a list of cars, a list of events, a single car, a single event). A layout is a way that that view is organized.&lt;br /&gt;
&lt;br /&gt;
In our component, we will have a single view called hello, and a single layout (default).&lt;br /&gt;
&lt;br /&gt;
=== Creating the View ===&lt;br /&gt;
The task of the view is very simple: It retrieves the data to be displayed and pushes it into the template. Data is pushed into the template using the JView::assignRef method.&lt;br /&gt;
&lt;br /&gt;
The code for the view is:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_1&lt;br /&gt;
 * @license    GNU/GPL&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
// no direct access&lt;br /&gt;
&lt;br /&gt;
defined( &#039;_JEXEC&#039; ) or die( &#039;Restricted access&#039; );&lt;br /&gt;
&lt;br /&gt;
jimport( &#039;joomla.application.component.view&#039;);&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * HTML View class for the HelloWorld Component&lt;br /&gt;
 *&lt;br /&gt;
 * @package    HelloWorld&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
class HelloViewHello extends JView&lt;br /&gt;
{&lt;br /&gt;
    function display($tpl = null)&lt;br /&gt;
    {&lt;br /&gt;
        $greeting = &amp;quot;Hello World!&amp;quot;;&lt;br /&gt;
        $this-&amp;gt;assignRef( &#039;greeting&#039;, $greeting );&lt;br /&gt;
&lt;br /&gt;
        parent::display($tpl);&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Creating the Template ====&lt;br /&gt;
&lt;br /&gt;
Joomla! templates/layouts are regular PHP files that are used to layout the data from the view in a particular manner. The variables assigned by the JView::assignRef method can be accessed from the template using $this-&amp;gt;{propertyname} (see the template code below for an example).&lt;br /&gt;
&lt;br /&gt;
Our template is very simple: we only want to display the greeting that was passed in from the view:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
&lt;br /&gt;
// No direct access&lt;br /&gt;
&lt;br /&gt;
defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;); ?&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;&amp;lt;?php echo $this-&amp;gt;greeting; ?&amp;gt;&amp;lt;/h1&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Wrapping It All Up - Creating the hello.xml File ===&lt;br /&gt;
It is possible to install a component manually by copying the files using an FTP client and modifying the database tables. It is more efficient to create a package file that will allow the Joomla! Installer to do this for you. This package file contains a variety of information:&lt;br /&gt;
&lt;br /&gt;
* basic descriptive details about your component (i.e. name), and optionally, a description, copyright and license information.&lt;br /&gt;
* a list of files that need to be copied.&lt;br /&gt;
* optionally, a PHP file that performs additional install and uninstall operations.&lt;br /&gt;
* optionally, an SQL file which contains database queries that should be executed upon install/uninstall&lt;br /&gt;
&lt;br /&gt;
The format of the XML file is as follows:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;&lt;br /&gt;
&amp;lt;install type=&amp;quot;component&amp;quot; version=&amp;quot;1.5.0&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;name&amp;gt;Hello&amp;lt;/name&amp;gt;&lt;br /&gt;
 &amp;lt;!-- The following elements are optional and free of formatting conttraints --&amp;gt;&lt;br /&gt;
 &amp;lt;creationDate&amp;gt;2007-02-22&amp;lt;/creationDate&amp;gt;&lt;br /&gt;
 &amp;lt;author&amp;gt;John Doe&amp;lt;/author&amp;gt;&lt;br /&gt;
 &amp;lt;authorEmail&amp;gt;john.doe@example.org&amp;lt;/authorEmail&amp;gt;&lt;br /&gt;
 &amp;lt;authorUrl&amp;gt;http://www.example.org&amp;lt;/authorUrl&amp;gt;&lt;br /&gt;
 &amp;lt;copyright&amp;gt;Copyright Info&amp;lt;/copyright&amp;gt;&lt;br /&gt;
 &amp;lt;license&amp;gt;License Info&amp;lt;/license&amp;gt;&lt;br /&gt;
 &amp;lt;!--  The version string is recorded in the components table --&amp;gt;&lt;br /&gt;
 &amp;lt;version&amp;gt;1.01&amp;lt;/version&amp;gt;&lt;br /&gt;
 &amp;lt;!-- The description is optional and defaults to the name --&amp;gt;&lt;br /&gt;
 &amp;lt;description&amp;gt;Description of the component ...&amp;lt;/description&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;!-- Site Main File Copy Section --&amp;gt;&lt;br /&gt;
 &amp;lt;!-- Note the folder attribute: This attribute describes the folder&lt;br /&gt;
      to copy FROM in the package to install therefore files copied&lt;br /&gt;
      in this section are copied from /site/ in the package --&amp;gt;&lt;br /&gt;
 &amp;lt;files folder=&amp;quot;site&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;controller.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/view.html.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/tmpl/default.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/tmpl/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
 &amp;lt;/files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;administration&amp;gt;&lt;br /&gt;
  &amp;lt;!-- Administration Menu Section --&amp;gt;&lt;br /&gt;
  &amp;lt;menu&amp;gt;Hello World!&amp;lt;/menu&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;!-- Administration Main File Copy Section --&amp;gt;&lt;br /&gt;
  &amp;lt;files folder=&amp;quot;admin&amp;quot;&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;/files&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
 &amp;lt;/administration&amp;gt;&lt;br /&gt;
&amp;lt;/install&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you look closely you will notice that there are some files that will be copied that we have not discussed. These are the index.html files. An index.html file is placed in each directory to prevent prying users from getting a directory listing. If there is no index.html file, some web servers will list the directory contents. This is often undesirable. These files have the simple line:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;html&amp;gt;&amp;lt;body bgcolor=&amp;quot;#FFFFFF&amp;quot;&amp;gt;&amp;lt;/body&amp;gt;&amp;lt;/html&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It will simply display a blank page.&lt;br /&gt;
&lt;br /&gt;
The other file is the admin.hello.php file. This is the entry point for the admin section of our component. Since we don&#039;t have an admin section of our component, it will have the same content as the index.html files at this point in time.&lt;br /&gt;
&lt;br /&gt;
== Contributors ==&lt;br /&gt;
* mjaz&lt;br /&gt;
* staalanden&lt;br /&gt;
&lt;br /&gt;
== Download ==&lt;br /&gt;
The component can be downloaded at: [http://joomlacode.org/gf/download/frsrelease/8104/29402/com_hello1_01.zip]&lt;/div&gt;</summary>
		<author><name>Tonie</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Using_the_Database&amp;diff=9487</id>
		<title>J1.5:Developing a MVC Component/Using the Database</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Using_the_Database&amp;diff=9487"/>
		<updated>2008-07-27T17:16:26Z</updated>

		<summary type="html">&lt;p&gt;Tonie: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
In the first two tutorials, we showed you how to build a simple model-view-controller component. We had one view which retrieved data from a model (which was created in the 2nd tutorial). In this tutorial, we will be working with the model. Instead of the data being hard coded in the model, the model will retrieve the data from a table in the database.&lt;br /&gt;
&lt;br /&gt;
This tutorial will demonstrate how to use the JDatabase class to retrieve data from the database.&lt;br /&gt;
&lt;br /&gt;
== Retrieving the Data ==&lt;br /&gt;
&lt;br /&gt;
Our model currently has one method: getGreeting(). This method is very simple - all it does is return the hard-coded greeting.&lt;br /&gt;
&lt;br /&gt;
To make things more interesting, we will load the greeting from a database table. We will demonstrate later how to create an SQL file and add the appropriate code to the XML manifest file so that the table and some sample data will be created when the component is installed. For now, we will simply replace our return statement with some code that will retrieve the greeting from the database and return it.&lt;br /&gt;
&lt;br /&gt;
The first step is to obtain a reference to a database object. Since Joomla! uses the database for its normal operation, a database connection already exists; therefore, it is not necessary to create your own. A reference to the existing database can be obtained using:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$db =&amp;amp; JFactory::getDBO();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
JFactory is a static class that is used to retrieve references to many of the system objects. More information about this class can be found in the API documentation.&lt;br /&gt;
&lt;br /&gt;
The method name (getDBO) stands for get DataBase Object, and is easy and important to remember.&lt;br /&gt;
&lt;br /&gt;
Now that we have obtained a reference to the database object, we can retrieve our data. We do this in two steps:&lt;br /&gt;
&lt;br /&gt;
  * store our query in the database object&lt;br /&gt;
&lt;br /&gt;
  * load the result&lt;br /&gt;
&lt;br /&gt;
Our new getGreeting() method will therefore look like:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;function getGreeting()&lt;br /&gt;
{&lt;br /&gt;
   $db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
&lt;br /&gt;
   $query = &#039;SELECT greeting FROM #__hello&#039;;&lt;br /&gt;
   $db-&amp;gt;setQuery( $query );&lt;br /&gt;
   $greeting = $db-&amp;gt;loadResult();&lt;br /&gt;
&lt;br /&gt;
   return $greeting;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
hello is the name of the table that we will create later, and greeting is the name of the field that stores the greetings. If you are not familiar with SQL, it would be helpful to take a tutorial or a lesson to get yourself up to speed. One such tutorial can be found at [[http://www.w3schools.com/sql/default.asp|w3schools]].&lt;br /&gt;
&lt;br /&gt;
The $db-&amp;gt;loadResult() method will execute the stored database query and return the first field of the first row of the result. See [[http://dev.joomla.org/component/option,com_jd-wiki/Itemid,/id,references:joomla.framework:database:jdatabase/|JDatabase API reference]] for more information about other load methods in the JDatabase class.&lt;br /&gt;
&lt;br /&gt;
== Creating the Installation SQL File ==&lt;br /&gt;
&lt;br /&gt;
The Joomla! installer has built-in support for executing queries during component installation. These queries are all stored in a standard text file.&lt;br /&gt;
&lt;br /&gt;
We will have three queries in our install file: the first will drop the table in case it already exists, the second will create the table with the appropriate fields, and the third will insert the data.&lt;br /&gt;
&lt;br /&gt;
Here are our queries:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;DROP TABLE IF EXISTS `#__hello`;&lt;br /&gt;
&lt;br /&gt;
CREATE TABLE `#__hello` (&lt;br /&gt;
  `id` int(11) NOT NULL auto_increment,&lt;br /&gt;
  `greeting` varchar(25) NOT NULL,&lt;br /&gt;
  PRIMARY KEY  (`id`)&lt;br /&gt;
) ENGINE=MyISAM AUTO_INCREMENT DEFAULT CHARSET=utf8;&lt;br /&gt;
&lt;br /&gt;
INSERT INTO `#__hello` (`greeting`) VALUES (&#039;Hello, World!&#039;),&lt;br /&gt;
(&#039;Bonjour, Monde!&#039;),&lt;br /&gt;
(&#039;Ciao, Mondo!&#039;);&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You might find the  prefix on the table names rather odd. Joomla! will replace this prefix with the prefix used by the current install. For most installs, this table will become jos_hello. This allows multiple installs of Joomla! to use the same database, and prevents collisions with other applications using the same table names (i.e. two applications might share a database, but might both require a &#039;users&#039; table. This convention avoids problems.)&lt;br /&gt;
&lt;br /&gt;
We have specified two fields in our database. The first field is id, and is called the &#039;primary key&#039;. The primary key of a database table is a field that is used to uniquely identify a record. This is often used to lookup rows in the database. The other field is greeting. This is the field that stores the greeting that is returned from the query that we used above.&lt;br /&gt;
&lt;br /&gt;
We will save our queries in a file called install.utf.sql.&lt;br /&gt;
&lt;br /&gt;
=== Creating the Uninstall SQL File ===&lt;br /&gt;
&lt;br /&gt;
Though we might hope that people will never want to uninstall our component, it is important that if they do, we don&#039;t leave anything behind. Joomla! will look after deleting the files and directories that were created during install, but you must manually include queries that will remove and tables that have been added to the database. Since we have only created one table, we only need one query:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;DROP TABLE IF EXISTS `#__hello`;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will save this query in a file called uninstall.utf.sql.&lt;br /&gt;
&lt;br /&gt;
== Updating our Install File ==&lt;br /&gt;
&lt;br /&gt;
We need to change a few things in our install file. First, we need to add our two new files to the list of files to install. SQL install file have to go in the admin directory. Second, we need to tell the installer to execute the queries in our files on install and uninstall.&lt;br /&gt;
&lt;br /&gt;
Our new file looks like this:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;&lt;br /&gt;
&amp;lt;!DOCTYPE install SYSTEM &amp;quot;http://dev.joomla.org/xml/1.5/component-install.dtd&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;install type=&amp;quot;component&amp;quot; version=&amp;quot;1.5.0&amp;quot;&amp;gt;&lt;br /&gt;
   &amp;lt;name&amp;gt;Hello&amp;lt;/name&amp;gt;&lt;br /&gt;
   &amp;lt;!-- The following elements are optional and free of formatting conttraints --&amp;gt;&lt;br /&gt;
   &amp;lt;creationDate&amp;gt;2007 02 22&amp;lt;/creationDate&amp;gt;&lt;br /&gt;
   &amp;lt;author&amp;gt;John Doe&amp;lt;/author&amp;gt;&lt;br /&gt;
   &amp;lt;authorEmail&amp;gt;john.doe@example.org&amp;lt;/authorEmail&amp;gt;&lt;br /&gt;
   &amp;lt;authorUrl&amp;gt;http://www.example.org&amp;lt;/authorUrl&amp;gt;&lt;br /&gt;
   &amp;lt;copyright&amp;gt;Copyright Info&amp;lt;/copyright&amp;gt;&lt;br /&gt;
   &amp;lt;license&amp;gt;License Info&amp;lt;/license&amp;gt;&lt;br /&gt;
   &amp;lt;!--  The version string is recorded in the components table --&amp;gt;&lt;br /&gt;
   &amp;lt;version&amp;gt;Component Version String&amp;lt;/version&amp;gt;&lt;br /&gt;
   &amp;lt;!-- The description is optional and defaults to the name --&amp;gt;&lt;br /&gt;
   &amp;lt;description&amp;gt;Description of the component ...&amp;lt;/description&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   &amp;lt;!-- Site Main File Copy Section --&amp;gt;&lt;br /&gt;
   &amp;lt;files folder=&amp;quot;site&amp;quot;&amp;gt;&lt;br /&gt;
      &amp;lt;filename&amp;gt;index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
      &amp;lt;filename&amp;gt;hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
      &amp;lt;filename&amp;gt;controller.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
      &amp;lt;filename&amp;gt;views/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
      &amp;lt;filename&amp;gt;views/hello/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
      &amp;lt;filename&amp;gt;views/hello/view.html.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
      &amp;lt;filename&amp;gt;views/hello/tmpl/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
      &amp;lt;filename&amp;gt;views/hello/tmpl/default.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
      &amp;lt;filename&amp;gt;models/hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
   &amp;lt;/files&amp;gt;&lt;br /&gt;
   &amp;lt;install&amp;gt;&lt;br /&gt;
      &amp;lt;sql&amp;gt;&lt;br /&gt;
         &amp;lt;file charset=&amp;quot;utf8&amp;quot; driver=&amp;quot;mysql&amp;quot;&amp;gt;install.sql&amp;lt;/file&amp;gt;&lt;br /&gt;
      &amp;lt;/sql&amp;gt;&lt;br /&gt;
   &amp;lt;/install&amp;gt;&lt;br /&gt;
   &amp;lt;uninstall&amp;gt;&lt;br /&gt;
      &amp;lt;sql&amp;gt;&lt;br /&gt;
         &amp;lt;file charset=&amp;quot;utf8&amp;quot; driver=&amp;quot;mysql&amp;quot;&amp;gt;uninstall.sql&amp;lt;/file&amp;gt;&lt;br /&gt;
      &amp;lt;/sql&amp;gt;&lt;br /&gt;
   &amp;lt;/uninstall&amp;gt;&lt;br /&gt;
   &amp;lt;administration&amp;gt;&lt;br /&gt;
      &amp;lt;!-- Administration Menu Section --&amp;gt;&lt;br /&gt;
      &amp;lt;menu&amp;gt;Hello World!&amp;lt;/menu&amp;gt;&lt;br /&gt;
      &lt;br /&gt;
      &amp;lt;!-- Administration Main File Copy Section --&amp;gt;&lt;br /&gt;
      &amp;lt;!-- Note the folder attribute: This attribute describes the folder&lt;br /&gt;
         to copy FROM in the package to install therefore files copied&lt;br /&gt;
         in this section are copied from /admin/ in the package --&amp;gt;&lt;br /&gt;
      &amp;lt;files folder=&amp;quot;admin&amp;quot;&amp;gt;&lt;br /&gt;
   &amp;lt;!-- Site Main File Copy Section --&amp;gt;&lt;br /&gt;
         &amp;lt;filename&amp;gt;index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
         &amp;lt;filename&amp;gt;admin.hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
         &amp;lt;filename&amp;gt;install.sql&amp;lt;/filename&amp;gt;&lt;br /&gt;
         &amp;lt;filename&amp;gt;uninstall.sql&amp;lt;/filename&amp;gt;&lt;br /&gt;
      &amp;lt;/files&amp;gt;      &lt;br /&gt;
   &amp;lt;/administration&amp;gt;&lt;br /&gt;
&amp;lt;/install&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You will notice two attributes present on the &amp;lt;file&amp;gt; tags within the &amp;lt;install&amp;gt; and &amp;lt;uninstall&amp;gt; sections: charset and driver. The charset is the type of charset to use. The only valid charset is utf8. If you want to create install files for non-utf8 databases (for older version of MySQL), you should omit this attribute.&lt;br /&gt;
&lt;br /&gt;
The driver attribute specifies which database the queries were written for. Currently, this can only be mysql, but in future versions of Joomla! there may be more database drivers available.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
We now have a component that takes advantage of both the Joomla! MVC framework classes and the JDatabase classes. You are now able to write MVC components that interact with the database and can use the Joomla! installer to create and populate database tables.&lt;br /&gt;
&lt;br /&gt;
== Contributors ==&lt;br /&gt;
&lt;br /&gt;
  * staalanden&lt;br /&gt;
&lt;br /&gt;
== Download ==&lt;br /&gt;
&lt;br /&gt;
The component can be downloaded at: [[http://dev.joomla.org/components/com_jd-wiki/data/media/components/com_hello3.zip|com_hello3.zip]]&lt;/div&gt;</summary>
		<author><name>Tonie</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Using_the_Database&amp;diff=9486</id>
		<title>J1.5:Developing a MVC Component/Using the Database</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Using_the_Database&amp;diff=9486"/>
		<updated>2008-07-27T17:04:07Z</updated>

		<summary type="html">&lt;p&gt;Tonie: /* Updating our Install File */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
In the first two tutorials, we showed you how to build a simple model-view-controller component. We had one view which retrieved data from a model (which was created in the 2nd tutorial). In this tutorial, we will be working with the model. Instead of the data being hard coded in the model, the model will retrieve the data from a table in the database.&lt;br /&gt;
&lt;br /&gt;
This tutorial will demonstrate how to use the JDatabase class to retrieve data from the database.&lt;br /&gt;
&lt;br /&gt;
== Retrieving the Data ==&lt;br /&gt;
&lt;br /&gt;
Our model currently has one method: getGreeting(). This method is very simple - all it does is return the hard-coded greeting.&lt;br /&gt;
&lt;br /&gt;
To make things more interesting, we will load the greeting from a database table. We will demonstrate later how to create an SQL file and add the appropriate code to the XML manifest file so that the table and some sample data will be created when the component is installed. For now, we will simply replace our return statement with some code that will retrieve the greeting from the database and return it.&lt;br /&gt;
&lt;br /&gt;
The first step is to obtain a reference to a database object. Since Joomla! uses the database for its normal operation, a database connection already exists; therefore, it is not necessary to create your own. A reference to the existing database can be obtained using:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$db =&amp;amp; JFactory::getDBO();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
JFactory is a static class that is used to retrieve references to many of the system objects. More information about this class can be found in the API documentation.&lt;br /&gt;
&lt;br /&gt;
The method name (getDBO) stands for get DataBase Object, and is easy and important to remember.&lt;br /&gt;
&lt;br /&gt;
Now that we have obtained a reference to the database object, we can retrieve our data. We do this in two steps:&lt;br /&gt;
&lt;br /&gt;
  * store our query in the database object&lt;br /&gt;
&lt;br /&gt;
  * load the result&lt;br /&gt;
&lt;br /&gt;
Our new getGreeting() method will therefore look like:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;function getGreeting()&lt;br /&gt;
{&lt;br /&gt;
   $db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
&lt;br /&gt;
   $query = &#039;SELECT greeting FROM #__hello&#039;;&lt;br /&gt;
   $db-&amp;gt;setQuery( $query );&lt;br /&gt;
   $greeting = $db-&amp;gt;loadResult();&lt;br /&gt;
&lt;br /&gt;
   return $greeting;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
hello is the name of the table that we will create later, and greeting is the name of the field that stores the greetings. If you are not familiar with SQL, it would be helpful to take a tutorial or a lesson to get yourself up to speed. One such tutorial can be found at [[http://www.w3schools.com/sql/default.asp|w3schools]].&lt;br /&gt;
&lt;br /&gt;
The $db-&amp;gt;loadResult() method will execute the stored database query and return the first field of the first row of the result. See [[http://dev.joomla.org/component/option,com_jd-wiki/Itemid,/id,references:joomla.framework:database:jdatabase/|JDatabase API reference]] for more information about other load methods in the JDatabase class.&lt;br /&gt;
&lt;br /&gt;
== Creating the Installation SQL File ==&lt;br /&gt;
&lt;br /&gt;
The Joomla! installer has built-in support for executing queries during component installation. These queries are all stored in a standard text file.&lt;br /&gt;
&lt;br /&gt;
We will have three queries in our install file: the first will drop the table in case it already exists, the second will create the table with the appropriate fields, and the third will insert the data.&lt;br /&gt;
&lt;br /&gt;
Here are our queries:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;DROP TABLE IF EXISTS `#__hello`;&lt;br /&gt;
&lt;br /&gt;
CREATE TABLE `#__hello` (&lt;br /&gt;
  `id` int(11) NOT NULL auto_increment,&lt;br /&gt;
  `greeting` varchar(25) NOT NULL,&lt;br /&gt;
  PRIMARY KEY  (`id`)&lt;br /&gt;
) ENGINE=MyISAM AUTO_INCREMENT DEFAULT CHARSET=utf8;&lt;br /&gt;
&lt;br /&gt;
INSERT INTO `#__hello` (`greeting`) VALUES (&#039;Hello, World!&#039;),&lt;br /&gt;
(&#039;Bonjour, Monde!&#039;),&lt;br /&gt;
(&#039;Ciao, Mondo!&#039;);&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You might find the  prefix on the table names rather odd. Joomla! will replace this prefix with the prefix used by the current install. For most installs, this table will become jos_hello. This allows multiple installs of Joomla! to use the same database, and prevents collisions with other applications using the same table names (i.e. two applications might share a database, but might both require a &#039;users&#039; table. This convention avoids problems.)&lt;br /&gt;
&lt;br /&gt;
We have specified two fields in our database. The first field is id, and is called the &#039;primary key&#039;. The primary key of a database table is a field that is used to uniquely identify a record. This is often used to lookup rows in the database. The other field is greeting. This is the field that stores the greeting that is returned from the query that we used above.&lt;br /&gt;
&lt;br /&gt;
We will save our queries in a file called install.utf.sql.&lt;br /&gt;
&lt;br /&gt;
=== Creating the Uninstall SQL File ===&lt;br /&gt;
&lt;br /&gt;
Though we might hope that people will never want to uninstall our component, it is important that if they do, we don&#039;t leave anything behind. Joomla! will look after deleting the files and directories that were created during install, but you must manually include queries that will remove and tables that have been added to the database. Since we have only created one table, we only need one query:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;DROP TABLE IF EXISTS `#__hello`;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will save this query in a file called uninstall.utf.sql.&lt;br /&gt;
&lt;br /&gt;
== Updating our Install File ==&lt;br /&gt;
&lt;br /&gt;
We need to change a few things in our install file. First, we need to add our two new files to the list of files to install. SQL install file have to go in the admin directory. Second, we need to tell the installer to execute the queries in our files on install and uninstall.&lt;br /&gt;
&lt;br /&gt;
Our new file looks like this:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;&lt;br /&gt;
&amp;lt;!DOCTYPE install SYSTEM &amp;quot;http://dev.joomla.org/xml/1.5/component-install.dtd&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;install type=&amp;quot;component&amp;quot; version=&amp;quot;1.5.0&amp;quot;&amp;gt;&lt;br /&gt;
   &amp;lt;name&amp;gt;Hello&amp;lt;/name&amp;gt;&lt;br /&gt;
   &amp;lt;!-- The following elements are optional and free of formatting conttraints --&amp;gt;&lt;br /&gt;
   &amp;lt;creationDate&amp;gt;2007 02 22&amp;lt;/creationDate&amp;gt;&lt;br /&gt;
   &amp;lt;author&amp;gt;John Doe&amp;lt;/author&amp;gt;&lt;br /&gt;
   &amp;lt;authorEmail&amp;gt;john.doe@example.org&amp;lt;/authorEmail&amp;gt;&lt;br /&gt;
   &amp;lt;authorUrl&amp;gt;http://www.example.org&amp;lt;/authorUrl&amp;gt;&lt;br /&gt;
   &amp;lt;copyright&amp;gt;Copyright Info&amp;lt;/copyright&amp;gt;&lt;br /&gt;
   &amp;lt;license&amp;gt;License Info&amp;lt;/license&amp;gt;&lt;br /&gt;
   &amp;lt;!--  The version string is recorded in the components table --&amp;gt;&lt;br /&gt;
   &amp;lt;version&amp;gt;Component Version String&amp;lt;/version&amp;gt;&lt;br /&gt;
   &amp;lt;!-- The description is optional and defaults to the name --&amp;gt;&lt;br /&gt;
   &amp;lt;description&amp;gt;Description of the component ...&amp;lt;/description&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   &amp;lt;!-- Site Main File Copy Section --&amp;gt;&lt;br /&gt;
   &amp;lt;files folder=&amp;quot;site&amp;quot;&amp;gt;&lt;br /&gt;
      &amp;lt;filename&amp;gt;index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
      &amp;lt;filename&amp;gt;hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
      &amp;lt;filename&amp;gt;controller.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
      &amp;lt;filename&amp;gt;views/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
      &amp;lt;filename&amp;gt;views/hello/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
      &amp;lt;filename&amp;gt;views/hello/view.html.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
      &amp;lt;filename&amp;gt;views/hello/tmpl/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
      &amp;lt;filename&amp;gt;views/hello/tmpl/default.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
      &amp;lt;filename&amp;gt;models/hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
   &amp;lt;/files&amp;gt;&lt;br /&gt;
   &amp;lt;install&amp;gt;&lt;br /&gt;
      &amp;lt;sql&amp;gt;&lt;br /&gt;
         &amp;lt;file charset=&amp;quot;utf8&amp;quot; driver=&amp;quot;mysql&amp;quot;&amp;gt;install.sql&amp;lt;/file&amp;gt;&lt;br /&gt;
      &amp;lt;/sql&amp;gt;&lt;br /&gt;
   &amp;lt;/install&amp;gt;&lt;br /&gt;
   &amp;lt;uninstall&amp;gt;&lt;br /&gt;
      &amp;lt;sql&amp;gt;&lt;br /&gt;
         &amp;lt;file charset=&amp;quot;utf8&amp;quot; driver=&amp;quot;mysql&amp;quot;&amp;gt;uninstall.sql&amp;lt;/file&amp;gt;&lt;br /&gt;
      &amp;lt;/sql&amp;gt;&lt;br /&gt;
   &amp;lt;/uninstall&amp;gt;   &lt;br /&gt;
   &amp;lt;administration&amp;gt;&lt;br /&gt;
      &amp;lt;!-- Administration Menu Section --&amp;gt;&lt;br /&gt;
      &amp;lt;menu&amp;gt;Hello World!&amp;lt;/menu&amp;gt;&lt;br /&gt;
      &lt;br /&gt;
      &amp;lt;!-- Administration Main File Copy Section --&amp;gt;&lt;br /&gt;
      &amp;lt;!-- Note the folder attribute: This attribute describes the folder&lt;br /&gt;
         to copy FROM in the package to install therefore files copied&lt;br /&gt;
         in this section are copied from /admin/ in the package --&amp;gt;&lt;br /&gt;
      &amp;lt;files folder=&amp;quot;admin&amp;quot;&amp;gt;&lt;br /&gt;
   &amp;lt;!-- Site Main File Copy Section --&amp;gt;&lt;br /&gt;
         &amp;lt;filename&amp;gt;index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
         &amp;lt;filename&amp;gt;admin.hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
         &amp;lt;filename&amp;gt;install.sql&amp;lt;/filename&amp;gt;&lt;br /&gt;
         &amp;lt;filename&amp;gt;uninstall.sql&amp;lt;/filename&amp;gt;&lt;br /&gt;
      &amp;lt;/files&amp;gt;      &lt;br /&gt;
   &amp;lt;/administration&amp;gt;&lt;br /&gt;
&amp;lt;/install&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You will notice two attributes present on the &amp;lt;file&amp;gt; tags within the &amp;lt;install&amp;gt; and &amp;lt;uninstall&amp;gt; sections: charset and driver. The charset is the type of charset to use. The only valid charset is utf8. If you want to create install files for non-utf8 databases (for older version of MySQL), you should omit this attribute.&lt;br /&gt;
&lt;br /&gt;
The driver attribute specifies which database the queries were written for. Currently, this can only be mysql, but in future versions of Joomla! there may be more database drivers available.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
We now have a component that takes advantage of both the Joomla! MVC framework classes and the JDatabase classes. You are now able to write MVC components that interact with the database and can use the Joomla! installer to create and populate database tables.&lt;br /&gt;
&lt;br /&gt;
== Contributors ==&lt;br /&gt;
&lt;br /&gt;
  * staalanden&lt;br /&gt;
&lt;br /&gt;
== Download ==&lt;br /&gt;
&lt;br /&gt;
The component can be downloaded at: [[http://dev.joomla.org/components/com_jd-wiki/data/media/components/com_hello3.zip|com_hello3.zip]]&lt;/div&gt;</summary>
		<author><name>Tonie</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Using_the_Database&amp;diff=9485</id>
		<title>J1.5:Developing a MVC Component/Using the Database</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Using_the_Database&amp;diff=9485"/>
		<updated>2008-07-27T17:03:50Z</updated>

		<summary type="html">&lt;p&gt;Tonie: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
In the first two tutorials, we showed you how to build a simple model-view-controller component. We had one view which retrieved data from a model (which was created in the 2nd tutorial). In this tutorial, we will be working with the model. Instead of the data being hard coded in the model, the model will retrieve the data from a table in the database.&lt;br /&gt;
&lt;br /&gt;
This tutorial will demonstrate how to use the JDatabase class to retrieve data from the database.&lt;br /&gt;
&lt;br /&gt;
== Retrieving the Data ==&lt;br /&gt;
&lt;br /&gt;
Our model currently has one method: getGreeting(). This method is very simple - all it does is return the hard-coded greeting.&lt;br /&gt;
&lt;br /&gt;
To make things more interesting, we will load the greeting from a database table. We will demonstrate later how to create an SQL file and add the appropriate code to the XML manifest file so that the table and some sample data will be created when the component is installed. For now, we will simply replace our return statement with some code that will retrieve the greeting from the database and return it.&lt;br /&gt;
&lt;br /&gt;
The first step is to obtain a reference to a database object. Since Joomla! uses the database for its normal operation, a database connection already exists; therefore, it is not necessary to create your own. A reference to the existing database can be obtained using:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$db =&amp;amp; JFactory::getDBO();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
JFactory is a static class that is used to retrieve references to many of the system objects. More information about this class can be found in the API documentation.&lt;br /&gt;
&lt;br /&gt;
The method name (getDBO) stands for get DataBase Object, and is easy and important to remember.&lt;br /&gt;
&lt;br /&gt;
Now that we have obtained a reference to the database object, we can retrieve our data. We do this in two steps:&lt;br /&gt;
&lt;br /&gt;
  * store our query in the database object&lt;br /&gt;
&lt;br /&gt;
  * load the result&lt;br /&gt;
&lt;br /&gt;
Our new getGreeting() method will therefore look like:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;function getGreeting()&lt;br /&gt;
{&lt;br /&gt;
   $db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
&lt;br /&gt;
   $query = &#039;SELECT greeting FROM #__hello&#039;;&lt;br /&gt;
   $db-&amp;gt;setQuery( $query );&lt;br /&gt;
   $greeting = $db-&amp;gt;loadResult();&lt;br /&gt;
&lt;br /&gt;
   return $greeting;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
hello is the name of the table that we will create later, and greeting is the name of the field that stores the greetings. If you are not familiar with SQL, it would be helpful to take a tutorial or a lesson to get yourself up to speed. One such tutorial can be found at [[http://www.w3schools.com/sql/default.asp|w3schools]].&lt;br /&gt;
&lt;br /&gt;
The $db-&amp;gt;loadResult() method will execute the stored database query and return the first field of the first row of the result. See [[http://dev.joomla.org/component/option,com_jd-wiki/Itemid,/id,references:joomla.framework:database:jdatabase/|JDatabase API reference]] for more information about other load methods in the JDatabase class.&lt;br /&gt;
&lt;br /&gt;
== Creating the Installation SQL File ==&lt;br /&gt;
&lt;br /&gt;
The Joomla! installer has built-in support for executing queries during component installation. These queries are all stored in a standard text file.&lt;br /&gt;
&lt;br /&gt;
We will have three queries in our install file: the first will drop the table in case it already exists, the second will create the table with the appropriate fields, and the third will insert the data.&lt;br /&gt;
&lt;br /&gt;
Here are our queries:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;DROP TABLE IF EXISTS `#__hello`;&lt;br /&gt;
&lt;br /&gt;
CREATE TABLE `#__hello` (&lt;br /&gt;
  `id` int(11) NOT NULL auto_increment,&lt;br /&gt;
  `greeting` varchar(25) NOT NULL,&lt;br /&gt;
  PRIMARY KEY  (`id`)&lt;br /&gt;
) ENGINE=MyISAM AUTO_INCREMENT DEFAULT CHARSET=utf8;&lt;br /&gt;
&lt;br /&gt;
INSERT INTO `#__hello` (`greeting`) VALUES (&#039;Hello, World!&#039;),&lt;br /&gt;
(&#039;Bonjour, Monde!&#039;),&lt;br /&gt;
(&#039;Ciao, Mondo!&#039;);&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You might find the  prefix on the table names rather odd. Joomla! will replace this prefix with the prefix used by the current install. For most installs, this table will become jos_hello. This allows multiple installs of Joomla! to use the same database, and prevents collisions with other applications using the same table names (i.e. two applications might share a database, but might both require a &#039;users&#039; table. This convention avoids problems.)&lt;br /&gt;
&lt;br /&gt;
We have specified two fields in our database. The first field is id, and is called the &#039;primary key&#039;. The primary key of a database table is a field that is used to uniquely identify a record. This is often used to lookup rows in the database. The other field is greeting. This is the field that stores the greeting that is returned from the query that we used above.&lt;br /&gt;
&lt;br /&gt;
We will save our queries in a file called install.utf.sql.&lt;br /&gt;
&lt;br /&gt;
=== Creating the Uninstall SQL File ===&lt;br /&gt;
&lt;br /&gt;
Though we might hope that people will never want to uninstall our component, it is important that if they do, we don&#039;t leave anything behind. Joomla! will look after deleting the files and directories that were created during install, but you must manually include queries that will remove and tables that have been added to the database. Since we have only created one table, we only need one query:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;DROP TABLE IF EXISTS `#__hello`;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will save this query in a file called uninstall.utf.sql.&lt;br /&gt;
&lt;br /&gt;
== Updating our Install File ==&lt;br /&gt;
&lt;br /&gt;
We need to change a few things in our install file. First, we need to add our two new files to the list of files to install. SQL install file have to go in the admin directory. Second, we need to tell the installer to execute the queries in our files on install and uninstall.&lt;br /&gt;
&lt;br /&gt;
Our new file looks like this:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;&lt;br /&gt;
&amp;lt;!DOCTYPE install SYSTEM &amp;quot;http://dev.joomla.org/xml/1.5/component-install.dtd&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;install type=&amp;quot;component&amp;quot; version=&amp;quot;1.5.0&amp;quot;&amp;gt;&lt;br /&gt;
   &amp;lt;name&amp;gt;Hello&amp;lt;/name&amp;gt;&lt;br /&gt;
   &amp;lt;!-- The following elements are optional and free of formatting conttraints --&amp;gt;&lt;br /&gt;
   &amp;lt;creationDate&amp;gt;2007 02 22&amp;lt;/creationDate&amp;gt;&lt;br /&gt;
   &amp;lt;author&amp;gt;John Doe&amp;lt;/author&amp;gt;&lt;br /&gt;
   &amp;lt;authorEmail&amp;gt;john.doe@example.org&amp;lt;/authorEmail&amp;gt;&lt;br /&gt;
   &amp;lt;authorUrl&amp;gt;http://www.example.org&amp;lt;/authorUrl&amp;gt;&lt;br /&gt;
   &amp;lt;copyright&amp;gt;Copyright Info&amp;lt;/copyright&amp;gt;&lt;br /&gt;
   &amp;lt;license&amp;gt;License Info&amp;lt;/license&amp;gt;&lt;br /&gt;
   &amp;lt;!--  The version string is recorded in the components table --&amp;gt;&lt;br /&gt;
   &amp;lt;version&amp;gt;Component Version String&amp;lt;/version&amp;gt;&lt;br /&gt;
   &amp;lt;!-- The description is optional and defaults to the name --&amp;gt;&lt;br /&gt;
   &amp;lt;description&amp;gt;Description of the component ...&amp;lt;/description&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   &amp;lt;!-- Site Main File Copy Section --&amp;gt;&lt;br /&gt;
   &amp;lt;files folder=&amp;quot;site&amp;quot;&amp;gt;&lt;br /&gt;
      &amp;lt;filename&amp;gt;index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
      &amp;lt;filename&amp;gt;hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
      &amp;lt;filename&amp;gt;controller.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
      &amp;lt;filename&amp;gt;views/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
      &amp;lt;filename&amp;gt;views/hello/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
      &amp;lt;filename&amp;gt;views/hello/view.html.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
      &amp;lt;filename&amp;gt;views/hello/tmpl/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
      &amp;lt;filename&amp;gt;views/hello/tmpl/default.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
      &amp;lt;filename&amp;gt;models/hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
   &amp;lt;/files&amp;gt;&lt;br /&gt;
   &amp;lt;install&amp;gt;&lt;br /&gt;
      &amp;lt;sql&amp;gt;&lt;br /&gt;
         &amp;lt;file charset=&amp;quot;utf8&amp;quot; driver=&amp;quot;mysql&amp;quot;&amp;gt;install.sql&amp;lt;/file&amp;gt;&lt;br /&gt;
      &amp;lt;/sql&amp;gt;&lt;br /&gt;
   &amp;lt;/install&amp;gt;&lt;br /&gt;
   &amp;lt;uninstall&amp;gt;&lt;br /&gt;
      &amp;lt;sql&amp;gt;&lt;br /&gt;
         &amp;lt;file charset=&amp;quot;utf8&amp;quot; driver=&amp;quot;mysql&amp;quot;&amp;gt;uninstall.sql&amp;lt;/file&amp;gt;&lt;br /&gt;
      &amp;lt;/sql&amp;gt;&lt;br /&gt;
   &amp;lt;/uninstall&amp;gt;   &lt;br /&gt;
   &amp;lt;administration&amp;gt;&lt;br /&gt;
      &amp;lt;!-- Administration Menu Section --&amp;gt;&lt;br /&gt;
      &amp;lt;menu&amp;gt;Hello World!&amp;lt;/menu&amp;gt;&lt;br /&gt;
      &lt;br /&gt;
      &amp;lt;!-- Administration Main File Copy Section --&amp;gt;&lt;br /&gt;
      &amp;lt;!-- Note the folder attribute: This attribute describes the folder&lt;br /&gt;
         to copy FROM in the package to install therefore files copied&lt;br /&gt;
         in this section are copied from /admin/ in the package --&amp;gt;&lt;br /&gt;
      &amp;lt;files folder=&amp;quot;admin&amp;quot;&amp;gt;&lt;br /&gt;
   &amp;lt;!-- Site Main File Copy Section --&amp;gt;&lt;br /&gt;
         &amp;lt;filename&amp;gt;index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
         &amp;lt;filename&amp;gt;admin.hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
         &amp;lt;filename&amp;gt;install.sql&amp;lt;/filename&amp;gt;&lt;br /&gt;
         &amp;lt;filename&amp;gt;uninstall.sql&amp;lt;/filename&amp;gt;&lt;br /&gt;
      &amp;lt;/files&amp;gt;      &lt;br /&gt;
   &amp;lt;/administration&amp;gt;&lt;br /&gt;
&amp;lt;/install&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You will notice two attributes present on the &amp;lt;file&amp;gt; tags within the &amp;lt;install&amp;gt; and &amp;lt;uninstall&amp;gt; sections: charset and driver. The charset is the type of charset to use. The only valid charset is utf8. If you want to create install files for non-utf8 databases (for older version of MySQL), you should omit this attribute.&lt;br /&gt;
&lt;br /&gt;
The driver attribute specifies which database the queries were written for. Currently, this can only be mysql, but in future versions of Joomla! there may be more database drivers available.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
We now have a component that takes advantage of both the Joomla! MVC framework classes and the JDatabase classes. You are now able to write MVC components that interact with the database and can use the Joomla! installer to create and populate database tables.&lt;br /&gt;
&lt;br /&gt;
== Contributors ==&lt;br /&gt;
&lt;br /&gt;
  * staalanden&lt;br /&gt;
&lt;br /&gt;
== Download ==&lt;br /&gt;
&lt;br /&gt;
The component can be downloaded at: [[http://dev.joomla.org/components/com_jd-wiki/data/media/components/com_hello3.zip|com_hello3.zip]]&lt;/div&gt;</summary>
		<author><name>Tonie</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Introduction&amp;diff=9484</id>
		<title>J1.5:Developing a MVC Component/Introduction</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Introduction&amp;diff=9484"/>
		<updated>2008-07-27T17:00:12Z</updated>

		<summary type="html">&lt;p&gt;Tonie: /* Creating the Entry Point */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
A software framework is the base of an application that can be used by a developer. The framework in Joomla! 1.5 unleashes a great deal of power for them. The Joomla! code has been completely overhauled and cleaned up. This tutorial will guide you through the process of developing a component using the framework.&lt;br /&gt;
&lt;br /&gt;
The scope of this project will be to develop a simple Hello World! component. In future tutorials, this simple framework will be built upon to show the full power and versatility of the MVC design pattern in Joomla!&lt;br /&gt;
&lt;br /&gt;
== Requirements ==&lt;br /&gt;
You need Joomla! 1.5 or greater for this tutorial.&lt;br /&gt;
&lt;br /&gt;
== Introduction to Model-View-Controller ==&lt;br /&gt;
While the idea behind a component may seem extremely simple, code can quickly become very complex as additional features are added or the interface is customized.&lt;br /&gt;
&lt;br /&gt;
Model-View-Controller (herein referred to as MVC) is a software design pattern that can be used to organize code in such a way that the business logic and data presentation are separate. The premise behind this approach is that if the business logic is grouped into one section, then the interface and user interaction that surrounds the data can revised and customized without having to reprogram the business logic.&lt;br /&gt;
&lt;br /&gt;
There are three main parts of an MVC component. They are described here in brief, but for a more thorough explanation, please refer to the links provided at the end of this tutorial.&lt;br /&gt;
&lt;br /&gt;
=== Model ===&lt;br /&gt;
The model is the part of the component that encapsulates the application&#039;s data. It will often provide routines to manage and manipulate this data in a meaningful way in addition to routines that retrieve the data from the model. In our case, the model will contain methods to add, remove and update information about the greetings in the database. It will also contain methods to retrieve the list of greetings from the database. In general, the underlying data access technique should be encapsulated in the model. In this way, if an application is to be moved from a system that utilizes a flat file to store its information to a system that uses a database, the model is the only element that needs to be changed, not the view or the controller.&lt;br /&gt;
&lt;br /&gt;
=== View ===&lt;br /&gt;
The view is the part of the component that is used to render the data from the model in a manner that is suitable for interaction. For a web-based application, the view would generally be an HTML page that is returned to the data. The view pulls data from the model (which is passed to it from the controller) and feeds the data into a template which is populated and presented to the user. The view does not cause the data to be modified in any way, it only displays data retrieved from the model.&lt;br /&gt;
&lt;br /&gt;
=== Controller ===&lt;br /&gt;
The controller is responsible for responding to user actions. In the case of a web application, a user action is (generally) a page request. The controller will determine what request is being made by the user and respond appropriately by triggering the model to manipulate the data appropriately and passing the model into the view. The controller does not display the data in the model, it only triggers methods in the model which modify the data, and then pass the model into the view which displays the data.&lt;br /&gt;
&lt;br /&gt;
== Joomla! MVC Implementation ==&lt;br /&gt;
In Joomla!, the MVC pattern is implemented using three classes: [http://api.joomla.org/Joomla-Framework/Application/JModel.html JModel], [http://api.joomla.org/Joomla-Framework/Application/JView.html JView] and [http://api.joomla.org/Joomla-Framework/Application/JController.html JController]. For more detailed information about these classes, please refer to the API reference documentation (WIP).&lt;br /&gt;
&lt;br /&gt;
== Creating a Component ==&lt;br /&gt;
For our basic component, we only require five files:&lt;br /&gt;
&lt;br /&gt;
* hello.php - this is the entry point to our component&lt;br /&gt;
* controller.php - this file contains our base controller&lt;br /&gt;
* views/hello/view.html.php - this file retrieves the necessary data and pushes it into the template&lt;br /&gt;
* views/hello/tmpl/default.php - this is the template for our output&lt;br /&gt;
* hello.xml - this is an XML file that tells Joomla! how to install our component.&lt;br /&gt;
&lt;br /&gt;
=== Creating the Entry Point ===&lt;br /&gt;
Joomla! is always accessed through a single point of entry: index.php for the Site Application or administrator/index.php for the Administrator Application. The application will then load the required component, based on the value of &#039;option&#039; in the URL or in the POST data. For our component, the URL would be:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;index.php?option=com_hello&amp;amp;view=hello&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will load our main file, which can be seen as the single point of entry for our component: components/com_hello/hello.php.&lt;br /&gt;
&lt;br /&gt;
The code for this file is fairly typical across components.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * components/com_hello/hello.php&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_1&lt;br /&gt;
 * @license    GNU/GPL&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
// No direct access&lt;br /&gt;
&lt;br /&gt;
defined( &#039;_JEXEC&#039; ) or die( &#039;Restricted access&#039; );&lt;br /&gt;
&lt;br /&gt;
// Require the base controller&lt;br /&gt;
&lt;br /&gt;
require_once( JPATH_COMPONENT.DS.&#039;controller.php&#039; );&lt;br /&gt;
&lt;br /&gt;
// Require specific controller if requested&lt;br /&gt;
if($controller = JRequest::getWord(&#039;controller&#039;)) {&lt;br /&gt;
    $path = JPATH_COMPONENT.DS.&#039;controllers&#039;.DS.$controller.&#039;.php&#039;;&lt;br /&gt;
    if (file_exists($path)) {&lt;br /&gt;
        require_once $path;&lt;br /&gt;
    } else {&lt;br /&gt;
        $controller = &#039;&#039;;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Create the controller&lt;br /&gt;
$classname    = &#039;HelloController&#039;.$controller;&lt;br /&gt;
$controller   = new $classname( );&lt;br /&gt;
&lt;br /&gt;
// Perform the Request task&lt;br /&gt;
$controller-&amp;gt;execute( JRequest::getVar( &#039;task&#039; ) );&lt;br /&gt;
&lt;br /&gt;
// Redirect if set by the controller&lt;br /&gt;
$controller-&amp;gt;redirect();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first statement is a security check.&lt;br /&gt;
&lt;br /&gt;
JPATH_COMPONENT is the absolute path to the current component, in our case components/com_hello. If you specifically need either the Site component or the Administrator component, you can use JPATH_COMPONENT_SITE or JPATH_COMPONENT_ADMINISTRATOR.&lt;br /&gt;
&lt;br /&gt;
DS is the directory separator of your system: either &#039;/&#039; or &#039;\&#039;. This is automatically set by the framework so the developer doesn&#039;t have to worry about developing different versions for different server OSs. DS should always be used when referring to files on the local server.&lt;br /&gt;
&lt;br /&gt;
After loading the base controller, we check if a specific controller is needed. In this component, the base controller is the only controller, but we will leave this here for future use.&lt;br /&gt;
&lt;br /&gt;
JRequest:getVar() finds a variable in the URL or the POST data. So if our URL is index.php?option=com_hello&amp;gt;controller=controller_name, then we can retrieve our controller name in our component using: echo JRequest::getVar(&#039;controller&#039;);&lt;br /&gt;
&lt;br /&gt;
Now we have our base controller &#039;HelloController&#039; in com_hello/controller.php, and, if needed, additional controllers like &#039;HelloControllerController1&#039; in com_hello/controllers/controller1.php. Using this standard naming scheme will make things easy later on: &#039;{Componentname}{Controller}{Controllername}&#039;&lt;br /&gt;
&lt;br /&gt;
After the controller is created, we instruct the controller to execute the task, as defined in the URL: index.php?option=com_hello&amp;amp;task=sometask. If no task is set, the default task &#039;display&#039; will be assumed. When display is used, the &#039;view&#039; variable will decide what will be displayed. Other common tasks are save, edit, new...&lt;br /&gt;
&lt;br /&gt;
The controller might decide to redirect the page, usually after a task like &#039;save&#039; has been completed. This last statement takes care of the actual redirection.&lt;br /&gt;
&lt;br /&gt;
The main entry point (hello.php) essentially passes control to the controller, which handles performing the task that was specified in the request.&lt;br /&gt;
&lt;br /&gt;
Note that we don&#039;t use a closing php tag in this file: ?&amp;gt;. The reason for this is that we will not have any unwanted whitespace in the output code. This is default practice since Joomla! 1.5, and will be used for all php-only files.&lt;br /&gt;
&lt;br /&gt;
=== Creating the Controller ===&lt;br /&gt;
Our component only has one task - greet the world. Therefore, the controller will be very simple. No data manipulation is required. All that needs to be done is the appropriate view loaded. We will have only one method in our controller: display(). Most of the required functionality is built into the JController class, so all that we need to do is invoke the JController::display() method.&lt;br /&gt;
&lt;br /&gt;
The code for the base controller is:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_1&lt;br /&gt;
 * @license    GNU/GPL&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// No direct access&lt;br /&gt;
&lt;br /&gt;
defined( &#039;_JEXEC&#039; ) or die( &#039;Restricted access&#039; );&lt;br /&gt;
&lt;br /&gt;
jimport(&#039;joomla.application.component.controller&#039;);&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Hello World Component Controller&lt;br /&gt;
 *&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 */&lt;br /&gt;
class HelloController extends JController&lt;br /&gt;
{&lt;br /&gt;
    /**&lt;br /&gt;
     * Method to display the view&lt;br /&gt;
     *&lt;br /&gt;
     * @access    public&lt;br /&gt;
     */&lt;br /&gt;
    function display()&lt;br /&gt;
    {&lt;br /&gt;
        parent::display();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The JController constructor will always register a display() task and unless otherwise specified (using the registerDefaultTask() method), it will set it as the default task.&lt;br /&gt;
&lt;br /&gt;
This barebones display() method isn&#039;t really even necessary since all it does is invoke the parent constructor. However, it is a good visual clue to indicate what is happening in the controller.&lt;br /&gt;
&lt;br /&gt;
The JController::display() method will determine the name of the view and layout from the request and load that view and set the layout. When you create a menu item for your component, the menu manager will allow the administrator to select the view that they would like the menu link to display and to specify the layout. A view usually refers to a view of a certain set of data (i.e. a list of cars, a list of events, a single car, a single event). A layout is a way that that view is organized.&lt;br /&gt;
&lt;br /&gt;
In our component, we will have a single view called hello, and a single layout (default).&lt;br /&gt;
&lt;br /&gt;
=== Creating the View ===&lt;br /&gt;
The task of the view is very simple: It retrieves the data to be displayed and pushes it into the template. Data is pushed into the template using the JView::assignRef method.&lt;br /&gt;
&lt;br /&gt;
The code for the view is:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_1&lt;br /&gt;
 * @license    GNU/GPL&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
// no direct access&lt;br /&gt;
&lt;br /&gt;
defined( &#039;_JEXEC&#039; ) or die( &#039;Restricted access&#039; );&lt;br /&gt;
&lt;br /&gt;
jimport( &#039;joomla.application.component.view&#039;);&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * HTML View class for the HelloWorld Component&lt;br /&gt;
 *&lt;br /&gt;
 * @package    HelloWorld&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
class HelloViewHello extends JView&lt;br /&gt;
{&lt;br /&gt;
    function display($tpl = null)&lt;br /&gt;
    {&lt;br /&gt;
        $greeting = &amp;quot;Hello World!&amp;quot;;&lt;br /&gt;
        $this-&amp;gt;assignRef( &#039;greeting&#039;, $greeting );&lt;br /&gt;
&lt;br /&gt;
        parent::display($tpl);&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Creating the Template ====&lt;br /&gt;
&lt;br /&gt;
Joomla! templates/layouts are regular PHP files that are used to layout the data from the view in a particular manner. The variables assigned by the JView::assignRef method can be accessed from the template using $this-&amp;gt;{propertyname} (see the template code below for an example).&lt;br /&gt;
&lt;br /&gt;
Our template is very simple: we only want to display the greeting that was passed in from the view:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
&lt;br /&gt;
// No direct access&lt;br /&gt;
&lt;br /&gt;
defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;); ?&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;&amp;lt;?php echo $this-&amp;gt;greeting; ?&amp;gt;&amp;lt;/h1&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Wrapping It All Up - Creating the hello.xml File ===&lt;br /&gt;
It is possible to install a component manually by copying the files using an FTP client and modifying the database tables. It is more efficient to create a package file that will allow the Joomla! Installer to do this for you. This package file contains a variety of information:&lt;br /&gt;
&lt;br /&gt;
* basic descriptive details about your component (i.e. name), and optionally, a description, copyright and license information.&lt;br /&gt;
* a list of files that need to be copied.&lt;br /&gt;
* optionally, a PHP file that performs additional install and uninstall operations.&lt;br /&gt;
* optionally, an SQL file which contains database queries that should be executed upon install/uninstall&lt;br /&gt;
&lt;br /&gt;
The format of the XML file is as follows:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;&lt;br /&gt;
&amp;lt;install type=&amp;quot;component&amp;quot; version=&amp;quot;1.5.0&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;name&amp;gt;Hello&amp;lt;/name&amp;gt;&lt;br /&gt;
 &amp;lt;!-- The following elements are optional and free of formatting conttraints --&amp;gt;&lt;br /&gt;
 &amp;lt;creationDate&amp;gt;2007-02-22&amp;lt;/creationDate&amp;gt;&lt;br /&gt;
 &amp;lt;author&amp;gt;John Doe&amp;lt;/author&amp;gt;&lt;br /&gt;
 &amp;lt;authorEmail&amp;gt;john.doe@example.org&amp;lt;/authorEmail&amp;gt;&lt;br /&gt;
 &amp;lt;authorUrl&amp;gt;http://www.example.org&amp;lt;/authorUrl&amp;gt;&lt;br /&gt;
 &amp;lt;copyright&amp;gt;Copyright Info&amp;lt;/copyright&amp;gt;&lt;br /&gt;
 &amp;lt;license&amp;gt;License Info&amp;lt;/license&amp;gt;&lt;br /&gt;
 &amp;lt;!--  The version string is recorded in the components table --&amp;gt;&lt;br /&gt;
 &amp;lt;version&amp;gt;Component Version String&amp;lt;/version&amp;gt;&lt;br /&gt;
 &amp;lt;!-- The description is optional and defaults to the name --&amp;gt;&lt;br /&gt;
 &amp;lt;description&amp;gt;Description of the component ...&amp;lt;/description&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;!-- Site Main File Copy Section --&amp;gt;&lt;br /&gt;
 &amp;lt;!-- Note the folder attribute: This attribute describes the folder&lt;br /&gt;
      to copy FROM in the package to install therefore files copied&lt;br /&gt;
      in this section are copied from /site/ in the package --&amp;gt;&lt;br /&gt;
 &amp;lt;files folder=&amp;quot;site&amp;quot;&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
 &amp;lt;/files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;administration&amp;gt;&lt;br /&gt;
  &amp;lt;!-- Administration Menu Section --&amp;gt;&lt;br /&gt;
  &amp;lt;menu&amp;gt;Hello World!&amp;lt;/menu&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;!-- Administration Main File Copy Section --&amp;gt;&lt;br /&gt;
  &amp;lt;files folder=&amp;quot;admin&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;controller.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/view.html.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/tmpl/default.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/tmpl/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;/files&amp;gt;  &lt;br /&gt;
 &amp;lt;/administration&amp;gt;&lt;br /&gt;
&amp;lt;/install&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you look closely you will notice that there are some files that will be copied that we have not discussed. These are the index.html files. An index.html file is placed in each directory to prevent prying users from getting a directory listing. If there is no index.html file, some web servers will list the directory contents. This is often undesirable. These files have the simple line:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;html&amp;gt;&amp;lt;body bgcolor=&amp;quot;#FFFFFF&amp;quot;&amp;gt;&amp;lt;/body&amp;gt;&amp;lt;/html&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It will simply display a blank page.&lt;br /&gt;
&lt;br /&gt;
The other file is the admin.hello.php file. This is the entry point for the admin section of our component. Since we don&#039;t have an admin section of our component, it will have the same content as the index.html files at this point in time.&lt;br /&gt;
&lt;br /&gt;
== Contributors ==&lt;br /&gt;
* mjaz&lt;br /&gt;
* staalanden&lt;br /&gt;
&lt;br /&gt;
== Download ==&lt;br /&gt;
The component can be downloaded at: [http://joomlacode.org/gf/download/frsrelease/8104/29402/com_hello1_01.zip]&lt;/div&gt;</summary>
		<author><name>Tonie</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Introduction&amp;diff=9483</id>
		<title>J1.5:Developing a MVC Component/Introduction</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Introduction&amp;diff=9483"/>
		<updated>2008-07-27T17:00:02Z</updated>

		<summary type="html">&lt;p&gt;Tonie: /* Creating the Controller */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
A software framework is the base of an application that can be used by a developer. The framework in Joomla! 1.5 unleashes a great deal of power for them. The Joomla! code has been completely overhauled and cleaned up. This tutorial will guide you through the process of developing a component using the framework.&lt;br /&gt;
&lt;br /&gt;
The scope of this project will be to develop a simple Hello World! component. In future tutorials, this simple framework will be built upon to show the full power and versatility of the MVC design pattern in Joomla!&lt;br /&gt;
&lt;br /&gt;
== Requirements ==&lt;br /&gt;
You need Joomla! 1.5 or greater for this tutorial.&lt;br /&gt;
&lt;br /&gt;
== Introduction to Model-View-Controller ==&lt;br /&gt;
While the idea behind a component may seem extremely simple, code can quickly become very complex as additional features are added or the interface is customized.&lt;br /&gt;
&lt;br /&gt;
Model-View-Controller (herein referred to as MVC) is a software design pattern that can be used to organize code in such a way that the business logic and data presentation are separate. The premise behind this approach is that if the business logic is grouped into one section, then the interface and user interaction that surrounds the data can revised and customized without having to reprogram the business logic.&lt;br /&gt;
&lt;br /&gt;
There are three main parts of an MVC component. They are described here in brief, but for a more thorough explanation, please refer to the links provided at the end of this tutorial.&lt;br /&gt;
&lt;br /&gt;
=== Model ===&lt;br /&gt;
The model is the part of the component that encapsulates the application&#039;s data. It will often provide routines to manage and manipulate this data in a meaningful way in addition to routines that retrieve the data from the model. In our case, the model will contain methods to add, remove and update information about the greetings in the database. It will also contain methods to retrieve the list of greetings from the database. In general, the underlying data access technique should be encapsulated in the model. In this way, if an application is to be moved from a system that utilizes a flat file to store its information to a system that uses a database, the model is the only element that needs to be changed, not the view or the controller.&lt;br /&gt;
&lt;br /&gt;
=== View ===&lt;br /&gt;
The view is the part of the component that is used to render the data from the model in a manner that is suitable for interaction. For a web-based application, the view would generally be an HTML page that is returned to the data. The view pulls data from the model (which is passed to it from the controller) and feeds the data into a template which is populated and presented to the user. The view does not cause the data to be modified in any way, it only displays data retrieved from the model.&lt;br /&gt;
&lt;br /&gt;
=== Controller ===&lt;br /&gt;
The controller is responsible for responding to user actions. In the case of a web application, a user action is (generally) a page request. The controller will determine what request is being made by the user and respond appropriately by triggering the model to manipulate the data appropriately and passing the model into the view. The controller does not display the data in the model, it only triggers methods in the model which modify the data, and then pass the model into the view which displays the data.&lt;br /&gt;
&lt;br /&gt;
== Joomla! MVC Implementation ==&lt;br /&gt;
In Joomla!, the MVC pattern is implemented using three classes: [http://api.joomla.org/Joomla-Framework/Application/JModel.html JModel], [http://api.joomla.org/Joomla-Framework/Application/JView.html JView] and [http://api.joomla.org/Joomla-Framework/Application/JController.html JController]. For more detailed information about these classes, please refer to the API reference documentation (WIP).&lt;br /&gt;
&lt;br /&gt;
== Creating a Component ==&lt;br /&gt;
For our basic component, we only require five files:&lt;br /&gt;
&lt;br /&gt;
* hello.php - this is the entry point to our component&lt;br /&gt;
* controller.php - this file contains our base controller&lt;br /&gt;
* views/hello/view.html.php - this file retrieves the necessary data and pushes it into the template&lt;br /&gt;
* views/hello/tmpl/default.php - this is the template for our output&lt;br /&gt;
* hello.xml - this is an XML file that tells Joomla! how to install our component.&lt;br /&gt;
&lt;br /&gt;
=== Creating the Entry Point ===&lt;br /&gt;
Joomla! is always accessed through a single point of entry: index.php for the Site Application or administrator/index.php for the Administrator Application. The application will then load the required component, based on the value of &#039;option&#039; in the URL or in the POST data. For our component, the URL would be:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;index.php?option=com_hello&amp;amp;view=hello&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will load our main file, which can be seen as the single point of entry for our component: components/com_hello/hello.php.&lt;br /&gt;
&lt;br /&gt;
The code for this file is fairly typical across components.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * components/com_hello/hello.php&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_1#Creating_the_Entry_Point&lt;br /&gt;
 * @license    GNU/GPL&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
// No direct access&lt;br /&gt;
&lt;br /&gt;
defined( &#039;_JEXEC&#039; ) or die( &#039;Restricted access&#039; );&lt;br /&gt;
&lt;br /&gt;
// Require the base controller&lt;br /&gt;
&lt;br /&gt;
require_once( JPATH_COMPONENT.DS.&#039;controller.php&#039; );&lt;br /&gt;
&lt;br /&gt;
// Require specific controller if requested&lt;br /&gt;
if($controller = JRequest::getWord(&#039;controller&#039;)) {&lt;br /&gt;
    $path = JPATH_COMPONENT.DS.&#039;controllers&#039;.DS.$controller.&#039;.php&#039;;&lt;br /&gt;
    if (file_exists($path)) {&lt;br /&gt;
        require_once $path;&lt;br /&gt;
    } else {&lt;br /&gt;
        $controller = &#039;&#039;;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Create the controller&lt;br /&gt;
$classname    = &#039;HelloController&#039;.$controller;&lt;br /&gt;
$controller   = new $classname( );&lt;br /&gt;
&lt;br /&gt;
// Perform the Request task&lt;br /&gt;
$controller-&amp;gt;execute( JRequest::getVar( &#039;task&#039; ) );&lt;br /&gt;
&lt;br /&gt;
// Redirect if set by the controller&lt;br /&gt;
$controller-&amp;gt;redirect();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first statement is a security check.&lt;br /&gt;
&lt;br /&gt;
JPATH_COMPONENT is the absolute path to the current component, in our case components/com_hello. If you specifically need either the Site component or the Administrator component, you can use JPATH_COMPONENT_SITE or JPATH_COMPONENT_ADMINISTRATOR.&lt;br /&gt;
&lt;br /&gt;
DS is the directory separator of your system: either &#039;/&#039; or &#039;\&#039;. This is automatically set by the framework so the developer doesn&#039;t have to worry about developing different versions for different server OSs. DS should always be used when referring to files on the local server.&lt;br /&gt;
&lt;br /&gt;
After loading the base controller, we check if a specific controller is needed. In this component, the base controller is the only controller, but we will leave this here for future use.&lt;br /&gt;
&lt;br /&gt;
JRequest:getVar() finds a variable in the URL or the POST data. So if our URL is index.php?option=com_hello&amp;gt;controller=controller_name, then we can retrieve our controller name in our component using: echo JRequest::getVar(&#039;controller&#039;);&lt;br /&gt;
&lt;br /&gt;
Now we have our base controller &#039;HelloController&#039; in com_hello/controller.php, and, if needed, additional controllers like &#039;HelloControllerController1&#039; in com_hello/controllers/controller1.php. Using this standard naming scheme will make things easy later on: &#039;{Componentname}{Controller}{Controllername}&#039;&lt;br /&gt;
&lt;br /&gt;
After the controller is created, we instruct the controller to execute the task, as defined in the URL: index.php?option=com_hello&amp;amp;task=sometask. If no task is set, the default task &#039;display&#039; will be assumed. When display is used, the &#039;view&#039; variable will decide what will be displayed. Other common tasks are save, edit, new...&lt;br /&gt;
&lt;br /&gt;
The controller might decide to redirect the page, usually after a task like &#039;save&#039; has been completed. This last statement takes care of the actual redirection.&lt;br /&gt;
&lt;br /&gt;
The main entry point (hello.php) essentially passes control to the controller, which handles performing the task that was specified in the request.&lt;br /&gt;
&lt;br /&gt;
Note that we don&#039;t use a closing php tag in this file: ?&amp;gt;. The reason for this is that we will not have any unwanted whitespace in the output code. This is default practice since Joomla! 1.5, and will be used for all php-only files.&lt;br /&gt;
&lt;br /&gt;
=== Creating the Controller ===&lt;br /&gt;
Our component only has one task - greet the world. Therefore, the controller will be very simple. No data manipulation is required. All that needs to be done is the appropriate view loaded. We will have only one method in our controller: display(). Most of the required functionality is built into the JController class, so all that we need to do is invoke the JController::display() method.&lt;br /&gt;
&lt;br /&gt;
The code for the base controller is:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_1&lt;br /&gt;
 * @license    GNU/GPL&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// No direct access&lt;br /&gt;
&lt;br /&gt;
defined( &#039;_JEXEC&#039; ) or die( &#039;Restricted access&#039; );&lt;br /&gt;
&lt;br /&gt;
jimport(&#039;joomla.application.component.controller&#039;);&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Hello World Component Controller&lt;br /&gt;
 *&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 */&lt;br /&gt;
class HelloController extends JController&lt;br /&gt;
{&lt;br /&gt;
    /**&lt;br /&gt;
     * Method to display the view&lt;br /&gt;
     *&lt;br /&gt;
     * @access    public&lt;br /&gt;
     */&lt;br /&gt;
    function display()&lt;br /&gt;
    {&lt;br /&gt;
        parent::display();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The JController constructor will always register a display() task and unless otherwise specified (using the registerDefaultTask() method), it will set it as the default task.&lt;br /&gt;
&lt;br /&gt;
This barebones display() method isn&#039;t really even necessary since all it does is invoke the parent constructor. However, it is a good visual clue to indicate what is happening in the controller.&lt;br /&gt;
&lt;br /&gt;
The JController::display() method will determine the name of the view and layout from the request and load that view and set the layout. When you create a menu item for your component, the menu manager will allow the administrator to select the view that they would like the menu link to display and to specify the layout. A view usually refers to a view of a certain set of data (i.e. a list of cars, a list of events, a single car, a single event). A layout is a way that that view is organized.&lt;br /&gt;
&lt;br /&gt;
In our component, we will have a single view called hello, and a single layout (default).&lt;br /&gt;
&lt;br /&gt;
=== Creating the View ===&lt;br /&gt;
The task of the view is very simple: It retrieves the data to be displayed and pushes it into the template. Data is pushed into the template using the JView::assignRef method.&lt;br /&gt;
&lt;br /&gt;
The code for the view is:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_1&lt;br /&gt;
 * @license    GNU/GPL&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
// no direct access&lt;br /&gt;
&lt;br /&gt;
defined( &#039;_JEXEC&#039; ) or die( &#039;Restricted access&#039; );&lt;br /&gt;
&lt;br /&gt;
jimport( &#039;joomla.application.component.view&#039;);&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * HTML View class for the HelloWorld Component&lt;br /&gt;
 *&lt;br /&gt;
 * @package    HelloWorld&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
class HelloViewHello extends JView&lt;br /&gt;
{&lt;br /&gt;
    function display($tpl = null)&lt;br /&gt;
    {&lt;br /&gt;
        $greeting = &amp;quot;Hello World!&amp;quot;;&lt;br /&gt;
        $this-&amp;gt;assignRef( &#039;greeting&#039;, $greeting );&lt;br /&gt;
&lt;br /&gt;
        parent::display($tpl);&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Creating the Template ====&lt;br /&gt;
&lt;br /&gt;
Joomla! templates/layouts are regular PHP files that are used to layout the data from the view in a particular manner. The variables assigned by the JView::assignRef method can be accessed from the template using $this-&amp;gt;{propertyname} (see the template code below for an example).&lt;br /&gt;
&lt;br /&gt;
Our template is very simple: we only want to display the greeting that was passed in from the view:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
&lt;br /&gt;
// No direct access&lt;br /&gt;
&lt;br /&gt;
defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;); ?&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;&amp;lt;?php echo $this-&amp;gt;greeting; ?&amp;gt;&amp;lt;/h1&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Wrapping It All Up - Creating the hello.xml File ===&lt;br /&gt;
It is possible to install a component manually by copying the files using an FTP client and modifying the database tables. It is more efficient to create a package file that will allow the Joomla! Installer to do this for you. This package file contains a variety of information:&lt;br /&gt;
&lt;br /&gt;
* basic descriptive details about your component (i.e. name), and optionally, a description, copyright and license information.&lt;br /&gt;
* a list of files that need to be copied.&lt;br /&gt;
* optionally, a PHP file that performs additional install and uninstall operations.&lt;br /&gt;
* optionally, an SQL file which contains database queries that should be executed upon install/uninstall&lt;br /&gt;
&lt;br /&gt;
The format of the XML file is as follows:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;&lt;br /&gt;
&amp;lt;install type=&amp;quot;component&amp;quot; version=&amp;quot;1.5.0&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;name&amp;gt;Hello&amp;lt;/name&amp;gt;&lt;br /&gt;
 &amp;lt;!-- The following elements are optional and free of formatting conttraints --&amp;gt;&lt;br /&gt;
 &amp;lt;creationDate&amp;gt;2007-02-22&amp;lt;/creationDate&amp;gt;&lt;br /&gt;
 &amp;lt;author&amp;gt;John Doe&amp;lt;/author&amp;gt;&lt;br /&gt;
 &amp;lt;authorEmail&amp;gt;john.doe@example.org&amp;lt;/authorEmail&amp;gt;&lt;br /&gt;
 &amp;lt;authorUrl&amp;gt;http://www.example.org&amp;lt;/authorUrl&amp;gt;&lt;br /&gt;
 &amp;lt;copyright&amp;gt;Copyright Info&amp;lt;/copyright&amp;gt;&lt;br /&gt;
 &amp;lt;license&amp;gt;License Info&amp;lt;/license&amp;gt;&lt;br /&gt;
 &amp;lt;!--  The version string is recorded in the components table --&amp;gt;&lt;br /&gt;
 &amp;lt;version&amp;gt;Component Version String&amp;lt;/version&amp;gt;&lt;br /&gt;
 &amp;lt;!-- The description is optional and defaults to the name --&amp;gt;&lt;br /&gt;
 &amp;lt;description&amp;gt;Description of the component ...&amp;lt;/description&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;!-- Site Main File Copy Section --&amp;gt;&lt;br /&gt;
 &amp;lt;!-- Note the folder attribute: This attribute describes the folder&lt;br /&gt;
      to copy FROM in the package to install therefore files copied&lt;br /&gt;
      in this section are copied from /site/ in the package --&amp;gt;&lt;br /&gt;
 &amp;lt;files folder=&amp;quot;site&amp;quot;&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
 &amp;lt;/files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;administration&amp;gt;&lt;br /&gt;
  &amp;lt;!-- Administration Menu Section --&amp;gt;&lt;br /&gt;
  &amp;lt;menu&amp;gt;Hello World!&amp;lt;/menu&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;!-- Administration Main File Copy Section --&amp;gt;&lt;br /&gt;
  &amp;lt;files folder=&amp;quot;admin&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;controller.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/view.html.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/tmpl/default.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/tmpl/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;/files&amp;gt;  &lt;br /&gt;
 &amp;lt;/administration&amp;gt;&lt;br /&gt;
&amp;lt;/install&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you look closely you will notice that there are some files that will be copied that we have not discussed. These are the index.html files. An index.html file is placed in each directory to prevent prying users from getting a directory listing. If there is no index.html file, some web servers will list the directory contents. This is often undesirable. These files have the simple line:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;html&amp;gt;&amp;lt;body bgcolor=&amp;quot;#FFFFFF&amp;quot;&amp;gt;&amp;lt;/body&amp;gt;&amp;lt;/html&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It will simply display a blank page.&lt;br /&gt;
&lt;br /&gt;
The other file is the admin.hello.php file. This is the entry point for the admin section of our component. Since we don&#039;t have an admin section of our component, it will have the same content as the index.html files at this point in time.&lt;br /&gt;
&lt;br /&gt;
== Contributors ==&lt;br /&gt;
* mjaz&lt;br /&gt;
* staalanden&lt;br /&gt;
&lt;br /&gt;
== Download ==&lt;br /&gt;
The component can be downloaded at: [http://joomlacode.org/gf/download/frsrelease/8104/29402/com_hello1_01.zip]&lt;/div&gt;</summary>
		<author><name>Tonie</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Introduction&amp;diff=9482</id>
		<title>J1.5:Developing a MVC Component/Introduction</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Introduction&amp;diff=9482"/>
		<updated>2008-07-27T16:59:53Z</updated>

		<summary type="html">&lt;p&gt;Tonie: /* Creating the View */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
A software framework is the base of an application that can be used by a developer. The framework in Joomla! 1.5 unleashes a great deal of power for them. The Joomla! code has been completely overhauled and cleaned up. This tutorial will guide you through the process of developing a component using the framework.&lt;br /&gt;
&lt;br /&gt;
The scope of this project will be to develop a simple Hello World! component. In future tutorials, this simple framework will be built upon to show the full power and versatility of the MVC design pattern in Joomla!&lt;br /&gt;
&lt;br /&gt;
== Requirements ==&lt;br /&gt;
You need Joomla! 1.5 or greater for this tutorial.&lt;br /&gt;
&lt;br /&gt;
== Introduction to Model-View-Controller ==&lt;br /&gt;
While the idea behind a component may seem extremely simple, code can quickly become very complex as additional features are added or the interface is customized.&lt;br /&gt;
&lt;br /&gt;
Model-View-Controller (herein referred to as MVC) is a software design pattern that can be used to organize code in such a way that the business logic and data presentation are separate. The premise behind this approach is that if the business logic is grouped into one section, then the interface and user interaction that surrounds the data can revised and customized without having to reprogram the business logic.&lt;br /&gt;
&lt;br /&gt;
There are three main parts of an MVC component. They are described here in brief, but for a more thorough explanation, please refer to the links provided at the end of this tutorial.&lt;br /&gt;
&lt;br /&gt;
=== Model ===&lt;br /&gt;
The model is the part of the component that encapsulates the application&#039;s data. It will often provide routines to manage and manipulate this data in a meaningful way in addition to routines that retrieve the data from the model. In our case, the model will contain methods to add, remove and update information about the greetings in the database. It will also contain methods to retrieve the list of greetings from the database. In general, the underlying data access technique should be encapsulated in the model. In this way, if an application is to be moved from a system that utilizes a flat file to store its information to a system that uses a database, the model is the only element that needs to be changed, not the view or the controller.&lt;br /&gt;
&lt;br /&gt;
=== View ===&lt;br /&gt;
The view is the part of the component that is used to render the data from the model in a manner that is suitable for interaction. For a web-based application, the view would generally be an HTML page that is returned to the data. The view pulls data from the model (which is passed to it from the controller) and feeds the data into a template which is populated and presented to the user. The view does not cause the data to be modified in any way, it only displays data retrieved from the model.&lt;br /&gt;
&lt;br /&gt;
=== Controller ===&lt;br /&gt;
The controller is responsible for responding to user actions. In the case of a web application, a user action is (generally) a page request. The controller will determine what request is being made by the user and respond appropriately by triggering the model to manipulate the data appropriately and passing the model into the view. The controller does not display the data in the model, it only triggers methods in the model which modify the data, and then pass the model into the view which displays the data.&lt;br /&gt;
&lt;br /&gt;
== Joomla! MVC Implementation ==&lt;br /&gt;
In Joomla!, the MVC pattern is implemented using three classes: [http://api.joomla.org/Joomla-Framework/Application/JModel.html JModel], [http://api.joomla.org/Joomla-Framework/Application/JView.html JView] and [http://api.joomla.org/Joomla-Framework/Application/JController.html JController]. For more detailed information about these classes, please refer to the API reference documentation (WIP).&lt;br /&gt;
&lt;br /&gt;
== Creating a Component ==&lt;br /&gt;
For our basic component, we only require five files:&lt;br /&gt;
&lt;br /&gt;
* hello.php - this is the entry point to our component&lt;br /&gt;
* controller.php - this file contains our base controller&lt;br /&gt;
* views/hello/view.html.php - this file retrieves the necessary data and pushes it into the template&lt;br /&gt;
* views/hello/tmpl/default.php - this is the template for our output&lt;br /&gt;
* hello.xml - this is an XML file that tells Joomla! how to install our component.&lt;br /&gt;
&lt;br /&gt;
=== Creating the Entry Point ===&lt;br /&gt;
Joomla! is always accessed through a single point of entry: index.php for the Site Application or administrator/index.php for the Administrator Application. The application will then load the required component, based on the value of &#039;option&#039; in the URL or in the POST data. For our component, the URL would be:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;index.php?option=com_hello&amp;amp;view=hello&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will load our main file, which can be seen as the single point of entry for our component: components/com_hello/hello.php.&lt;br /&gt;
&lt;br /&gt;
The code for this file is fairly typical across components.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * components/com_hello/hello.php&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_1#Creating_the_Entry_Point&lt;br /&gt;
 * @license    GNU/GPL&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
// No direct access&lt;br /&gt;
&lt;br /&gt;
defined( &#039;_JEXEC&#039; ) or die( &#039;Restricted access&#039; );&lt;br /&gt;
&lt;br /&gt;
// Require the base controller&lt;br /&gt;
&lt;br /&gt;
require_once( JPATH_COMPONENT.DS.&#039;controller.php&#039; );&lt;br /&gt;
&lt;br /&gt;
// Require specific controller if requested&lt;br /&gt;
if($controller = JRequest::getWord(&#039;controller&#039;)) {&lt;br /&gt;
    $path = JPATH_COMPONENT.DS.&#039;controllers&#039;.DS.$controller.&#039;.php&#039;;&lt;br /&gt;
    if (file_exists($path)) {&lt;br /&gt;
        require_once $path;&lt;br /&gt;
    } else {&lt;br /&gt;
        $controller = &#039;&#039;;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Create the controller&lt;br /&gt;
$classname    = &#039;HelloController&#039;.$controller;&lt;br /&gt;
$controller   = new $classname( );&lt;br /&gt;
&lt;br /&gt;
// Perform the Request task&lt;br /&gt;
$controller-&amp;gt;execute( JRequest::getVar( &#039;task&#039; ) );&lt;br /&gt;
&lt;br /&gt;
// Redirect if set by the controller&lt;br /&gt;
$controller-&amp;gt;redirect();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first statement is a security check.&lt;br /&gt;
&lt;br /&gt;
JPATH_COMPONENT is the absolute path to the current component, in our case components/com_hello. If you specifically need either the Site component or the Administrator component, you can use JPATH_COMPONENT_SITE or JPATH_COMPONENT_ADMINISTRATOR.&lt;br /&gt;
&lt;br /&gt;
DS is the directory separator of your system: either &#039;/&#039; or &#039;\&#039;. This is automatically set by the framework so the developer doesn&#039;t have to worry about developing different versions for different server OSs. DS should always be used when referring to files on the local server.&lt;br /&gt;
&lt;br /&gt;
After loading the base controller, we check if a specific controller is needed. In this component, the base controller is the only controller, but we will leave this here for future use.&lt;br /&gt;
&lt;br /&gt;
JRequest:getVar() finds a variable in the URL or the POST data. So if our URL is index.php?option=com_hello&amp;gt;controller=controller_name, then we can retrieve our controller name in our component using: echo JRequest::getVar(&#039;controller&#039;);&lt;br /&gt;
&lt;br /&gt;
Now we have our base controller &#039;HelloController&#039; in com_hello/controller.php, and, if needed, additional controllers like &#039;HelloControllerController1&#039; in com_hello/controllers/controller1.php. Using this standard naming scheme will make things easy later on: &#039;{Componentname}{Controller}{Controllername}&#039;&lt;br /&gt;
&lt;br /&gt;
After the controller is created, we instruct the controller to execute the task, as defined in the URL: index.php?option=com_hello&amp;amp;task=sometask. If no task is set, the default task &#039;display&#039; will be assumed. When display is used, the &#039;view&#039; variable will decide what will be displayed. Other common tasks are save, edit, new...&lt;br /&gt;
&lt;br /&gt;
The controller might decide to redirect the page, usually after a task like &#039;save&#039; has been completed. This last statement takes care of the actual redirection.&lt;br /&gt;
&lt;br /&gt;
The main entry point (hello.php) essentially passes control to the controller, which handles performing the task that was specified in the request.&lt;br /&gt;
&lt;br /&gt;
Note that we don&#039;t use a closing php tag in this file: ?&amp;gt;. The reason for this is that we will not have any unwanted whitespace in the output code. This is default practice since Joomla! 1.5, and will be used for all php-only files.&lt;br /&gt;
&lt;br /&gt;
=== Creating the Controller ===&lt;br /&gt;
Our component only has one task - greet the world. Therefore, the controller will be very simple. No data manipulation is required. All that needs to be done is the appropriate view loaded. We will have only one method in our controller: display(). Most of the required functionality is built into the JController class, so all that we need to do is invoke the JController::display() method.&lt;br /&gt;
&lt;br /&gt;
The code for the base controller is:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_1#Creating_the_Entry_Point&lt;br /&gt;
 * @license    GNU/GPL&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// No direct access&lt;br /&gt;
&lt;br /&gt;
defined( &#039;_JEXEC&#039; ) or die( &#039;Restricted access&#039; );&lt;br /&gt;
&lt;br /&gt;
jimport(&#039;joomla.application.component.controller&#039;);&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Hello World Component Controller&lt;br /&gt;
 *&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 */&lt;br /&gt;
class HelloController extends JController&lt;br /&gt;
{&lt;br /&gt;
    /**&lt;br /&gt;
     * Method to display the view&lt;br /&gt;
     *&lt;br /&gt;
     * @access    public&lt;br /&gt;
     */&lt;br /&gt;
    function display()&lt;br /&gt;
    {&lt;br /&gt;
        parent::display();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The JController constructor will always register a display() task and unless otherwise specified (using the registerDefaultTask() method), it will set it as the default task.&lt;br /&gt;
&lt;br /&gt;
This barebones display() method isn&#039;t really even necessary since all it does is invoke the parent constructor. However, it is a good visual clue to indicate what is happening in the controller.&lt;br /&gt;
&lt;br /&gt;
The JController::display() method will determine the name of the view and layout from the request and load that view and set the layout. When you create a menu item for your component, the menu manager will allow the administrator to select the view that they would like the menu link to display and to specify the layout. A view usually refers to a view of a certain set of data (i.e. a list of cars, a list of events, a single car, a single event). A layout is a way that that view is organized.&lt;br /&gt;
&lt;br /&gt;
In our component, we will have a single view called hello, and a single layout (default).&lt;br /&gt;
&lt;br /&gt;
=== Creating the View ===&lt;br /&gt;
The task of the view is very simple: It retrieves the data to be displayed and pushes it into the template. Data is pushed into the template using the JView::assignRef method.&lt;br /&gt;
&lt;br /&gt;
The code for the view is:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_1&lt;br /&gt;
 * @license    GNU/GPL&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
// no direct access&lt;br /&gt;
&lt;br /&gt;
defined( &#039;_JEXEC&#039; ) or die( &#039;Restricted access&#039; );&lt;br /&gt;
&lt;br /&gt;
jimport( &#039;joomla.application.component.view&#039;);&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * HTML View class for the HelloWorld Component&lt;br /&gt;
 *&lt;br /&gt;
 * @package    HelloWorld&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
class HelloViewHello extends JView&lt;br /&gt;
{&lt;br /&gt;
    function display($tpl = null)&lt;br /&gt;
    {&lt;br /&gt;
        $greeting = &amp;quot;Hello World!&amp;quot;;&lt;br /&gt;
        $this-&amp;gt;assignRef( &#039;greeting&#039;, $greeting );&lt;br /&gt;
&lt;br /&gt;
        parent::display($tpl);&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Creating the Template ====&lt;br /&gt;
&lt;br /&gt;
Joomla! templates/layouts are regular PHP files that are used to layout the data from the view in a particular manner. The variables assigned by the JView::assignRef method can be accessed from the template using $this-&amp;gt;{propertyname} (see the template code below for an example).&lt;br /&gt;
&lt;br /&gt;
Our template is very simple: we only want to display the greeting that was passed in from the view:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
&lt;br /&gt;
// No direct access&lt;br /&gt;
&lt;br /&gt;
defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;); ?&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;&amp;lt;?php echo $this-&amp;gt;greeting; ?&amp;gt;&amp;lt;/h1&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Wrapping It All Up - Creating the hello.xml File ===&lt;br /&gt;
It is possible to install a component manually by copying the files using an FTP client and modifying the database tables. It is more efficient to create a package file that will allow the Joomla! Installer to do this for you. This package file contains a variety of information:&lt;br /&gt;
&lt;br /&gt;
* basic descriptive details about your component (i.e. name), and optionally, a description, copyright and license information.&lt;br /&gt;
* a list of files that need to be copied.&lt;br /&gt;
* optionally, a PHP file that performs additional install and uninstall operations.&lt;br /&gt;
* optionally, an SQL file which contains database queries that should be executed upon install/uninstall&lt;br /&gt;
&lt;br /&gt;
The format of the XML file is as follows:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;&lt;br /&gt;
&amp;lt;install type=&amp;quot;component&amp;quot; version=&amp;quot;1.5.0&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;name&amp;gt;Hello&amp;lt;/name&amp;gt;&lt;br /&gt;
 &amp;lt;!-- The following elements are optional and free of formatting conttraints --&amp;gt;&lt;br /&gt;
 &amp;lt;creationDate&amp;gt;2007-02-22&amp;lt;/creationDate&amp;gt;&lt;br /&gt;
 &amp;lt;author&amp;gt;John Doe&amp;lt;/author&amp;gt;&lt;br /&gt;
 &amp;lt;authorEmail&amp;gt;john.doe@example.org&amp;lt;/authorEmail&amp;gt;&lt;br /&gt;
 &amp;lt;authorUrl&amp;gt;http://www.example.org&amp;lt;/authorUrl&amp;gt;&lt;br /&gt;
 &amp;lt;copyright&amp;gt;Copyright Info&amp;lt;/copyright&amp;gt;&lt;br /&gt;
 &amp;lt;license&amp;gt;License Info&amp;lt;/license&amp;gt;&lt;br /&gt;
 &amp;lt;!--  The version string is recorded in the components table --&amp;gt;&lt;br /&gt;
 &amp;lt;version&amp;gt;Component Version String&amp;lt;/version&amp;gt;&lt;br /&gt;
 &amp;lt;!-- The description is optional and defaults to the name --&amp;gt;&lt;br /&gt;
 &amp;lt;description&amp;gt;Description of the component ...&amp;lt;/description&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;!-- Site Main File Copy Section --&amp;gt;&lt;br /&gt;
 &amp;lt;!-- Note the folder attribute: This attribute describes the folder&lt;br /&gt;
      to copy FROM in the package to install therefore files copied&lt;br /&gt;
      in this section are copied from /site/ in the package --&amp;gt;&lt;br /&gt;
 &amp;lt;files folder=&amp;quot;site&amp;quot;&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
 &amp;lt;/files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;administration&amp;gt;&lt;br /&gt;
  &amp;lt;!-- Administration Menu Section --&amp;gt;&lt;br /&gt;
  &amp;lt;menu&amp;gt;Hello World!&amp;lt;/menu&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;!-- Administration Main File Copy Section --&amp;gt;&lt;br /&gt;
  &amp;lt;files folder=&amp;quot;admin&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;controller.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/view.html.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/tmpl/default.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/tmpl/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;/files&amp;gt;  &lt;br /&gt;
 &amp;lt;/administration&amp;gt;&lt;br /&gt;
&amp;lt;/install&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you look closely you will notice that there are some files that will be copied that we have not discussed. These are the index.html files. An index.html file is placed in each directory to prevent prying users from getting a directory listing. If there is no index.html file, some web servers will list the directory contents. This is often undesirable. These files have the simple line:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;html&amp;gt;&amp;lt;body bgcolor=&amp;quot;#FFFFFF&amp;quot;&amp;gt;&amp;lt;/body&amp;gt;&amp;lt;/html&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It will simply display a blank page.&lt;br /&gt;
&lt;br /&gt;
The other file is the admin.hello.php file. This is the entry point for the admin section of our component. Since we don&#039;t have an admin section of our component, it will have the same content as the index.html files at this point in time.&lt;br /&gt;
&lt;br /&gt;
== Contributors ==&lt;br /&gt;
* mjaz&lt;br /&gt;
* staalanden&lt;br /&gt;
&lt;br /&gt;
== Download ==&lt;br /&gt;
The component can be downloaded at: [http://joomlacode.org/gf/download/frsrelease/8104/29402/com_hello1_01.zip]&lt;/div&gt;</summary>
		<author><name>Tonie</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Adding_a_Model&amp;diff=9481</id>
		<title>J1.5:Developing a MVC Component/Adding a Model</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Adding_a_Model&amp;diff=9481"/>
		<updated>2008-07-27T16:58:41Z</updated>

		<summary type="html">&lt;p&gt;Tonie: /* Creating the Model */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
In the first tutorial of this series, creating a simple view-controller component using the Joomla! 1.5 CMS framework was demonstrated.&lt;br /&gt;
&lt;br /&gt;
In the first tutorial, the greeting was hardcoded into the view. This doesn&#039;t follow the MVC pattern exactly because the view is intended to only display the data, and not contain it.&lt;br /&gt;
&lt;br /&gt;
In this second part of the tutorial we will demonstrate how to move this out of the view and into a model. In future tutorials we will demonstrate the power and flexibility that this design pattern provides.&lt;br /&gt;
&lt;br /&gt;
== Creating the Model ==&lt;br /&gt;
&lt;br /&gt;
The concept of model gets its name because this class is intended to represent (or &#039;model&#039;) some entity. In our case, our first model will represent a &#039;hello&#039;, or a greeting. This is in line with our design thus far, because we have one view (&#039;hello&#039;), which is a view of our greeting.&lt;br /&gt;
&lt;br /&gt;
The naming convention for models in the Joomla! framework is that the class name starts with the name of the component (in our case &#039;hello&#039;, followed by &#039;model&#039;, followed by the model name. Therefore, our model class is called HelloModelHello.&lt;br /&gt;
&lt;br /&gt;
At this point, we will only model one behaviour of our hello, and that is retrieving the greeting. We will thus have one method, called getGreeting(). It will simply return the string &#039;Hello, World!&#039;.&lt;br /&gt;
&lt;br /&gt;
Here is the code for our model class:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * Hello Model for Hello World Component&lt;br /&gt;
 * &lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_2&lt;br /&gt;
 * @license    GNU/GPL&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// No direct access&lt;br /&gt;
&lt;br /&gt;
defined( &#039;_JEXEC&#039; ) or die( &#039;Restricted access&#039; );&lt;br /&gt;
&lt;br /&gt;
jimport( &#039;joomla.application.component.model&#039; );&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Hello Model&lt;br /&gt;
 *&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 */&lt;br /&gt;
class HelloModelHello extends JModel&lt;br /&gt;
{&lt;br /&gt;
    /**&lt;br /&gt;
    * Gets the greeting&lt;br /&gt;
    * @return string The greeting to be displayed to the user&lt;br /&gt;
    */&lt;br /&gt;
    function getGreeting()&lt;br /&gt;
    {&lt;br /&gt;
        return &#039;Hello, World!&#039;;&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You will notice a line that starts with jimport. The jimport function is used to load files from the Joomla! framework that are required for our component. This particular statement will load the file /libraries/joomla/application/component/model.php. The &#039;.&#039;s are used as directory separators and the last part is the name of the file to load. All files are loaded relative to the libraries directory. This particular file contains the class definition for the JModel class, which is necessary because our model extends this class.&lt;br /&gt;
&lt;br /&gt;
Now that we have created our model, we must modify our view so that it uses it to obtain the greeting.&lt;br /&gt;
&lt;br /&gt;
== Using the Model ==&lt;br /&gt;
&lt;br /&gt;
The Joomla! framework is setup in such a way that the controller will automatically load the model that has the same name as the view and will push it into the view. Since our view is called &#039;Hello&#039;, our &#039;Hello&#039; model will automatically be loaded and pushed into the view. Therefore, we can easily retrieve a reference to our model using the JView::getModel() method.&lt;br /&gt;
&lt;br /&gt;
Our previous view code contained the lines:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$greeting = &amp;quot;Hello World!&amp;quot;;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To take advantage of our model, we change this line to:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$model =&amp;amp; $this-&amp;gt;getModel();&lt;br /&gt;
$greeting = $model-&amp;gt;getGreeting();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The complete view now looks like:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_2&lt;br /&gt;
 * @license    GNU/GPL&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
// No direct access&lt;br /&gt;
&lt;br /&gt;
defined( &#039;_JEXEC&#039; ) or die( &#039;Restricted access&#039; );&lt;br /&gt;
&lt;br /&gt;
jimport( &#039;joomla.application.component.view&#039;);&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * HTML View class for the HelloWorld Component&lt;br /&gt;
 *&lt;br /&gt;
 * @package    HelloWorld&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
class HelloViewHello extends JView&lt;br /&gt;
{&lt;br /&gt;
    function display($tpl = null)&lt;br /&gt;
    {&lt;br /&gt;
        $model =&amp;amp; $this-&amp;gt;getModel();&lt;br /&gt;
        $greeting = $model-&amp;gt;getGreeting();&lt;br /&gt;
        $this-&amp;gt;assignRef( &#039;greeting&#039;,	$greeting );&lt;br /&gt;
&lt;br /&gt;
        parent::display($tpl);&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Adding the File to the Package ===&lt;br /&gt;
&lt;br /&gt;
All that remains is to add an entry to the XML file so that our new model will be copied. The Joomla! framework will look for our model in the models directory, so the entry for this file will look like (it should be added to the site section):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;filename&amp;gt;models/hello.php&amp;lt;/filename&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Our new hello.xml file will look like:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;&lt;br /&gt;
&amp;lt;install type=&amp;quot;component&amp;quot; version=&amp;quot;1.5.0&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;name&amp;gt;Hello&amp;lt;/name&amp;gt;&lt;br /&gt;
 &amp;lt;!-- The following elements are optional and free of formatting conttraints --&amp;gt;&lt;br /&gt;
 &amp;lt;creationDate&amp;gt;2007-02-22&amp;lt;/creationDate&amp;gt;&lt;br /&gt;
 &amp;lt;author&amp;gt;John Doe&amp;lt;/author&amp;gt;&lt;br /&gt;
 &amp;lt;authorEmail&amp;gt;john.doe@example.org&amp;lt;/authorEmail&amp;gt;&lt;br /&gt;
 &amp;lt;authorUrl&amp;gt;http://www.example.org&amp;lt;/authorUrl&amp;gt;&lt;br /&gt;
 &amp;lt;copyright&amp;gt;Copyright Info&amp;lt;/copyright&amp;gt;&lt;br /&gt;
 &amp;lt;license&amp;gt;License Info&amp;lt;/license&amp;gt;&lt;br /&gt;
 &amp;lt;!--  The version string is recorded in the components table --&amp;gt;&lt;br /&gt;
 &amp;lt;version&amp;gt;Component Version String&amp;lt;/version&amp;gt;&lt;br /&gt;
 &amp;lt;!-- The description is optional and defaults to the name --&amp;gt;&lt;br /&gt;
 &amp;lt;description&amp;gt;Description of the component ...&amp;lt;/description&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;!-- Site Main File Copy Section --&amp;gt;&lt;br /&gt;
 &amp;lt;!-- Note the folder attribute: This attribute describes the folder&lt;br /&gt;
      to copy FROM in the package to install therefore files copied&lt;br /&gt;
      in this section are copied from /site/ in the package --&amp;gt;&lt;br /&gt;
 &amp;lt;files folder=&amp;quot;site&amp;quot;&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
 &amp;lt;/files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;administration&amp;gt;&lt;br /&gt;
  &amp;lt;!-- Administration Menu Section --&amp;gt;&lt;br /&gt;
  &amp;lt;menu&amp;gt;Hello World!&amp;lt;/menu&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;!-- Administration Main File Copy Section --&amp;gt;&lt;br /&gt;
  &amp;lt;files folder=&amp;quot;admin&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;controller.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;models/hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;models/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/view.html.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/tmpl/default.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/tmpl/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;/files&amp;gt;  &lt;br /&gt;
 &amp;lt;/administration&amp;gt;&lt;br /&gt;
&amp;lt;/install&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
We now have a simple MVC component. Each element is very simple at this point, but provides a great deal of flexibility and power.&lt;br /&gt;
&lt;br /&gt;
== Contributors ==&lt;br /&gt;
* staalanden&lt;br /&gt;
&lt;br /&gt;
== Download ==&lt;br /&gt;
&lt;br /&gt;
The component can be downloaded at: [http://joomlacode.org/gf/download/frsrelease/8104/29402/com_hello2_01.zip]&lt;/div&gt;</summary>
		<author><name>Tonie</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Adding_a_Model&amp;diff=9479</id>
		<title>J1.5:Developing a MVC Component/Adding a Model</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Adding_a_Model&amp;diff=9479"/>
		<updated>2008-07-27T16:56:23Z</updated>

		<summary type="html">&lt;p&gt;Tonie: /* Download */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
In the first tutorial of this series, creating a simple view-controller component using the Joomla! 1.5 CMS framework was demonstrated.&lt;br /&gt;
&lt;br /&gt;
In the first tutorial, the greeting was hardcoded into the view. This doesn&#039;t follow the MVC pattern exactly because the view is intended to only display the data, and not contain it.&lt;br /&gt;
&lt;br /&gt;
In this second part of the tutorial we will demonstrate how to move this out of the view and into a model. In future tutorials we will demonstrate the power and flexibility that this design pattern provides.&lt;br /&gt;
&lt;br /&gt;
== Creating the Model ==&lt;br /&gt;
&lt;br /&gt;
The concept of model gets its name because this class is intended to represent (or &#039;model&#039;) some entity. In our case, our first model will represent a &#039;hello&#039;, or a greeting. This is in line with our design thus far, because we have one view (&#039;hello&#039;), which is a view of our greeting.&lt;br /&gt;
&lt;br /&gt;
The naming convention for models in the Joomla! framework is that the class name starts with the name of the component (in our case &#039;hello&#039;, followed by &#039;model&#039;, followed by the model name. Therefore, our model class is called HelloModelHello.&lt;br /&gt;
&lt;br /&gt;
At this point, we will only model one behaviour of our hello, and that is retrieving the greeting. We will thus have one method, called getGreeting(). It will simply return the string &#039;Hello, World!&#039;.&lt;br /&gt;
&lt;br /&gt;
Here is the code for our model class:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * Hello Model for Hello World Component&lt;br /&gt;
 * &lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://dev.joomla.org/component/option,com_jd-wiki/Itemid,31/id,tutorials:modules/&lt;br /&gt;
 * @license    GNU/GPL&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// No direct access&lt;br /&gt;
&lt;br /&gt;
defined( &#039;_JEXEC&#039; ) or die( &#039;Restricted access&#039; );&lt;br /&gt;
&lt;br /&gt;
jimport( &#039;joomla.application.component.model&#039; );&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Hello Model&lt;br /&gt;
 *&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 */&lt;br /&gt;
class HelloModelHello extends JModel&lt;br /&gt;
{&lt;br /&gt;
    /**&lt;br /&gt;
    * Gets the greeting&lt;br /&gt;
    * @return string The greeting to be displayed to the user&lt;br /&gt;
    */&lt;br /&gt;
    function getGreeting()&lt;br /&gt;
    {&lt;br /&gt;
        return &#039;Hello, World!&#039;;&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You will notice a line that starts with jimport. The jimport function is used to load files from the Joomla! framework that are required for our component. This particular statement will load the file /libraries/joomla/application/component/model.php. The &#039;.&#039;s are used as directory separators and the last part is the name of the file to load. All files are loaded relative to the libraries directory. This particular file contains the class definition for the JModel class, which is necessary because our model extends this class.&lt;br /&gt;
&lt;br /&gt;
Now that we have created our model, we must modify our view so that it uses it to obtain the greeting.&lt;br /&gt;
&lt;br /&gt;
== Using the Model ==&lt;br /&gt;
&lt;br /&gt;
The Joomla! framework is setup in such a way that the controller will automatically load the model that has the same name as the view and will push it into the view. Since our view is called &#039;Hello&#039;, our &#039;Hello&#039; model will automatically be loaded and pushed into the view. Therefore, we can easily retrieve a reference to our model using the JView::getModel() method.&lt;br /&gt;
&lt;br /&gt;
Our previous view code contained the lines:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$greeting = &amp;quot;Hello World!&amp;quot;;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To take advantage of our model, we change this line to:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$model =&amp;amp; $this-&amp;gt;getModel();&lt;br /&gt;
$greeting = $model-&amp;gt;getGreeting();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The complete view now looks like:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_2&lt;br /&gt;
 * @license    GNU/GPL&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
// No direct access&lt;br /&gt;
&lt;br /&gt;
defined( &#039;_JEXEC&#039; ) or die( &#039;Restricted access&#039; );&lt;br /&gt;
&lt;br /&gt;
jimport( &#039;joomla.application.component.view&#039;);&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * HTML View class for the HelloWorld Component&lt;br /&gt;
 *&lt;br /&gt;
 * @package    HelloWorld&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
class HelloViewHello extends JView&lt;br /&gt;
{&lt;br /&gt;
    function display($tpl = null)&lt;br /&gt;
    {&lt;br /&gt;
        $model =&amp;amp; $this-&amp;gt;getModel();&lt;br /&gt;
        $greeting = $model-&amp;gt;getGreeting();&lt;br /&gt;
        $this-&amp;gt;assignRef( &#039;greeting&#039;,	$greeting );&lt;br /&gt;
&lt;br /&gt;
        parent::display($tpl);&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Adding the File to the Package ===&lt;br /&gt;
&lt;br /&gt;
All that remains is to add an entry to the XML file so that our new model will be copied. The Joomla! framework will look for our model in the models directory, so the entry for this file will look like (it should be added to the site section):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;filename&amp;gt;models/hello.php&amp;lt;/filename&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Our new hello.xml file will look like:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;&lt;br /&gt;
&amp;lt;install type=&amp;quot;component&amp;quot; version=&amp;quot;1.5.0&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;name&amp;gt;Hello&amp;lt;/name&amp;gt;&lt;br /&gt;
 &amp;lt;!-- The following elements are optional and free of formatting conttraints --&amp;gt;&lt;br /&gt;
 &amp;lt;creationDate&amp;gt;2007-02-22&amp;lt;/creationDate&amp;gt;&lt;br /&gt;
 &amp;lt;author&amp;gt;John Doe&amp;lt;/author&amp;gt;&lt;br /&gt;
 &amp;lt;authorEmail&amp;gt;john.doe@example.org&amp;lt;/authorEmail&amp;gt;&lt;br /&gt;
 &amp;lt;authorUrl&amp;gt;http://www.example.org&amp;lt;/authorUrl&amp;gt;&lt;br /&gt;
 &amp;lt;copyright&amp;gt;Copyright Info&amp;lt;/copyright&amp;gt;&lt;br /&gt;
 &amp;lt;license&amp;gt;License Info&amp;lt;/license&amp;gt;&lt;br /&gt;
 &amp;lt;!--  The version string is recorded in the components table --&amp;gt;&lt;br /&gt;
 &amp;lt;version&amp;gt;Component Version String&amp;lt;/version&amp;gt;&lt;br /&gt;
 &amp;lt;!-- The description is optional and defaults to the name --&amp;gt;&lt;br /&gt;
 &amp;lt;description&amp;gt;Description of the component ...&amp;lt;/description&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;!-- Site Main File Copy Section --&amp;gt;&lt;br /&gt;
 &amp;lt;!-- Note the folder attribute: This attribute describes the folder&lt;br /&gt;
      to copy FROM in the package to install therefore files copied&lt;br /&gt;
      in this section are copied from /site/ in the package --&amp;gt;&lt;br /&gt;
 &amp;lt;files folder=&amp;quot;site&amp;quot;&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
 &amp;lt;/files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;administration&amp;gt;&lt;br /&gt;
  &amp;lt;!-- Administration Menu Section --&amp;gt;&lt;br /&gt;
  &amp;lt;menu&amp;gt;Hello World!&amp;lt;/menu&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;!-- Administration Main File Copy Section --&amp;gt;&lt;br /&gt;
  &amp;lt;files folder=&amp;quot;admin&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;controller.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;models/hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;models/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/view.html.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/tmpl/default.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/tmpl/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;/files&amp;gt;  &lt;br /&gt;
 &amp;lt;/administration&amp;gt;&lt;br /&gt;
&amp;lt;/install&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
We now have a simple MVC component. Each element is very simple at this point, but provides a great deal of flexibility and power.&lt;br /&gt;
&lt;br /&gt;
== Contributors ==&lt;br /&gt;
* staalanden&lt;br /&gt;
&lt;br /&gt;
== Download ==&lt;br /&gt;
&lt;br /&gt;
The component can be downloaded at: [http://joomlacode.org/gf/download/frsrelease/8104/29402/com_hello2_01.zip]&lt;/div&gt;</summary>
		<author><name>Tonie</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Introduction&amp;diff=9478</id>
		<title>J1.5:Developing a MVC Component/Introduction</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Introduction&amp;diff=9478"/>
		<updated>2008-07-27T16:56:20Z</updated>

		<summary type="html">&lt;p&gt;Tonie: /* Download */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
A software framework is the base of an application that can be used by a developer. The framework in Joomla! 1.5 unleashes a great deal of power for them. The Joomla! code has been completely overhauled and cleaned up. This tutorial will guide you through the process of developing a component using the framework.&lt;br /&gt;
&lt;br /&gt;
The scope of this project will be to develop a simple Hello World! component. In future tutorials, this simple framework will be built upon to show the full power and versatility of the MVC design pattern in Joomla!&lt;br /&gt;
&lt;br /&gt;
== Requirements ==&lt;br /&gt;
You need Joomla! 1.5 or greater for this tutorial.&lt;br /&gt;
&lt;br /&gt;
== Introduction to Model-View-Controller ==&lt;br /&gt;
While the idea behind a component may seem extremely simple, code can quickly become very complex as additional features are added or the interface is customized.&lt;br /&gt;
&lt;br /&gt;
Model-View-Controller (herein referred to as MVC) is a software design pattern that can be used to organize code in such a way that the business logic and data presentation are separate. The premise behind this approach is that if the business logic is grouped into one section, then the interface and user interaction that surrounds the data can revised and customized without having to reprogram the business logic.&lt;br /&gt;
&lt;br /&gt;
There are three main parts of an MVC component. They are described here in brief, but for a more thorough explanation, please refer to the links provided at the end of this tutorial.&lt;br /&gt;
&lt;br /&gt;
=== Model ===&lt;br /&gt;
The model is the part of the component that encapsulates the application&#039;s data. It will often provide routines to manage and manipulate this data in a meaningful way in addition to routines that retrieve the data from the model. In our case, the model will contain methods to add, remove and update information about the greetings in the database. It will also contain methods to retrieve the list of greetings from the database. In general, the underlying data access technique should be encapsulated in the model. In this way, if an application is to be moved from a system that utilizes a flat file to store its information to a system that uses a database, the model is the only element that needs to be changed, not the view or the controller.&lt;br /&gt;
&lt;br /&gt;
=== View ===&lt;br /&gt;
The view is the part of the component that is used to render the data from the model in a manner that is suitable for interaction. For a web-based application, the view would generally be an HTML page that is returned to the data. The view pulls data from the model (which is passed to it from the controller) and feeds the data into a template which is populated and presented to the user. The view does not cause the data to be modified in any way, it only displays data retrieved from the model.&lt;br /&gt;
&lt;br /&gt;
=== Controller ===&lt;br /&gt;
The controller is responsible for responding to user actions. In the case of a web application, a user action is (generally) a page request. The controller will determine what request is being made by the user and respond appropriately by triggering the model to manipulate the data appropriately and passing the model into the view. The controller does not display the data in the model, it only triggers methods in the model which modify the data, and then pass the model into the view which displays the data.&lt;br /&gt;
&lt;br /&gt;
== Joomla! MVC Implementation ==&lt;br /&gt;
In Joomla!, the MVC pattern is implemented using three classes: [http://api.joomla.org/Joomla-Framework/Application/JModel.html JModel], [http://api.joomla.org/Joomla-Framework/Application/JView.html JView] and [http://api.joomla.org/Joomla-Framework/Application/JController.html JController]. For more detailed information about these classes, please refer to the API reference documentation (WIP).&lt;br /&gt;
&lt;br /&gt;
== Creating a Component ==&lt;br /&gt;
For our basic component, we only require five files:&lt;br /&gt;
&lt;br /&gt;
* hello.php - this is the entry point to our component&lt;br /&gt;
* controller.php - this file contains our base controller&lt;br /&gt;
* views/hello/view.html.php - this file retrieves the necessary data and pushes it into the template&lt;br /&gt;
* views/hello/tmpl/default.php - this is the template for our output&lt;br /&gt;
* hello.xml - this is an XML file that tells Joomla! how to install our component.&lt;br /&gt;
&lt;br /&gt;
=== Creating the Entry Point ===&lt;br /&gt;
Joomla! is always accessed through a single point of entry: index.php for the Site Application or administrator/index.php for the Administrator Application. The application will then load the required component, based on the value of &#039;option&#039; in the URL or in the POST data. For our component, the URL would be:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;index.php?option=com_hello&amp;amp;view=hello&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will load our main file, which can be seen as the single point of entry for our component: components/com_hello/hello.php.&lt;br /&gt;
&lt;br /&gt;
The code for this file is fairly typical across components.&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * components/com_hello/hello.php&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_1#Creating_the_Entry_Point&lt;br /&gt;
 * @license    GNU/GPL&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
// No direct access&lt;br /&gt;
&lt;br /&gt;
defined( &#039;_JEXEC&#039; ) or die( &#039;Restricted access&#039; );&lt;br /&gt;
&lt;br /&gt;
// Require the base controller&lt;br /&gt;
&lt;br /&gt;
require_once( JPATH_COMPONENT.DS.&#039;controller.php&#039; );&lt;br /&gt;
&lt;br /&gt;
// Require specific controller if requested&lt;br /&gt;
if($controller = JRequest::getWord(&#039;controller&#039;)) {&lt;br /&gt;
    $path = JPATH_COMPONENT.DS.&#039;controllers&#039;.DS.$controller.&#039;.php&#039;;&lt;br /&gt;
    if (file_exists($path)) {&lt;br /&gt;
        require_once $path;&lt;br /&gt;
    } else {&lt;br /&gt;
        $controller = &#039;&#039;;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Create the controller&lt;br /&gt;
$classname    = &#039;HelloController&#039;.$controller;&lt;br /&gt;
$controller   = new $classname( );&lt;br /&gt;
&lt;br /&gt;
// Perform the Request task&lt;br /&gt;
$controller-&amp;gt;execute( JRequest::getVar( &#039;task&#039; ) );&lt;br /&gt;
&lt;br /&gt;
// Redirect if set by the controller&lt;br /&gt;
$controller-&amp;gt;redirect();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first statement is a security check.&lt;br /&gt;
&lt;br /&gt;
JPATH_COMPONENT is the absolute path to the current component, in our case components/com_hello. If you specifically need either the Site component or the Administrator component, you can use JPATH_COMPONENT_SITE or JPATH_COMPONENT_ADMINISTRATOR.&lt;br /&gt;
&lt;br /&gt;
DS is the directory separator of your system: either &#039;/&#039; or &#039;\&#039;. This is automatically set by the framework so the developer doesn&#039;t have to worry about developing different versions for different server OSs. DS should always be used when referring to files on the local server.&lt;br /&gt;
&lt;br /&gt;
After loading the base controller, we check if a specific controller is needed. In this component, the base controller is the only controller, but we will leave this here for future use.&lt;br /&gt;
&lt;br /&gt;
JRequest:getVar() finds a variable in the URL or the POST data. So if our URL is index.php?option=com_hello&amp;gt;controller=controller_name, then we can retrieve our controller name in our component using: echo JRequest::getVar(&#039;controller&#039;);&lt;br /&gt;
&lt;br /&gt;
Now we have our base controller &#039;HelloController&#039; in com_hello/controller.php, and, if needed, additional controllers like &#039;HelloControllerController1&#039; in com_hello/controllers/controller1.php. Using this standard naming scheme will make things easy later on: &#039;{Componentname}{Controller}{Controllername}&#039;&lt;br /&gt;
&lt;br /&gt;
After the controller is created, we instruct the controller to execute the task, as defined in the URL: index.php?option=com_hello&amp;amp;task=sometask. If no task is set, the default task &#039;display&#039; will be assumed. When display is used, the &#039;view&#039; variable will decide what will be displayed. Other common tasks are save, edit, new...&lt;br /&gt;
&lt;br /&gt;
The controller might decide to redirect the page, usually after a task like &#039;save&#039; has been completed. This last statement takes care of the actual redirection.&lt;br /&gt;
&lt;br /&gt;
The main entry point (hello.php) essentially passes control to the controller, which handles performing the task that was specified in the request.&lt;br /&gt;
&lt;br /&gt;
Note that we don&#039;t use a closing php tag in this file: ?&amp;gt;. The reason for this is that we will not have any unwanted whitespace in the output code. This is default practice since Joomla! 1.5, and will be used for all php-only files.&lt;br /&gt;
&lt;br /&gt;
=== Creating the Controller ===&lt;br /&gt;
Our component only has one task - greet the world. Therefore, the controller will be very simple. No data manipulation is required. All that needs to be done is the appropriate view loaded. We will have only one method in our controller: display(). Most of the required functionality is built into the JController class, so all that we need to do is invoke the JController::display() method.&lt;br /&gt;
&lt;br /&gt;
The code for the base controller is:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_1#Creating_the_Entry_Point&lt;br /&gt;
 * @license    GNU/GPL&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// No direct access&lt;br /&gt;
&lt;br /&gt;
defined( &#039;_JEXEC&#039; ) or die( &#039;Restricted access&#039; );&lt;br /&gt;
&lt;br /&gt;
jimport(&#039;joomla.application.component.controller&#039;);&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Hello World Component Controller&lt;br /&gt;
 *&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 */&lt;br /&gt;
class HelloController extends JController&lt;br /&gt;
{&lt;br /&gt;
    /**&lt;br /&gt;
     * Method to display the view&lt;br /&gt;
     *&lt;br /&gt;
     * @access    public&lt;br /&gt;
     */&lt;br /&gt;
    function display()&lt;br /&gt;
    {&lt;br /&gt;
        parent::display();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The JController constructor will always register a display() task and unless otherwise specified (using the registerDefaultTask() method), it will set it as the default task.&lt;br /&gt;
&lt;br /&gt;
This barebones display() method isn&#039;t really even necessary since all it does is invoke the parent constructor. However, it is a good visual clue to indicate what is happening in the controller.&lt;br /&gt;
&lt;br /&gt;
The JController::display() method will determine the name of the view and layout from the request and load that view and set the layout. When you create a menu item for your component, the menu manager will allow the administrator to select the view that they would like the menu link to display and to specify the layout. A view usually refers to a view of a certain set of data (i.e. a list of cars, a list of events, a single car, a single event). A layout is a way that that view is organized.&lt;br /&gt;
&lt;br /&gt;
In our component, we will have a single view called hello, and a single layout (default).&lt;br /&gt;
&lt;br /&gt;
=== Creating the View ===&lt;br /&gt;
The task of the view is very simple: It retrieves the data to be displayed and pushes it into the template. Data is pushed into the template using the JView::assignRef method.&lt;br /&gt;
&lt;br /&gt;
The code for the view is:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_1#Creating_the_Entry_Point&lt;br /&gt;
 * @license    GNU/GPL&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
// no direct access&lt;br /&gt;
&lt;br /&gt;
defined( &#039;_JEXEC&#039; ) or die( &#039;Restricted access&#039; );&lt;br /&gt;
&lt;br /&gt;
jimport( &#039;joomla.application.component.view&#039;);&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * HTML View class for the HelloWorld Component&lt;br /&gt;
 *&lt;br /&gt;
 * @package    HelloWorld&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
class HelloViewHello extends JView&lt;br /&gt;
{&lt;br /&gt;
    function display($tpl = null)&lt;br /&gt;
    {&lt;br /&gt;
        $greeting = &amp;quot;Hello World!&amp;quot;;&lt;br /&gt;
        $this-&amp;gt;assignRef( &#039;greeting&#039;, $greeting );&lt;br /&gt;
&lt;br /&gt;
        parent::display($tpl);&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Creating the Template ====&lt;br /&gt;
&lt;br /&gt;
Joomla! templates/layouts are regular PHP files that are used to layout the data from the view in a particular manner. The variables assigned by the JView::assignRef method can be accessed from the template using $this-&amp;gt;{propertyname} (see the template code below for an example).&lt;br /&gt;
&lt;br /&gt;
Our template is very simple: we only want to display the greeting that was passed in from the view:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
&lt;br /&gt;
// No direct access&lt;br /&gt;
&lt;br /&gt;
defined(&#039;_JEXEC&#039;) or die(&#039;Restricted access&#039;); ?&amp;gt;&lt;br /&gt;
&amp;lt;h1&amp;gt;&amp;lt;?php echo $this-&amp;gt;greeting; ?&amp;gt;&amp;lt;/h1&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Wrapping It All Up - Creating the hello.xml File ===&lt;br /&gt;
It is possible to install a component manually by copying the files using an FTP client and modifying the database tables. It is more efficient to create a package file that will allow the Joomla! Installer to do this for you. This package file contains a variety of information:&lt;br /&gt;
&lt;br /&gt;
* basic descriptive details about your component (i.e. name), and optionally, a description, copyright and license information.&lt;br /&gt;
* a list of files that need to be copied.&lt;br /&gt;
* optionally, a PHP file that performs additional install and uninstall operations.&lt;br /&gt;
* optionally, an SQL file which contains database queries that should be executed upon install/uninstall&lt;br /&gt;
&lt;br /&gt;
The format of the XML file is as follows:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;&lt;br /&gt;
&amp;lt;install type=&amp;quot;component&amp;quot; version=&amp;quot;1.5.0&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;name&amp;gt;Hello&amp;lt;/name&amp;gt;&lt;br /&gt;
 &amp;lt;!-- The following elements are optional and free of formatting conttraints --&amp;gt;&lt;br /&gt;
 &amp;lt;creationDate&amp;gt;2007-02-22&amp;lt;/creationDate&amp;gt;&lt;br /&gt;
 &amp;lt;author&amp;gt;John Doe&amp;lt;/author&amp;gt;&lt;br /&gt;
 &amp;lt;authorEmail&amp;gt;john.doe@example.org&amp;lt;/authorEmail&amp;gt;&lt;br /&gt;
 &amp;lt;authorUrl&amp;gt;http://www.example.org&amp;lt;/authorUrl&amp;gt;&lt;br /&gt;
 &amp;lt;copyright&amp;gt;Copyright Info&amp;lt;/copyright&amp;gt;&lt;br /&gt;
 &amp;lt;license&amp;gt;License Info&amp;lt;/license&amp;gt;&lt;br /&gt;
 &amp;lt;!--  The version string is recorded in the components table --&amp;gt;&lt;br /&gt;
 &amp;lt;version&amp;gt;Component Version String&amp;lt;/version&amp;gt;&lt;br /&gt;
 &amp;lt;!-- The description is optional and defaults to the name --&amp;gt;&lt;br /&gt;
 &amp;lt;description&amp;gt;Description of the component ...&amp;lt;/description&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;!-- Site Main File Copy Section --&amp;gt;&lt;br /&gt;
 &amp;lt;!-- Note the folder attribute: This attribute describes the folder&lt;br /&gt;
      to copy FROM in the package to install therefore files copied&lt;br /&gt;
      in this section are copied from /site/ in the package --&amp;gt;&lt;br /&gt;
 &amp;lt;files folder=&amp;quot;site&amp;quot;&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
 &amp;lt;/files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;administration&amp;gt;&lt;br /&gt;
  &amp;lt;!-- Administration Menu Section --&amp;gt;&lt;br /&gt;
  &amp;lt;menu&amp;gt;Hello World!&amp;lt;/menu&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;!-- Administration Main File Copy Section --&amp;gt;&lt;br /&gt;
  &amp;lt;files folder=&amp;quot;admin&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;controller.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/view.html.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/tmpl/default.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/tmpl/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;/files&amp;gt;  &lt;br /&gt;
 &amp;lt;/administration&amp;gt;&lt;br /&gt;
&amp;lt;/install&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you look closely you will notice that there are some files that will be copied that we have not discussed. These are the index.html files. An index.html file is placed in each directory to prevent prying users from getting a directory listing. If there is no index.html file, some web servers will list the directory contents. This is often undesirable. These files have the simple line:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;html&amp;gt;&amp;lt;body bgcolor=&amp;quot;#FFFFFF&amp;quot;&amp;gt;&amp;lt;/body&amp;gt;&amp;lt;/html&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It will simply display a blank page.&lt;br /&gt;
&lt;br /&gt;
The other file is the admin.hello.php file. This is the entry point for the admin section of our component. Since we don&#039;t have an admin section of our component, it will have the same content as the index.html files at this point in time.&lt;br /&gt;
&lt;br /&gt;
== Contributors ==&lt;br /&gt;
* mjaz&lt;br /&gt;
* staalanden&lt;br /&gt;
&lt;br /&gt;
== Download ==&lt;br /&gt;
The component can be downloaded at: [http://joomlacode.org/gf/download/frsrelease/8104/29402/com_hello1_01.zip]&lt;/div&gt;</summary>
		<author><name>Tonie</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Adding_a_Model&amp;diff=9477</id>
		<title>J1.5:Developing a MVC Component/Adding a Model</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Adding_a_Model&amp;diff=9477"/>
		<updated>2008-07-27T16:54:44Z</updated>

		<summary type="html">&lt;p&gt;Tonie: /* Using the Model */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
In the first tutorial of this series, creating a simple view-controller component using the Joomla! 1.5 CMS framework was demonstrated.&lt;br /&gt;
&lt;br /&gt;
In the first tutorial, the greeting was hardcoded into the view. This doesn&#039;t follow the MVC pattern exactly because the view is intended to only display the data, and not contain it.&lt;br /&gt;
&lt;br /&gt;
In this second part of the tutorial we will demonstrate how to move this out of the view and into a model. In future tutorials we will demonstrate the power and flexibility that this design pattern provides.&lt;br /&gt;
&lt;br /&gt;
== Creating the Model ==&lt;br /&gt;
&lt;br /&gt;
The concept of model gets its name because this class is intended to represent (or &#039;model&#039;) some entity. In our case, our first model will represent a &#039;hello&#039;, or a greeting. This is in line with our design thus far, because we have one view (&#039;hello&#039;), which is a view of our greeting.&lt;br /&gt;
&lt;br /&gt;
The naming convention for models in the Joomla! framework is that the class name starts with the name of the component (in our case &#039;hello&#039;, followed by &#039;model&#039;, followed by the model name. Therefore, our model class is called HelloModelHello.&lt;br /&gt;
&lt;br /&gt;
At this point, we will only model one behaviour of our hello, and that is retrieving the greeting. We will thus have one method, called getGreeting(). It will simply return the string &#039;Hello, World!&#039;.&lt;br /&gt;
&lt;br /&gt;
Here is the code for our model class:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * Hello Model for Hello World Component&lt;br /&gt;
 * &lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://dev.joomla.org/component/option,com_jd-wiki/Itemid,31/id,tutorials:modules/&lt;br /&gt;
 * @license    GNU/GPL&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// No direct access&lt;br /&gt;
&lt;br /&gt;
defined( &#039;_JEXEC&#039; ) or die( &#039;Restricted access&#039; );&lt;br /&gt;
&lt;br /&gt;
jimport( &#039;joomla.application.component.model&#039; );&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Hello Model&lt;br /&gt;
 *&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 */&lt;br /&gt;
class HelloModelHello extends JModel&lt;br /&gt;
{&lt;br /&gt;
    /**&lt;br /&gt;
    * Gets the greeting&lt;br /&gt;
    * @return string The greeting to be displayed to the user&lt;br /&gt;
    */&lt;br /&gt;
    function getGreeting()&lt;br /&gt;
    {&lt;br /&gt;
        return &#039;Hello, World!&#039;;&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You will notice a line that starts with jimport. The jimport function is used to load files from the Joomla! framework that are required for our component. This particular statement will load the file /libraries/joomla/application/component/model.php. The &#039;.&#039;s are used as directory separators and the last part is the name of the file to load. All files are loaded relative to the libraries directory. This particular file contains the class definition for the JModel class, which is necessary because our model extends this class.&lt;br /&gt;
&lt;br /&gt;
Now that we have created our model, we must modify our view so that it uses it to obtain the greeting.&lt;br /&gt;
&lt;br /&gt;
== Using the Model ==&lt;br /&gt;
&lt;br /&gt;
The Joomla! framework is setup in such a way that the controller will automatically load the model that has the same name as the view and will push it into the view. Since our view is called &#039;Hello&#039;, our &#039;Hello&#039; model will automatically be loaded and pushed into the view. Therefore, we can easily retrieve a reference to our model using the JView::getModel() method.&lt;br /&gt;
&lt;br /&gt;
Our previous view code contained the lines:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$greeting = &amp;quot;Hello World!&amp;quot;;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To take advantage of our model, we change this line to:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$model =&amp;amp; $this-&amp;gt;getModel();&lt;br /&gt;
$greeting = $model-&amp;gt;getGreeting();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The complete view now looks like:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_2&lt;br /&gt;
 * @license    GNU/GPL&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
// No direct access&lt;br /&gt;
&lt;br /&gt;
defined( &#039;_JEXEC&#039; ) or die( &#039;Restricted access&#039; );&lt;br /&gt;
&lt;br /&gt;
jimport( &#039;joomla.application.component.view&#039;);&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * HTML View class for the HelloWorld Component&lt;br /&gt;
 *&lt;br /&gt;
 * @package    HelloWorld&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
class HelloViewHello extends JView&lt;br /&gt;
{&lt;br /&gt;
    function display($tpl = null)&lt;br /&gt;
    {&lt;br /&gt;
        $model =&amp;amp; $this-&amp;gt;getModel();&lt;br /&gt;
        $greeting = $model-&amp;gt;getGreeting();&lt;br /&gt;
        $this-&amp;gt;assignRef( &#039;greeting&#039;,	$greeting );&lt;br /&gt;
&lt;br /&gt;
        parent::display($tpl);&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Adding the File to the Package ===&lt;br /&gt;
&lt;br /&gt;
All that remains is to add an entry to the XML file so that our new model will be copied. The Joomla! framework will look for our model in the models directory, so the entry for this file will look like (it should be added to the site section):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;filename&amp;gt;models/hello.php&amp;lt;/filename&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Our new hello.xml file will look like:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;&lt;br /&gt;
&amp;lt;install type=&amp;quot;component&amp;quot; version=&amp;quot;1.5.0&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;name&amp;gt;Hello&amp;lt;/name&amp;gt;&lt;br /&gt;
 &amp;lt;!-- The following elements are optional and free of formatting conttraints --&amp;gt;&lt;br /&gt;
 &amp;lt;creationDate&amp;gt;2007-02-22&amp;lt;/creationDate&amp;gt;&lt;br /&gt;
 &amp;lt;author&amp;gt;John Doe&amp;lt;/author&amp;gt;&lt;br /&gt;
 &amp;lt;authorEmail&amp;gt;john.doe@example.org&amp;lt;/authorEmail&amp;gt;&lt;br /&gt;
 &amp;lt;authorUrl&amp;gt;http://www.example.org&amp;lt;/authorUrl&amp;gt;&lt;br /&gt;
 &amp;lt;copyright&amp;gt;Copyright Info&amp;lt;/copyright&amp;gt;&lt;br /&gt;
 &amp;lt;license&amp;gt;License Info&amp;lt;/license&amp;gt;&lt;br /&gt;
 &amp;lt;!--  The version string is recorded in the components table --&amp;gt;&lt;br /&gt;
 &amp;lt;version&amp;gt;Component Version String&amp;lt;/version&amp;gt;&lt;br /&gt;
 &amp;lt;!-- The description is optional and defaults to the name --&amp;gt;&lt;br /&gt;
 &amp;lt;description&amp;gt;Description of the component ...&amp;lt;/description&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;!-- Site Main File Copy Section --&amp;gt;&lt;br /&gt;
 &amp;lt;!-- Note the folder attribute: This attribute describes the folder&lt;br /&gt;
      to copy FROM in the package to install therefore files copied&lt;br /&gt;
      in this section are copied from /site/ in the package --&amp;gt;&lt;br /&gt;
 &amp;lt;files folder=&amp;quot;site&amp;quot;&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
 &amp;lt;/files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;administration&amp;gt;&lt;br /&gt;
  &amp;lt;!-- Administration Menu Section --&amp;gt;&lt;br /&gt;
  &amp;lt;menu&amp;gt;Hello World!&amp;lt;/menu&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;!-- Administration Main File Copy Section --&amp;gt;&lt;br /&gt;
  &amp;lt;files folder=&amp;quot;admin&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;controller.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;models/hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;models/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/view.html.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/tmpl/default.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/tmpl/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;/files&amp;gt;  &lt;br /&gt;
 &amp;lt;/administration&amp;gt;&lt;br /&gt;
&amp;lt;/install&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
We now have a simple MVC component. Each element is very simple at this point, but provides a great deal of flexibility and power.&lt;br /&gt;
&lt;br /&gt;
== Contributors ==&lt;br /&gt;
* staalanden&lt;br /&gt;
&lt;br /&gt;
== Download ==&lt;br /&gt;
&lt;br /&gt;
The component can be downloaded at: [[http://dev.joomla.org/components/com_jd-wiki/data/media/components/com_hello2.zip|com_hello2.zip]]&lt;/div&gt;</summary>
		<author><name>Tonie</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Adding_a_Model&amp;diff=9476</id>
		<title>J1.5:Developing a MVC Component/Adding a Model</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Adding_a_Model&amp;diff=9476"/>
		<updated>2008-07-27T16:51:56Z</updated>

		<summary type="html">&lt;p&gt;Tonie: /* Creating the Model */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
In the first tutorial of this series, creating a simple view-controller component using the Joomla! 1.5 CMS framework was demonstrated.&lt;br /&gt;
&lt;br /&gt;
In the first tutorial, the greeting was hardcoded into the view. This doesn&#039;t follow the MVC pattern exactly because the view is intended to only display the data, and not contain it.&lt;br /&gt;
&lt;br /&gt;
In this second part of the tutorial we will demonstrate how to move this out of the view and into a model. In future tutorials we will demonstrate the power and flexibility that this design pattern provides.&lt;br /&gt;
&lt;br /&gt;
== Creating the Model ==&lt;br /&gt;
&lt;br /&gt;
The concept of model gets its name because this class is intended to represent (or &#039;model&#039;) some entity. In our case, our first model will represent a &#039;hello&#039;, or a greeting. This is in line with our design thus far, because we have one view (&#039;hello&#039;), which is a view of our greeting.&lt;br /&gt;
&lt;br /&gt;
The naming convention for models in the Joomla! framework is that the class name starts with the name of the component (in our case &#039;hello&#039;, followed by &#039;model&#039;, followed by the model name. Therefore, our model class is called HelloModelHello.&lt;br /&gt;
&lt;br /&gt;
At this point, we will only model one behaviour of our hello, and that is retrieving the greeting. We will thus have one method, called getGreeting(). It will simply return the string &#039;Hello, World!&#039;.&lt;br /&gt;
&lt;br /&gt;
Here is the code for our model class:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * Hello Model for Hello World Component&lt;br /&gt;
 * &lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://dev.joomla.org/component/option,com_jd-wiki/Itemid,31/id,tutorials:modules/&lt;br /&gt;
 * @license    GNU/GPL&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// No direct access&lt;br /&gt;
&lt;br /&gt;
defined( &#039;_JEXEC&#039; ) or die( &#039;Restricted access&#039; );&lt;br /&gt;
&lt;br /&gt;
jimport( &#039;joomla.application.component.model&#039; );&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Hello Model&lt;br /&gt;
 *&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 */&lt;br /&gt;
class HelloModelHello extends JModel&lt;br /&gt;
{&lt;br /&gt;
    /**&lt;br /&gt;
    * Gets the greeting&lt;br /&gt;
    * @return string The greeting to be displayed to the user&lt;br /&gt;
    */&lt;br /&gt;
    function getGreeting()&lt;br /&gt;
    {&lt;br /&gt;
        return &#039;Hello, World!&#039;;&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You will notice a line that starts with jimport. The jimport function is used to load files from the Joomla! framework that are required for our component. This particular statement will load the file /libraries/joomla/application/component/model.php. The &#039;.&#039;s are used as directory separators and the last part is the name of the file to load. All files are loaded relative to the libraries directory. This particular file contains the class definition for the JModel class, which is necessary because our model extends this class.&lt;br /&gt;
&lt;br /&gt;
Now that we have created our model, we must modify our view so that it uses it to obtain the greeting.&lt;br /&gt;
&lt;br /&gt;
== Using the Model ==&lt;br /&gt;
&lt;br /&gt;
The Joomla! framework is setup in such a way that the controller will automatically load the model that has the same name as the view and will push it into the view. Since our view is called &#039;Hello&#039;, our &#039;Hello&#039; model will automatically be loaded and pushed into the view. Therefore, we can easily retrieve a reference to our model using the JView::getModel() method.&lt;br /&gt;
&lt;br /&gt;
Our previous view code contained the lines:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$greeting = &amp;quot;Hello World!&amp;quot;;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To take advantage of our model, we change this line to:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$model =&amp;amp; $this-&amp;gt;getModel();&lt;br /&gt;
$greeting = $model-&amp;gt;getGreeting();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The complete view now looks like:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;?php&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Hello View for Hello World Component&lt;br /&gt;
 * &lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 * @link http://dev.joomla.org/component/option,com_jd-wiki/Itemid,31/id,tutorials:modules/&lt;br /&gt;
 * @license    GNU/GPL&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
// no direct access&lt;br /&gt;
&lt;br /&gt;
defined( &#039;_JEXEC&#039; ) or die( &#039;Restricted access&#039; );&lt;br /&gt;
&lt;br /&gt;
jimport( &#039;joomla.application.component.view&#039;);&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * HTML View class for the HelloWorld Component&lt;br /&gt;
 *&lt;br /&gt;
 * @package    Joomla.Tutorials&lt;br /&gt;
 * @subpackage Components&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
class HelloViewHello extends JView&lt;br /&gt;
{&lt;br /&gt;
    function display($tpl = null)&lt;br /&gt;
    {&lt;br /&gt;
        $model =&amp;amp; $this-&amp;gt;getModel();&lt;br /&gt;
        $greeting = $model-&amp;gt;getGreeting();&lt;br /&gt;
        $this-&amp;gt;assignRef( &#039;greeting&#039;,	$greeting );&lt;br /&gt;
&lt;br /&gt;
        parent::display($tpl);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
?&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Adding the File to the Package ===&lt;br /&gt;
&lt;br /&gt;
All that remains is to add an entry to the XML file so that our new model will be copied. The Joomla! framework will look for our model in the models directory, so the entry for this file will look like (it should be added to the site section):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&amp;lt;filename&amp;gt;models/hello.php&amp;lt;/filename&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Our new hello.xml file will look like:&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;&lt;br /&gt;
&amp;lt;install type=&amp;quot;component&amp;quot; version=&amp;quot;1.5.0&amp;quot;&amp;gt;&lt;br /&gt;
 &amp;lt;name&amp;gt;Hello&amp;lt;/name&amp;gt;&lt;br /&gt;
 &amp;lt;!-- The following elements are optional and free of formatting conttraints --&amp;gt;&lt;br /&gt;
 &amp;lt;creationDate&amp;gt;2007-02-22&amp;lt;/creationDate&amp;gt;&lt;br /&gt;
 &amp;lt;author&amp;gt;John Doe&amp;lt;/author&amp;gt;&lt;br /&gt;
 &amp;lt;authorEmail&amp;gt;john.doe@example.org&amp;lt;/authorEmail&amp;gt;&lt;br /&gt;
 &amp;lt;authorUrl&amp;gt;http://www.example.org&amp;lt;/authorUrl&amp;gt;&lt;br /&gt;
 &amp;lt;copyright&amp;gt;Copyright Info&amp;lt;/copyright&amp;gt;&lt;br /&gt;
 &amp;lt;license&amp;gt;License Info&amp;lt;/license&amp;gt;&lt;br /&gt;
 &amp;lt;!--  The version string is recorded in the components table --&amp;gt;&lt;br /&gt;
 &amp;lt;version&amp;gt;Component Version String&amp;lt;/version&amp;gt;&lt;br /&gt;
 &amp;lt;!-- The description is optional and defaults to the name --&amp;gt;&lt;br /&gt;
 &amp;lt;description&amp;gt;Description of the component ...&amp;lt;/description&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;!-- Site Main File Copy Section --&amp;gt;&lt;br /&gt;
 &amp;lt;!-- Note the folder attribute: This attribute describes the folder&lt;br /&gt;
      to copy FROM in the package to install therefore files copied&lt;br /&gt;
      in this section are copied from /site/ in the package --&amp;gt;&lt;br /&gt;
 &amp;lt;files folder=&amp;quot;site&amp;quot;&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
   &amp;lt;filename&amp;gt;index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
 &amp;lt;/files&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;administration&amp;gt;&lt;br /&gt;
  &amp;lt;!-- Administration Menu Section --&amp;gt;&lt;br /&gt;
  &amp;lt;menu&amp;gt;Hello World!&amp;lt;/menu&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;!-- Administration Main File Copy Section --&amp;gt;&lt;br /&gt;
  &amp;lt;files folder=&amp;quot;admin&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;controller.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;models/hello.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;models/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/view.html.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/tmpl/default.php&amp;lt;/filename&amp;gt;&lt;br /&gt;
  &amp;lt;filename&amp;gt;views/hello/tmpl/index.html&amp;lt;/filename&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;/files&amp;gt;  &lt;br /&gt;
 &amp;lt;/administration&amp;gt;&lt;br /&gt;
&amp;lt;/install&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
We now have a simple MVC component. Each element is very simple at this point, but provides a great deal of flexibility and power.&lt;br /&gt;
&lt;br /&gt;
== Contributors ==&lt;br /&gt;
* staalanden&lt;br /&gt;
&lt;br /&gt;
== Download ==&lt;br /&gt;
&lt;br /&gt;
The component can be downloaded at: [[http://dev.joomla.org/components/com_jd-wiki/data/media/components/com_hello2.zip|com_hello2.zip]]&lt;/div&gt;</summary>
		<author><name>Tonie</name></author>
	</entry>
</feed>