<?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=Nicholasjohn16</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=Nicholasjohn16"/>
	<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/Special:Contributions/Nicholasjohn16"/>
	<updated>2026-05-25T11:57:33Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.43.0</generator>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=Generating_JSON_output&amp;diff=62835</id>
		<title>Generating JSON output</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=Generating_JSON_output&amp;diff=62835"/>
		<updated>2011-11-05T23:44:49Z</updated>

		<summary type="html">&lt;p&gt;Nicholasjohn16: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;PHP has native functions to encode and decode [[JSON]] data.  You can encode data using the [[php:json_encode|json_encode]] function, like this:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
// Set up the data to be sent in the response.&lt;br /&gt;
$data = array( &#039;some data&#039; );&lt;br /&gt;
&lt;br /&gt;
// Output the JSON data.&lt;br /&gt;
echo json_encode( $data );&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
The json_encode function can encode almost all data types, such as strings, arrays and objects, although you may need to be aware that the corresponding [[php:json_decode|json_decode]] function will only return an object (or optionally, an associative array).&lt;br /&gt;
&lt;br /&gt;
It is good practice to set the MIME-type for the output correctly.  In some applications you might also want to change the suggested filename to something other than the &amp;quot;index.php&amp;quot; that you will probably get by default.  In the following example the suggested filename is changed to the name of the view, with a &amp;quot;.json&amp;quot; extension added.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
// Set up the data to be sent in the response.&lt;br /&gt;
$data = array(&#039;some data&#039;);&lt;br /&gt;
&lt;br /&gt;
// Get the document object.&lt;br /&gt;
$document =&amp;amp; JFactory::getDocument();&lt;br /&gt;
&lt;br /&gt;
// Set the MIME type for JSON output.&lt;br /&gt;
$document-&amp;gt;setMimeEncoding(&#039;application/json&#039;);&lt;br /&gt;
&lt;br /&gt;
// Change the suggested filename.&lt;br /&gt;
JResponse::setHeader(&#039;Content-Disposition&#039;,&#039;attachment;filename=&amp;quot;&#039;.$view-&amp;gt;getName().&#039;.json&amp;quot;&#039;);&lt;br /&gt;
&lt;br /&gt;
// Output the JSON data.&lt;br /&gt;
echo json_encode($data);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;noinclude&amp;gt;[[Category:Development]][[Category:Tutorials]]&amp;lt;/noinclude&amp;gt;&lt;/div&gt;</summary>
		<author><name>Nicholasjohn16</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J1.5:Ajax_client_code_using_MooTools&amp;diff=62834</id>
		<title>J1.5:Ajax client code using MooTools</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J1.5:Ajax_client_code_using_MooTools&amp;diff=62834"/>
		<updated>2011-11-05T23:43:30Z</updated>

		<summary type="html">&lt;p&gt;Nicholasjohn16: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{notice|This article applies to Joomla! 1.5 only as Joomla! 1.6 ships with a later version of MooTools which has a different way&lt;br /&gt;
of handling Ajax requests.}}&lt;br /&gt;
&lt;br /&gt;
In a typical Ajax application you will want to pull some data from a server, which could be your own Joomla site or a remote web service, and update some element on the web page with the data returned by the server.  There are three elements to a typical Ajax implementation:&lt;br /&gt;
* An HTML element whose change of state will trigger the Ajax request.&lt;br /&gt;
* Another HTML element where the response data will be placed.  Often this will show an &amp;quot;Ajax loading&amp;quot; icon or message while the response from the server is being awaited.&lt;br /&gt;
* The Ajax JavaScript code itself.&lt;br /&gt;
&lt;br /&gt;
Starting with the first of these, you need to identify the element on the page that will trigger the Ajax request.  The element needs to be identified by a unique &amp;lt;tt&amp;gt;id&amp;lt;/tt&amp;gt; attribute.  For example, suppose you have a drop-down select box on your page and you want to do an Ajax request whenever the user changes the item selected.  Then you must ensure that the select element has a unique &amp;lt;tt&amp;gt;id&amp;lt;/tt&amp;gt; attribute like this:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;html4strict&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;select name=&amp;quot;drop-down&amp;quot; id=&amp;quot;drop-down&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;option value=&amp;quot;1&amp;quot;&amp;gt;Item 1&amp;lt;/option&amp;gt;&lt;br /&gt;
        &amp;lt;option value=&amp;quot;2&amp;quot;&amp;gt;Item 2&amp;lt;/option&amp;gt;&lt;br /&gt;
        &amp;lt;option value=&amp;quot;3&amp;quot;&amp;gt;Item 3&amp;lt;/option&amp;gt;&lt;br /&gt;
&amp;lt;/select&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
You can generate this select list programmatically using the [[JHTML]] class like this:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
$options = array();&lt;br /&gt;
$options[] = JHTML::_( &#039;select.option&#039;, &#039;1&#039;, &#039;Item 1&#039; );&lt;br /&gt;
$options[] = JHTML::_( &#039;select.option&#039;, &#039;2&#039;, &#039;Item 2&#039; );&lt;br /&gt;
$options[] = JHTML::_( &#039;select.option&#039;, &#039;3&#039;, &#039;Item 3&#039; );&lt;br /&gt;
echo JHTML::_( &#039;select.genericlist&#039;, $options, &#039;drop-down&#039; );&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Secondly, you need to add an HTML element that will hold the output from the Ajax call.  This could be a suitably-placed DIV, which must also have a unique &amp;lt;tt&amp;gt;id&amp;lt;/tt&amp;gt; attribute, like this:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;html4strict&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;div id=&amp;quot;ajax-container&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
You can, of course, use the &amp;lt;tt&amp;gt;id&amp;lt;/tt&amp;gt; in a selector to style the output using CSS.&lt;br /&gt;
&lt;br /&gt;
Thirdly, you need to add the JavaScript code that will make the Ajax request and place the response into the screen output.  You generally don&#039;t need to be concerned about loading MooTools itself as this is done automatically for you by Joomla, but sometimes you need to do this manually by adding the following code:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
JHTML::_( &#039;behavior.mootools&#039; );&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
There are many ways to add JavaScript code to the output from Joomla.  One way, which avoids complex quoting, is to use the PHP &amp;quot;heredoc&amp;quot; syntax (see [http://uk.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc] for more details) like this:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
$ajax = &amp;lt;&amp;lt;&amp;lt;EOD&lt;br /&gt;
Your JavaScript code goes here.&lt;br /&gt;
EOD;&lt;br /&gt;
&lt;br /&gt;
$doc = &amp;amp; JFactory::getDocument();&lt;br /&gt;
$doc-&amp;gt;addScriptDeclaration( $ajax );&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can embed PHP variables in the heredoc text by surrounding them with braces, like this:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
$ajax = &amp;lt;&amp;lt;&amp;lt;EOD&lt;br /&gt;
This is some JavaScript code with {$this-&amp;gt;embedded} PHP variable in it.&lt;br /&gt;
EOD;&lt;br /&gt;
&lt;br /&gt;
$doc = &amp;amp; JFactory::getDocument();&lt;br /&gt;
$doc-&amp;gt;addScriptDeclaration( $ajax );&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The JavaScript code must add an event handler to the element that will trigger the Ajax call.  This is done in MooTools using the following call:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
window.addEvent( &#039;domready&#039;, function() {&lt;br /&gt;
&lt;br /&gt;
	$(&#039;drop-down&#039;).addEvent( &#039;change&#039;, &amp;lt;function-declaration&amp;gt; );&lt;br /&gt;
&lt;br /&gt;
});&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
where &amp;lt;function-declaration&amp;gt; is the JavaScript code that is to be called when the state of the element identified as &amp;lt;tt&amp;gt;drop-down&amp;lt;/tt&amp;gt; is changed.  Notice that you should always delay the call to &amp;lt;tt&amp;gt;addEvent&amp;lt;/tt&amp;gt; until the DOM is ready following a page load.  This is done by telling &amp;lt;tt&amp;gt;window.addEvent&amp;lt;/tt&amp;gt; to hang the Ajax addEvent function onto the onDomReady event.&lt;br /&gt;
&lt;br /&gt;
You don&#039;t have to hang the Ajax call on the onChange event; for example, you could use onClick as the trigger.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;function-declaration&amp;gt; that you will add will be an instantiation of the MooTools Ajax class, looking something like this:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
var a = new Ajax( {$url}, {&lt;br /&gt;
	method: &#039;get&#039;,&lt;br /&gt;
	update: $(&#039;ajax-container&#039;)&lt;br /&gt;
}).request();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
where &amp;lt;tt&amp;gt;{$url}&amp;lt;/tt&amp;gt; is a PHP variable containing the URL for the Ajax request.  In this example the &amp;lt;tt&amp;gt;update&amp;lt;/tt&amp;gt; argument has been used to copy the entire response from the server into the &amp;lt;tt&amp;gt;ajax-container&amp;lt;/tt&amp;gt; element.  This is quick and convenient, but very often you will want to process the response in some way before showing it to the user.  Commonly, the response is JSON-encoded and you must decode the response and format it appropriately before updating &amp;lt;tt&amp;gt;ajax-container&amp;lt;/tt&amp;gt;.  To do this, use the &amp;lt;tt&amp;gt;onComplete&amp;lt;/tt&amp;gt; argument to the &amp;lt;tt&amp;gt;Ajax&amp;lt;/tt&amp;gt; object rather than the &amp;lt;tt&amp;gt;update&amp;lt;/tt&amp;gt; argument.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
var a = new Ajax( {$url}, {&lt;br /&gt;
	method: &#039;get&#039;,&lt;br /&gt;
	onComplete: &amp;lt;completion-function&amp;gt;&lt;br /&gt;
}).request();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
where &amp;lt;completion-function&amp;gt; is a JavaScript function that will be called when a response from the remote server is received.  Typically, this function will process the raw data from the server before pushing it into the &amp;lt;tt&amp;gt;ajax-container&amp;lt;/tt&amp;gt; element.&lt;br /&gt;
&lt;br /&gt;
The following is a more complete example of an Ajax function which receives data from the server in JSON format, decodes it, then pushes data from the response into &amp;lt;tt&amp;gt;ajax-container&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
window.addEvent(&#039;domready&#039;,function() {&lt;br /&gt;
&lt;br /&gt;
     $(&#039;drop-down&#039;).addEvent(&#039;change&#039;,function(){&lt;br /&gt;
&lt;br /&gt;
          $(&#039;ajax-container&#039;).empty().addClass(&#039;ajax-loading&#039;);&lt;br /&gt;
&lt;br /&gt;
          var a = new Ajax({$url},{&lt;br /&gt;
               method:&#039;get&#039;,&lt;br /&gt;
               onComplete:function(response){&lt;br /&gt;
                    var resp=Json.evaluate(response);&lt;br /&gt;
&lt;br /&gt;
                    // Other code to execute when the request completes.&lt;br /&gt;
                    $(&#039;ajax-container&#039;).removeClass(&#039;ajax-loading&#039;).setHTML(output);&lt;br /&gt;
               }&lt;br /&gt;
          }).request();&lt;br /&gt;
     });&lt;br /&gt;
});&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Notice that in this example there is also some code to add, and subsequently remove, an &amp;lt;tt&amp;gt;ajax-loading&amp;lt;/tt&amp;gt; CSS class from the &amp;lt;tt&amp;gt;ajax-container&amp;lt;/tt&amp;gt; element.  Typically, the presence of this class will cause a &amp;quot;spinner&amp;quot; graphic element to be loaded as a background image to make the user aware that the system is still alive.&lt;br /&gt;
&amp;lt;noinclude&amp;gt;[[Category:Development]][[Category:Topics]]&amp;lt;/noinclude&amp;gt;&lt;/div&gt;</summary>
		<author><name>Nicholasjohn16</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J1.5:Avoiding_web_browser_same_origin_policy_in_Ajax_applications&amp;diff=62833</id>
		<title>J1.5:Avoiding web browser same origin policy in Ajax applications</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J1.5:Avoiding_web_browser_same_origin_policy_in_Ajax_applications&amp;diff=62833"/>
		<updated>2011-11-05T23:39:32Z</updated>

		<summary type="html">&lt;p&gt;Nicholasjohn16: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;If you need to access a web service, using Ajax, that is running on a server that is not in your domain, then you will run up against the &amp;quot;same origin policy&amp;quot; [http://en.wikipedia.org/wiki/Same_origin_policy] that will prevent you from retrieving data from outside your own domain for security reasons.  There needs to be some way to circumvent this restriction on those occasions where the web service is trusted.&lt;br /&gt;
&lt;br /&gt;
The most common method of circumventing the same origin policy in Ajax implementations is to access the remote web service via a proxy running in your domain.  The proxy uses an HTTP client library (such as [http://www.phpclasses.org/browse/package/3.html]) to access the remote server.  This technique is usually simple enough, but can get tougher if the remote server requires authentication or uses cookies to track state between requests.  On the plus side, using a proxy generally makes it easier to cache Ajax responses; something that is normally quite difficult.&lt;br /&gt;
&lt;br /&gt;
To add a simple proxy to your Joomla component you simply need to add an extra task to the controller.  The following code shows roughly what it might look like, given a static HTTP client class called &amp;lt;tt&amp;gt;httpClient&amp;lt;/tt&amp;gt; with a method that will request a page from a remote server.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
/**&lt;br /&gt;
 * Support cross-domain Ajax request by using the component as a proxy.&lt;br /&gt;
 * To use this facility replace &amp;quot;option&amp;quot; with &amp;quot;type&amp;quot; and &amp;quot;task&amp;quot; with &amp;quot;request&amp;quot;&lt;br /&gt;
 * in the query that would otherwise be sent to the remote server.&lt;br /&gt;
 */&lt;br /&gt;
function proxy()&lt;br /&gt;
{&lt;br /&gt;
	$uri = &amp;amp; JFactory::getURI();&lt;br /&gt;
	$query = $uri-&amp;gt;getQuery( true );&lt;br /&gt;
	$query[&#039;option&#039;] = $query[&#039;type&#039;];&lt;br /&gt;
	unset( $query[&#039;type&#039;]);&lt;br /&gt;
	unset( $query[&#039;task&#039;]);&lt;br /&gt;
	if (isset( $query[&#039;request&#039;] )) {&lt;br /&gt;
		$query[&#039;task&#039;] = $query[&#039;request&#039;];&lt;br /&gt;
		unset( $query[&#039;request&#039;]);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	// Make the API call.&lt;br /&gt;
	$response = httpClient::call( $query );&lt;br /&gt;
	if ($response-&amp;gt;status != &#039;200&#039;) {&lt;br /&gt;
		JError::raiseError( 500, JText::_( &#039;Remote server error&#039; ) );&lt;br /&gt;
		return false;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	// And return the response.&lt;br /&gt;
	echo $response-&amp;gt;data;&lt;br /&gt;
	jexit();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This example also shows how to work around the situation where the remote server is also running Joomla, in which case it will require a URL containing &amp;lt;tt&amp;gt;option&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;task&amp;lt;/tt&amp;gt; arguments that may not match those required by the proxy.  For example, suppose you want to make an Ajax request to a remote server using this URL:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
http://www.remoteserver.com/index.php?option=com_remotecomponent&amp;amp;task=remotetask&amp;amp;arg=something&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
To avoid the same origin policy you send the request to the proxy instead using this URL:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
http://www.localserver.com/index.php&lt;br /&gt;
     ?option=com_localcomponent&lt;br /&gt;
     &amp;amp;task=proxy&amp;amp;type=com_remotecomponent&lt;br /&gt;
     &amp;amp;request=remotetask&lt;br /&gt;
     &amp;amp;arg=something&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
You will need to adapt this code to meet your particular requirements, such as if you need to specify the controller in order to reach the proxy.&lt;br /&gt;
&lt;br /&gt;
Adding cookie and cache support is left as an exercise for the reader.&lt;br /&gt;
&amp;lt;noinclude&amp;gt;[[Category:Development]]&amp;lt;/noinclude&amp;gt;&lt;br /&gt;
[[Category:Tutorials]][[Category:Component Development]]&lt;/div&gt;</summary>
		<author><name>Nicholasjohn16</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=Generating_XML_output&amp;diff=62832</id>
		<title>Generating XML output</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=Generating_XML_output&amp;diff=62832"/>
		<updated>2011-11-05T23:37:50Z</updated>

		<summary type="html">&lt;p&gt;Nicholasjohn16: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Joomla supports a simple and quite efficient class, [[JSimpleXML]], which can be used to generate XML output in, for example, an Ajax implementation.  However, using an object-based XML generator suffers from the drawback that it tends to be slow and memory-intensive, even though [[JSimpleXML]] is a lightweight implementation.  Unless your requirements are particularly complex, directly outputting XML in string form will result in faster response times and a lower server footprint.&lt;br /&gt;
&lt;br /&gt;
For example, the following code will output an XML document consisting of a root element, called &amp;lt;root&amp;gt;, containing an &amp;lt;items&amp;gt; element which itself contains one or more &amp;lt;item&amp;gt; elements with the actual data.  You will need to adjust the code to cope with your particular data requirements.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
$document =&amp;amp; JFactory::getDocument();&lt;br /&gt;
$document-&amp;gt;setMimeEncoding( &#039;text/xml&#039; );&lt;br /&gt;
&lt;br /&gt;
// Output XML header.&lt;br /&gt;
echo &#039;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot; ?&amp;gt;&#039; . &amp;quot;\n&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
// Output root element.&lt;br /&gt;
echo &#039;&amp;lt;root&amp;gt;&#039;.&amp;quot;\n&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
// Output the data.&lt;br /&gt;
echo &amp;quot;\t&amp;quot;.&#039;&amp;lt;items&amp;gt;&#039;.&amp;quot;\n&amp;quot;;&lt;br /&gt;
if(!empty($data)){&lt;br /&gt;
     foreach($data as $datum){&lt;br /&gt;
          echo &amp;quot;\t\t&amp;quot;.&#039;&amp;lt;item&amp;gt;&#039;.&amp;quot;\n&amp;quot;;&lt;br /&gt;
          foreach ($datum as $key =&amp;gt; $value) {&lt;br /&gt;
               echo &amp;quot;\t\t\t&amp;quot;.&#039;&amp;lt;&#039;.$key.&#039;&amp;gt;&#039;.htmlspecialchars($value).&#039;&amp;lt;/&#039;.$key.&#039;&amp;gt;&#039;.&amp;quot;\n&amp;quot;;&lt;br /&gt;
          }&lt;br /&gt;
          echo &amp;quot;\t\t&amp;quot;.&#039;&amp;lt;/item&amp;gt;&#039;.&amp;quot;\n&amp;quot;;&lt;br /&gt;
     }&lt;br /&gt;
}&lt;br /&gt;
echo &amp;quot;\t&amp;quot;.&#039;&amp;lt;/items&amp;gt;&#039;.&amp;quot;\n&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
// Terminate root element.&lt;br /&gt;
echo &#039;&amp;lt;/root&amp;gt;&#039;.&amp;quot;\n&amp;quot;;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Note that the data should be passed through [[php:htmlspecialchars|htmlspecialchars]] to ensure that HTML characters are properly escaped.&lt;br /&gt;
&lt;br /&gt;
Note also that in this example some attempt has been made to &amp;quot;pretty print&amp;quot; the XML output with tabs and carriage returns.  This is not strictly necessary and these extra characters can be removed if you prefer.&lt;br /&gt;
&amp;lt;noinclude&amp;gt;[[Category:Development]][[Category:Tutorials]]&amp;lt;/noinclude&amp;gt;&lt;/div&gt;</summary>
		<author><name>Nicholasjohn16</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=Generating_XML_output&amp;diff=62831</id>
		<title>Generating XML output</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=Generating_XML_output&amp;diff=62831"/>
		<updated>2011-11-05T23:35:55Z</updated>

		<summary type="html">&lt;p&gt;Nicholasjohn16: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Joomla supports a simple and quite efficient class, [[JSimpleXML]], which can be used to generate XML output in, for example, an Ajax implementation.  However, using an object-based XML generator suffers from the drawback that it tends to be slow and memory-intensive, even though [[JSimpleXML]] is a lightweight implementation.  Unless your requirements are particularly complex, directly outputting XML in string form will result in faster response times and a lower server footprint.&lt;br /&gt;
&lt;br /&gt;
For example, the following code will output an XML document consisting of a root element, called &amp;lt;root&amp;gt;, containing an &amp;lt;items&amp;gt; element which itself contains one or more &amp;lt;item&amp;gt; elements with the actual data.  You will need to adjust the code to cope with your particular data requirements.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
$document =&amp;amp; JFactory::getDocument();&lt;br /&gt;
$document-&amp;gt;setMimeEncoding( &#039;text/xml&#039; );&lt;br /&gt;
&lt;br /&gt;
// Output XML header.&lt;br /&gt;
echo &#039;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot; ?&amp;gt;&#039; . &amp;quot;\n&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
// Output root element.&lt;br /&gt;
echo &#039;&amp;lt;root&amp;gt;&#039; . &amp;quot;\n&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
// Output the data.&lt;br /&gt;
echo &amp;quot;\t&amp;quot; . &#039;&amp;lt;items&amp;gt;&#039; . &amp;quot;\n&amp;quot;;&lt;br /&gt;
if (!empty( $data )) {&lt;br /&gt;
	foreach ($data as $datum) {&lt;br /&gt;
		echo &amp;quot;\t\t&amp;quot; . &#039;&amp;lt;item&amp;gt;&#039; . &amp;quot;\n&amp;quot;;&lt;br /&gt;
		foreach ($datum as $key =&amp;gt; $value) {&lt;br /&gt;
			echo &amp;quot;\t\t\t&amp;quot;.&#039;&amp;lt;&#039;.$key.&#039;&amp;gt;&#039;.htmlspecialchars($value).&#039;&amp;lt;/&#039;.$key.&#039;&amp;gt;&#039;.&amp;quot;\n&amp;quot;;&lt;br /&gt;
				}&lt;br /&gt;
		echo &amp;quot;\t\t&amp;quot; . &#039;&amp;lt;/item&amp;gt;&#039; . &amp;quot;\n&amp;quot;;&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
echo &amp;quot;\t&amp;quot; . &#039;&amp;lt;/items&amp;gt;&#039; . &amp;quot;\n&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
// Terminate root element.&lt;br /&gt;
echo &#039;&amp;lt;/root&amp;gt;&#039; . &amp;quot;\n&amp;quot;;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Note that the data should be passed through [[php:htmlspecialchars|htmlspecialchars]] to ensure that HTML characters are properly escaped.&lt;br /&gt;
&lt;br /&gt;
Note also that in this example some attempt has been made to &amp;quot;pretty print&amp;quot; the XML output with tabs and carriage returns.  This is not strictly necessary and these extra characters can be removed if you prefer.&lt;br /&gt;
&amp;lt;noinclude&amp;gt;[[Category:Development]][[Category:Tutorials]]&amp;lt;/noinclude&amp;gt;&lt;/div&gt;</summary>
		<author><name>Nicholasjohn16</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J1.5:Adding_sortable_columns_to_a_table_in_a_component&amp;diff=58597</id>
		<title>J1.5:Adding sortable columns to a table in a component</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J1.5:Adding_sortable_columns_to_a_table_in_a_component&amp;diff=58597"/>
		<updated>2011-05-23T23:50:48Z</updated>

		<summary type="html">&lt;p&gt;Nicholasjohn16: /* Step 1: The model */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Given that you have a table of data already in your component, how do you make some, or all, of the table columns sortable, like many of them are in the Joomla Administrator?  It&#039;s not particularly hard to do, but there are several steps required and details you need to be aware of so everything fits together properly.  There are variations on the procedure given here and once you are confident that you understand how it all works you should feel free to explore other possibilities that may suit your purposes better.&lt;br /&gt;
&lt;br /&gt;
This procedure assumes that your component is structured according to the [[Model-View-Controller]] (MVC) design pattern.  The general idea behind the procedure will still be applicable to non-MVC components if you apply a bit of imagination!&lt;br /&gt;
&lt;br /&gt;
==Step 1: The Model==&lt;br /&gt;
&lt;br /&gt;
The first thing is to add the filter_order and filter_order_Dir request into the __construct() function. If you&#039;ve used JPagination, this step will already be familiar to you. Change &#039;default_column_name&#039; to the name of the column you want to use as the default sort, and change the filter order direction if you wish  Adding this information to the  State object here insures it&#039;s accessible to all of the code that might need it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
function __construct()&lt;br /&gt;
{&lt;br /&gt;
	parent::__construct();&lt;br /&gt;
&lt;br /&gt;
	global $mainframe, $option;&lt;br /&gt;
&lt;br /&gt;
	$filter_order     = $mainframe-&amp;gt;getUserStateFromRequest(  $option.&#039;filter_order&#039;, &#039;filter_order&#039;, &#039;default_column_name&#039;, &#039;cmd&#039; );&lt;br /&gt;
	$filter_order_Dir = $mainframe-&amp;gt;getUserStateFromRequest( $option.&#039;filter_order_Dir&#039;, &#039;filter_order_Dir&#039;, &#039;asc&#039;, &#039;word&#039; );&lt;br /&gt;
&lt;br /&gt;
	$this-&amp;gt;setState(&#039;filter_order&#039;, $filter_order);&lt;br /&gt;
	$this-&amp;gt;setState(&#039;filter_order_Dir&#039;, $filter_order_Dir);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In the model which generates the data that will form the table, you need to make a change to the method which builds the database query that will be used to populate the table.  Most often this is the [[JModel/getData|getData()]] method, but might not be; check in the view to see which method is actually being used.&lt;br /&gt;
&lt;br /&gt;
The model could pull data in from anywhere; it doesn&#039;t have to be a database, but in the vast majority of cases the model will be using the Joomla database API to submit SQL queries to a database.   Assuming that to be the case, you need to adjust the query so that the sort parameters are taken into account.&lt;br /&gt;
&lt;br /&gt;
This information gets called by whatever function builds the ORDER BY clause, typically a private function like this: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
function _buildContentOrderBy()&lt;br /&gt;
{&lt;br /&gt;
	global $mainframe, $option;&lt;br /&gt;
&lt;br /&gt;
		$orderby = &#039;&#039;;&lt;br /&gt;
		$filter_order     = $this-&amp;gt;getState(&#039;filter_order&#039;);&lt;br /&gt;
		$filter_order_Dir = $this-&amp;gt;getState(&#039;filter_order_Dir&#039;);&lt;br /&gt;
		&lt;br /&gt;
		/* Error handling is never a bad thing*/&lt;br /&gt;
		if(!empty($filter_order) &amp;amp;&amp;amp; !empty($filter_order_Dir) ){&lt;br /&gt;
			$orderby = &#039; ORDER BY &#039;.$filter_order.&#039; &#039;.$filter_order_Dir;&lt;br /&gt;
		}&lt;br /&gt;
		&lt;br /&gt;
		return $orderby;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Step 2: The View==&lt;br /&gt;
Having generated the sorting variables in the model, you need to assign them to the view so that they show up on the page when it is displayed.&lt;br /&gt;
&lt;br /&gt;
To do this you need to add a few lines of code to your view file, typically view.html.php with code similar to this:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
	function display($tpl = null)&lt;br /&gt;
	{&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;
		$this-&amp;gt;assignRef(&#039;items&#039;,	$items);&lt;br /&gt;
		&lt;br /&gt;
		/* Call the state object */&lt;br /&gt;
		$state =&amp;amp; $this-&amp;gt;get( &#039;state&#039; );&lt;br /&gt;
&lt;br /&gt;
		/* Get the values from the state object that were inserted in the model&#039;s construct function */&lt;br /&gt;
		$lists[&#039;order_Dir&#039;] = $state-&amp;gt;get( &#039;filter_order_Dir&#039; );&lt;br /&gt;
		$lists[&#039;order&#039;]     = $state-&amp;gt;get( &#039;filter_order&#039; );&lt;br /&gt;
&lt;br /&gt;
		$this-&amp;gt;assignRef( &#039;lists&#039;, $lists );&lt;br /&gt;
&lt;br /&gt;
		parent::display($tpl);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Step 3: The Template==&lt;br /&gt;
Now you need to add some elements to the component layout file.  The table must be included in a form.  This might already be the case as, for example, you might already have implemented pagination or filtering on the table.  But if the table is not yet in a form then now is the time to wrap it in &amp;lt;form&amp;gt; and &amp;lt;/form&amp;gt; tags.  The reason that a form is required is that the sortable columns rely on a bit of JavaScript that will submit the form with sort parameters added.  Naturally, this will involve a page load, so if you would prefer an [[AJAX]]-based solution, then this procedure is not for you.&lt;br /&gt;
&lt;br /&gt;
The form tags will look something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;form id=&amp;quot;adminForm&amp;quot; action=&amp;quot;&amp;lt;?php echo JRoute::_( &#039;index.php&#039; );?&amp;gt;&amp;quot; method=&amp;quot;post&amp;quot; name=&amp;quot;adminForm&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
.... table goes here ....&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/form&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Notice that the form name must be &amp;quot;adminForm&amp;quot;.  You may need to adjust the action URL in some cases.&lt;br /&gt;
&lt;br /&gt;
You also need to add a couple of hidden fields to the form.  They can be placed anywhere between the &amp;lt;form&amp;gt; and &amp;lt;/form&amp;gt; tags, but generally they are placed just before the closing tag, like this:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;form id=&amp;quot;adminForm&amp;quot; action=&amp;quot;&amp;lt;?php echo JRoute::_( &#039;index.php&#039; );?&amp;gt;&amp;quot; method=&amp;quot;post&amp;quot; name=&amp;quot;adminForm&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
.... table goes here ....&lt;br /&gt;
&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;filter_order&amp;quot; value=&amp;quot;&amp;lt;?php echo $this-&amp;gt;lists[&#039;order&#039;]; ?&amp;gt;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;filter_order_Dir&amp;quot; value=&amp;quot;&amp;lt;?php echo $this-&amp;gt;lists[&#039;order_Dir&#039;]; ?&amp;gt;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/form&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now look at the table itself.  You might have a table with static headings already, looking vaguely like this:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
	&amp;lt;th&amp;gt;Name&amp;lt;/th&amp;gt;&lt;br /&gt;
	&amp;lt;th&amp;gt;Description&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
You need to replace the static column names with calls to the Joomla [[JHTML]] static class, so that your code will look something like this:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
	&amp;lt;th&amp;gt;&amp;lt;?php echo JHTML::_( &#039;grid.sort&#039;, &#039;Name&#039;, &#039;DbNameColumn&#039;, $this-&amp;gt;lists[&#039;order_Dir&#039;], $this-&amp;gt;lists[&#039;order&#039;]); ?&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
	&amp;lt;th&amp;gt;&amp;lt;?php echo JHTML::_( &#039;grid.sort&#039;, &#039;Description&#039;, &#039;DbDescriptionColumn&#039;, $this-&amp;gt;lists[&#039;order_Dir&#039;], $this-&amp;gt;lists[&#039;order&#039;]); ?&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
You will definitely need to adapt this code to your specific requirements.  The arguments to the [[JHTML]] call are as follows:&lt;br /&gt;
# Must be &#039;grid.sort&#039; so that [[JHTML]] will insert the correct behaviour for a sortable column.&lt;br /&gt;
# This is the name of the column that your visitors will actually see.  You need to change this for your particular table columns.&lt;br /&gt;
# This is the name of the corresponding database field (column) that is to be sorted on.  This will be passed to the model, most likely so it can be added to an &amp;quot;ORDER BY&amp;quot; clause in the SQL query statement.&lt;br /&gt;
# Must be exactly as shown here.  It is the current order direction (ascending or descending) and comes from the view (see later).&lt;br /&gt;
# Must be exactly as shown here.  It is the name of the column that the table is currently sorted on and comes from the view (see later).&lt;br /&gt;
&lt;br /&gt;
In short, you need to amend the second and third arguments to each [[JHTML]] call appropriately.&lt;br /&gt;
&lt;br /&gt;
Finally, if your sortable table is going to be in the front-end of your site, then you need to add a little snippet of JavaScript to the layout.  Alternatively, you can add it to the view code (using [[JDocument/addScriptDeclaration|JDocument-&amp;gt;addScriptDeclaration]]) if you would rather keep your JavaScript code in the HTML &amp;lt;head&amp;gt; section.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;script language=&amp;quot;javascript&amp;quot; type=&amp;quot;text/javascript&amp;quot;&amp;gt;&lt;br /&gt;
function tableOrdering( order, dir, task )&lt;br /&gt;
{&lt;br /&gt;
	var form = document.adminForm;&lt;br /&gt;
&lt;br /&gt;
	form.filter_order.value = order;&lt;br /&gt;
	form.filter_order_Dir.value = dir;&lt;br /&gt;
	document.adminForm.submit( task );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
You don&#039;t need to add this code if your sortable table is in the Administrator as this code is loaded for you automatically anyway.&lt;br /&gt;
&lt;br /&gt;
This completes the changes you need to make to the layout.  [[JHTML]] [[JHTMLGrid|grid.sort]] will now add a call to the &#039;&#039;tableOrdering&#039;&#039; function so that &#039;&#039;tableOrdering&#039;&#039; will be called whenever the user clicks on the column header.  &#039;&#039;tableOrdering&#039;&#039; puts the name of the column that was clicked, and the sort direction, into the hidden form fields and submits the form.  In the next step you will see how the model picks up those field values and amends the database query appropriately.&lt;br /&gt;
&lt;br /&gt;
==Step 4: Styling the result==&lt;br /&gt;
Finally. you might want to apply a bit of CSS styling to make the output a bit more attractive.&lt;br /&gt;
&lt;br /&gt;
Selecting the sortable columns can only be done via their context, so you will probably need to add a CSS class to the &amp;lt;nowiki&amp;gt;&amp;lt;table&amp;gt;&amp;lt;/nowiki&amp;gt;, the &amp;lt;nowiki&amp;gt;&amp;lt;tr&amp;gt;&amp;lt;/nowiki&amp;gt; or to the &amp;lt;nowiki&amp;gt;&amp;lt;th&amp;gt;&amp;lt;/nowiki&amp;gt; tags.  This is what the output might look like with a class added to the &amp;lt;tr&amp;gt; tag:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;html4strict&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;tr class=&amp;quot;sortable&amp;quot;&amp;gt;&lt;br /&gt;
	&amp;lt;th&amp;gt;&amp;lt;a href=&amp;quot;javascript:tableOrdering(&#039;DbName&#039;,&#039;asc&#039;,&#039;&#039;);&amp;quot; title=&amp;quot;Click to sort by this column&amp;quot;&amp;gt;Training provider&amp;lt;/a&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
	&amp;lt;th&amp;gt;&amp;lt;a href=&amp;quot;javascript:tableOrdering(&#039;DbDescription&#039;,&#039;asc&#039;,&#039;&#039;);&amp;quot; title=&amp;quot;Click to sort by this column&amp;quot;&amp;gt;Location&amp;lt;/a&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
To add a bit of space between the column name and the ascending/descending indicator image (a common requirement), you could then apply CSS like this:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;css&amp;quot;&amp;gt;&lt;br /&gt;
tr.sortable th img {&lt;br /&gt;
	margin-left: 5px;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
* [[JHTMLGrid]]&lt;br /&gt;
* [[JModel/getData|JModel-&amp;gt;getData]]&lt;br /&gt;
* [[JApplication/getUserStateFromRequest|JApplication-&amp;gt;getUserStateFromRequest]]&lt;br /&gt;
* [[Using JPagination in your component]]&lt;br /&gt;
* [[Developing a Model-View-Controller Component - Part 1]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Component Development]]&lt;br /&gt;
[[Category:Tutorials]]&lt;/div&gt;</summary>
		<author><name>Nicholasjohn16</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J1.5:Adding_sortable_columns_to_a_table_in_a_component&amp;diff=58596</id>
		<title>J1.5:Adding sortable columns to a table in a component</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J1.5:Adding_sortable_columns_to_a_table_in_a_component&amp;diff=58596"/>
		<updated>2011-05-23T23:50:36Z</updated>

		<summary type="html">&lt;p&gt;Nicholasjohn16: /* Step 2: The view */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Given that you have a table of data already in your component, how do you make some, or all, of the table columns sortable, like many of them are in the Joomla Administrator?  It&#039;s not particularly hard to do, but there are several steps required and details you need to be aware of so everything fits together properly.  There are variations on the procedure given here and once you are confident that you understand how it all works you should feel free to explore other possibilities that may suit your purposes better.&lt;br /&gt;
&lt;br /&gt;
This procedure assumes that your component is structured according to the [[Model-View-Controller]] (MVC) design pattern.  The general idea behind the procedure will still be applicable to non-MVC components if you apply a bit of imagination!&lt;br /&gt;
&lt;br /&gt;
==Step 1: The model==&lt;br /&gt;
&lt;br /&gt;
The first thing is to add the filter_order and filter_order_Dir request into the __construct() function. If you&#039;ve used JPagination, this step will already be familiar to you. Change &#039;default_column_name&#039; to the name of the column you want to use as the default sort, and change the filter order direction if you wish  Adding this information to the  State object here insures it&#039;s accessible to all of the code that might need it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
function __construct()&lt;br /&gt;
{&lt;br /&gt;
	parent::__construct();&lt;br /&gt;
&lt;br /&gt;
	global $mainframe, $option;&lt;br /&gt;
&lt;br /&gt;
	$filter_order     = $mainframe-&amp;gt;getUserStateFromRequest(  $option.&#039;filter_order&#039;, &#039;filter_order&#039;, &#039;default_column_name&#039;, &#039;cmd&#039; );&lt;br /&gt;
	$filter_order_Dir = $mainframe-&amp;gt;getUserStateFromRequest( $option.&#039;filter_order_Dir&#039;, &#039;filter_order_Dir&#039;, &#039;asc&#039;, &#039;word&#039; );&lt;br /&gt;
&lt;br /&gt;
	$this-&amp;gt;setState(&#039;filter_order&#039;, $filter_order);&lt;br /&gt;
	$this-&amp;gt;setState(&#039;filter_order_Dir&#039;, $filter_order_Dir);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In the model which generates the data that will form the table, you need to make a change to the method which builds the database query that will be used to populate the table.  Most often this is the [[JModel/getData|getData()]] method, but might not be; check in the view to see which method is actually being used.&lt;br /&gt;
&lt;br /&gt;
The model could pull data in from anywhere; it doesn&#039;t have to be a database, but in the vast majority of cases the model will be using the Joomla database API to submit SQL queries to a database.   Assuming that to be the case, you need to adjust the query so that the sort parameters are taken into account.&lt;br /&gt;
&lt;br /&gt;
This information gets called by whatever function builds the ORDER BY clause, typically a private function like this: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
function _buildContentOrderBy()&lt;br /&gt;
{&lt;br /&gt;
	global $mainframe, $option;&lt;br /&gt;
&lt;br /&gt;
		$orderby = &#039;&#039;;&lt;br /&gt;
		$filter_order     = $this-&amp;gt;getState(&#039;filter_order&#039;);&lt;br /&gt;
		$filter_order_Dir = $this-&amp;gt;getState(&#039;filter_order_Dir&#039;);&lt;br /&gt;
		&lt;br /&gt;
		/* Error handling is never a bad thing*/&lt;br /&gt;
		if(!empty($filter_order) &amp;amp;&amp;amp; !empty($filter_order_Dir) ){&lt;br /&gt;
			$orderby = &#039; ORDER BY &#039;.$filter_order.&#039; &#039;.$filter_order_Dir;&lt;br /&gt;
		}&lt;br /&gt;
		&lt;br /&gt;
		return $orderby;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Step 2: The View==&lt;br /&gt;
Having generated the sorting variables in the model, you need to assign them to the view so that they show up on the page when it is displayed.&lt;br /&gt;
&lt;br /&gt;
To do this you need to add a few lines of code to your view file, typically view.html.php with code similar to this:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
	function display($tpl = null)&lt;br /&gt;
	{&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;
		$this-&amp;gt;assignRef(&#039;items&#039;,	$items);&lt;br /&gt;
		&lt;br /&gt;
		/* Call the state object */&lt;br /&gt;
		$state =&amp;amp; $this-&amp;gt;get( &#039;state&#039; );&lt;br /&gt;
&lt;br /&gt;
		/* Get the values from the state object that were inserted in the model&#039;s construct function */&lt;br /&gt;
		$lists[&#039;order_Dir&#039;] = $state-&amp;gt;get( &#039;filter_order_Dir&#039; );&lt;br /&gt;
		$lists[&#039;order&#039;]     = $state-&amp;gt;get( &#039;filter_order&#039; );&lt;br /&gt;
&lt;br /&gt;
		$this-&amp;gt;assignRef( &#039;lists&#039;, $lists );&lt;br /&gt;
&lt;br /&gt;
		parent::display($tpl);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Step 3: The Template==&lt;br /&gt;
Now you need to add some elements to the component layout file.  The table must be included in a form.  This might already be the case as, for example, you might already have implemented pagination or filtering on the table.  But if the table is not yet in a form then now is the time to wrap it in &amp;lt;form&amp;gt; and &amp;lt;/form&amp;gt; tags.  The reason that a form is required is that the sortable columns rely on a bit of JavaScript that will submit the form with sort parameters added.  Naturally, this will involve a page load, so if you would prefer an [[AJAX]]-based solution, then this procedure is not for you.&lt;br /&gt;
&lt;br /&gt;
The form tags will look something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;form id=&amp;quot;adminForm&amp;quot; action=&amp;quot;&amp;lt;?php echo JRoute::_( &#039;index.php&#039; );?&amp;gt;&amp;quot; method=&amp;quot;post&amp;quot; name=&amp;quot;adminForm&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
.... table goes here ....&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/form&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Notice that the form name must be &amp;quot;adminForm&amp;quot;.  You may need to adjust the action URL in some cases.&lt;br /&gt;
&lt;br /&gt;
You also need to add a couple of hidden fields to the form.  They can be placed anywhere between the &amp;lt;form&amp;gt; and &amp;lt;/form&amp;gt; tags, but generally they are placed just before the closing tag, like this:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;form id=&amp;quot;adminForm&amp;quot; action=&amp;quot;&amp;lt;?php echo JRoute::_( &#039;index.php&#039; );?&amp;gt;&amp;quot; method=&amp;quot;post&amp;quot; name=&amp;quot;adminForm&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
.... table goes here ....&lt;br /&gt;
&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;filter_order&amp;quot; value=&amp;quot;&amp;lt;?php echo $this-&amp;gt;lists[&#039;order&#039;]; ?&amp;gt;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;filter_order_Dir&amp;quot; value=&amp;quot;&amp;lt;?php echo $this-&amp;gt;lists[&#039;order_Dir&#039;]; ?&amp;gt;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/form&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now look at the table itself.  You might have a table with static headings already, looking vaguely like this:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
	&amp;lt;th&amp;gt;Name&amp;lt;/th&amp;gt;&lt;br /&gt;
	&amp;lt;th&amp;gt;Description&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
You need to replace the static column names with calls to the Joomla [[JHTML]] static class, so that your code will look something like this:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
	&amp;lt;th&amp;gt;&amp;lt;?php echo JHTML::_( &#039;grid.sort&#039;, &#039;Name&#039;, &#039;DbNameColumn&#039;, $this-&amp;gt;lists[&#039;order_Dir&#039;], $this-&amp;gt;lists[&#039;order&#039;]); ?&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
	&amp;lt;th&amp;gt;&amp;lt;?php echo JHTML::_( &#039;grid.sort&#039;, &#039;Description&#039;, &#039;DbDescriptionColumn&#039;, $this-&amp;gt;lists[&#039;order_Dir&#039;], $this-&amp;gt;lists[&#039;order&#039;]); ?&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
You will definitely need to adapt this code to your specific requirements.  The arguments to the [[JHTML]] call are as follows:&lt;br /&gt;
# Must be &#039;grid.sort&#039; so that [[JHTML]] will insert the correct behaviour for a sortable column.&lt;br /&gt;
# This is the name of the column that your visitors will actually see.  You need to change this for your particular table columns.&lt;br /&gt;
# This is the name of the corresponding database field (column) that is to be sorted on.  This will be passed to the model, most likely so it can be added to an &amp;quot;ORDER BY&amp;quot; clause in the SQL query statement.&lt;br /&gt;
# Must be exactly as shown here.  It is the current order direction (ascending or descending) and comes from the view (see later).&lt;br /&gt;
# Must be exactly as shown here.  It is the name of the column that the table is currently sorted on and comes from the view (see later).&lt;br /&gt;
&lt;br /&gt;
In short, you need to amend the second and third arguments to each [[JHTML]] call appropriately.&lt;br /&gt;
&lt;br /&gt;
Finally, if your sortable table is going to be in the front-end of your site, then you need to add a little snippet of JavaScript to the layout.  Alternatively, you can add it to the view code (using [[JDocument/addScriptDeclaration|JDocument-&amp;gt;addScriptDeclaration]]) if you would rather keep your JavaScript code in the HTML &amp;lt;head&amp;gt; section.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;script language=&amp;quot;javascript&amp;quot; type=&amp;quot;text/javascript&amp;quot;&amp;gt;&lt;br /&gt;
function tableOrdering( order, dir, task )&lt;br /&gt;
{&lt;br /&gt;
	var form = document.adminForm;&lt;br /&gt;
&lt;br /&gt;
	form.filter_order.value = order;&lt;br /&gt;
	form.filter_order_Dir.value = dir;&lt;br /&gt;
	document.adminForm.submit( task );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
You don&#039;t need to add this code if your sortable table is in the Administrator as this code is loaded for you automatically anyway.&lt;br /&gt;
&lt;br /&gt;
This completes the changes you need to make to the layout.  [[JHTML]] [[JHTMLGrid|grid.sort]] will now add a call to the &#039;&#039;tableOrdering&#039;&#039; function so that &#039;&#039;tableOrdering&#039;&#039; will be called whenever the user clicks on the column header.  &#039;&#039;tableOrdering&#039;&#039; puts the name of the column that was clicked, and the sort direction, into the hidden form fields and submits the form.  In the next step you will see how the model picks up those field values and amends the database query appropriately.&lt;br /&gt;
&lt;br /&gt;
==Step 4: Styling the result==&lt;br /&gt;
Finally. you might want to apply a bit of CSS styling to make the output a bit more attractive.&lt;br /&gt;
&lt;br /&gt;
Selecting the sortable columns can only be done via their context, so you will probably need to add a CSS class to the &amp;lt;nowiki&amp;gt;&amp;lt;table&amp;gt;&amp;lt;/nowiki&amp;gt;, the &amp;lt;nowiki&amp;gt;&amp;lt;tr&amp;gt;&amp;lt;/nowiki&amp;gt; or to the &amp;lt;nowiki&amp;gt;&amp;lt;th&amp;gt;&amp;lt;/nowiki&amp;gt; tags.  This is what the output might look like with a class added to the &amp;lt;tr&amp;gt; tag:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;html4strict&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;tr class=&amp;quot;sortable&amp;quot;&amp;gt;&lt;br /&gt;
	&amp;lt;th&amp;gt;&amp;lt;a href=&amp;quot;javascript:tableOrdering(&#039;DbName&#039;,&#039;asc&#039;,&#039;&#039;);&amp;quot; title=&amp;quot;Click to sort by this column&amp;quot;&amp;gt;Training provider&amp;lt;/a&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
	&amp;lt;th&amp;gt;&amp;lt;a href=&amp;quot;javascript:tableOrdering(&#039;DbDescription&#039;,&#039;asc&#039;,&#039;&#039;);&amp;quot; title=&amp;quot;Click to sort by this column&amp;quot;&amp;gt;Location&amp;lt;/a&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
To add a bit of space between the column name and the ascending/descending indicator image (a common requirement), you could then apply CSS like this:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;css&amp;quot;&amp;gt;&lt;br /&gt;
tr.sortable th img {&lt;br /&gt;
	margin-left: 5px;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
* [[JHTMLGrid]]&lt;br /&gt;
* [[JModel/getData|JModel-&amp;gt;getData]]&lt;br /&gt;
* [[JApplication/getUserStateFromRequest|JApplication-&amp;gt;getUserStateFromRequest]]&lt;br /&gt;
* [[Using JPagination in your component]]&lt;br /&gt;
* [[Developing a Model-View-Controller Component - Part 1]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Component Development]]&lt;br /&gt;
[[Category:Tutorials]]&lt;/div&gt;</summary>
		<author><name>Nicholasjohn16</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J1.5:Adding_sortable_columns_to_a_table_in_a_component&amp;diff=58595</id>
		<title>J1.5:Adding sortable columns to a table in a component</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J1.5:Adding_sortable_columns_to_a_table_in_a_component&amp;diff=58595"/>
		<updated>2011-05-23T23:50:17Z</updated>

		<summary type="html">&lt;p&gt;Nicholasjohn16: /* Step 2: The View */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Given that you have a table of data already in your component, how do you make some, or all, of the table columns sortable, like many of them are in the Joomla Administrator?  It&#039;s not particularly hard to do, but there are several steps required and details you need to be aware of so everything fits together properly.  There are variations on the procedure given here and once you are confident that you understand how it all works you should feel free to explore other possibilities that may suit your purposes better.&lt;br /&gt;
&lt;br /&gt;
This procedure assumes that your component is structured according to the [[Model-View-Controller]] (MVC) design pattern.  The general idea behind the procedure will still be applicable to non-MVC components if you apply a bit of imagination!&lt;br /&gt;
&lt;br /&gt;
==Step 1: The model==&lt;br /&gt;
&lt;br /&gt;
The first thing is to add the filter_order and filter_order_Dir request into the __construct() function. If you&#039;ve used JPagination, this step will already be familiar to you. Change &#039;default_column_name&#039; to the name of the column you want to use as the default sort, and change the filter order direction if you wish  Adding this information to the  State object here insures it&#039;s accessible to all of the code that might need it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
function __construct()&lt;br /&gt;
{&lt;br /&gt;
	parent::__construct();&lt;br /&gt;
&lt;br /&gt;
	global $mainframe, $option;&lt;br /&gt;
&lt;br /&gt;
	$filter_order     = $mainframe-&amp;gt;getUserStateFromRequest(  $option.&#039;filter_order&#039;, &#039;filter_order&#039;, &#039;default_column_name&#039;, &#039;cmd&#039; );&lt;br /&gt;
	$filter_order_Dir = $mainframe-&amp;gt;getUserStateFromRequest( $option.&#039;filter_order_Dir&#039;, &#039;filter_order_Dir&#039;, &#039;asc&#039;, &#039;word&#039; );&lt;br /&gt;
&lt;br /&gt;
	$this-&amp;gt;setState(&#039;filter_order&#039;, $filter_order);&lt;br /&gt;
	$this-&amp;gt;setState(&#039;filter_order_Dir&#039;, $filter_order_Dir);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In the model which generates the data that will form the table, you need to make a change to the method which builds the database query that will be used to populate the table.  Most often this is the [[JModel/getData|getData()]] method, but might not be; check in the view to see which method is actually being used.&lt;br /&gt;
&lt;br /&gt;
The model could pull data in from anywhere; it doesn&#039;t have to be a database, but in the vast majority of cases the model will be using the Joomla database API to submit SQL queries to a database.   Assuming that to be the case, you need to adjust the query so that the sort parameters are taken into account.&lt;br /&gt;
&lt;br /&gt;
This information gets called by whatever function builds the ORDER BY clause, typically a private function like this: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
function _buildContentOrderBy()&lt;br /&gt;
{&lt;br /&gt;
	global $mainframe, $option;&lt;br /&gt;
&lt;br /&gt;
		$orderby = &#039;&#039;;&lt;br /&gt;
		$filter_order     = $this-&amp;gt;getState(&#039;filter_order&#039;);&lt;br /&gt;
		$filter_order_Dir = $this-&amp;gt;getState(&#039;filter_order_Dir&#039;);&lt;br /&gt;
		&lt;br /&gt;
		/* Error handling is never a bad thing*/&lt;br /&gt;
		if(!empty($filter_order) &amp;amp;&amp;amp; !empty($filter_order_Dir) ){&lt;br /&gt;
			$orderby = &#039; ORDER BY &#039;.$filter_order.&#039; &#039;.$filter_order_Dir;&lt;br /&gt;
		}&lt;br /&gt;
		&lt;br /&gt;
		return $orderby;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Step 2: The view==&lt;br /&gt;
Having generated the sorting variables in the model, you need to assign them to the view so that they show up on the page when it is displayed.&lt;br /&gt;
&lt;br /&gt;
To do this you need to add a few lines of code to your view file, typically view.html.php with code similar to this:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
	function display($tpl = null)&lt;br /&gt;
	{&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;
		$this-&amp;gt;assignRef(&#039;items&#039;,	$items);&lt;br /&gt;
		&lt;br /&gt;
		/* Call the state object */&lt;br /&gt;
		$state =&amp;amp; $this-&amp;gt;get( &#039;state&#039; );&lt;br /&gt;
&lt;br /&gt;
		/* Get the values from the state object that were inserted in the model&#039;s construct function */&lt;br /&gt;
		$lists[&#039;order_Dir&#039;] = $state-&amp;gt;get( &#039;filter_order_Dir&#039; );&lt;br /&gt;
		$lists[&#039;order&#039;]     = $state-&amp;gt;get( &#039;filter_order&#039; );&lt;br /&gt;
&lt;br /&gt;
		$this-&amp;gt;assignRef( &#039;lists&#039;, $lists );&lt;br /&gt;
&lt;br /&gt;
		parent::display($tpl);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Step 3: The Template==&lt;br /&gt;
Now you need to add some elements to the component layout file.  The table must be included in a form.  This might already be the case as, for example, you might already have implemented pagination or filtering on the table.  But if the table is not yet in a form then now is the time to wrap it in &amp;lt;form&amp;gt; and &amp;lt;/form&amp;gt; tags.  The reason that a form is required is that the sortable columns rely on a bit of JavaScript that will submit the form with sort parameters added.  Naturally, this will involve a page load, so if you would prefer an [[AJAX]]-based solution, then this procedure is not for you.&lt;br /&gt;
&lt;br /&gt;
The form tags will look something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;form id=&amp;quot;adminForm&amp;quot; action=&amp;quot;&amp;lt;?php echo JRoute::_( &#039;index.php&#039; );?&amp;gt;&amp;quot; method=&amp;quot;post&amp;quot; name=&amp;quot;adminForm&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
.... table goes here ....&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/form&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Notice that the form name must be &amp;quot;adminForm&amp;quot;.  You may need to adjust the action URL in some cases.&lt;br /&gt;
&lt;br /&gt;
You also need to add a couple of hidden fields to the form.  They can be placed anywhere between the &amp;lt;form&amp;gt; and &amp;lt;/form&amp;gt; tags, but generally they are placed just before the closing tag, like this:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;form id=&amp;quot;adminForm&amp;quot; action=&amp;quot;&amp;lt;?php echo JRoute::_( &#039;index.php&#039; );?&amp;gt;&amp;quot; method=&amp;quot;post&amp;quot; name=&amp;quot;adminForm&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
.... table goes here ....&lt;br /&gt;
&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;filter_order&amp;quot; value=&amp;quot;&amp;lt;?php echo $this-&amp;gt;lists[&#039;order&#039;]; ?&amp;gt;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;filter_order_Dir&amp;quot; value=&amp;quot;&amp;lt;?php echo $this-&amp;gt;lists[&#039;order_Dir&#039;]; ?&amp;gt;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/form&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now look at the table itself.  You might have a table with static headings already, looking vaguely like this:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
	&amp;lt;th&amp;gt;Name&amp;lt;/th&amp;gt;&lt;br /&gt;
	&amp;lt;th&amp;gt;Description&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
You need to replace the static column names with calls to the Joomla [[JHTML]] static class, so that your code will look something like this:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
	&amp;lt;th&amp;gt;&amp;lt;?php echo JHTML::_( &#039;grid.sort&#039;, &#039;Name&#039;, &#039;DbNameColumn&#039;, $this-&amp;gt;lists[&#039;order_Dir&#039;], $this-&amp;gt;lists[&#039;order&#039;]); ?&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
	&amp;lt;th&amp;gt;&amp;lt;?php echo JHTML::_( &#039;grid.sort&#039;, &#039;Description&#039;, &#039;DbDescriptionColumn&#039;, $this-&amp;gt;lists[&#039;order_Dir&#039;], $this-&amp;gt;lists[&#039;order&#039;]); ?&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
You will definitely need to adapt this code to your specific requirements.  The arguments to the [[JHTML]] call are as follows:&lt;br /&gt;
# Must be &#039;grid.sort&#039; so that [[JHTML]] will insert the correct behaviour for a sortable column.&lt;br /&gt;
# This is the name of the column that your visitors will actually see.  You need to change this for your particular table columns.&lt;br /&gt;
# This is the name of the corresponding database field (column) that is to be sorted on.  This will be passed to the model, most likely so it can be added to an &amp;quot;ORDER BY&amp;quot; clause in the SQL query statement.&lt;br /&gt;
# Must be exactly as shown here.  It is the current order direction (ascending or descending) and comes from the view (see later).&lt;br /&gt;
# Must be exactly as shown here.  It is the name of the column that the table is currently sorted on and comes from the view (see later).&lt;br /&gt;
&lt;br /&gt;
In short, you need to amend the second and third arguments to each [[JHTML]] call appropriately.&lt;br /&gt;
&lt;br /&gt;
Finally, if your sortable table is going to be in the front-end of your site, then you need to add a little snippet of JavaScript to the layout.  Alternatively, you can add it to the view code (using [[JDocument/addScriptDeclaration|JDocument-&amp;gt;addScriptDeclaration]]) if you would rather keep your JavaScript code in the HTML &amp;lt;head&amp;gt; section.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;script language=&amp;quot;javascript&amp;quot; type=&amp;quot;text/javascript&amp;quot;&amp;gt;&lt;br /&gt;
function tableOrdering( order, dir, task )&lt;br /&gt;
{&lt;br /&gt;
	var form = document.adminForm;&lt;br /&gt;
&lt;br /&gt;
	form.filter_order.value = order;&lt;br /&gt;
	form.filter_order_Dir.value = dir;&lt;br /&gt;
	document.adminForm.submit( task );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
You don&#039;t need to add this code if your sortable table is in the Administrator as this code is loaded for you automatically anyway.&lt;br /&gt;
&lt;br /&gt;
This completes the changes you need to make to the layout.  [[JHTML]] [[JHTMLGrid|grid.sort]] will now add a call to the &#039;&#039;tableOrdering&#039;&#039; function so that &#039;&#039;tableOrdering&#039;&#039; will be called whenever the user clicks on the column header.  &#039;&#039;tableOrdering&#039;&#039; puts the name of the column that was clicked, and the sort direction, into the hidden form fields and submits the form.  In the next step you will see how the model picks up those field values and amends the database query appropriately.&lt;br /&gt;
&lt;br /&gt;
==Step 4: Styling the result==&lt;br /&gt;
Finally. you might want to apply a bit of CSS styling to make the output a bit more attractive.&lt;br /&gt;
&lt;br /&gt;
Selecting the sortable columns can only be done via their context, so you will probably need to add a CSS class to the &amp;lt;nowiki&amp;gt;&amp;lt;table&amp;gt;&amp;lt;/nowiki&amp;gt;, the &amp;lt;nowiki&amp;gt;&amp;lt;tr&amp;gt;&amp;lt;/nowiki&amp;gt; or to the &amp;lt;nowiki&amp;gt;&amp;lt;th&amp;gt;&amp;lt;/nowiki&amp;gt; tags.  This is what the output might look like with a class added to the &amp;lt;tr&amp;gt; tag:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;html4strict&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;tr class=&amp;quot;sortable&amp;quot;&amp;gt;&lt;br /&gt;
	&amp;lt;th&amp;gt;&amp;lt;a href=&amp;quot;javascript:tableOrdering(&#039;DbName&#039;,&#039;asc&#039;,&#039;&#039;);&amp;quot; title=&amp;quot;Click to sort by this column&amp;quot;&amp;gt;Training provider&amp;lt;/a&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
	&amp;lt;th&amp;gt;&amp;lt;a href=&amp;quot;javascript:tableOrdering(&#039;DbDescription&#039;,&#039;asc&#039;,&#039;&#039;);&amp;quot; title=&amp;quot;Click to sort by this column&amp;quot;&amp;gt;Location&amp;lt;/a&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
To add a bit of space between the column name and the ascending/descending indicator image (a common requirement), you could then apply CSS like this:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;css&amp;quot;&amp;gt;&lt;br /&gt;
tr.sortable th img {&lt;br /&gt;
	margin-left: 5px;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
* [[JHTMLGrid]]&lt;br /&gt;
* [[JModel/getData|JModel-&amp;gt;getData]]&lt;br /&gt;
* [[JApplication/getUserStateFromRequest|JApplication-&amp;gt;getUserStateFromRequest]]&lt;br /&gt;
* [[Using JPagination in your component]]&lt;br /&gt;
* [[Developing a Model-View-Controller Component - Part 1]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Component Development]]&lt;br /&gt;
[[Category:Tutorials]]&lt;/div&gt;</summary>
		<author><name>Nicholasjohn16</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J1.5:Adding_sortable_columns_to_a_table_in_a_component&amp;diff=58594</id>
		<title>J1.5:Adding sortable columns to a table in a component</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J1.5:Adding_sortable_columns_to_a_table_in_a_component&amp;diff=58594"/>
		<updated>2011-05-23T23:49:52Z</updated>

		<summary type="html">&lt;p&gt;Nicholasjohn16: /* Step 3: The component layout file */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Given that you have a table of data already in your component, how do you make some, or all, of the table columns sortable, like many of them are in the Joomla Administrator?  It&#039;s not particularly hard to do, but there are several steps required and details you need to be aware of so everything fits together properly.  There are variations on the procedure given here and once you are confident that you understand how it all works you should feel free to explore other possibilities that may suit your purposes better.&lt;br /&gt;
&lt;br /&gt;
This procedure assumes that your component is structured according to the [[Model-View-Controller]] (MVC) design pattern.  The general idea behind the procedure will still be applicable to non-MVC components if you apply a bit of imagination!&lt;br /&gt;
&lt;br /&gt;
==Step 1: The model==&lt;br /&gt;
&lt;br /&gt;
The first thing is to add the filter_order and filter_order_Dir request into the __construct() function. If you&#039;ve used JPagination, this step will already be familiar to you. Change &#039;default_column_name&#039; to the name of the column you want to use as the default sort, and change the filter order direction if you wish  Adding this information to the  State object here insures it&#039;s accessible to all of the code that might need it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
function __construct()&lt;br /&gt;
{&lt;br /&gt;
	parent::__construct();&lt;br /&gt;
&lt;br /&gt;
	global $mainframe, $option;&lt;br /&gt;
&lt;br /&gt;
	$filter_order     = $mainframe-&amp;gt;getUserStateFromRequest(  $option.&#039;filter_order&#039;, &#039;filter_order&#039;, &#039;default_column_name&#039;, &#039;cmd&#039; );&lt;br /&gt;
	$filter_order_Dir = $mainframe-&amp;gt;getUserStateFromRequest( $option.&#039;filter_order_Dir&#039;, &#039;filter_order_Dir&#039;, &#039;asc&#039;, &#039;word&#039; );&lt;br /&gt;
&lt;br /&gt;
	$this-&amp;gt;setState(&#039;filter_order&#039;, $filter_order);&lt;br /&gt;
	$this-&amp;gt;setState(&#039;filter_order_Dir&#039;, $filter_order_Dir);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In the model which generates the data that will form the table, you need to make a change to the method which builds the database query that will be used to populate the table.  Most often this is the [[JModel/getData|getData()]] method, but might not be; check in the view to see which method is actually being used.&lt;br /&gt;
&lt;br /&gt;
The model could pull data in from anywhere; it doesn&#039;t have to be a database, but in the vast majority of cases the model will be using the Joomla database API to submit SQL queries to a database.   Assuming that to be the case, you need to adjust the query so that the sort parameters are taken into account.&lt;br /&gt;
&lt;br /&gt;
This information gets called by whatever function builds the ORDER BY clause, typically a private function like this: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
function _buildContentOrderBy()&lt;br /&gt;
{&lt;br /&gt;
	global $mainframe, $option;&lt;br /&gt;
&lt;br /&gt;
		$orderby = &#039;&#039;;&lt;br /&gt;
		$filter_order     = $this-&amp;gt;getState(&#039;filter_order&#039;);&lt;br /&gt;
		$filter_order_Dir = $this-&amp;gt;getState(&#039;filter_order_Dir&#039;);&lt;br /&gt;
		&lt;br /&gt;
		/* Error handling is never a bad thing*/&lt;br /&gt;
		if(!empty($filter_order) &amp;amp;&amp;amp; !empty($filter_order_Dir) ){&lt;br /&gt;
			$orderby = &#039; ORDER BY &#039;.$filter_order.&#039; &#039;.$filter_order_Dir;&lt;br /&gt;
		}&lt;br /&gt;
		&lt;br /&gt;
		return $orderby;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Step 2: The view==&lt;br /&gt;
Having generated the sorting variables in the model, you need to assign them to the view so that they show up on the page when it is displayed.&lt;br /&gt;
&lt;br /&gt;
To do this you need to add a few lines of code to your view file, typically view.html.php with code similar to this:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
	function display($tpl = null)&lt;br /&gt;
	{&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;
		$this-&amp;gt;assignRef(&#039;items&#039;,	$items);&lt;br /&gt;
		&lt;br /&gt;
		/* Call the state object */&lt;br /&gt;
		$state =&amp;amp; $this-&amp;gt;get( &#039;state&#039; );&lt;br /&gt;
&lt;br /&gt;
		/* Get the values from the state object that were inserted in the model&#039;s construct function */&lt;br /&gt;
		$lists[&#039;order_Dir&#039;] = $state-&amp;gt;get( &#039;filter_order_Dir&#039; );&lt;br /&gt;
		$lists[&#039;order&#039;]     = $state-&amp;gt;get( &#039;filter_order&#039; );&lt;br /&gt;
&lt;br /&gt;
		$this-&amp;gt;assignRef( &#039;lists&#039;, $lists );&lt;br /&gt;
&lt;br /&gt;
		parent::display($tpl);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Step 3: The Template==&lt;br /&gt;
Now you need to add some elements to the component layout file.  The table must be included in a form.  This might already be the case as, for example, you might already have implemented pagination or filtering on the table.  But if the table is not yet in a form then now is the time to wrap it in &amp;lt;form&amp;gt; and &amp;lt;/form&amp;gt; tags.  The reason that a form is required is that the sortable columns rely on a bit of JavaScript that will submit the form with sort parameters added.  Naturally, this will involve a page load, so if you would prefer an [[AJAX]]-based solution, then this procedure is not for you.&lt;br /&gt;
&lt;br /&gt;
The form tags will look something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;form id=&amp;quot;adminForm&amp;quot; action=&amp;quot;&amp;lt;?php echo JRoute::_( &#039;index.php&#039; );?&amp;gt;&amp;quot; method=&amp;quot;post&amp;quot; name=&amp;quot;adminForm&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
.... table goes here ....&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/form&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Notice that the form name must be &amp;quot;adminForm&amp;quot;.  You may need to adjust the action URL in some cases.&lt;br /&gt;
&lt;br /&gt;
You also need to add a couple of hidden fields to the form.  They can be placed anywhere between the &amp;lt;form&amp;gt; and &amp;lt;/form&amp;gt; tags, but generally they are placed just before the closing tag, like this:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;form id=&amp;quot;adminForm&amp;quot; action=&amp;quot;&amp;lt;?php echo JRoute::_( &#039;index.php&#039; );?&amp;gt;&amp;quot; method=&amp;quot;post&amp;quot; name=&amp;quot;adminForm&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
.... table goes here ....&lt;br /&gt;
&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;filter_order&amp;quot; value=&amp;quot;&amp;lt;?php echo $this-&amp;gt;lists[&#039;order&#039;]; ?&amp;gt;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;filter_order_Dir&amp;quot; value=&amp;quot;&amp;lt;?php echo $this-&amp;gt;lists[&#039;order_Dir&#039;]; ?&amp;gt;&amp;quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/form&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now look at the table itself.  You might have a table with static headings already, looking vaguely like this:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
	&amp;lt;th&amp;gt;Name&amp;lt;/th&amp;gt;&lt;br /&gt;
	&amp;lt;th&amp;gt;Description&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
You need to replace the static column names with calls to the Joomla [[JHTML]] static class, so that your code will look something like this:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;tr&amp;gt;&lt;br /&gt;
	&amp;lt;th&amp;gt;&amp;lt;?php echo JHTML::_( &#039;grid.sort&#039;, &#039;Name&#039;, &#039;DbNameColumn&#039;, $this-&amp;gt;lists[&#039;order_Dir&#039;], $this-&amp;gt;lists[&#039;order&#039;]); ?&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
	&amp;lt;th&amp;gt;&amp;lt;?php echo JHTML::_( &#039;grid.sort&#039;, &#039;Description&#039;, &#039;DbDescriptionColumn&#039;, $this-&amp;gt;lists[&#039;order_Dir&#039;], $this-&amp;gt;lists[&#039;order&#039;]); ?&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
You will definitely need to adapt this code to your specific requirements.  The arguments to the [[JHTML]] call are as follows:&lt;br /&gt;
# Must be &#039;grid.sort&#039; so that [[JHTML]] will insert the correct behaviour for a sortable column.&lt;br /&gt;
# This is the name of the column that your visitors will actually see.  You need to change this for your particular table columns.&lt;br /&gt;
# This is the name of the corresponding database field (column) that is to be sorted on.  This will be passed to the model, most likely so it can be added to an &amp;quot;ORDER BY&amp;quot; clause in the SQL query statement.&lt;br /&gt;
# Must be exactly as shown here.  It is the current order direction (ascending or descending) and comes from the view (see later).&lt;br /&gt;
# Must be exactly as shown here.  It is the name of the column that the table is currently sorted on and comes from the view (see later).&lt;br /&gt;
&lt;br /&gt;
In short, you need to amend the second and third arguments to each [[JHTML]] call appropriately.&lt;br /&gt;
&lt;br /&gt;
Finally, if your sortable table is going to be in the front-end of your site, then you need to add a little snippet of JavaScript to the layout.  Alternatively, you can add it to the view code (using [[JDocument/addScriptDeclaration|JDocument-&amp;gt;addScriptDeclaration]]) if you would rather keep your JavaScript code in the HTML &amp;lt;head&amp;gt; section.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;script language=&amp;quot;javascript&amp;quot; type=&amp;quot;text/javascript&amp;quot;&amp;gt;&lt;br /&gt;
function tableOrdering( order, dir, task )&lt;br /&gt;
{&lt;br /&gt;
	var form = document.adminForm;&lt;br /&gt;
&lt;br /&gt;
	form.filter_order.value = order;&lt;br /&gt;
	form.filter_order_Dir.value = dir;&lt;br /&gt;
	document.adminForm.submit( task );&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
You don&#039;t need to add this code if your sortable table is in the Administrator as this code is loaded for you automatically anyway.&lt;br /&gt;
&lt;br /&gt;
This completes the changes you need to make to the layout.  [[JHTML]] [[JHTMLGrid|grid.sort]] will now add a call to the &#039;&#039;tableOrdering&#039;&#039; function so that &#039;&#039;tableOrdering&#039;&#039; will be called whenever the user clicks on the column header.  &#039;&#039;tableOrdering&#039;&#039; puts the name of the column that was clicked, and the sort direction, into the hidden form fields and submits the form.  In the next step you will see how the model picks up those field values and amends the database query appropriately.&lt;br /&gt;
&lt;br /&gt;
==Step 4: Styling the result==&lt;br /&gt;
Finally. you might want to apply a bit of CSS styling to make the output a bit more attractive.&lt;br /&gt;
&lt;br /&gt;
Selecting the sortable columns can only be done via their context, so you will probably need to add a CSS class to the &amp;lt;nowiki&amp;gt;&amp;lt;table&amp;gt;&amp;lt;/nowiki&amp;gt;, the &amp;lt;nowiki&amp;gt;&amp;lt;tr&amp;gt;&amp;lt;/nowiki&amp;gt; or to the &amp;lt;nowiki&amp;gt;&amp;lt;th&amp;gt;&amp;lt;/nowiki&amp;gt; tags.  This is what the output might look like with a class added to the &amp;lt;tr&amp;gt; tag:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;html4strict&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;tr class=&amp;quot;sortable&amp;quot;&amp;gt;&lt;br /&gt;
	&amp;lt;th&amp;gt;&amp;lt;a href=&amp;quot;javascript:tableOrdering(&#039;DbName&#039;,&#039;asc&#039;,&#039;&#039;);&amp;quot; title=&amp;quot;Click to sort by this column&amp;quot;&amp;gt;Training provider&amp;lt;/a&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
	&amp;lt;th&amp;gt;&amp;lt;a href=&amp;quot;javascript:tableOrdering(&#039;DbDescription&#039;,&#039;asc&#039;,&#039;&#039;);&amp;quot; title=&amp;quot;Click to sort by this column&amp;quot;&amp;gt;Location&amp;lt;/a&amp;gt;&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
To add a bit of space between the column name and the ascending/descending indicator image (a common requirement), you could then apply CSS like this:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;css&amp;quot;&amp;gt;&lt;br /&gt;
tr.sortable th img {&lt;br /&gt;
	margin-left: 5px;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
* [[JHTMLGrid]]&lt;br /&gt;
* [[JModel/getData|JModel-&amp;gt;getData]]&lt;br /&gt;
* [[JApplication/getUserStateFromRequest|JApplication-&amp;gt;getUserStateFromRequest]]&lt;br /&gt;
* [[Using JPagination in your component]]&lt;br /&gt;
* [[Developing a Model-View-Controller Component - Part 1]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Component Development]]&lt;br /&gt;
[[Category:Tutorials]]&lt;/div&gt;</summary>
		<author><name>Nicholasjohn16</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Basic_Backend_Framework&amp;diff=58482</id>
		<title>J1.5:Developing a MVC Component/Basic Backend Framework</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Basic_Backend_Framework&amp;diff=58482"/>
		<updated>2011-05-21T22:03:15Z</updated>

		<summary type="html">&lt;p&gt;Nicholasjohn16: /* The Hellos Template */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
This article focuses on the entry page/article for the administrator. Whilst the MVC pattern is the same as for the frontend user, this chapter will rapidly go though all steps and setup the backend counter part in the admin section. This article will only focus on setting up the Basic Framework with a list of all the &#039;&#039;Hellos&#039;&#039; but without user interaction. The actual user interaction is added in the succeeding article [[Developing a Model-View-Controller Component - Part 6 - Adding Backend Actions]].&lt;br /&gt;
&lt;br /&gt;
== Tutorial specific naming ==&lt;br /&gt;
Within the next articles the explanation of this administrator section we will keep as close as possible to the component name. For the general overview, lists from the database, we will use &#039;&#039;Hellos&#039;&#039; as identification. The &#039;&#039;Hellos&#039;&#039; naming will be used for viewing and controlling multiple Hellos at once from the database. When selecting a single Hello for Editing or Adding we will use the singular &#039;&#039;Hello&#039;&#039; as naming for the Controller and View. This &#039;&#039;Admin Hello&#039;&#039; has no functional relation with the &#039;&#039;Site Hello&#039;&#039; (the only dependency is the database content, of course).&lt;br /&gt;
&lt;br /&gt;
Where parts 1,2 and 3 of the MVC explanation were working in the site directory tree of com_hello, part 5 and 6 will focus on the admin directory tree. Part 5 and 6 will not add or change files in the site directory tree. Look at the XML file in the attached example of the full com_hello implementation. The XML configuration file will help you with the exact location of the different files being used in this and the following chapter.&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_5&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. (&#039;&#039;&amp;lt;-- Naming convention conflict. Private or Protected data should be preceded with a &#039;_&#039; but as this reference is returned to the view where this data is directly accessed, this data can only be considered as public.&#039;&#039;)&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_5&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 frontend, so is it in the administrator section. Methods that start with &amp;quot;get&amp;quot; [e.g. getData()] 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_5&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;
Look carefully at the almost hidden differences compared to &#039;&#039;site&#039;&#039; example. The data is stored in a variable that is encapsulated within the model. Because the data amount is huge due to using the very handy _getList(), the need for returning a reference instead of a value is obvious. This is handled in:&lt;br /&gt;
 $items =&amp;amp; $this-&amp;gt;get( &#039;Data&#039;);&lt;br /&gt;
Looking again at this line and you will notice another difference with respect to the &#039;&#039;site&#039;&#039; view.html.php. The calling of the model function is done implicitly. The actual model function name is getData(). In the &#039;&#039;site&#039;&#039; example you had to call following two lines:&lt;br /&gt;
 $model =&amp;amp; $this-&amp;gt;getModel();&lt;br /&gt;
 $greeting = $model-&amp;gt;getData();&lt;br /&gt;
Both lines are now taken care of by calling: &amp;lt;code&amp;gt;$this-&amp;gt;get( &#039;Data&#039;);&amp;lt;/code&amp;gt;. Under the surface of this &amp;lt;code&amp;gt;get()&amp;lt;/code&amp;gt; the &#039;Data&#039; is prefixed with &#039;get&#039; so when using this function make sure the model functions are preceded with &#039;get&#039;. This function can also be used in the &#039;&#039;site&#039;&#039; section. Keeping the data in the model and accessing it by reference, via &amp;lt;code&amp;gt;get()&amp;lt;/code&amp;gt; methods, is the preferred way of getting data from the model.&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;
    foreach ($this-&amp;gt;items as &amp;amp;$row)&lt;br /&gt;
    {&lt;br /&gt;
        ?&amp;gt;&lt;br /&gt;
        &amp;lt;tr class=&amp;quot;&amp;lt;?php echo &amp;quot;row&amp;quot; . $k; ?&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;
== Conclusion ==&lt;br /&gt;
&lt;br /&gt;
We have now implemented a basic framework for the backend admin component. A list where all of the Hellos are displayed. The next chapter will extend this framework and add user interaction / database manipulation.&lt;br /&gt;
&lt;br /&gt;
== Articles in this Series ==&lt;br /&gt;
[[Developing a Model-View-Controller Component - Part 1]]&lt;br /&gt;
&lt;br /&gt;
[[Developing a Model-View-Controller Component - Part 2 - Adding a Model]]&lt;br /&gt;
&lt;br /&gt;
[[Developing a Model-View-Controller Component - Part 3 - Using the Database]]&lt;br /&gt;
&lt;br /&gt;
[[Developing a Model-View-Controller Component - Part 4 - Creating an Administrator Interface]]&lt;br /&gt;
&lt;br /&gt;
[[Developing a Model-View-Controller Component - Part 5 - Basic Backend Framework]]&lt;br /&gt;
&lt;br /&gt;
[[Developing a Model-View-Controller Component - Part 6 - Adding Backend Actions]]&lt;br /&gt;
&lt;br /&gt;
== Contributors ==&lt;br /&gt;
* staalanden&lt;br /&gt;
* jamesconroyuk&lt;br /&gt;
&lt;br /&gt;
== Download ==&lt;br /&gt;
&lt;br /&gt;
The full admin component can be downloaded at: [http://joomlacode.org/gf/download/frsrelease/8111/29436/com_hello4_01.zip com_hello4_01].&lt;br /&gt;
&lt;br /&gt;
[[Category:Tutorials]]&lt;br /&gt;
[[Category:Component Development]]&lt;/div&gt;</summary>
		<author><name>Nicholasjohn16</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J1.5:Developing_a_MVC_Component/Introduction&amp;diff=58254</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=58254"/>
		<updated>2011-05-16T05:04:59Z</updated>

		<summary type="html">&lt;p&gt;Nicholasjohn16: /* Creating the Entry Point */   Missing a colon in JRequest::getWord()&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 designed for extensibility. 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;
[[image:MVC basics.png|frameless|right]]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 be revised and customized without having to reprogram the business logic. MVC was originally developed to map the traditional input, processing, output roles into a logical GUI architecture.&lt;br /&gt;
&lt;br /&gt;
These three main roles are the basis for the Joomla MVC. 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;
=== MVC connection ===&lt;br /&gt;
[[image:MVC joomla.png|frameless|right]]The simplified picture on the right depicts the basic components being used within Joomla. Besides the Model, the View and the Controller, an Entry Point has been added that is depicted as a small circle. Attached to the viewer (view) a Template has been added. With these five components you should be able to understand this tutorial about making a basic Joomla! MVC component.&lt;br /&gt;
&lt;br /&gt;
Part 1 of the tutorial only focuses on the Controller and the View (with the use of the Template); these are marked with the blue colour in the picture. Part 2 adds and Part 3 extends the model functionality for the data manipulation abstraction; marked with the green colour in the picture. &lt;br /&gt;
&lt;br /&gt;
Keep in mind that this simplified picture only applies for the site section (i.e the front-end). An identical picture is applicable for the admin section (i.e. the back-end). The administrative section is taken care of in Parts 4 through 6 of this component development tutorial. Both the site and the admin section are maintained and configured in an XML based installation file (typically termed a manifest file).&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;
For learning purposes and debugging, adding a run-time debugger to your Joomla! site might be a good extension especially during development of your (tutorial) component. A good example is the community project [http://extensions.joomla.org/extensions/miscellaneous/development/1509/details J!Dump] that has the advantage of being a pop-up which leaves the view output unchanged. The J!Dump system allows you to view not only your development properties but also the methods.&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;
* site/hello.php - this is the entry point to our component&lt;br /&gt;
* site/controller.php - this file contains our base controller&lt;br /&gt;
* site/views/hello/view.html.php - this file retrieves the necessary data and pushes it into the template&lt;br /&gt;
* site/views/hello/tmpl/default.php - this is the template for our output&lt;br /&gt;
* hello.xml - this is an XML (manifest) file that tells Joomla! how to install our component.&lt;br /&gt;
&lt;br /&gt;
Remember that the filename for the entry point must have the same name of the component. For example, if you call your component &amp;quot;Very Intricate Name Component&amp;quot;, at install time (see below in the hello.xml section) Joomla! will create the folder com_veryintricatenamecomponent and the entry point php file must be named veryintricatenamecomponent.php otherwise it will not work. Be aware that use of some special characters, notibly the underscore &#039;_&#039;, may have special meaning in Joomla and should be avoided in component names or files.&lt;br /&gt;
&lt;br /&gt;
The site directory here is for the parts of the component which are installed in the front end site.&lt;br /&gt;
&lt;br /&gt;
=== Naming conventions ===&lt;br /&gt;
&lt;br /&gt;
Main article: [[Naming conventions]]&lt;br /&gt;
&lt;br /&gt;
At this point it is important to say that some words are reserved for using in component or its class names, and violating some of that will produce a hard for debugging error. One of them is &amp;quot;view&amp;quot; (in any character case) for view class (subclass of JView) and controller class (subclass of JController), because view class need to have first part of name the same as controller class name, and component name (although violating of last one won&#039;t produce an error, it&#039;s just a useful convention).&lt;br /&gt;
&lt;br /&gt;
All filenames and foldernames for models, views and controllers must be lower-case in order to operate well on Unix/Linux-systems.&lt;br /&gt;
&lt;br /&gt;
=== Creating the Entry Point ===&lt;br /&gt;
{{review}}&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;
site/hello.php:&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::getWord( &#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. The &#039;DS&#039; constant 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 conditional check &amp;quot;in place&amp;quot; for future use.&lt;br /&gt;
&lt;br /&gt;
JRequest::getWord() finds a word variable in the URL or the POST data. So if our URL is index.php?option=com_hello&amp;amp;controller=controller_name, then we can retrieve our controller name in our component using: echo JRequest::getWord(&#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 site/controller.php is:&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;
&amp;lt;small&amp;gt;(Note: The key (the first argument) passed to the assignRef method cannot be preceded by an underscore i.e. $this-&amp;gt;assignRef(&#039;_greeting&#039;,$greeting). Doing so will cause the assignRef method to return false and your variable will not be pushed into the template.)&amp;lt;/small&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The code for the view at site/views/hello/view.html.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_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 - this file is:&amp;lt;br&amp;gt;site/views/hello/tmpl/default.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;
// 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 at hello.xml 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 constraints --&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;
{{notice|&lt;br /&gt;
Put this xml file, also called manifest, in the root of your package (because the installer will take its path as the root path for all other files).{{1}}Don&#039;t include itself under &amp;lt;files&amp;gt;...}}&lt;br /&gt;
&lt;br /&gt;
You may have noticed the manifest source above mentioned files we have not discussed. These are the index.html files and the admin 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 hello.php file in the admin folder is the entry point for the our component&#039;s admin section. Since our component has no administrator needs (yet), this file will have the same content as the index.html files for now.&lt;br /&gt;
&lt;br /&gt;
If you&#039;ve followed along, you can visit URL index.php?option=com_hello to see your work.&lt;br /&gt;
&lt;br /&gt;
== Articles in this Series ==&lt;br /&gt;
[[Developing a Model-View-Controller Component - Part 1]]&lt;br /&gt;
&lt;br /&gt;
[[Developing a Model-View-Controller Component - Part 2 - Adding a Model]]&lt;br /&gt;
&lt;br /&gt;
[[Developing a Model-View-Controller Component - Part 3 - Using the Database]]&lt;br /&gt;
&lt;br /&gt;
[[Developing a Model-View-Controller Component - Part 4 - Creating an Administrator Interface]]&lt;br /&gt;
&lt;br /&gt;
[[Developing a Model-View-Controller Component - Part 5 - Basic Backend Framework]]&lt;br /&gt;
&lt;br /&gt;
[[Developing a Model-View-Controller Component - Part 6 - Adding Backend Actions]]&lt;br /&gt;
&lt;br /&gt;
== Contributors ==&lt;br /&gt;
* mjaz&lt;br /&gt;
* staalanden&lt;br /&gt;
* dannystaple&lt;br /&gt;
* Stilgar&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;br /&gt;
&lt;br /&gt;
[[Category:Tutorials]]&lt;br /&gt;
[[Category:Component Development]]&lt;/div&gt;</summary>
		<author><name>Nicholasjohn16</name></author>
	</entry>
</feed>