<?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=Hasse+bjork</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=Hasse+bjork"/>
	<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/Special:Contributions/Hasse_bjork"/>
	<updated>2026-05-28T15:36:17Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.43.0</generator>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=Sending_email_from_extensions&amp;diff=21338</id>
		<title>Sending email from extensions</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=Sending_email_from_extensions&amp;diff=21338"/>
		<updated>2010-01-21T13:32:43Z</updated>

		<summary type="html">&lt;p&gt;Hasse bjork: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This is an example of how to send an email from a component. You would typically put this into your components controller.&lt;br /&gt;
&lt;br /&gt;
=== Fetch the mail object ===&lt;br /&gt;
A reference to the global mail object (JMail) is fetched through the JFactory object. This is the object creating our mail.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$mailer =&amp;amp; JFactory::getMailer();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Set a sender ===&lt;br /&gt;
The sender of an email is set with [http://api.joomla.org/Joomla-Framework/Mail/JMail.html#setSender setSender]. The function takes an array with an email address and a name as an argument. We fetch the sites email address and name from the global configuration. These are set in the administration back-end (Global Configuration -&amp;gt; Server -&amp;gt; Mail Settings).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$config =&amp;amp; JFactory::getConfig();&lt;br /&gt;
$sender = array( &lt;br /&gt;
    $config-&amp;gt;getValue( &#039;config.mailfrom&#039; ),&lt;br /&gt;
    $config-&amp;gt;getValue( &#039;config.fromname&#039; ) );&lt;br /&gt;
&lt;br /&gt;
$mailer-&amp;gt;setSender($sender);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Recipient ===&lt;br /&gt;
You set the recipient of an email with the function [http://api.joomla.org/Joomla-Framework/Mail/JMail.html# addRecipient].&lt;br /&gt;
To set the email address to the currently logged in user, we fetch it from the user object.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$user =&amp;amp; JFactory::getUser();&lt;br /&gt;
$recipient = $user-&amp;gt;email;&lt;br /&gt;
&lt;br /&gt;
$mailer-&amp;gt;addRecipient($recipient);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we had multiple recipients we would put each recipients email address in an array.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$recipient = array( &#039;person1@domain.com&#039;, &#039;person2@domain.com&#039;, &#039;person3@domain.com&#039; );&lt;br /&gt;
&lt;br /&gt;
$mailer-&amp;gt;addRecipient($recipient);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Create the mail ===&lt;br /&gt;
We need to set a subject line and create the text body. The subject is set with [http://api.joomla.org/Joomla-Framework/Mail/JMail.html#setSubject setSubject]. &lt;br /&gt;
&lt;br /&gt;
The easy way to create an email body is as a string with plain text. Use the  function [http://api.joomla.org/Joomla-Framework/Mail/JMail.html#setBody setBody] to add a message to the mail body. You can also attach a file with [http://api.joomla.org/Joomla-Framework/Mail/JMail.html#addAttachment addAttachment]. It takes a single file name or an array of file names as argument.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$body   = &amp;quot;Your body string\nin double quotes if you want to parse the \nnewlines etc&amp;quot;;&lt;br /&gt;
$mailer-&amp;gt;setSubject(&#039;Your subject string&#039;);&lt;br /&gt;
$mailer-&amp;gt;setBody($body);&lt;br /&gt;
// Optional file attached&lt;br /&gt;
$mailer-&amp;gt;addAttachment(PATH_COMPONENT.DS.&#039;assets&#039;.DS.&#039;document.pdf&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you prefer to format your email in HTML, you need to tell the mailer it is HTML. This is done with [http://api.joomla.org/Unknown/PHPMailer.html#methodIsHTML IsHTML]. The subject line and any attachments are handled as above, with the exception of images embedded in the HTML. These are taken care of with the function [http://api.joomla.org/Unknown/PHPMailer.html#AddEmbeddedImage AddEmbeddedImage].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$body   = &#039;&amp;lt;h2&amp;gt;Our mail&amp;lt;/h2&amp;gt;&#039;&lt;br /&gt;
    . &#039;&amp;lt;div&amp;gt;A message to our dear readers&#039;&lt;br /&gt;
    . &#039;&amp;lt;img src=&amp;quot;cid:logo_id&amp;quot; alt=&amp;quot;logo&amp;quot;/&amp;gt;&amp;lt;/div&amp;gt;&#039;;&lt;br /&gt;
$mailer-&amp;gt;isHTML(true);&lt;br /&gt;
$mailer-&amp;gt;setBody($body);&lt;br /&gt;
// Optionally add embedded image&lt;br /&gt;
$mailer-&amp;gt;AddEmbeddedImage( PATH_COMPONENT.DS.&#039;assets&#039;.DS.&#039;logo128.jpg&#039;, &#039;logo_id&#039;, &#039;logo.jpg&#039;, &#039;base64&#039;, &#039;image/jpeg&#039; );&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Normally you would leave any images on your server and refer to them with an ordinary HTML image tag, to reduce size of the mail and the time sending it.&lt;br /&gt;
&lt;br /&gt;
=== Sending the mail ===&lt;br /&gt;
&lt;br /&gt;
The mail is sent with the function [http://api.joomla.org/Joomla-Framework/Mail/JMail.html#Send Send]. It returns &#039;&#039;&#039;true&#039;&#039;&#039; on success or a JError object.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$send =&amp;amp; $mailer-&amp;gt;Send();&lt;br /&gt;
if ( $send !== true ) {&lt;br /&gt;
    echo &#039;Error sending email: &#039; . $send-&amp;gt;message;&lt;br /&gt;
} else {&lt;br /&gt;
    echo &#039;Mail sent&#039;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You would probably want to write your own error handler, if there is an error sending the mail.&lt;br /&gt;
&lt;br /&gt;
The JMail object is used for sending mail in Joomlas contact manager. See the file joomla/components/com_contact/controller.php&lt;br /&gt;
&lt;br /&gt;
===See also===&lt;br /&gt;
* [http://docs.joomla.org/JFactory/getMailer Docs on JFactory-&amp;gt;getMailer]&lt;br /&gt;
* [http://api.joomla.org/Joomla-Framework/JFactory.html#getMailer JFactory-&amp;gt;getMailer on api.joomla.org]&lt;/div&gt;</summary>
		<author><name>Hasse bjork</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=Sending_email_from_extensions&amp;diff=21337</id>
		<title>Sending email from extensions</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=Sending_email_from_extensions&amp;diff=21337"/>
		<updated>2010-01-21T13:08:25Z</updated>

		<summary type="html">&lt;p&gt;Hasse bjork: Added attachements&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This is an example of how to send an email from a component. You would typically put this into your components controller.&lt;br /&gt;
&lt;br /&gt;
=== Fetch the mail object ===&lt;br /&gt;
A reference to the global mail object (JMail) is fetched through the JFactory object. This is the object creating our mail.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$mailer =&amp;amp; JFactory::getMailer();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Set a sender ===&lt;br /&gt;
The mailer expect the sender to be an array of an email address and a name. We fetch the sites email address and name from the configuration. These are set in the administration back-end (Global Configuration -&amp;gt; Server -&amp;gt; Mail Settings).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$config =&amp;amp; JFactory::getConfig();&lt;br /&gt;
$sender = array( &lt;br /&gt;
    $config-&amp;gt;getValue( &#039;config.mailfrom&#039; ),&lt;br /&gt;
    $config-&amp;gt;getValue( &#039;config.fromname&#039; ) );&lt;br /&gt;
&lt;br /&gt;
$mailer-&amp;gt;setSender($sender);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Recipient ===&lt;br /&gt;
The currently logged in users email address is stored in the user object.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$user =&amp;amp; JFactory::getUser();&lt;br /&gt;
$recipient = $user-&amp;gt;email;&lt;br /&gt;
&lt;br /&gt;
$mailer-&amp;gt;addRecipient($recipient);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we had multiple recipients we would put each recipients email in an array.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$recipient = array( &#039;person1@domain.com&#039;, &#039;person2@domain.com&#039;, &#039;person3@domain.com&#039; );&lt;br /&gt;
&lt;br /&gt;
$mailer-&amp;gt;addRecipient($recipient);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Create the mail ===&lt;br /&gt;
We need to set a subject line and create the text body. The subject is set with [http://api.joomla.org/Joomla-Framework/Mail/JMail.html#setSubject setSubject]. &lt;br /&gt;
&lt;br /&gt;
The easy way to create an email body is as a string with plain text. Use the  function [http://api.joomla.org/Joomla-Framework/Mail/JMail.html#setBody setBody] to add a message to the mail body. You can also attach a file with [http://api.joomla.org/Joomla-Framework/Mail/JMail.html#addAttachment addAttachment]. It takes a single file name or an array of file names as argument.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$body   = &amp;quot;Your body string\nin double quotes if you want to parse the \nnewlines etc&amp;quot;;&lt;br /&gt;
$mailer-&amp;gt;setSubject(&#039;Your subject string&#039;);&lt;br /&gt;
$mailer-&amp;gt;setBody($body);&lt;br /&gt;
// Optional file attached&lt;br /&gt;
$mailer-&amp;gt;addAttachment(PATH_COMPONENT.DS.&#039;assets&#039;.DS.&#039;document.pdf&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you prefer to format your email in HTML, you need to tell the mailer it is HTML. This is done with [http://api.joomla.org/Unknown/PHPMailer.html#methodIsHTML IsHTML]. The subject line and any attachments are handled as above, with the exception of images embedded in the HTML. These are taken care of with the function [http://api.joomla.org/Unknown/PHPMailer.html#AddEmbeddedImage AddEmbeddedImage].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$body   = &#039;&amp;lt;h2&amp;gt;Our mail&amp;lt;/h2&amp;gt;&#039;&lt;br /&gt;
    . &#039;&amp;lt;div&amp;gt;A message to our dear readers&#039;&lt;br /&gt;
    . &#039;&amp;lt;img src=&amp;quot;cid:logo_id&amp;quot; alt=&amp;quot;logo&amp;quot;/&amp;gt;&amp;lt;/div&amp;gt;&#039;;&lt;br /&gt;
$mailer-&amp;gt;isHTML(true);&lt;br /&gt;
$mailer-&amp;gt;setBody($body);&lt;br /&gt;
// Optionally add embedded image&lt;br /&gt;
$mailer-&amp;gt;AddEmbeddedImage( PATH_COMPONENT.DS.&#039;assets&#039;.DS.&#039;logo128.jpg&#039;, &#039;logo_id&#039;, &#039;logo.jpg&#039;, &#039;base64&#039;, &#039;image/jpeg&#039; );&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Normally you would leave any images on your server and refer to them with an ordinary HTML image tag, to reduce size of the mail and the time sending it.&lt;br /&gt;
&lt;br /&gt;
=== Sending the mail ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$send = $mailer-&amp;gt;Send();&lt;br /&gt;
if ( $send !== true ) {&lt;br /&gt;
    echo &#039;Error sending email: &#039; . $send-&amp;gt;message;&lt;br /&gt;
} else {&lt;br /&gt;
    echo &#039;Mail sent&#039;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You would probably want to write your own error handler, if there is an error sending the mail.&lt;br /&gt;
&lt;br /&gt;
The JMail object is used for sending mail in Joomlas contact manager. See the file joomla/components/com_contact/controller.php&lt;br /&gt;
&lt;br /&gt;
===See also===&lt;br /&gt;
* [http://docs.joomla.org/JFactory/getMailer Docs on JFactory-&amp;gt;getMailer]&lt;br /&gt;
* [http://api.joomla.org/Joomla-Framework/JFactory.html#getMailer JFactory-&amp;gt;getMailer on api.joomla.org]&lt;/div&gt;</summary>
		<author><name>Hasse bjork</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=Sending_email_from_extensions&amp;diff=21336</id>
		<title>Sending email from extensions</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=Sending_email_from_extensions&amp;diff=21336"/>
		<updated>2010-01-21T11:58:48Z</updated>

		<summary type="html">&lt;p&gt;Hasse bjork: Complete rewrite&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This is an example of how to send an email from a component. You would typically put this into your components controller.&lt;br /&gt;
&lt;br /&gt;
=== Fetch the mail object ===&lt;br /&gt;
A reference to the global mail object (JMail) is fetched through the JFactory object. This is the object creating our mail.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$mailer =&amp;amp; JFactory::getMailer();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Set a sender ===&lt;br /&gt;
The mailer expect the sender to be an array of an email address and a name. We fetch the sites email address and name from the configuration. These are set in the administration back-end (Global Configuration -&amp;gt; Server -&amp;gt; Mail Settings).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$config =&amp;amp; JFactory::getConfig();&lt;br /&gt;
$sender = array( &lt;br /&gt;
    $config-&amp;gt;getValue( &#039;config.mailfrom&#039; ),&lt;br /&gt;
    $config-&amp;gt;getValue( &#039;config.fromname&#039; ) );&lt;br /&gt;
&lt;br /&gt;
$mailer-&amp;gt;setSender($sender);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Recipient ===&lt;br /&gt;
The currently logged in users email address is stored in the user object.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$user =&amp;amp; JFactory::getUser();&lt;br /&gt;
$recipient = $user-&amp;gt;email;&lt;br /&gt;
&lt;br /&gt;
$mailer-&amp;gt;addRecipient($recipient);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If we had multiple recipients we would put each recipients email in an array.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$recipient = array( &#039;person1@domain.com&#039;, &#039;person2@domain.com&#039;, &#039;person3@domain.com&#039; );&lt;br /&gt;
&lt;br /&gt;
$mailer-&amp;gt;addRecipient($recipient);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Create the mail ===&lt;br /&gt;
We need to set a mail subject and create the body of the mail. The subject is easy to set using the mailer object&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$mailer-&amp;gt;setSubject(&#039;Your subject string&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The body is a text string of your choice set with the setBody function.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$body   = &amp;quot;Your body string\nin double quotes if you want to parse the \nnewlines etc&amp;quot;;&lt;br /&gt;
$mailer-&amp;gt;setBody($body);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you prefer an email formatted in HTML, you need to tell the mailer it is html.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$body   = &amp;quot;&amp;lt;h2&amp;gt;Our mail&amp;lt;/h2&amp;gt;&amp;lt;div&amp;gt;A message to our dear readers&amp;lt;/div&amp;gt;&amp;quot;;&lt;br /&gt;
$mailer-&amp;gt;isHTML(true);&lt;br /&gt;
$mailer-&amp;gt;setBody($body);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
=== Sending the mail ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$send = $mailer-&amp;gt;Send();&lt;br /&gt;
if ( $send !== true ) {&lt;br /&gt;
    echo &#039;Error sending email: &#039; . $send-&amp;gt;message;&lt;br /&gt;
} else {&lt;br /&gt;
    echo &#039;Mail sent&#039;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You would probably want to write your own error handler, if there is an error sending the mail.&lt;br /&gt;
&lt;br /&gt;
The JMail object is used for sending mail in Joomlas contact manager. See the file joomla/components/com_contact/controller.php&lt;br /&gt;
&lt;br /&gt;
===See also===&lt;br /&gt;
* [http://docs.joomla.org/JFactory/getMailer Docs on JFactory-&amp;gt;getMailer]&lt;br /&gt;
* [http://api.joomla.org/Joomla-Framework/JFactory.html#getMailer JFactory-&amp;gt;getMailer on api.joomla.org]&lt;/div&gt;</summary>
		<author><name>Hasse bjork</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=Sending_email_from_extensions&amp;diff=21335</id>
		<title>Sending email from extensions</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=Sending_email_from_extensions&amp;diff=21335"/>
		<updated>2010-01-21T10:28:37Z</updated>

		<summary type="html">&lt;p&gt;Hasse bjork: Added link to &amp;quot;Docs JFactory-&amp;gt;getMailer&amp;quot; and tagge source as php&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This works to send an email to the currently logged in user when used in a function in a model for a component and called from a view:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
   $user =&amp;amp; JFactory::getUser();&lt;br /&gt;
   $message =&amp;amp; JFactory::getMailer();&lt;br /&gt;
   $message-&amp;gt;addRecipient($user-&amp;gt;email);&lt;br /&gt;
   $message-&amp;gt;setSubject(&#039;Your subject string&#039;);&lt;br /&gt;
   $message-&amp;gt;setBody(&amp;quot;Your body string\nin double quotes if you want to parse the \nnewlines etc&amp;quot;);&lt;br /&gt;
   $sender = array( &#039;sender@email.address.org&#039;, &#039;Sender Name&#039; );&lt;br /&gt;
   $message-&amp;gt;setSender($sender);&lt;br /&gt;
   $sent = $message-&amp;gt;send();&lt;br /&gt;
   if ($sent != 1) echo &#039;Error sending email&#039;;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===See also===&lt;br /&gt;
* [http://docs.joomla.org/JFactory/getMailer Docs on JFactory-&amp;gt;getMailer]&lt;/div&gt;</summary>
		<author><name>Hasse bjork</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=Accessing_the_database_using_JDatabase&amp;diff=21257</id>
		<title>Accessing the database using JDatabase</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=Accessing_the_database_using_JDatabase&amp;diff=21257"/>
		<updated>2010-01-15T13:26:28Z</updated>

		<summary type="html">&lt;p&gt;Hasse bjork: Added &amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt; to code&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{ambox|type=merge|image=merge|text=It has been suggested that this page or section be merged with [[Tutorial:How to use the database classes in your script]]. ([[Talk:How to use the database classes in your script|Discuss here]])}}&lt;br /&gt;
Joomla provides a sophisticated database abstraction layer to simplify the usage for 3PD. This guide should help you using this layer.&lt;br /&gt;
&lt;br /&gt;
==Why should I use the Joomla database class?==&lt;br /&gt;
Joomla has been built with the ability to use several different kinds of SQL-database-systems and to run in a variety of environments with different table-prefixes. In addition to these functions, the class automatically creates the database connection. Besides instantiating the object, at a minimum, you only need 2 lines of code to get a result from the database in a variety of formats. Using the Joomla database layer ensures a maximum of compatibility and flexibility for your extension.&lt;br /&gt;
&lt;br /&gt;
==Preparing the query==&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
// Get a database object&lt;br /&gt;
$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
&lt;br /&gt;
$query = &amp;quot;SELECT * FROM #__example_table WHERE id = 999999;&amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
First we instantiate the database object, then we prepare the query. You can use the normal SQL-syntax, the only thing you have to change is the table-prefix. To make this as flexible as possible, Joomla uses a placeholder for the prefix, the &amp;amp;ldquo;#__&amp;amp;rdquo;. In the next step, the $db-&amp;gt;setQuery(), this string is replaced with the correct prefix.&lt;br /&gt;
&lt;br /&gt;
Now, if we don&#039;t want to get information from the database, but insert a row into it, we need one more function. Every string-value in the SQL-syntax should be quoted. For example, MySQL uses backticks &amp;amp;#096;&amp;amp;#096; for names and single quotes &amp;amp;lsquo;&amp;amp;lsquo; for values. Joomla has some functions to do this for us and to ensure code compatibility between different databases. We can pass the names to the function $db-&amp;gt;nameQuote($name) and the values to the function $db-&amp;gt;Quote($value). &lt;br /&gt;
&lt;br /&gt;
A fully quoted query example is:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT * &lt;br /&gt;
    FROM &amp;quot;.$db-&amp;gt;nameQuote(&#039;#__example_table&#039;).&amp;quot;  &lt;br /&gt;
    WHERE &amp;quot;.$db-&amp;gt;nameQuote(&#039;id&#039;).&amp;quot; = &amp;quot;.$db-&amp;gt;quote(&#039;999999&#039;).&amp;quot;;&lt;br /&gt;
  &amp;quot;;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Whatever we want to do, we have to set the query with the $db-&amp;gt;setQuery() function. Although you could write the query directly as a parameter for $db-&amp;gt;setQuery(), it&#039;s commonly done by first saving it in a variable, normally $query, and then handing this variable over. This helps writing clean, readable code.&lt;br /&gt;
&lt;br /&gt;
==== setQuery($query) ====&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;setQuery($query)&#039;&#039; method sets up a database query for later execution either by the query() method or one of the Load result methods.  &amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
$query = &amp;quot;/* some valid sql string */&amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
The parameter $query must be a valid sql string, it can either be added as a string parameter or as a variable; generally a variable is preferred as it leads to more legible code and can help in debugging.&lt;br /&gt;
&lt;br /&gt;
setQuery() also takes three other parameters: $offset, $limit - both used in list pagination; and $prefix - an alternative table prefix. All three of these variables have default values set and can usually be ignored.&lt;br /&gt;
&lt;br /&gt;
==Executing the Query==&lt;br /&gt;
To execute the query, Joomla provides several functions, which differ in their return value.&lt;br /&gt;
&lt;br /&gt;
=== Basic Query Execution ===&lt;br /&gt;
&lt;br /&gt;
==== query() ====&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;query()&#039;&#039; method is the the basic tool for executing sql queries on a database. In Joomla it is most often used for updating or administering the database simple because the various load methods detail on this page have the query step built in to them.&lt;br /&gt;
&lt;br /&gt;
The syntax is very straightforward:&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
$query = &amp;quot;/* some valid sql string */&amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;query();&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: $query() returns an appropriate database resource if successful, or FALSE if not&lt;br /&gt;
&lt;br /&gt;
=== Query Execution Information ===&lt;br /&gt;
* getAffectedRows()&lt;br /&gt;
* explain()&lt;br /&gt;
* insertid()&lt;br /&gt;
&lt;br /&gt;
=== Insert Query Execution ===&lt;br /&gt;
* insertObject()&lt;br /&gt;
&lt;br /&gt;
==Query Results ==&lt;br /&gt;
The database class contains many methods for working with a query&#039;s result set.&lt;br /&gt;
&lt;br /&gt;
=== Single Value Result ===&lt;br /&gt;
&lt;br /&gt;
==== loadResult() ====&lt;br /&gt;
&lt;br /&gt;
Use &#039;&#039;&#039;loadResult()&#039;&#039;&#039; when you expect just a single value back from your database query. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- &lt;br /&gt;
| 1 || style=&amp;quot;background:yellow&amp;quot; | John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|-&lt;br /&gt;
| 2 || Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|-&lt;br /&gt;
| 3 || Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This is often the result of a &#039;count&#039; query to get a number of records:&lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT COUNT(*)&lt;br /&gt;
    FROM &amp;quot;.$db-&amp;gt;nameQuote(&#039;#__my_table&#039;).&amp;quot;&lt;br /&gt;
    WHERE &amp;quot;.$db-&amp;gt;nameQuote(&#039;name&#039;).&amp;quot; = &amp;quot;.$db-&amp;gt;quote($value).&amp;quot;;&lt;br /&gt;
  &amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$count = $db-&amp;gt;loadResult();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
or where you are just looking for a single field from a single row of the table (or possibly a single field from the first row returned).&lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT &amp;quot;.$db-&amp;gt;nameQuote(&#039;field_name&#039;).&amp;quot;&lt;br /&gt;
    FROM &amp;quot;.$db-&amp;gt;nameQuote(&#039;#__my_table&#039;).&amp;quot;&lt;br /&gt;
    WHERE &amp;quot;.$db-&amp;gt;nameQuote(&#039;some_name&#039;).&amp;quot; = &amp;quot;.$db-&amp;gt;quote($some_value).&amp;quot;;&lt;br /&gt;
  &amp;quot;;&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;loadResult();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Single Row Results ===&lt;br /&gt;
&lt;br /&gt;
Each of these results functions will return a single record from the database even though there may be several records that meet the criteria that you have set. To get more records you need to call the function again.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 1 || John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|-&lt;br /&gt;
| 2 || Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|-&lt;br /&gt;
| 3 || Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== loadRow() ====&lt;br /&gt;
&lt;br /&gt;
loadRow() returns an indexed array from a single record in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadRow();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [0] =&amp;gt; 1 [1] =&amp;gt; John Smith [2] =&amp;gt; johnsmith@example.com [3] =&amp;gt; johnsmith ) &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
# Whilst you can repeat the call to get further rows, one of the functions that returns multiple rows might be more useful&lt;br /&gt;
&lt;br /&gt;
==== loadAssoc() ====&lt;br /&gt;
&lt;br /&gt;
loadAssoc() returns an associated array from a single record in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadAssoc();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;name&#039;] // e.g. $row[&#039;name&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# Whilst you can repeat the call to get further rows, one of the functions that returns multiple rows might be more useful&lt;br /&gt;
&lt;br /&gt;
==== loadObject() ====&lt;br /&gt;
&lt;br /&gt;
loadObject returns a PHP object from a single record in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;loadObject();&lt;br /&gt;
print_r($result);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;stdClass Object ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$row-&amp;gt;index // e.g. $row-&amp;gt;email&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# Whilst you can repeat the call to get further rows, one of the functions that returns multiple rows might be more useful&lt;br /&gt;
&lt;br /&gt;
===Single Column Results ===&lt;br /&gt;
&lt;br /&gt;
Each of these results functions will return a single column from the database. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- &lt;br /&gt;
| 1 || style=&amp;quot;background:yellow&amp;quot; | John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|-&lt;br /&gt;
| 2 || style=&amp;quot;background:yellow&amp;quot; | Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|-&lt;br /&gt;
| 3 || style=&amp;quot;background:yellow&amp;quot; | Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== loadResultArray() ====&lt;br /&gt;
&lt;br /&gt;
loadResultArray() returns an indexed array from a single column in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT name, email, username&lt;br /&gt;
    FROM . . . &amp;quot;;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$column= $db-&amp;gt;loadResultArray();&lt;br /&gt;
print_r($column);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [0] =&amp;gt; John Smith [1] =&amp;gt; Magda Hellman [2] =&amp;gt; Yvonne de Gaulle )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$column[&#039;index&#039;] // e.g. $column[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
# loadResultArray() is equivalent to loadResultArray(0)&lt;br /&gt;
&lt;br /&gt;
==== loadResultArray($index) ====&lt;br /&gt;
&lt;br /&gt;
loadResultArray($index) returns an indexed array from a single column in the table: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
$query = &amp;quot;&lt;br /&gt;
  SELECT name, email, username&lt;br /&gt;
    FROM . . . &amp;quot;;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$column= $db-&amp;gt;loadResultArray(1);&lt;br /&gt;
print_r($column);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [0] =&amp;gt; johnsmith@example.com [1] =&amp;gt; magda_h@example.com [2] =&amp;gt; ydg@example.com )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual values by using:&amp;lt;pre&amp;gt;$column[&#039;index&#039;] // e.g. $column[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
loadResultArray($index) allows you to iterate through a series of columns in the results&lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
for ( $i = 0; $i &amp;lt;= 2; $i++ ) {&lt;br /&gt;
  $column= $db-&amp;gt;loadResultArray($i);&lt;br /&gt;
  print_r($column);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give:&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( [0] =&amp;gt; John Smith [1] =&amp;gt; Magda Hellman [2] =&amp;gt; Yvonne de Gaulle )&lt;br /&gt;
Array ( [0] =&amp;gt; johnsmith@example.com [1] =&amp;gt; magda_h@example.com [2] =&amp;gt; ydg@example.com )&lt;br /&gt;
Array ( [0] =&amp;gt; johnsmith [1] =&amp;gt; magdah [2] =&amp;gt; ydegaulle )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
&lt;br /&gt;
=== Multi-Row Results ===&lt;br /&gt;
&lt;br /&gt;
Each of these results functions will return multiple records from the database. &lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;text-align:center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! id !! name !! email !! username&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 1 || John Smith || johnsmith@example.com || johnsmith&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 2 || Magda Hellman || magda_h@example.com || magdah&lt;br /&gt;
|- style=&amp;quot;background:yellow&amp;quot;&lt;br /&gt;
| 3 || Yvonne de Gaulle || ydg@example.com || ydegaulle&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== loadRowList() ====&lt;br /&gt;
&lt;br /&gt;
loadRowList() returns an indexed array of indexed arrays from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadRowList();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[0] =&amp;gt; Array ( [0] =&amp;gt; 1 [1] =&amp;gt; John Smith [2] =&amp;gt; johnsmith@example.com [3] =&amp;gt; johnsmith ) &lt;br /&gt;
[1] =&amp;gt; Array ( [0] =&amp;gt; 2 [1] =&amp;gt; Magda Hellman [2] =&amp;gt; magda_h@example.com [3] =&amp;gt; magdah ) &lt;br /&gt;
[2] =&amp;gt; Array ( [0] =&amp;gt; 3 [1] =&amp;gt; Yvonne de Gaulle [2] =&amp;gt; ydg@example.com [3] =&amp;gt; ydegaulle ) &lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;][&#039;index&#039;] // e.g. $row[&#039;2&#039;][&#039;3&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
# The array indices are numeric starting from zero.&lt;br /&gt;
&lt;br /&gt;
==== loadAssocList() ====&lt;br /&gt;
&lt;br /&gt;
loadAssocList() returns an indexed array of associated arrays from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadAssocList();&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[0] =&amp;gt; Array ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith ) &lt;br /&gt;
[1] =&amp;gt; Array ( [id] =&amp;gt; 2 [name] =&amp;gt; Magda Hellman [email] =&amp;gt; magda_h@example.com [username] =&amp;gt; magdah ) &lt;br /&gt;
[2] =&amp;gt; Array ( [id] =&amp;gt; 3 [name] =&amp;gt; Yvonne de Gaulle [email] =&amp;gt; ydg@example.com [username] =&amp;gt; ydegaulle ) &lt;br /&gt;
) &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;][&#039;column_name&#039;] // e.g. $row[&#039;2&#039;][&#039;email&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== loadAssocList($key) ====&lt;br /&gt;
&lt;br /&gt;
loadAssocList(&#039;key&#039;) returns an associated array - indexed on &#039;key&#039; - of associated arrays from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadAssocList(&#039;username&#039;);&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[johnsmith] =&amp;gt; Array ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith ) &lt;br /&gt;
[magdah] =&amp;gt; Array ( [id] =&amp;gt; 2 [name] =&amp;gt; Magda Hellman [email] =&amp;gt; magda_h@example.com [username] =&amp;gt; magdah ) &lt;br /&gt;
[ydegaulle] =&amp;gt; Array ( [id] =&amp;gt; 3 [name] =&amp;gt; Yvonne de Gaulle [email] =&amp;gt; ydg@example.com [username] =&amp;gt; ydegaulle ) &lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;key_value&#039;] // e.g. $row[&#039;johnsmith&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;key_value&#039;][&#039;column_name&#039;] // e.g. $row[&#039;johnsmith&#039;][&#039;email&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: Key must be a valid column name from the table; it does not have to be an Index or a Primary Key. But if it does not have a unique value you may not be able to retrieve results reliably.&lt;br /&gt;
&lt;br /&gt;
==== loadObjectList() ====&lt;br /&gt;
&lt;br /&gt;
loadObjectList() returns an indexed array of PHP objects from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$result = $db-&amp;gt;loadObjectList();&lt;br /&gt;
print_r($result);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[0] =&amp;gt; stdClass Object ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith &lt;br /&gt;
    [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith ) &lt;br /&gt;
[1] =&amp;gt; stdClass Object ( [id] =&amp;gt; 2 [name] =&amp;gt; Magda Hellman &lt;br /&gt;
    [email] =&amp;gt; magda_h@example.com [username] =&amp;gt; magdah ) &lt;br /&gt;
[2] =&amp;gt; stdClass Object ( [id] =&amp;gt; 3 [name] =&amp;gt; Yvonne de Gaulle &lt;br /&gt;
    [email] =&amp;gt; ydg@example.com [username] =&amp;gt; ydegaulle ) &lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;] // e.g. $row[&#039;2&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;index&#039;]-&amp;gt;name // e.g. $row[&#039;2&#039;]-&amp;gt;email&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== loadObjectList(&#039;key&#039;) ====&lt;br /&gt;
&lt;br /&gt;
loadObjectList($key) returns an associated array - indexed on &#039;key&#039; - of objects from the table records returned by the query: &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$row = $db-&amp;gt;loadObjectList(&#039;username&#039;);&lt;br /&gt;
print_r($row);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will give (with line breaks added for clarity):&lt;br /&gt;
&amp;lt;pre&amp;gt;Array ( &lt;br /&gt;
[johnsmith] =&amp;gt; stdClass Object ( [id] =&amp;gt; 1 [name] =&amp;gt; John Smith &lt;br /&gt;
    [email] =&amp;gt; johnsmith@example.com [username] =&amp;gt; johnsmith ) &lt;br /&gt;
[magdah] =&amp;gt; stdClass Object ( [id] =&amp;gt; 2 [name] =&amp;gt; Magda Hellman &lt;br /&gt;
    [email] =&amp;gt; magda_h@example.com [username] =&amp;gt; magdah ) &lt;br /&gt;
[ydegaulle] =&amp;gt; stdClass Object ( [id] =&amp;gt; 3 [name] =&amp;gt; Yvonne de Gaulle &lt;br /&gt;
    [email] =&amp;gt; ydg@example.com [username] =&amp;gt; ydegaulle ) &lt;br /&gt;
)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the individual rows by using:&amp;lt;pre&amp;gt;$row[&#039;key_value&#039;] // e.g. $row[&#039;johnsmith&#039;]&amp;lt;/pre&amp;gt;&lt;br /&gt;
and you can access the individual values by using:&amp;lt;pre&amp;gt;$row[&#039;key_value&#039;]-&amp;gt;column_name // e.g. $row[&#039;johnsmith&#039;]-&amp;gt;email&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: Key must be a valid column name from the table; it does not have to be an Index or a Primary Key. But if it does not have a unique value you may not be able to retrieve results reliably.&lt;br /&gt;
&lt;br /&gt;
=== Misc Result Set Methods ===&lt;br /&gt;
&lt;br /&gt;
==== getNumRows() ====&lt;br /&gt;
&lt;br /&gt;
getNumRows() will return the number of result rows found by the last query and waiting to be read. To get a result from getNumRows() you have to run it &#039;&#039;&#039;after&#039;&#039;&#039; the query and &#039;&#039;&#039;before&#039;&#039;&#039; you have retrieved any results.  &lt;br /&gt;
&amp;lt;source lang=&#039;php&#039;&amp;gt;&lt;br /&gt;
. . .&lt;br /&gt;
$db-&amp;gt;setQuery($query);&lt;br /&gt;
$db-&amp;gt;query();&lt;br /&gt;
$num_rows = $db-&amp;gt;getNumRows();&lt;br /&gt;
print_r($num_rows);&lt;br /&gt;
$result = $db-&amp;gt;loadRowList();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
will return &amp;lt;pre&amp;gt;3&amp;lt;/pre&amp;gt;&lt;br /&gt;
Note: if you run getNumRows() after loadRowList() - or any other retrieval method - you may get a PHP Warning:&lt;br /&gt;
&amp;lt;pre&amp;gt;Warning: mysql_num_rows(): 80 is not a valid MySQL result resource &lt;br /&gt;
in D:\xampp\htdocs\joomla1.5a\libraries\joomla\database\database\mysql.php on line 344&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Tips, Tricks &amp;amp; FAQ==&lt;br /&gt;
===Subqueries===&lt;br /&gt;
We had a few people lately using sub-queries like these:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;SQL&amp;quot;&amp;gt;&lt;br /&gt;
SELECT * FROM #__example WHERE id IN (SELECT id FROM #__example2);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
These kind of queries are only possible in MySQL 4.1 and above. Another way to achieve this, is splitting the query into two:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$query = &amp;quot;SELECT id FROM #__example2&amp;quot;;&lt;br /&gt;
$database-&amp;gt;setQuery($query);&lt;br /&gt;
$query = &amp;quot;SELECT * FROM #__example WHERE id IN (&amp;quot;. implode(&amp;quot;,&amp;quot;, $database-&amp;gt;loadResultArray()) .&amp;quot;)&amp;quot;;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
===Developer-friendly tip===&lt;br /&gt;
Here is a quick way to do 4 developer-friendly things at once:&lt;br /&gt;
* Use a simple constant as SQL-seperator (which can probably be used in many queries)&lt;br /&gt;
* Make your SQL-in-php code easy to read (for yourself and possibly other developers later on)&lt;br /&gt;
* Give an error inside your (component-) content without really setting debugging on&lt;br /&gt;
* Have a visibly nice SQL with splitting SQL-groups with linebreaks in your error&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$db =&amp;amp; JFactory::getDBO();&lt;br /&gt;
$jAp=&amp;amp; JFactory::getApplication();&lt;br /&gt;
    //We define a linebreak constant&lt;br /&gt;
define(&#039;L&#039;, chr(10));&lt;br /&gt;
    //Here is the most magic&lt;br /&gt;
$db-&amp;gt;setQuery(&lt;br /&gt;
	&#039;SELECT * FROM #__table&#039;.L.&lt;br /&gt;
	&#039;WHERE something=&amp;quot;something else&amp;quot;)&#039;.L.&lt;br /&gt;
	&#039;ORDER BY date desc&#039;&lt;br /&gt;
); &lt;br /&gt;
$db-&amp;gt;query();&lt;br /&gt;
    //display and convert to HTML when SQL error&lt;br /&gt;
if (is_null($posts=$db-&amp;gt;loadRowList())) {$jAp-&amp;gt;enqueueMessage(nl2br($db-&amp;gt;getErrorMsg()),&#039;error&#039;); return;} &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;noinclude&amp;gt;[[Category:Development]][[Category:Database]]&amp;lt;/noinclude&amp;gt;&lt;/div&gt;</summary>
		<author><name>Hasse bjork</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J1.5:Ajax_client_code_using_MooTools&amp;diff=21224</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=21224"/>
		<updated>2010-01-13T09:07:12Z</updated>

		<summary type="html">&lt;p&gt;Hasse bjork: Removed redundant curly bracket in javascript code&lt;/p&gt;
&lt;hr /&gt;
&lt;div&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;
/* &amp;lt;![CDATA[ */&lt;br /&gt;
Your JavaScript code goes here.&lt;br /&gt;
/* ]]&amp;gt; */&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;
Wrapping the JavaScript code between &amp;lt;tt&amp;gt;/* &amp;lt;![CDATA[ */&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;/* ]]&amp;gt; */&amp;lt;/tt&amp;gt; allows the JavaScript to be output without causing problems with HTML validators.&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;
/* &amp;lt;![CDATA[ */&lt;br /&gt;
This is some JavaScript code with {$this-&amp;gt;embedded} PHP variable in it.&lt;br /&gt;
/* ]]&amp;gt; */&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;
&lt;br /&gt;
				$(&#039;ajax-container&#039;).removeClass(&#039;ajax-loading&#039;).setHTML( output );&lt;br /&gt;
&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]]&amp;lt;/noinclude&amp;gt;&lt;/div&gt;</summary>
		<author><name>Hasse bjork</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=J1.5:Component_parameters&amp;diff=20830</id>
		<title>J1.5:Component parameters</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=J1.5:Component_parameters&amp;diff=20830"/>
		<updated>2009-12-18T14:17:55Z</updated>

		<summary type="html">&lt;p&gt;Hasse bjork: /* 3. &amp;#039;Article&amp;#039; specific parameters */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Background==&lt;br /&gt;
One thing that sets Joomla! Components apart from other types of Extension (i.e. Modules and Plugins) is the ability to store significant quantities of data in the Joomla! MySQL database.  Typically, this data will be content for displaying on a page (such as the content of an article, or its title) or data about how that content should be displayed (e.g. a setting specifying whether or not the title should be shown).  A Component normally creates at least one table in the database to store this data - for example, data for articles associated with the &amp;lt;code&amp;gt;com_content&amp;lt;/code&amp;gt; Component are stored in the &amp;lt;code&amp;gt;jos_content&amp;lt;/code&amp;gt; table (where &#039;&#039;jos&#039;&#039; is the table prefix for the site).&lt;br /&gt;
&lt;br /&gt;
However, choosing exactly how to store each item of data requires a little more thought.  There are essentially two choices:  each data item may be stored in its own field, or a collection of data items may be stored within a single parameters field.  (Of course, there is nothing to stop you making use of both methods, for different data items, within the same component).  Which approach you use for a given data item is largely a matter of personal choice, although there are some factors to consider:&lt;br /&gt;
* Core code included in the Joomla! installation makes it very easy to produce the appropriate form elements for entering data into &#039;&#039;&#039;parameters&#039;&#039;&#039; in the Back-end.  If you choose to save the data in separate fields, you will need to code the appropriate form elements yourself.&lt;br /&gt;
* Again, Joomla! core code allows you to set default values for &#039;&#039;&#039;parameters&#039;&#039;&#039; across the whole component.  Individual component items (e.g. individual articles) can then choose to use the default values, or to override them with values specific to that item.  See below for details of how to do this.&lt;br /&gt;
* It is not possible to extract individual parameter values directly from the database.  Instead, the whole parameters field must be extracted and parsed to obtain the different parameter values.  This means that it is not very easy, for example, to run MySQL queries that search for a parameter with a particular value.  Any data that you want to run queries against should be given its &#039;&#039;&#039;own table field&#039;&#039;&#039;.&lt;br /&gt;
* Similarly, it is not possible to run data validation on parameter fields before saving data.&lt;br /&gt;
In general, parameters are conventionally used to store data relating to the &#039;&#039;presentation&#039;&#039; of information.  The information itself is generally stored in separate fields.&lt;br /&gt;
&lt;br /&gt;
==Types of Component parameter==&lt;br /&gt;
&lt;br /&gt;
===1. Component-wide default parameters===&lt;br /&gt;
&lt;br /&gt;
These parameters are set once for the whole Component, and are generally used to provide the &#039;default&#039; parameter settings for the Component on that particular site.  &lt;br /&gt;
&lt;br /&gt;
===2. Menu item specific parameters===&lt;br /&gt;
&lt;br /&gt;
A menu link to an individual Component item (e.g. an article) can also have parameters associated with it, which can affect the way the Component item is displayed &#039;&#039;using that link&#039;&#039;.  The types of parameters available will depend on the way that the Component item is displayed (the &#039;view&#039; in MVC terms, but don&#039;t worry if this doesn&#039;t mean anything to you!).  However, the precise values of those parameters can be set for each menu link individually.  If there is another menu link to that same Component item, then it is important to realise each menu link may have different parameter values, and so the same article may be displayed differently in the two cases.&lt;br /&gt;
&lt;br /&gt;
A menu link can also override the Component-wide default parameters, so that the override values (if set) are used in place of the default values &#039;&#039;for that Component item when accessed via that menu link&#039;&#039;.  As you would expect, when no override value is set, the default value is used instead, in the normal manner.&lt;br /&gt;
&lt;br /&gt;
===3. &#039;Article&#039; specific parameters===&lt;br /&gt;
&lt;br /&gt;
Finally, each Component item (e.g. article) can have parameters which apply to that item, regardless of how it is accessed (i.e. via any available menu link, or even without reference to a menu at all!).  If the Component designer wants, the article specific parameters can also be set up to use the &#039;override&#039; behaviour, so that they default to the current values of the corresponding Component-wide parameters, though this is not essential.  Again, this is described further below.&lt;br /&gt;
&lt;br /&gt;
==Storing the parameters and their values.==&lt;br /&gt;
&lt;br /&gt;
In general, a set of parameters is stored within the database in a single &amp;lt;code&amp;gt;text&amp;lt;/code&amp;gt; field.  Different parameters are separated by &amp;lt;code&amp;gt;newline&amp;lt;/code&amp;gt; characters (&#039;&amp;lt;code&amp;gt;\n&amp;lt;/code&amp;gt;&#039; in MySQL - each parameter will appear on a different line when you look at the data in something like phpMyAdmin).  Each parameter consists of the parameter name and value, separated by an equals sign (=).  As an example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;text&amp;quot;&amp;gt;&lt;br /&gt;
parameter1=parameter1value&lt;br /&gt;
parameter2=&lt;br /&gt;
parameter3=parameter3value&lt;br /&gt;
parameter4=parameter4value&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here, parameter1, parameter3, and parameter4 have all been assigned values, whilst parameter2 has no value.  Typically, a parameter will be given no value where it is set up to allow override of a default value, but &#039;no override is required&#039; - i.e. it is desired to use the default value.&lt;br /&gt;
&lt;br /&gt;
===1. Component-wide default parameters===&lt;br /&gt;
&lt;br /&gt;
These are stored in the &amp;lt;code&amp;gt;params&amp;lt;/code&amp;gt; field of the jos_components table, in the row corresponding to the particular component to which they apply.&lt;br /&gt;
&lt;br /&gt;
===2. Menu item specific parameters===&lt;br /&gt;
&lt;br /&gt;
These are stored in the &amp;lt;code&amp;gt;params&amp;lt;/code&amp;gt; field of the jos_menu table, in the row corresponding to the particular menu item to which they apply.&lt;br /&gt;
&lt;br /&gt;
===3. &#039;Article&#039; specific parameters===&lt;br /&gt;
&lt;br /&gt;
These are stored in a text field in the table created by the Component.  Whilst there is no strict rule for naming the field, it is common to extend the above pattern using the &amp;lt;code&amp;gt;params&amp;lt;/code&amp;gt; name.  However, com_content stores article parameters in a field called &amp;lt;code&amp;gt;attribs&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Accessing the parameters - Back-end==&lt;br /&gt;
&lt;br /&gt;
Joomla! includes some shortcuts to enable component creators to easily add parameter forms to the Back-end of their component.  These forms allow a site administrator to enter a parameter value by, for example, entering some text in a box, choosing from a drop-down list, or selecting a radio button.  To create these forms, each parameter needs to have an associated &amp;lt;code&amp;gt;&amp;lt;param /&amp;gt;&amp;lt;/code&amp;gt; object in an XML file.  The type of &amp;lt;code&amp;gt;&amp;lt;nowiki&amp;gt;&amp;lt;param /&amp;gt;&amp;lt;/nowiki&amp;gt;&amp;lt;/code&amp;gt; object determines the form elements that will be shown in the Back-end.  A list of the available form elements is given [[Standard_parameter_types|here]].  More information is given below about the XML file names, structure and location required for each set of parameters.&lt;br /&gt;
&lt;br /&gt;
===1. Component-wide default parameters===&lt;br /&gt;
&lt;br /&gt;
These can be created at installation of the Component, by including &amp;lt;code&amp;gt;&amp;lt;param /&amp;gt;&amp;lt;/code&amp;gt; elements in the installation XML file (typically called COMPONENT_NAME.xml, and located in the root folder of the Component package.  As far as the parameters are concerned, this XML file has the following structure:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;install&amp;gt;&lt;br /&gt;
  ...&lt;br /&gt;
  &amp;lt;params&amp;gt;&lt;br /&gt;
    &amp;lt;param /&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
    &amp;lt;!-- Example: the following will create a line in the parameters field &lt;br /&gt;
    reading &#039;font_size=16\n&#039;: --&amp;gt;&lt;br /&gt;
    &amp;lt;param name=&amp;quot;font_size&amp;quot; default=&amp;quot;16&amp;quot; /&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
    ...&lt;br /&gt;
  &amp;lt;/params&amp;gt;&lt;br /&gt;
  &amp;lt;url addpath=&amp;quot;....&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;param /&amp;gt;&lt;br /&gt;
  &amp;lt;/url&amp;gt;&lt;br /&gt;
  ...&lt;br /&gt;
&amp;lt;/install&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Each &amp;lt;code&amp;gt;&amp;lt;param /&amp;gt;&amp;lt;/code&amp;gt; element must be given the attributes &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;default&amp;lt;/code&amp;gt;, which define the parameter name and value respectively.  These values are then copied to the jos_components table in the database.&lt;br /&gt;
&lt;br /&gt;
However, on its own this is not very useful, as it doesn&#039;t allow users to change the values of the parameters.  To do this, parameter form elements need to be defined in a file named &amp;lt;code&amp;gt;config.xml&amp;lt;/code&amp;gt; which is located in &amp;lt;code&amp;gt;/PATH_TO_JOOMLA/administrator/components/com_COMPONENT_NAME/&amp;lt;/code&amp;gt;.  The file should have the following structure:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;root&amp;gt;&lt;br /&gt;
  &amp;lt;params&amp;gt;&lt;br /&gt;
    &amp;lt;url addpath=&amp;quot;/administrator/components/com_COMPONENT_NAME/elements&amp;quot;&amp;gt;&lt;br /&gt;
      &amp;lt;param name=&amp;quot;cid&amp;quot; type=&amp;quot;JELEMENT_TYPE_NAME&amp;quot; scope=&amp;quot;com_COMPONENT_NAME&amp;quot; default=&amp;quot;0&amp;quot; label=&amp;quot;LABEL&amp;quot; description=&amp;quot;DESCRIPTION&amp;quot; /&amp;gt;&lt;br /&gt;
    &amp;lt;/url&amp;gt;&lt;br /&gt;
    &amp;lt;param type=&amp;quot;type&amp;quot; name=&amp;quot;name&amp;quot; description=&amp;quot;description&amp;quot; /&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
    &amp;lt;!-- Example: the following will produce a text box 3 characters wide with the label &#039;Font size:&#039;.  &lt;br /&gt;
    When the user hovers the mouse over the label, a tooltip will pop up which states &#039;Please enter the &lt;br /&gt;
    required font size&#039;.  On saving, a value entered into the text box will be saved as the &#039;font_size&#039; &lt;br /&gt;
    parameter value. --&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
    &amp;lt;param type=&amp;quot;text&amp;quot; name=&amp;quot;font_size&amp;quot; size=&amp;quot;3&amp;quot; label=&amp;quot;Font size:&amp;quot; description=&amp;quot;Please enter the required font size&amp;quot; /&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
    ...&lt;br /&gt;
  &amp;lt;/params&amp;gt;&lt;br /&gt;
&amp;lt;/root&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Note that the &amp;lt;code&amp;gt;&amp;lt;root&amp;gt;&amp;lt;/code&amp;gt; element can have any name you wish, but there must be one element at the root of the document (this is a requirement for a well-formed XML document). You can have more than one &amp;lt;code&amp;gt;&amp;lt;params&amp;gt;&amp;lt;/code&amp;gt; element, but only the first one will be considered.  It is not currently possible to group certain parameter form elements together.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;&amp;lt;url&amp;gt;&amp;lt;/code&amp;gt; section contains parameters which are added to the URL of the menu item. To enable this functionality, the &amp;lt;code&amp;gt;type&amp;lt;/code&amp;gt; attribute of the &amp;lt;code&amp;gt;&amp;lt;param&amp;gt;&amp;lt;/code&amp;gt; elements must specify the name of a class which extends JElement, and is able to generate HTML forms to display and set the parameter. You can use one of the [[Standard parameter types]]. Or, you can create your own subclass of JElement. Put the class definition file in your component&#039;s &amp;lt;code&amp;gt;/administrator/components/com_COMPONENT_NAME/&amp;lt;/code&amp;gt; directory, and refer to this path in the &amp;lt;code&amp;gt;addpath&amp;lt;/code&amp;gt; attribute of the &amp;lt;code&amp;gt;&amp;lt;URL&amp;gt;&amp;lt;/code&amp;gt; element. More information here: http://forum.joomla.org/viewtopic.php?f=476&amp;amp;t=206062.&lt;br /&gt;
&lt;br /&gt;
So that administrators to access these parameters, you will need to add a button to the toolbar in the Back-end of your component.  This can be done using the code:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
JToolBarHelper::preferences( &#039;com_COMPONENT_NAME&#039; );&lt;br /&gt;
// e.g. for the core Content component, you would use JToolBarHelper::preferences( &#039;com_content&#039; );&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
which is typically placed in the PHP file responsible for the Back-end output.  (For example, using an MVC structure, you might place this code at the top of &amp;lt;code&amp;gt;/PATH_TO_JOOMLA/administrator/components/com_COMPONENT_NAME/views/VIEW_NAME/tmpl/default.php&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
This adds a &amp;lt;code&amp;gt;Parameters&amp;lt;/code&amp;gt; button to the toolbar:&lt;br /&gt;
&lt;br /&gt;
[[Image:Toolbar_newsfeed.png]]&lt;br /&gt;
&lt;br /&gt;
Clicking on the toolbar button produces a pop-up window containing the form fields for each parameter defined in &amp;lt;code&amp;gt;config.xml&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Note that there is no need to define &amp;lt;code&amp;gt;default&amp;lt;/code&amp;gt; attributes for the &amp;lt;code&amp;gt;&amp;lt;param /&amp;gt;&amp;lt;/code&amp;gt; elements in the &amp;lt;code&amp;gt;config.xml&amp;lt;/code&amp;gt; file, since the default values will already have been saved in the installation of the Component.&lt;br /&gt;
&lt;br /&gt;
===2. Menu item specific parameters===&lt;br /&gt;
&lt;br /&gt;
A menu item is associated with a particular view from your Component&#039;s Front-end - following the menu link in the Front-end will display that view.  Joomla! 1.5 allows you to define parameters for a particular view, so that a copy of those parameters can be associated with each menu item using the view.  In addition, you can also choose to override the Component-wide default parameters defined as above, so that the override values will apply to the particular menu item.&lt;br /&gt;
&lt;br /&gt;
The view parameters are defined in an XML file stored in the &#039;&#039;Front-end&#039;&#039; view output template directory.  (This is not to be confused with the site Template directory, which is different!). The XML file has the same name as the layout used with the template. For must applications you will be using the default layout. Thus the path the XML file is something like: &amp;lt;code&amp;gt;/PATH_TO_JOOMLA/components/com_COMPONENT_NAME/views/VIEW_NAME/tmpl/default.xml&amp;lt;/code&amp;gt;.  The structure of the file is:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;metadata&amp;gt;&lt;br /&gt;
  ...&lt;br /&gt;
  &amp;lt;state&amp;gt;&lt;br /&gt;
    ...&lt;br /&gt;
    &amp;lt;params&amp;gt;&lt;br /&gt;
      &amp;lt;param /&amp;gt;&lt;br /&gt;
      ...&lt;br /&gt;
    &amp;lt;/params&amp;gt;&lt;br /&gt;
    &amp;lt;url&amp;gt;&lt;br /&gt;
      &amp;lt;param /&amp;gt;&lt;br /&gt;
      ...&lt;br /&gt;
    &amp;lt;/url&amp;gt;&lt;br /&gt;
    &amp;lt;advanced&amp;gt;&lt;br /&gt;
      &amp;lt;param /&amp;gt;&lt;br /&gt;
      ...&lt;br /&gt;
    &amp;lt;/advanced&amp;gt;&lt;br /&gt;
  &amp;lt;/state&amp;gt;&lt;br /&gt;
&amp;lt;/metadata&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
See [[Standard parameter types]] for a full list of the core parameter types. (You can use these types within a &amp;lt;code&amp;gt;&amp;lt;url&amp;gt;&amp;lt;/code&amp;gt; section without needing to supply an &amp;lt;code&amp;gt;addpath&amp;lt;/code&amp;gt; attribute.)&lt;br /&gt;
&lt;br /&gt;
Displaying the forms for these parameters is handled automatically by the core &amp;lt;code&amp;gt;com_menus&amp;lt;/code&amp;gt; component, which creates a number of slider panes on the right hand side (at least when viewed using the default Khepri backend site Template) of the menu item edit screen.  Any parameters defined within the &amp;lt;code&amp;gt;&amp;lt;params&amp;gt;&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;&amp;lt;url&amp;gt;&amp;lt;/code&amp;gt; tags will appear in the &amp;lt;code&amp;gt;Parameters - Basic&amp;lt;/code&amp;gt; slider pane.  Any parameters defined within the &amp;lt;code&amp;gt;&amp;lt;advanced&amp;gt;&amp;lt;/code&amp;gt; tag will appear in the &amp;lt;code&amp;gt;Parameters - Advanced&amp;lt;/code&amp;gt; slider pane.&lt;br /&gt;
&lt;br /&gt;
The following screenshot shows the menu admin page for the Category &#039;List&#039; layout.  You can see that the &amp;lt;code&amp;gt;Parameters - Basic&amp;lt;/code&amp;gt; slider pane is open, and contains form elements for five parameters.  There are no &#039;Advanced&#039; parameters for this view, and so the &amp;lt;code&amp;gt;Parameters - Advanced&amp;lt;/code&amp;gt; slider pane is not shown:&lt;br /&gt;
&lt;br /&gt;
[[Image:Menu items edit.png]]&lt;br /&gt;
&lt;br /&gt;
For the override of the Component-wide default parameters, &amp;lt;code&amp;gt;com_menus&amp;lt;/code&amp;gt; uses the same &amp;lt;code&amp;gt;config.xml&amp;lt;/code&amp;gt; file as above, so there is no need to define these parameters again.  The override forms are automatically added to a slider pane named &amp;lt;code&amp;gt;Parameters - Component&amp;lt;/code&amp;gt;.  However, there are a number of changes from the forms used in the Preferences pop-up window (despite being based on the same &amp;lt;code&amp;gt;config.xml&amp;lt;/code&amp;gt; file):&lt;br /&gt;
* Any &amp;lt;code&amp;gt;&amp;lt;param type=&amp;quot;radio&amp;quot; /&amp;gt;&amp;lt;/code&amp;gt; parameter is shown not as a radio button form field, but by a drop-down list.&lt;br /&gt;
* All drop-down lists (both those defined as such in &amp;lt;code&amp;gt;config.xml&amp;lt;/code&amp;gt;, and those defined as radio buttons) are given an extra  option.  This extra option displays the text &amp;quot;Use Global&amp;quot; but has an associated value of an empty string.  The &amp;quot;Use Global&amp;quot; option is set as the default option for the list.  Thus, by default, these parameters will use the Component-wide default values, rather than overriding them.  (However, note that any text box fields defined with a default value in &amp;lt;code&amp;gt;config.xml&amp;lt;/code&amp;gt; retain that default value, and so will override the component-wide default value if that is different).&lt;br /&gt;
&lt;br /&gt;
===3. &#039;Article&#039; specific parameters===&lt;br /&gt;
&lt;br /&gt;
These are defined in an XML file which can be stored anywhere you like, but is typically placed within the &amp;lt;code&amp;gt;models&amp;lt;/code&amp;gt; folder of the Component Back-end (if you are using an MVC structure) and named according to the model to which they apply, i.e. &amp;lt;code&amp;gt;PATH_TO_JOOMLA/administrator/components/com_COMPONENT_NAME/models/MODEL_NAME.xml&amp;lt;/code&amp;gt;.  The XML file has the following structure:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;xml&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;root&amp;gt;&lt;br /&gt;
  &amp;lt;params&amp;gt;&lt;br /&gt;
    &amp;lt;param /&amp;gt;&lt;br /&gt;
    ...&lt;br /&gt;
  &amp;lt;/params&amp;gt;&lt;br /&gt;
  &amp;lt;params group=&amp;quot;GROUP_NAME&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;param /&amp;gt;&lt;br /&gt;
    ...&lt;br /&gt;
  &amp;lt;/params&amp;gt;&lt;br /&gt;
  ...&lt;br /&gt;
&amp;lt;/root&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As above, you can give the &amp;lt;code&amp;gt;&amp;lt;root&amp;gt;&amp;lt;/code&amp;gt; element whatever name you wish.  You can also have as many &amp;lt;code&amp;gt;&amp;lt;params&amp;gt;&amp;lt;/code&amp;gt; groups as you like, though each group must have a &amp;lt;code&amp;gt;group&amp;lt;/code&amp;gt; attribute with a different &amp;lt;code&amp;gt;GROUP_NAME&amp;lt;/code&amp;gt;.  If there are two or more &amp;lt;code&amp;gt;&amp;lt;params&amp;gt;&amp;lt;/code&amp;gt; groups with the same &amp;lt;code&amp;gt;GROUP_NAME&amp;lt;/code&amp;gt;, only the last one in the list will be considered.&lt;br /&gt;
&lt;br /&gt;
To display the forms for these parameters in the Back-end of your Component, you will need to do a little work yourself.  Firstly, you need to access the saved parameter data from the database.  For this, you will need to identify both the location of the stored parameters data in the database, and the XML file that you are using to define the parameter form fields.  Assuming that you have already created a &amp;lt;code&amp;gt;$row&amp;lt;/code&amp;gt; variable to hold your component item (i.e. &#039;article&#039;) data, that the parameters data is stored in the &amp;lt;code&amp;gt;params&amp;lt;/code&amp;gt; field of the &amp;lt;code&amp;gt;$row&amp;lt;/code&amp;gt;, and that the XML file is located in the models folder as specified above, you could use the following code:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$paramsdata = $row-&amp;gt;params;&lt;br /&gt;
$paramsdefs = JPATH_COMPONENT.DS.&#039;models&#039;.DS.&#039;MODEL_NAME.xml&#039;;&lt;br /&gt;
$params = new JParameter( $paramsdata, $paramsdefs );&lt;br /&gt;
&lt;br /&gt;
$this-&amp;gt;assignRef(&#039;params&#039;, $params);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This code is typically placed in the View file (e.g. view.html.php) within the corresponding folder.&lt;br /&gt;
&lt;br /&gt;
To display the parameters forms in slider panes (in the same way that they are displayed in the menu item Back-end), you should use the following code in the template file (e.g. &amp;lt;code&amp;gt;PATH_TO_JOOMLA/administrator/com_COMPONENT_NAME/views/VIEW_NAME/tmpl/default.php&amp;lt;/code&amp;gt;):&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
jimport(&#039;joomla.html.pane&#039;);&lt;br /&gt;
&lt;br /&gt;
$pane =&amp;amp; JPane::getInstance( &#039;sliders&#039; );&lt;br /&gt;
&lt;br /&gt;
echo $pane-&amp;gt;startPane( &#039;content-pane&#039; );&lt;br /&gt;
&lt;br /&gt;
// First slider panel&lt;br /&gt;
// Create a slider panel with a title of SLIDER_PANEL_1_TITLE and a title id attribute of SLIDER_PANEL_1_NAME&lt;br /&gt;
echo $pane-&amp;gt;startPanel( JText::_( &#039;SLIDER_PANEL_1_TITLE&#039; ), &#039;SLIDER_PANEL_1_NAME&#039; );&lt;br /&gt;
// Display the parameters defined in the &amp;lt;params&amp;gt; group with no &#039;group&#039; attribute&lt;br /&gt;
echo $this-&amp;gt;params-&amp;gt;render( &#039;params&#039; );&lt;br /&gt;
echo $pane-&amp;gt;endPanel();&lt;br /&gt;
&lt;br /&gt;
//Second slider panel&lt;br /&gt;
// Create a slider panel with a title of SLIDER_PANEL_2_TITLE and a title id attribute of SLIDER_PANEL_2_NAME&lt;br /&gt;
echo $pane-&amp;gt;startPanel( JText::_( &#039;SLIDER_PANEL_2_TITLE&#039; ), &#039;SLIDER_PANEL_2_NAME&#039; );&lt;br /&gt;
// Display the parameters defined in the &amp;lt;params&amp;gt; group with the &#039;group&#039; attribute of &#039;GROUP_NAME&#039;&lt;br /&gt;
echo $this-&amp;gt;params-&amp;gt;render( &#039;params&#039;, &#039;GROUP_NAME&#039; );&lt;br /&gt;
echo $pane-&amp;gt;endPanel();&lt;br /&gt;
&lt;br /&gt;
// Repeat for each additional slider panel required&lt;br /&gt;
&lt;br /&gt;
echo $pane-&amp;gt;endPane();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
In order to save the parameters to the database you have to overload the bind() function which can be found in &lt;br /&gt;
&amp;lt;code&amp;gt;PATH_TO_JOOMLA/administrator/com_COMPONENT_NAME/tables/COMPONENT_NAME.php&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
function bind($array, $ignore = &#039;&#039;)&lt;br /&gt;
{&lt;br /&gt;
	if (key_exists( &#039;params&#039;, $array ) &amp;amp;&amp;amp; is_array( $array[&#039;params&#039;] ))&lt;br /&gt;
	{&lt;br /&gt;
		$registry = new JRegistry();&lt;br /&gt;
		$registry-&amp;gt;loadArray($array[&#039;params&#039;]);&lt;br /&gt;
		$array[&#039;params&#039;] = $registry-&amp;gt;toString();&lt;br /&gt;
	}&lt;br /&gt;
	return parent::bind($array, $ignore);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Accessing the parameters - frontend==&lt;br /&gt;
&lt;br /&gt;
The Front-end situation is a bit more complex than the Back-end.  The &#039;standard&#039; way to deal with parameters in the Front-end is to set up a cascade of overrides, as described above:&lt;br /&gt;
&amp;lt;blockquote&amp;gt;Item-specific &#039;&#039;overrides&#039;&#039; Menu-specific &#039;&#039;overrides&#039;&#039; Component-default&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
If you &#039;&#039;&#039;don&#039;t&#039;&#039;&#039; want to do this, then a little extra work is required&lt;br /&gt;
&lt;br /&gt;
===With overrides===&lt;br /&gt;
&lt;br /&gt;
The following code will access the Component-wide default parameters, &#039;&#039;already overridden&#039;&#039; with those for the menu item (if applicable):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
  $params = &amp;amp;JComponentHelper::getParams( &#039;COMPONENT_NAME&#039; );&lt;br /&gt;
&lt;br /&gt;
  // e.g. for the com_weblinks component, you would use:&lt;br /&gt;
  // $params = &amp;amp;JComponentHelper::getParams( &#039;com_weblinks&#039; )&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In an MVC structure, this code would be placed in the &amp;lt;code&amp;gt;view.html.php&amp;lt;/code&amp;gt; file for the particular view required.&lt;br /&gt;
&lt;br /&gt;
Assuming that the item parameters are stored in the &amp;lt;code&amp;gt;params&amp;lt;/code&amp;gt; field, and that the particular item has been loaded into the &amp;lt;code&amp;gt;$row&amp;lt;/code&amp;gt; object by the method, you would then add in the item parameters to the &amp;lt;code&amp;gt;$params&amp;lt;/code&amp;gt; object using:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
  $params-&amp;gt;merge( new JParameter( &amp;amp;$row-&amp;gt;params ) );&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that, in both cases (i.e. overrides by menu-specific parameters, and overrides by item-specific parameters), we aren&#039;t &#039;&#039;just&#039;&#039; doing overrides - we also add in any new parameters that weren&#039;t found in the defaults.  In other words, the &amp;lt;code&amp;gt;$params&amp;lt;/code&amp;gt; object ends up with all the parameters we need!&lt;br /&gt;
&lt;br /&gt;
To access the value of a specific parameter, we would use:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
  $params-&amp;gt;get( &#039;PARAMETER_NAME&#039; );&lt;br /&gt;
&lt;br /&gt;
  // e.g. to obtain the value of &#039;parameter1&#039;, we use:&lt;br /&gt;
  // $params-&amp;gt;get( &#039;parameter1&#039; );&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is important to realise that any &amp;lt;code&amp;gt;merge&amp;lt;/code&amp;gt; carried out is persistent, so the following code (where for example you have an array containing the parameters sets for several items, and you want the parameters for each item to individually override the defaults) would not produce the result you might expect:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
  foreach ($itemparamsgroup as $itemparams)&lt;br /&gt;
  {&lt;br /&gt;
    $params = &amp;amp;JComponentHelper::getParams( &#039;COMPONENT_NAME&#039; );&lt;br /&gt;
    $params-&amp;gt;merge( $itemparams );&lt;br /&gt;
    echo $params-&amp;gt;get( &#039;PARAMETER_NAME&#039; );&lt;br /&gt;
  }&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In such cases, you will need to use the non-override method, and then force the overrides manually, as described below.&lt;br /&gt;
&lt;br /&gt;
===Without overrides===&lt;br /&gt;
&lt;br /&gt;
There will be cases where you don&#039;t want to use the default override pattern. For example, you may want to ignore the menu-specific parameters, and have only the item-specific parameters overriding the defaults. This is a little more difficult to accomplish, although it can still be done.&lt;br /&gt;
&lt;br /&gt;
You can access the Component-wide default parameter values, &#039;&#039;without the menu overrides&#039;&#039;, as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
  $component = JComponentHelper::getComponent( &#039;COMPONENT_NAME&#039; );&lt;br /&gt;
  $params = new JParameter( $component-&amp;gt;params );&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you want to override these with the menu parameters, but &#039;&#039;without&#039;&#039; the &#039;persistence&#039; effect described above, you can:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
  $menuitemid = JRequest::getInt( &#039;Itemid&#039; );&lt;br /&gt;
  if ($menuitemid)&lt;br /&gt;
  {&lt;br /&gt;
    $menu = JSite::getMenu();&lt;br /&gt;
    $menuparams = $menu-&amp;gt;getParams( $menuitemid );&lt;br /&gt;
    $params-&amp;gt;merge( $menuparams );&lt;br /&gt;
  }&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can access the item parameters as before, and choose whether to merge them in using the code above, or simply store them in a separate object:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
  $itemparams = new JParameter( $row-&amp;gt;params );&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can combine this code to achieve the effect that you want.  For example, you could override the Component-wide defaults with the item-specific parameters, but ignore those for the menu.  Or you could use the menu-specific parameters in some circumstances, and the item-specific ones in others.  [Whenever you depart from the standard approach, though, you should be careful that you inform your users how your Component works.  Users will expect your component to work the same way as those from the Joomla! core, so you should have a good reason for departing from this approach.]&lt;br /&gt;
&lt;br /&gt;
Accessing the value of a particular parameter is done in the same way as described above.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;noinclude&amp;gt;[[Category:Development]][[Category:Components]][[Category:Parameters]]&amp;lt;/noinclude&amp;gt;&lt;/div&gt;</summary>
		<author><name>Hasse bjork</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=Archived_talk:Creating_a_file_uploader_in_your_component&amp;diff=20771</id>
		<title>Archived talk:Creating a file uploader in your component</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=Archived_talk:Creating_a_file_uploader_in_your_component&amp;diff=20771"/>
		<updated>2009-12-14T11:12:59Z</updated>

		<summary type="html">&lt;p&gt;Hasse bjork: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Hi&lt;br /&gt;
&lt;br /&gt;
Implementing the SWFUpload Application demo for my component. The joomla document explains only the simple demo  (http://docs.joomla.org/Creating_a_file_uploader_in_your_component )&lt;br /&gt;
&lt;br /&gt;
what are the changes i have to do from the joomla doc. How i rewrite the thumbnail picture function for joomla 1.5&lt;br /&gt;
&lt;br /&gt;
I tried so many time but not works out&lt;br /&gt;
&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
	// This script accepts an ID and looks in the user&#039;s session for stored thumbnail data.&lt;br /&gt;
	// It then streams the data to the browser as an image&lt;br /&gt;
	&lt;br /&gt;
	// Work around the Flash Player Cookie Bug&lt;br /&gt;
	if (isset($_POST[&amp;quot;PHPSESSID&amp;quot;])) {&lt;br /&gt;
		session_id($_POST[&amp;quot;PHPSESSID&amp;quot;]);&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	session_start();&lt;br /&gt;
	&lt;br /&gt;
	$image_id = isset($_GET[&amp;quot;id&amp;quot;]) ? $_GET[&amp;quot;id&amp;quot;] : false;&lt;br /&gt;
&lt;br /&gt;
	if ($image_id === false) {&lt;br /&gt;
		header(&amp;quot;HTTP/1.1 500 Internal Server Error&amp;quot;);&lt;br /&gt;
		echo &amp;quot;No ID&amp;quot;;&lt;br /&gt;
		exit(0);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	if (!is_array($_SESSION[&amp;quot;file_info&amp;quot;]) || !isset($_SESSION[&amp;quot;file_info&amp;quot;][$image_id])) {&lt;br /&gt;
		header(&amp;quot;HTTP/1.1 404 Not found&amp;quot;);&lt;br /&gt;
		exit(0);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	header(&amp;quot;Content-type: image/jpeg&amp;quot;) ;&lt;br /&gt;
	header(&amp;quot;Content-Length: &amp;quot;.strlen($_SESSION[&amp;quot;file_info&amp;quot;][$image_id]));&lt;br /&gt;
	echo $_SESSION[&amp;quot;file_info&amp;quot;][$image_id];&lt;br /&gt;
	exit(0);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Pls help&lt;br /&gt;
&lt;br /&gt;
Regards&lt;br /&gt;
Gopi&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Changed PHP script in Part 5 ==&lt;br /&gt;
&lt;br /&gt;
I made some changes to my own work, before using it. Some tests to use JFile versions and the code is shorter. Return value &#039;&#039;&#039;true&#039;&#039;&#039; on success or else &#039;&#039;&#039;false&#039;&#039;&#039; if there was an error.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
jimport(&#039;joomla.filesystem.file&#039;);&lt;br /&gt;
jimport(&#039;joomla.filesystem.folder&#039;);&lt;br /&gt;
&lt;br /&gt;
$fieldName  = &#039;file_upload&#039;;&lt;br /&gt;
$fileTemp   = $_FILES[$fieldName][&#039;tmp_name&#039;];&lt;br /&gt;
$fileName   = JFile::makeSafe( $_FILES[$fieldName][&#039;name&#039;] );&lt;br /&gt;
$fileExt    = strtolower( JFile::getExt( $fileName ) );&lt;br /&gt;
$imageinfo  = getimagesize( $fileTemp );&lt;br /&gt;
$mimetypes  = array( &#039;image/jpeg&#039;, &#039;image/pjpeg&#039;, &#039;image/png&#039;, &#039;image/x-png&#039;, &#039;image/gif&#039; );&lt;br /&gt;
$uploadPath = JPATH_COMPONENT . DS . &#039;images&#039; . DS . $fileName;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
switch ( $_FILES[$fieldName][&#039;error&#039;] ) {&lt;br /&gt;
	case 1:&lt;br /&gt;
		echo JText::_( &#039;FILE LARGER THAN PHP INI ALLOW&#039; );&lt;br /&gt;
		return false;&lt;br /&gt;
	&lt;br /&gt;
	case 2:&lt;br /&gt;
		echo JText::_( &#039;FILE LARGER THAN HTML FORM ALLOW&#039; );&lt;br /&gt;
		return false;&lt;br /&gt;
	&lt;br /&gt;
	case 3:&lt;br /&gt;
		echo JText::_( &#039;ERROR PARTIAL UPLOAD&#039; );&lt;br /&gt;
		return false;&lt;br /&gt;
	&lt;br /&gt;
	case 4:&lt;br /&gt;
		echo JText::_( &#039;ERROR NO FILE&#039; );&lt;br /&gt;
		return false;&lt;br /&gt;
&lt;br /&gt;
	default:&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
if ( $_FILES[$fieldName][&#039;size&#039;] &amp;gt; 2000000 ) {&lt;br /&gt;
	echo JText::_( &#039;FILE BIGGER THAN 2MB&#039; );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
if ( !in_array( $fileExt, explode(&#039;,&#039;, &#039;jpeg,jpg,png,gif&#039;) ) ) {&lt;br /&gt;
	echo JText::_( &#039;INVALID EXTENSION&#039; );&lt;br /&gt;
	return false;&lt;br /&gt;
} else if ( !in_array( $imageinfo[&#039;mime&#039;], $mimetypes ) ) {&lt;br /&gt;
	echo JText::_( &#039;INVALID FILE TYPE&#039; );&lt;br /&gt;
	return false;&lt;br /&gt;
} else if ( !is_int( $imageinfo[0] ) || !is_int( $imageinfo[0] ) ) {&lt;br /&gt;
	echo JText::_( &#039;INVALID FILE SIZE&#039; );&lt;br /&gt;
	return false;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
if( !JFile::upload( $fileTemp, $uploadPath ) ) {&lt;br /&gt;
	echo JText::_( &#039;ERROR MOVING FILE&#039; );&lt;br /&gt;
	return false;&lt;br /&gt;
}&lt;br /&gt;
return true;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Regards&lt;br /&gt;
hasse_bjork&lt;/div&gt;</summary>
		<author><name>Hasse bjork</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=Archived_talk:Creating_a_file_uploader_in_your_component&amp;diff=20770</id>
		<title>Archived talk:Creating a file uploader in your component</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=Archived_talk:Creating_a_file_uploader_in_your_component&amp;diff=20770"/>
		<updated>2009-12-14T11:10:39Z</updated>

		<summary type="html">&lt;p&gt;Hasse bjork: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Hi&lt;br /&gt;
&lt;br /&gt;
Implementing the SWFUpload Application demo for my component. The joomla document explains only the simple demo  (http://docs.joomla.org/Creating_a_file_uploader_in_your_component )&lt;br /&gt;
&lt;br /&gt;
what are the changes i have to do from the joomla doc. How i rewrite the thumbnail picture function for joomla 1.5&lt;br /&gt;
&lt;br /&gt;
I tried so many time but not works out&lt;br /&gt;
&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
	// This script accepts an ID and looks in the user&#039;s session for stored thumbnail data.&lt;br /&gt;
	// It then streams the data to the browser as an image&lt;br /&gt;
	&lt;br /&gt;
	// Work around the Flash Player Cookie Bug&lt;br /&gt;
	if (isset($_POST[&amp;quot;PHPSESSID&amp;quot;])) {&lt;br /&gt;
		session_id($_POST[&amp;quot;PHPSESSID&amp;quot;]);&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
	session_start();&lt;br /&gt;
	&lt;br /&gt;
	$image_id = isset($_GET[&amp;quot;id&amp;quot;]) ? $_GET[&amp;quot;id&amp;quot;] : false;&lt;br /&gt;
&lt;br /&gt;
	if ($image_id === false) {&lt;br /&gt;
		header(&amp;quot;HTTP/1.1 500 Internal Server Error&amp;quot;);&lt;br /&gt;
		echo &amp;quot;No ID&amp;quot;;&lt;br /&gt;
		exit(0);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	if (!is_array($_SESSION[&amp;quot;file_info&amp;quot;]) || !isset($_SESSION[&amp;quot;file_info&amp;quot;][$image_id])) {&lt;br /&gt;
		header(&amp;quot;HTTP/1.1 404 Not found&amp;quot;);&lt;br /&gt;
		exit(0);&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	header(&amp;quot;Content-type: image/jpeg&amp;quot;) ;&lt;br /&gt;
	header(&amp;quot;Content-Length: &amp;quot;.strlen($_SESSION[&amp;quot;file_info&amp;quot;][$image_id]));&lt;br /&gt;
	echo $_SESSION[&amp;quot;file_info&amp;quot;][$image_id];&lt;br /&gt;
	exit(0);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Pls help&lt;br /&gt;
&lt;br /&gt;
Regards&lt;br /&gt;
Gopi&lt;br /&gt;
&lt;br /&gt;
A few comments on Part 5 and the PHP script&lt;br /&gt;
&lt;br /&gt;
I changed some tests to use JFile versions and shortened the code. Return value &#039;&#039;&#039;true&#039;&#039;&#039; on success or else &#039;&#039;&#039;false&#039;&#039;&#039; if there was an error.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
jimport(&#039;joomla.filesystem.file&#039;);&lt;br /&gt;
jimport(&#039;joomla.filesystem.folder&#039;);&lt;br /&gt;
&lt;br /&gt;
$fieldName  = &#039;file_upload&#039;;&lt;br /&gt;
$fileTemp   = $_FILES[$fieldName][&#039;tmp_name&#039;];&lt;br /&gt;
$fileName   = JFile::makeSafe( $_FILES[$fieldName][&#039;name&#039;] );&lt;br /&gt;
$fileExt    = strtolower( JFile::getExt( $fileName ) );&lt;br /&gt;
$imageinfo  = getimagesize( $fileTemp );&lt;br /&gt;
$mimetypes  = array( &#039;image/jpeg&#039;, &#039;image/pjpeg&#039;, &#039;image/png&#039;, &#039;image/x-png&#039;, &#039;image/gif&#039; );&lt;br /&gt;
$uploadPath = JPATH_COMPONENT . DS . &#039;images&#039; . DS . $fileName;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
switch ( $_FILES[$fieldName][&#039;error&#039;] ) {&lt;br /&gt;
	case 1:&lt;br /&gt;
		echo JText::_( &#039;FILE LARGER THAN PHP INI ALLOW&#039; );&lt;br /&gt;
		return false;&lt;br /&gt;
	&lt;br /&gt;
	case 2:&lt;br /&gt;
		echo JText::_( &#039;FILE LARGER THAN HTML FORM ALLOW&#039; );&lt;br /&gt;
		return false;&lt;br /&gt;
	&lt;br /&gt;
	case 3:&lt;br /&gt;
		echo JText::_( &#039;ERROR PARTIAL UPLOAD&#039; );&lt;br /&gt;
		return false;&lt;br /&gt;
	&lt;br /&gt;
	case 4:&lt;br /&gt;
		echo JText::_( &#039;ERROR NO FILE&#039; );&lt;br /&gt;
		return false;&lt;br /&gt;
&lt;br /&gt;
	default:&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
if ( $_FILES[$fieldName][&#039;size&#039;] &amp;gt; 2000000 ) {&lt;br /&gt;
	echo JText::_( &#039;FILE BIGGER THAN 2MB&#039; );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
if ( !in_array( $fileExt, explode(&#039;,&#039;, &#039;jpeg,jpg,png,gif&#039;) ) ) {&lt;br /&gt;
	echo JText::_( &#039;INVALID EXTENSION&#039; );&lt;br /&gt;
	return false;&lt;br /&gt;
} else if ( !in_array( $imageinfo[&#039;mime&#039;], $mimetypes ) ) {&lt;br /&gt;
	echo JText::_( &#039;INVALID FILE TYPE&#039; );&lt;br /&gt;
	return false;&lt;br /&gt;
} else if ( !is_int( $imageinfo[0] ) || !is_int( $imageinfo[0] ) ) {&lt;br /&gt;
	echo JText::_( &#039;INVALID FILE SIZE&#039; );&lt;br /&gt;
	return false;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
if( !JFile::upload( $fileTemp, $uploadPath ) ) {&lt;br /&gt;
	echo JText::_( &#039;ERROR MOVING FILE&#039; );&lt;br /&gt;
	return false;&lt;br /&gt;
}&lt;br /&gt;
return true;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Hasse bjork</name></author>
	</entry>
	<entry>
		<id>https://docs.sandbox.joomla.org/index.php?title=Archived:Creating_a_file_uploader_in_your_component&amp;diff=20769</id>
		<title>Archived:Creating a file uploader in your component</title>
		<link rel="alternate" type="text/html" href="https://docs.sandbox.joomla.org/index.php?title=Archived:Creating_a_file_uploader_in_your_component&amp;diff=20769"/>
		<updated>2009-12-14T10:51:40Z</updated>

		<summary type="html">&lt;p&gt;Hasse bjork: /* Part 5: Recieveing The File With PHP */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Creating a file uploader in your component: Method 1, SWFUpload ==&lt;br /&gt;
&lt;br /&gt;
=== Part 1: Introduction ===&lt;br /&gt;
&lt;br /&gt;
SWFUpload is a free open source multiple file uploader available for download from http://swfupload.org/&lt;br /&gt;
&lt;br /&gt;
A demo of swfupload is available here: http://demo.swfupload.org/v220/simpledemo/index.php&lt;br /&gt;
&lt;br /&gt;
The standard way to upload a file is with a HTML form. This works great if you are uploading one file, but browsers do not let you select and upload more than one file at a time.&lt;br /&gt;
&lt;br /&gt;
The most common way of selecting multiple files for upload is to use flash and javascript. The flash part of the script opens up a select file dialog where multiple files can be selected, and the flash/javascript sends each file, and recieves the response from the server.&lt;br /&gt;
&lt;br /&gt;
The most popular scripts used to do this are SWFUpload and Fancy upload (http://digitarald.de/project/fancyupload/). The flash uploader in Joomla&#039;s media manager is based on Fancy Upload version 1.0. As Fancy upload 2.0 and 3.0 use mootools 1.2, and Joomla 1.5 uses mootools 1.11, we can not use Fancy Upload 2.0/3.0 in the admin pages of Joomla. Fancy Upload 1.0 could be used, but it is not updated or supported anymore.&lt;br /&gt;
&lt;br /&gt;
SWFUpload does not use Mootools or Jquery. Because of this it will likely work on any page with Mootools 1.11 (Joomla 1.5), or Mootools 1.2 (Joomla 1.6). Therefore we will be using SWFUpload in this article.&lt;br /&gt;
&lt;br /&gt;
Download ths latest version of SWFUpload from the download page (http://code.google.com/p/swfupload/). Download the SWFUpload v2.2.0.1 Samples.zip file.&lt;br /&gt;
&lt;br /&gt;
Inside the file there will be a demos folder, and inside that a simpledemo folder. This is the example we are going to integrate into a Joomla component.&lt;br /&gt;
&lt;br /&gt;
This tutorial assumes you have followed the http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_1 tutorial and are familiar with Joomla&#039;s MVC structure.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
=== Part 2: Adding The External Javascript ===&lt;br /&gt;
&lt;br /&gt;
We need to add in the external links in the head of the document, put some javascript into the head of the doc, and put some HTML in our body.&lt;br /&gt;
&lt;br /&gt;
If you look at the simpledemo/index.php page you will see links to 5 files in the head of the page.&lt;br /&gt;
&lt;br /&gt;
Make a folder within the root of your components folder called swfupload, copy the ../css/default.css, ../swfupload/swfupload.js, js/swfupload.queue.js, js/fileprogress.js and js/handlers.js files into this folder.&lt;br /&gt;
&lt;br /&gt;
We also need to copy the ../swfupload/swfupload.swf and images/TestImageNoText_65x29.png files into this folder.&lt;br /&gt;
&lt;br /&gt;
Within your view.html.php file add in the following code:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
//get the hosts name&lt;br /&gt;
jimport(&#039;joomla.environment.uri&#039; );&lt;br /&gt;
$host = JURI::root();&lt;br /&gt;
&lt;br /&gt;
//add the links to the external files into the head of the webpage (note the &#039;administrator&#039; in the path, which is not nescessary if you are in the frontend)&lt;br /&gt;
$document =&amp;amp; JFactory::getDocument();&lt;br /&gt;
$document-&amp;gt;addScript($host.&#039;administrator/components/com_mycomponent/swfupload/swfupload.js&#039;);&lt;br /&gt;
$document-&amp;gt;addScript($host.&#039;administrator/components/com_mycomponent/swfupload/swfupload.queue.js&#039;);&lt;br /&gt;
$document-&amp;gt;addScript($host.&#039;administrator/components/com_mycomponent/swfupload/fileprogress.js&#039;);&lt;br /&gt;
$document-&amp;gt;addScript($host.&#039;administrator/components/com_mycomponent/swfupload/handlers.js&#039;);&lt;br /&gt;
$document-&amp;gt;addStyleSheet($host.&#039;administrator/components/com_mycomponent/swfupload/default.css&#039;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
That is the external files part done. We still have to add in the head javascript, and the body html.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
=== Part 3: Adding The Head Javascript ===&lt;br /&gt;
&lt;br /&gt;
Head javascript part:&lt;br /&gt;
The javascript below is almost the same as the javascript from the simple demo, the few modifications are commented. It is nescessary to read through it and change the parts (such as task) that are relevant to your component.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
//when we send the files for upload, we have to tell Joomla our session, or we will get logged out &lt;br /&gt;
$session = &amp;amp; JFactory::getSession();&lt;br /&gt;
		&lt;br /&gt;
$swfUploadHeadJs =&#039;&lt;br /&gt;
var swfu;&lt;br /&gt;
&lt;br /&gt;
window.onload = function()&lt;br /&gt;
{&lt;br /&gt;
			&lt;br /&gt;
var settings = &lt;br /&gt;
{&lt;br /&gt;
        //this is the path to the flash file, you need to put your components name into it&lt;br /&gt;
	flash_url : &amp;quot;&#039;.$host.&#039;administrator/components/com_mycomponent/swfupload/swfupload.swf&amp;quot;,&lt;br /&gt;
	&lt;br /&gt;
        //we can not put any vars into the url for complicated reasons, but we can put them into the post...&lt;br /&gt;
        upload_url: &amp;quot;index.php&amp;quot;,&lt;br /&gt;
	post_params: &lt;br /&gt;
	{&lt;br /&gt;
		&amp;quot;option&amp;quot; : &amp;quot;com_mycomponent&amp;quot;,&lt;br /&gt;
		&amp;quot;controller&amp;quot; : &amp;quot;mycontroller&amp;quot;,&lt;br /&gt;
		&amp;quot;task&amp;quot; : &amp;quot;mytask&amp;quot;,&lt;br /&gt;
		&amp;quot;id&amp;quot; : &amp;quot;&#039;.$myItemObject-&amp;gt;id.&#039;&amp;quot;,&lt;br /&gt;
		&amp;quot;&#039;.$session-&amp;gt;getName().&#039;&amp;quot; : &amp;quot;&#039;.$session-&amp;gt;getId().&#039;&amp;quot;,&lt;br /&gt;
		&amp;quot;format&amp;quot; : &amp;quot;raw&amp;quot;&lt;br /&gt;
	}, &lt;br /&gt;
        //you need to put the session and the &amp;quot;format raw&amp;quot; in there, the other ones are what you would normally put in the url&lt;br /&gt;
	file_size_limit : &amp;quot;5 MB&amp;quot;,&lt;br /&gt;
        //client side file chacking is for usability only, you need to check server side for security&lt;br /&gt;
	file_types : &amp;quot;*.jpg;*.jpeg;*.gif;*.png&amp;quot;,&lt;br /&gt;
	file_types_description : &amp;quot;All Files&amp;quot;,&lt;br /&gt;
	file_upload_limit : 100,&lt;br /&gt;
	file_queue_limit : 100,&lt;br /&gt;
	custom_settings : &lt;br /&gt;
	{&lt;br /&gt;
		progressTarget : &amp;quot;fsUploadProgress&amp;quot;,&lt;br /&gt;
		cancelButtonId : &amp;quot;btnCancel&amp;quot;&lt;br /&gt;
	},&lt;br /&gt;
	debug: false,&lt;br /&gt;
&lt;br /&gt;
	// Button settings&lt;br /&gt;
	button_image_url: &amp;quot;&#039;.$host.&#039;administrator/components/com_mycomponent/swfupload/images/TestImageNoText_65x29.png&amp;quot;,&lt;br /&gt;
	button_width: &amp;quot;85&amp;quot;,&lt;br /&gt;
	button_height: &amp;quot;29&amp;quot;,&lt;br /&gt;
	button_placeholder_id: &amp;quot;spanButtonPlaceHolder&amp;quot;,&lt;br /&gt;
	button_text: \&#039;&amp;lt;span class=&amp;quot;theFont&amp;quot;&amp;gt;Choose Files&amp;lt;/span&amp;gt;\&#039;,&lt;br /&gt;
	button_text_style: &amp;quot;.theFont { font-size: 13; }&amp;quot;,&lt;br /&gt;
	button_text_left_padding: 5,&lt;br /&gt;
	button_text_top_padding: 5,&lt;br /&gt;
				&lt;br /&gt;
	// The event handler functions are defined in handlers.js&lt;br /&gt;
	file_queued_handler : fileQueued,&lt;br /&gt;
	file_queue_error_handler : fileQueueError,&lt;br /&gt;
	file_dialog_complete_handler : fileDialogComplete,&lt;br /&gt;
	upload_start_handler : uploadStart,&lt;br /&gt;
	upload_progress_handler : uploadProgress,&lt;br /&gt;
	upload_error_handler : uploadError,&lt;br /&gt;
	upload_success_handler : uploadSuccess,&lt;br /&gt;
	upload_complete_handler : uploadComplete,&lt;br /&gt;
	queue_complete_handler : queueComplete	// Queue plugin event&lt;br /&gt;
};&lt;br /&gt;
swfu = new SWFUpload(settings);&lt;br /&gt;
};&lt;br /&gt;
	     &lt;br /&gt;
&#039;;&lt;br /&gt;
&lt;br /&gt;
//add the javascript to the head of the html document&lt;br /&gt;
$document-&amp;gt;addScriptDeclaration($swfUploadHeadJs);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
If you want more info on what each paramater is, have a look at the swfupload docs:&lt;br /&gt;
http://demo.swfupload.org/Documentation/&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
=== Part 4: Adding The Body HTML ===&lt;br /&gt;
&lt;br /&gt;
We still have to add in our body HTML, in you tmpl/default.php file add in:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;html4strict&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;div id=&amp;quot;swfuploader&amp;quot;&amp;gt;&lt;br /&gt;
	&amp;lt;form id=&amp;quot;form1&amp;quot; action=&amp;quot;index.php&amp;quot; method=&amp;quot;post&amp;quot; enctype=&amp;quot;multipart/form-data&amp;quot;&amp;gt;&lt;br /&gt;
	&amp;lt;fieldset class=&amp;quot;adminform&amp;quot;&amp;gt;&lt;br /&gt;
		&lt;br /&gt;
			&amp;lt;div class=&amp;quot;fieldset flash&amp;quot; id=&amp;quot;fsUploadProgress&amp;quot;&amp;gt;&lt;br /&gt;
			&amp;lt;span class=&amp;quot;legend&amp;quot;&amp;gt;Upload Queue&amp;lt;/span&amp;gt;&lt;br /&gt;
			&amp;lt;/div&amp;gt;&lt;br /&gt;
		&amp;lt;div id=&amp;quot;divStatus&amp;quot;&amp;gt;0 Files Uploaded&amp;lt;/div&amp;gt;&lt;br /&gt;
			&amp;lt;div&amp;gt;&lt;br /&gt;
				&amp;lt;span id=&amp;quot;spanButtonPlaceHolder&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
				&amp;lt;input id=&amp;quot;btnCancel&amp;quot; type=&amp;quot;button&amp;quot; value=&amp;quot;Cancel All Uploads&amp;quot; onclick=&amp;quot;swfu.cancelQueue();&amp;quot; disabled=&amp;quot;disabled&amp;quot; style=&amp;quot;margin-left: 2px; font-size: 8pt; height: 29px;&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
			&amp;lt;/div&amp;gt;&lt;br /&gt;
	&amp;lt;/fieldset&amp;gt;&lt;br /&gt;
	&amp;lt;/form&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
If all has gone well, you will be able to upload files to your components script, although nothing will happen on the server as there is no PHP to recieve the image.&lt;br /&gt;
&lt;br /&gt;
===  Part 5: Recieveing The File With PHP ===&lt;br /&gt;
&lt;br /&gt;
It is worth noting that allowing users to upload files to the server is possibly the most dangerous thing to do in terms of security, and if someone with malicious intentions manages to upload a php file to your server, then they can easily take control of the site.&lt;br /&gt;
&lt;br /&gt;
It is recommeneded you do some reading on file upload security. This is a good start:&lt;br /&gt;
&lt;br /&gt;
http://www.scanit.be/uploads/php-file-upload.pdf&lt;br /&gt;
&lt;br /&gt;
And there are many other good resources on the internet. It is important you understand how file upload security works, rather than copying and pasting some code that checks the MIME type, and assuming the script is safe.&lt;br /&gt;
&lt;br /&gt;
Here is some example PHP that will check if the file is an image, and upload it. It is not an exhaustive example of security checking, but it is a good start:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
//import joomlas filesystem functions, we will do all the filewriting with joomlas functions,&lt;br /&gt;
//so if the ftp layer is on, joomla will write with that, not the apache user, which might&lt;br /&gt;
//not have the correct permissions&lt;br /&gt;
jimport(&#039;joomla.filesystem.file&#039;);&lt;br /&gt;
jimport(&#039;joomla.filesystem.folder&#039;);&lt;br /&gt;
		&lt;br /&gt;
//this is the name of the field in the html form, filedata is the default name for swfupload&lt;br /&gt;
//so we will leave it as that&lt;br /&gt;
$fieldName = &#039;Filedata&#039;;&lt;br /&gt;
&lt;br /&gt;
//any errors the server registered on uploading&lt;br /&gt;
$fileError = $_FILES[$fieldName][&#039;error&#039;];&lt;br /&gt;
if ($fileError &amp;gt; 0) &lt;br /&gt;
{&lt;br /&gt;
        switch ($fileError) &lt;br /&gt;
	{&lt;br /&gt;
        case 1:&lt;br /&gt;
        echo JText::_( &#039;FILE TO LARGE THAN PHP INI ALLOWS&#039; );&lt;br /&gt;
        return;&lt;br /&gt;
&lt;br /&gt;
        case 2:&lt;br /&gt;
        echo JText::_( &#039;FILE TO LARGE THAN HTML FORM ALLOWS&#039; );&lt;br /&gt;
        return;&lt;br /&gt;
&lt;br /&gt;
        case 3:&lt;br /&gt;
        echo JText::_( &#039;ERROR PARTIAL UPLOAD&#039; );&lt;br /&gt;
        return;&lt;br /&gt;
&lt;br /&gt;
        case 4:&lt;br /&gt;
        echo JText::_( &#039;ERROR NO FILE&#039; );&lt;br /&gt;
        return;&lt;br /&gt;
        }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
//check for filesize&lt;br /&gt;
$fileSize = $_FILES[$fieldName][&#039;size&#039;];&lt;br /&gt;
if($fileSize &amp;gt; 2000000)&lt;br /&gt;
{&lt;br /&gt;
    echo JText::_( &#039;FILE BIGGER THAN 2MB&#039; );&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
//check the file extension is ok&lt;br /&gt;
$fileName = $_FILES[$fieldName][&#039;name&#039;];&lt;br /&gt;
$uploadedFileNameParts = explode(&#039;.&#039;,$fileName);&lt;br /&gt;
$uploadedFileExtension = array_pop($uploadedFileNameParts);&lt;br /&gt;
&lt;br /&gt;
$validFileExts = explode(&#039;,&#039;, &#039;jpeg,jpg,png,gif&#039;);&lt;br /&gt;
&lt;br /&gt;
//assume the extension is false until we know its ok&lt;br /&gt;
$extOk = false;&lt;br /&gt;
&lt;br /&gt;
//go through every ok extension, if the ok extension matches the file extension (case insensitive)&lt;br /&gt;
//then the file extension is ok&lt;br /&gt;
foreach($validFileExts as $key =&amp;gt; $value)&lt;br /&gt;
{&lt;br /&gt;
	if( preg_match(&amp;quot;/$value/i&amp;quot;, $uploadedFileExtension ) )&lt;br /&gt;
	{&lt;br /&gt;
		$extOk = true;&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
if ($extOk == false) &lt;br /&gt;
{&lt;br /&gt;
	echo JText::_( &#039;INVALID EXTENSION&#039; );&lt;br /&gt;
        return;&lt;br /&gt;
}&lt;br /&gt;
	&lt;br /&gt;
//the name of the file in PHP&#039;s temp directory that we are going to move to our folder&lt;br /&gt;
$fileTemp = $_FILES[$fieldName][&#039;tmp_name&#039;];&lt;br /&gt;
		&lt;br /&gt;
//for security purposes, we will also do a getimagesize on the temp file (before we have moved it &lt;br /&gt;
//to the folder) to check the MIME type of the file, and whether it has a width and height&lt;br /&gt;
$imageinfo = getimagesize($fileTemp);&lt;br /&gt;
		&lt;br /&gt;
//we are going to define what file extensions/MIMEs are ok, and only let these ones in (whitelisting), rather than try to scan for bad&lt;br /&gt;
//types, where we might miss one (whitelisting is always better than blacklisting) &lt;br /&gt;
$okMIMETypes = &#039;image/jpeg,image/pjpeg,image/png,image/x-png,image/gif&#039;;&lt;br /&gt;
$validFileTypes = explode(&amp;quot;,&amp;quot;, $okMIMETypes);		&lt;br /&gt;
&lt;br /&gt;
//if the temp file does not have a width or a height, or it has a non ok MIME, return&lt;br /&gt;
if( !is_int($imageinfo[0]) || !is_int($imageinfo[1]) ||  !in_array($imageinfo[&#039;mime&#039;], $validFileTypes) )&lt;br /&gt;
{&lt;br /&gt;
	echo JText::_( &#039;INVALID FILETYPE&#039; );&lt;br /&gt;
        return;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
//lose any special characters in the filename&lt;br /&gt;
$fileName = ereg_replace(&amp;quot;[^A-Za-z0-9.]&amp;quot;, &amp;quot;-&amp;quot;, $fileName);&lt;br /&gt;
&lt;br /&gt;
//always use constants when making file paths, to avoid the possibilty of remote file inclusion&lt;br /&gt;
$uploadPath = JPATH_SITE.DS.&#039;images&#039;.DS.&#039;stories&#039;.DS.$fileName;&lt;br /&gt;
&lt;br /&gt;
if(!JFile::upload($fileTemp, $uploadPath)) &lt;br /&gt;
{&lt;br /&gt;
	echo JText::_( &#039;ERROR MOVING FILE&#039; );&lt;br /&gt;
        return;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
If all want well the uploaded files will be in the images/stories folder.&lt;br /&gt;
&lt;br /&gt;
===  Part 6: Flash plugin COOKIE bug on non-IE browsers ===&lt;br /&gt;
&lt;br /&gt;
Also you should pay attention to Flash player cookie bug!&lt;br /&gt;
&lt;br /&gt;
It is described here http://swfupload.org/forum/generaldiscussion/383&lt;br /&gt;
&lt;br /&gt;
The Flash Player Plugin for FireFox, Opera and Safari (and probably other non-IE based browsers) has a bug which sends persistent cookies from IE to the upload URL instead of the cookies from the browser. Session only cookies from IE are not sent.&lt;br /&gt;
&lt;br /&gt;
1) Any cookies from a non-IE browser (ie, authentication, login, etc) will not be sent with the file upload. Rather the cookies from IE will be sent. So, no cookies or the wrong cookies will be sent. In most cases this means sessions and cookie based authentication are lost when making an upload.&lt;br /&gt;
&lt;br /&gt;
2) Cookies set by the upload script do get set, but only for Flash. The browser will not see them.&lt;br /&gt;
&lt;br /&gt;
3) This bug affects Flash 8 and Flash 9 and all versions of SWFUpload&lt;br /&gt;
&lt;br /&gt;
4) You cannot rely on cookies when using SWFUpload (or any Flash based upload tool). You must send the data you need from the cookies in another way. There are several threads regarding this issue in the forum and many of the demos show workarounds for restoring PHP sessions and some sample files show how to restore the cookies so Session and Authentication are restored in ASP.Net.&lt;br /&gt;
&lt;br /&gt;
5) This could be considered a security issue but probably not severe enough to actually compromise any data. The cookies created in IE by Flash still have all the rules and restricts associated with cookies in any browser.&lt;br /&gt;
&lt;br /&gt;
Solution is modify function _start() (libraries\joomla\session\session.php):&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
$sn = session_name();&lt;br /&gt;
&lt;br /&gt;
if(isset($_COOKIE[$sn]) &amp;amp;&amp;amp; isset($_POST[$sn])) &lt;br /&gt;
   {&lt;br /&gt;
	$_COOKIE[$sn] = $_POST[$sn];&lt;br /&gt;
	session_id($_POST[$sn]);&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
session_start();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Hasse bjork</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=20734</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=20734"/>
		<updated>2009-12-13T09:31:27Z</updated>

		<summary type="html">&lt;p&gt;Hasse bjork: /* Step 2: 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 and you need to be aware of the details so that 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 component layout file==&lt;br /&gt;
Starting with the user interface, 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 fresh parameters to obtain a new table.  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;DbName&#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;DbDescription&#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 2: The model==&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 (see later).&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;
The following is typical of a private method used to build the ORDER BY clause:&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;
	$filter_order     = $mainframe-&amp;gt;getUserStateFromRequest( $option.&#039;filter_order&#039;, &#039;filter_order&#039;, &#039;DbOrder&#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;
	$orderby = &#039; ORDER BY &#039;.$filter_order.&#039; &#039;.$filter_order_Dir;&lt;br /&gt;
&lt;br /&gt;
	return $orderby;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
You will need to adjust the &amp;quot;DbOrder&amp;quot; to match the name of the database column to sort on by default.  If you want the default ordering to be descending order then change the &amp;quot;asc&amp;quot; to &amp;quot;desc&amp;quot; in the $filter_order_Dir statement.&lt;br /&gt;
&lt;br /&gt;
==Step 3: The view==&lt;br /&gt;
Having generated the data in the model, using the sort parameters that were passed using the form which wraps the table in the layout, you now need to make sure that these same sort parameters are passed back into the layout so that when the page loads with freshly sorted data, the correct defaults for the sort column and sort direction are shown to the user.&lt;br /&gt;
&lt;br /&gt;
To do this you need to add a few lines of code to your view file, view.html.php:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$state =&amp;amp; $this-&amp;gt;get( &#039;state&#039; );&lt;br /&gt;
&lt;br /&gt;
// Table ordering.&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;
&amp;lt;/source&amp;gt;&lt;br /&gt;
There shouldn&#039;t be any need to customise this code.&lt;br /&gt;
&lt;br /&gt;
You should now test to make sure it all works as it should.&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;
&amp;lt;noinclude&amp;gt;[[Category:Development]]&amp;lt;/noinclude&amp;gt;&lt;/div&gt;</summary>
		<author><name>Hasse bjork</name></author>
	</entry>
</feed>