<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
    xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
    xmlns:admin="http://webns.net/mvcb/"
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:content="http://purl.org/rss/1.0/modules/content/">

    <channel>
    
    <title><![CDATA[Articles]]></title>
    <link>http://ee-recipes/</link>
    <description></description>
    <dc:language>en</dc:language>
    <dc:creator>aweaver@brandnewbox.co.uk</dc:creator>
    <dc:rights>Copyright 2011</dc:rights>
    <dc:date>2011-09-29T20:07:42+00:00</dc:date>
    <admin:generatorAgent rdf:resource="http://expressionengine.com/" />
    

    <item>
      <title><![CDATA[An introduction]]></title>
      <link>http://ee-recipes.com/articles/details/an_introduction</link>
      <guid>http://ee-recipes.com/articles/details/an_introduction#When:13:35:28Z</guid>
      <description><![CDATA[<p>EE Recipes is a new site to explain and discuss the development of ExpressionEngine add-ons. </p>

<p>Starting from the basics of how to create <a href="http://ee-recipes.com/articles/details/the_different_types_of_expressionengine_add_ons">common add-on types</a>, we&#8217;ll look at a range of techniques to harness the power of EE&#8217;s (and CodeIgniter&#8217;s) built-in classes and functions to build add-ons that enhance your own EE sites and/or that can be published for the community. </p>

<p>We&#8217;ll focus on:</p>

<ul>
<li>how to build different types of add-on</li>
<li>useful classes and functions available within EE and CI</li>
<li>the database structure and how to retrieve and modify data</li>
<li>EE add-on &#8216;design patterns&#8217; — conventions that you should follow when confronted with common situations</li>
</ul>

<p>If there are any other aspects of add-on development that you want see let me know.</p>

<p>The site is aimed at PHP developers who use EE and want to start developing their own add-ons, but there should be enough to keep experienced add-on developers interested (in fact, I’d welcome any suggestions of improvements). This site won’t teach you how to build websites with ExpressionEngine — there are <a href="http://ee-recipes.com/articles/details/resources">better resources</a> available for that. </p>

<p>To keep the frequency of posts as high as possible, articles will initially be released in a blog format, as-and-when they are ready, and larger topics will be split into several articles to keep the subject matter more manageable. When there is a little more content I’ll organise the site into a more useful structure (and fill in any gaps).</p>

<p>Thanks for your interest. Any suggestions will be greatly received.</p>

<p>- Andrew Weaver (<a href="http://brandnewbox.co.uk/">brandnewbox.co.uk</a>)</p>

<p>&nbsp;</p>]]></description>
      <dc:subject><![CDATA[News,]]></dc:subject>
      <dc:date>2011-09-16T13:35:28+00:00</dc:date>
    </item>

    <item>
      <title><![CDATA[Our first module, part 8: displaying a history of bids]]></title>
      <link>http://ee-recipes.com/articles/details/our-first-module-part-8-displaying-a-history-of-bids</link>
      <guid>http://ee-recipes.com/articles/details/our-first-module-part-8-displaying-a-history-of-bids#When:11:25:55Z</guid>
      <description><![CDATA[<p>In this part we will add a new tag to display a list of bids placed for an auction. We&#8217;ll call it <code>&#123;exp:auction:history}</code> so we need to add a new function in <code>mod.auction.php</code> called <code>history</code>.</p>

<p>It was useful for the summary tag to be able to run within a channel entries tag as this would be how we would often want to use it to (ie, to display lists of auctions).</p>

<p>However, it is good practise in ExpressionEngine not to nest tags inside others if possible.</p>

<p>Nested tags can lead to you inadvertently using a large number of database queries. A tag that is included inside another tag that loops over a number of iterations will be run multiple times. Depending on the complexity of the internal tag this could be a very bad thing.</p>

<p>From a template processing point of view it can also lead to variable scope clashes if both tags use the same variable. EE automatically generates some variables if you use the <code>$this-&gt;EE-&gt;TMPL-&gt;parse_variables</code> function, such as <code>&#123;count}</code>, <code>&#123;total_results}</code> and <code>&#123;switch}</code> and if you nest tags then it is impossible to control which version of the variable will be used (it will always be the value from the outer tag).</p>

<p>So where possible it is best to design tags that can be used on their own. With this in mind the history tag will be designed to used outside the channel entries tag.</p>

<div class="codeblock"><code><span style="color: #000000">
<span style="color: #007700">&lt;</span><span style="color: #0000BB">h4</span><span style="color: #007700">&gt;</span><span style="color: #0000BB">History</span><span style="color: #007700">&lt;/</span><span style="color: #0000BB">h4</span><span style="color: #007700">&gt;<br />&lt;</span><span style="color: #0000BB">table</span><span style="color: #007700">&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color: #0000BB">thead</span><span style="color: #007700">&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color: #0000BB">tr</span><span style="color: #007700">&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color: #0000BB">th</span><span style="color: #007700">&gt;</span><span style="color: #0000BB">Bidder</span><span style="color: #007700">&lt;/</span><span style="color: #0000BB">th</span><span style="color: #007700">&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color: #0000BB">th</span><span style="color: #007700">&gt;</span><span style="color: #0000BB">Amount</span><span style="color: #007700">&lt;/</span><span style="color: #0000BB">th</span><span style="color: #007700">&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color: #0000BB">th</span><span style="color: #007700">&gt;</span><span style="color: #0000BB">When</span><span style="color: #007700">&lt;/</span><span style="color: #0000BB">th</span><span style="color: #007700">&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/</span><span style="color: #0000BB">tr</span><span style="color: #007700">&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;/</span><span style="color: #0000BB">thead</span><span style="color: #007700">&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color: #0000BB">tbody</span><span style="color: #007700">&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">&#123;exp</span><span style="color: #007700">:</span><span style="color: #0000BB">auction</span><span style="color: #007700">:</span><span style="color: #0000BB">history&nbsp;url_title</span><span style="color: #007700">=</span><span style="color: #DD0000">"&#123;segment_3&#125;"</span><span style="color: #0000BB">&#125;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#123;if&nbsp;no_results&#125;</span><span style="color: #007700">&lt;</span><span style="color: #0000BB">tr</span><span style="color: #007700">&gt;&lt;</span><span style="color: #0000BB">td&nbsp;colspan</span><span style="color: #007700">=</span><span style="color: #DD0000">"3"</span><span style="color: #007700">&gt;</span><span style="color: #0000BB">No&nbsp;bids&nbsp;yet</span><span style="color: #007700">&lt;/</span><span style="color: #0000BB">td</span><span style="color: #007700">&gt;&lt;/</span><span style="color: #0000BB">tr</span><span style="color: #007700">&gt;</span><span style="color: #0000BB">&#123;</span><span style="color: #007700">/</span><span style="color: #0000BB">if&#125;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">&lt;</span><span style="color: #0000BB">tr&nbsp;&#123;if&nbsp;count</span><span style="color: #007700">==</span><span style="color: #0000BB">1&#125;class</span><span style="color: #007700">=</span><span style="color: #DD0000">"highlighted"</span><span style="color: #0000BB">&#123;</span><span style="color: #007700">/</span><span style="color: #0000BB">if&#125;</span><span style="color: #007700">&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color: #0000BB">td</span><span style="color: #007700">&gt;</span><span style="color: #0000BB">&#123;screen_name&#125;</span><span style="color: #007700">&lt;/</span><span style="color: #0000BB">td</span><span style="color: #007700">&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color: #0000BB">td</span><span style="color: #007700">&gt;&amp;</span><span style="color: #0000BB">pound</span><span style="color: #007700">;</span><span style="color: #0000BB">&#123;bid_amount&#125;</span><span style="color: #007700">&lt;/</span><span style="color: #0000BB">td</span><span style="color: #007700">&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color: #0000BB">td</span><span style="color: #007700">&gt;</span><span style="color: #0000BB">&#123;bid_date&nbsp;format</span><span style="color: #007700">=</span><span style="color: #DD0000">"%D,&nbsp;%M&nbsp;%j,&nbsp;%Y"</span><span style="color: #0000BB">&#125;</span><span style="color: #007700">&lt;/</span><span style="color: #0000BB">td</span><span style="color: #007700">&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/</span><span style="color: #0000BB">tr</span><span style="color: #007700">&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">&#123;</span><span style="color: #007700">/</span><span style="color: #0000BB">exp</span><span style="color: #007700">:</span><span style="color: #0000BB">auction</span><span style="color: #007700">:</span><span style="color: #0000BB">history&#125;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">&lt;/</span><span style="color: #0000BB">tbody</span><span style="color: #007700">&gt;<br />&lt;/</span><span style="color: #0000BB">table</span><span style="color: #007700">&gt;&nbsp;</span>
</span>
</code></div>

<p>The history tag will be used on single entry pages, that is, pages where only a single auction is displayed rather than a list of them. These pages will be identified by having either the auction entry&#8217;s entry id or url title in the page&#8217;s URL. </p>

<p>We can then use the segment variables to identify which auction&#8217;s history to display. To keep the tag flexible we&#8217;ll make it optional whether we use entry id or url title.</p>

<p>To get the auction&#8217;s <code>entry_id</code> from the entry&#8217;s <code>url_title</code>, we&#8217;ll need to <em>join</em> the <code>exp_channel_titles</code> table.</p>

<p>We also want to include some details about the bidder, so we need to join the members table to fetch the user&#8217;s screen name.</p>

<p><img src="http://ee-recipes.com/images/article/history-join.png" alt="Joining tables" width="560" height="302"  /></p>

<p>The <code>$query-&gt;result_array()</code> function returns an array of results which is exactly how <code>$this-&gt;EE-&gt;TMPL-&gt;parse_variables</code> likes it, so it easy to produce the final results of.</p>

<p>This leaves us with the final <code>history</code> function of:</p>

<div class="codeblock"><code><span style="color: #000000">
<span style="color: #007700">public&nbsp;function&nbsp;</span><span style="color: #0000BB">history</span><span style="color: #007700">()&nbsp;</span><span style="color: #0000BB">&#123;<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Find&nbsp;the&nbsp;url&nbsp;title&nbsp;of&nbsp;the&nbsp;auction&nbsp;to&nbsp;add&nbsp;the&nbsp;form&nbsp;for<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$url_title&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">TMPL</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fetch_param</span><span style="color: #007700">(</span><span style="color: #DD0000">'url_title'</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Find&nbsp;the&nbsp;url&nbsp;title&nbsp;of&nbsp;the&nbsp;auction&nbsp;to&nbsp;add&nbsp;the&nbsp;form&nbsp;for<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$entry_id&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">TMPL</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fetch_param</span><span style="color: #007700">(</span><span style="color: #DD0000">'entry_id'</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Check&nbsp;that&nbsp;one&nbsp;of&nbsp;them&nbsp;is&nbsp;used<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">if(&nbsp;</span><span style="color: #0000BB">$entry_id&nbsp;</span><span style="color: #007700">===&nbsp;</span><span style="color: #0000BB">FALSE&nbsp;</span><span style="color: #007700">&amp;&amp;&nbsp;</span><span style="color: #0000BB">$url_title&nbsp;</span><span style="color: #007700">===&nbsp;</span><span style="color: #0000BB">FALSE&nbsp;</span><span style="color: #007700">)&nbsp;</span><span style="color: #0000BB">&#123;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">return&nbsp;</span><span style="color: #DD0000">""</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">&#125;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Find&nbsp;list&nbsp;of&nbsp;bids&nbsp;for&nbsp;this&nbsp;auction<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">select</span><span style="color: #007700">(&nbsp;</span><span style="color: #DD0000">"exp_auction.bid_amount,&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exp_auction.bid_date,&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exp_members.member_id,&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exp_members.screen_name,&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exp_members.username,&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exp_members.email"&nbsp;</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">from</span><span style="color: #007700">(&nbsp;</span><span style="color: #DD0000">"auction"&nbsp;</span><span style="color: #007700">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">join</span><span style="color: #007700">(&nbsp;</span><span style="color: #DD0000">"exp_members"</span><span style="color: #007700">,&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"exp_members.member_id&nbsp;=&nbsp;auction.member_id"&nbsp;</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;if(&nbsp;</span><span style="color: #0000BB">$entry_id&nbsp;</span><span style="color: #007700">!==&nbsp;</span><span style="color: #0000BB">FALSE&nbsp;</span><span style="color: #007700">)&nbsp;</span><span style="color: #0000BB">&#123;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;we&nbsp;have&nbsp;the&nbsp;entry_id,&nbsp;so&nbsp;we&nbsp;don't&nbsp;need&nbsp;exp_channel_titles&nbsp;table<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">where</span><span style="color: #007700">(&nbsp;</span><span style="color: #DD0000">"auction.entry_id"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$entry_id&nbsp;</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">&#125;&nbsp;</span><span style="color: #007700">else&nbsp;</span><span style="color: #0000BB">&#123;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;we&nbsp;have&nbsp;the&nbsp;url_title,&nbsp;so&nbsp;we&nbsp;need&nbsp;exp_channel_titles&nbsp;table<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;to&nbsp;find&nbsp;the&nbsp;entry_id<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">join</span><span style="color: #007700">(&nbsp;</span><span style="color: #DD0000">"exp_channel_titles"</span><span style="color: #007700">,&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"exp_channel_titles.entry_id&nbsp;=&nbsp;auction.entry_id"&nbsp;</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">where</span><span style="color: #007700">(&nbsp;</span><span style="color: #DD0000">"exp_channel_titles.url_title"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$url_title&nbsp;</span><span style="color: #007700">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">&#125;<br />&nbsp;&nbsp;&nbsp;&nbsp;$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">order_by</span><span style="color: #007700">(&nbsp;</span><span style="color: #DD0000">"bid_date&nbsp;desc"&nbsp;</span><span style="color: #007700">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$query&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">get</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;If&nbsp;no&nbsp;results&nbsp;are&nbsp;found...<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">if(&nbsp;</span><span style="color: #0000BB">$query</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">num_rows</span><span style="color: #007700">()&nbsp;==&nbsp;</span><span style="color: #0000BB">0&nbsp;</span><span style="color: #007700">)&nbsp;</span><span style="color: #0000BB">&#123;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;...&nbsp;return&nbsp;the&nbsp;&#123;if&nbsp;no_results&#125;&nbsp;...&nbsp;&#123;/if&#125;&nbsp;conditional<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">return&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">TMPL</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">no_results</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">&#125;<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Put&nbsp;history&nbsp;data&nbsp;in&nbsp;an&nbsp;array&nbsp;and&nbsp;return&nbsp;the&nbsp;parsed&nbsp;the&nbsp;tag&nbsp;data<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$data&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$query</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">result_array</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$tagdata&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">TMPL</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">tagdata</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">TMPL</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">parse_variables</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$tagdata</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$data&nbsp;</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">&#125;&nbsp;</span>
</span>
</code></div>

<p>&nbsp;</p>]]></description>
      <dc:subject><![CDATA[Modules, Our first module,]]></dc:subject>
      <dc:date>2012-10-16T11:25:55+00:00</dc:date>
    </item>

    <item>
      <title><![CDATA[Our first module, part 7: finishing off our summary tag]]></title>
      <link>http://ee-recipes.com/articles/details/our-first-module-part-7-finishing-off-our-summary-tag</link>
      <guid>http://ee-recipes.com/articles/details/our-first-module-part-7-finishing-off-our-summary-tag#When:12:33:14Z</guid>
      <description><![CDATA[<p>Now we are adding the users&#8217; bids to the database, let&#8217;s go back and finish our <code>&#123;exp:auction:summary}</code> tag. </p>

<p>The purpose of this tag was to display the auction&#8217;s current bid price and the total number of bids.</p>

<p>So far we had got:</p>

<div class="codeblock"><code><span style="color: #000000">
<span style="color: #007700">public&nbsp;function&nbsp;</span><span style="color: #0000BB">summary</span><span style="color: #007700">()&nbsp;</span><span style="color: #0000BB">&#123;<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Find&nbsp;the&nbsp;entry_id&nbsp;of&nbsp;the&nbsp;auction&nbsp;to&nbsp;display<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$entry_id&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">TMPL</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fetch_param</span><span style="color: #007700">(</span><span style="color: #DD0000">'entry_id'</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;if(&nbsp;</span><span style="color: #0000BB">$entry_id&nbsp;</span><span style="color: #007700">===&nbsp;</span><span style="color: #0000BB">FALSE&nbsp;</span><span style="color: #007700">)&nbsp;</span><span style="color: #0000BB">&#123;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">return&nbsp;</span><span style="color: #DD0000">""</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">&#125;<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;$tagdata&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">TMPL</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">tagdata</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Build&nbsp;array&nbsp;of&nbsp;our&nbsp;variables<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$data&nbsp;</span><span style="color: #007700">=&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"current_bid"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"0.00"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"total_bids"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">0<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Construct&nbsp;$variables&nbsp;array&nbsp;for&nbsp;use&nbsp;in&nbsp;parse_variables&nbsp;method<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$variables&nbsp;</span><span style="color: #007700">=&nbsp;array();<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$variables&#91;&#93;&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$data</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">TMPL</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">parse_variables</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$tagdata</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$variables&nbsp;</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">&#125;&nbsp;</span>
</span>
</code></div>

<p>We were hard-coding the <code>current_bid</code> and <code>total_bids</code> values in the <code>$data</code> array as (at that stage) the auction database was empty.</p>

<p>All we need to do is to replace the &#8216;dummy&#8217; values in the <code>$data</code> array with the correct values from the auction database table:</p>

<div class="codeblock"><code><span style="color: #000000">
<span style="color: #007700">public&nbsp;function&nbsp;</span><span style="color: #0000BB">summary</span><span style="color: #007700">()&nbsp;</span><span style="color: #0000BB">&#123;<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;$tagdata&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">TMPL</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">tagdata</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Fetch&nbsp;entry&nbsp;id&nbsp;from&nbsp;tag&nbsp;parameters<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$entry_id&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">TMPL</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fetch_param</span><span style="color: #007700">(</span><span style="color: #DD0000">'entry_id'</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;if(&nbsp;</span><span style="color: #0000BB">$entry_id&nbsp;</span><span style="color: #007700">===&nbsp;</span><span style="color: #0000BB">FALSE&nbsp;</span><span style="color: #007700">)&nbsp;</span><span style="color: #0000BB">&#123;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">return&nbsp;</span><span style="color: #DD0000">""</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">&#125;<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Fetch&nbsp;data&nbsp;from&nbsp;database<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">select</span><span style="color: #007700">(&nbsp;</span><span style="color: #DD0000">"MAX(bid_amount)&nbsp;as&nbsp;current_bid,&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;COUNT(*)&nbsp;as&nbsp;total_bids"&nbsp;</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">where</span><span style="color: #007700">(&nbsp;</span><span style="color: #DD0000">"entry_id"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$entry_id&nbsp;</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">group_by</span><span style="color: #007700">(&nbsp;</span><span style="color: #DD0000">"entry_id"&nbsp;</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$query&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">get</span><span style="color: #007700">(&nbsp;</span><span style="color: #DD0000">"auction"&nbsp;</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;if(&nbsp;</span><span style="color: #0000BB">$query</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">num_rows</span><span style="color: #007700">()&nbsp;==&nbsp;</span><span style="color: #0000BB">0&nbsp;</span><span style="color: #007700">)&nbsp;</span><span style="color: #0000BB">&#123;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;No&nbsp;bids&nbsp;exist&nbsp;in&nbsp;the&nbsp;table,&nbsp;so&nbsp;we'll&nbsp;return&nbsp;zeros<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$data&nbsp;</span><span style="color: #007700">=&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"current_bid"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"0.00"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"total_bids"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">0<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">&#125;&nbsp;</span><span style="color: #007700">else&nbsp;</span><span style="color: #0000BB">&#123;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Fetch&nbsp;the&nbsp;first&nbsp;(and&nbsp;only)&nbsp;result&nbsp;from&nbsp;the&nbsp;SQL&nbsp;query<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$data&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$query</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">row_array</span><span style="color: #007700">();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">&#125;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Construct&nbsp;$variables&nbsp;array&nbsp;for&nbsp;use&nbsp;in&nbsp;parse_variables&nbsp;method<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$variables&nbsp;</span><span style="color: #007700">=&nbsp;array();<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$variables&#91;&#93;&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$data</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">TMPL</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">parse_variables</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$tagdata</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$variables&nbsp;</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">&#125;&nbsp;</span>
</span>
</code></div>

<p>Here we&#8217;ve used the Active Record class to build the SQL to fetch the data:</p>

<div class="codeblock"><code><span style="color: #000000">
<span style="color: #0000BB">SELECT&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;MAX</span><span style="color: #007700">(</span><span style="color: #0000BB">bid_amount</span><span style="color: #007700">)&nbsp;as&nbsp;</span><span style="color: #0000BB">current_bid</span><span style="color: #007700">,&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">COUNT</span><span style="color: #007700">(*)&nbsp;as&nbsp;</span><span style="color: #0000BB">total_bids<br />FROM&nbsp;exp_auction<br />WHERE&nbsp;entry_id&nbsp;</span><span style="color: #007700">=&nbsp;?<br /></span><span style="color: #0000BB">GROUP&nbsp;BY&nbsp;entry_id&nbsp;</span>
</span>
</code></div>

<p>One thing to note is that the database functions are intelligent enough to add (or replace) the database prefix that you configured when you installed EE. For example, my auction database table is actually called <code>exp_auction</code>. I could use <code>$this-&gt;EE-&gt;db-&gt;get( &quot;auction&quot; )</code> or <code>$this-&gt;EE-&gt;db-&gt;get( &quot;exp_auction&quot; )</code> to access it. Equally, if I used a different prefix (you can choose to do this when you install EE), the database functions will use that value. Even if you use <code>$this-&gt;EE-&gt;db-&gt;get( &quot;exp_auction&quot; )</code> and you have chosen to use a different prefix, EE will use the appropriate table name.</p>

<p>We then test to see if any rows have been returned. If not, either the auction&#8217;s entry id does not exist or no bids have been placed yet. In either case, we&#8217;ll display the price and number of bids as zero.</p>

<p>If the SQL query does return a row, we&#8217;ll use that. The <code>$query-&gt;row_array()</code> will return the next (in this case, first) row&#8217;s result as an array, with columns mapped to array elements, eg:</p>

<div class="codeblock"><code><span style="color: #000000">
<span style="color: #007700">Array<br />(<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">&#91;current_bid&#93;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">18.00<br />&nbsp;&nbsp;&nbsp;&nbsp;&#91;total_bids&#93;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">4<br /></span><span style="color: #007700">)&nbsp;</span>
</span>
</code></div>

<h3 id="summary">Summary</h3>

<p>So we now have the <code>&#123;exp:auction:summary}</code> tag working. We are now able to display an auction item with details of the current bidding situation, with a working form that allows visitors to place their own bid.</p>

<p>&nbsp;</p>]]></description>
      <dc:subject><![CDATA[Modules, Our first module,]]></dc:subject>
      <dc:date>2012-05-29T12:33:14+00:00</dc:date>
    </item>

    <item>
      <title><![CDATA[Our first module, part 6: processing the form results]]></title>
      <link>http://ee-recipes.com/articles/details/our-first-module-part-6-processing-the-form-results</link>
      <guid>http://ee-recipes.com/articles/details/our-first-module-part-6-processing-the-form-results#When:12:30:49Z</guid>
      <description><![CDATA[<p>We&#8217;ve built the form to allow users to place a bid for an auction and set up an action method in our module to send the submitted data to. Let&#8217;s read that submitted data and add it to our database of bids.</p>

<p><a href="http://ee-recipes.com/articles/details/adding-a-form">In the previous article</a>, we set up the <code>place_bid()</code> function to receive the form submission. To check <code>place_bids()</code> is actually getting the form data, we&#8217;ll add a line to display any <em>POST-ed</em> data:</p>

<div class="codeblock"><code><span style="color: #000000">
<span style="color: #007700">public&nbsp;function&nbsp;</span><span style="color: #0000BB">place_bid</span><span style="color: #007700">()&nbsp;</span><span style="color: #0000BB">&#123;<br />&nbsp;&nbsp;&nbsp;&nbsp;print_r</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$_POST&nbsp;</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;exit;<br /></span><span style="color: #0000BB">&#125;&nbsp;</span>
</span>
</code></div>

<p>If we submit a bid for one our auctions we should now see:</p>

<div class="codeblock"><code><span style="color: #000000">
<span style="color: #007700">Array<br />(<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">&#91;XID&#93;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">781a30d69d42b2e054bae0fe4a29db4767757caa<br />&nbsp;&nbsp;&nbsp;&nbsp;&#91;entry_id&#93;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">1390<br />&nbsp;&nbsp;&nbsp;&nbsp;&#91;ACT&#93;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">25032029<br />&nbsp;&nbsp;&nbsp;&nbsp;&#91;site_id&#93;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">1<br />&nbsp;&nbsp;&nbsp;&nbsp;&#91;bid_amount&#93;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">10.00<br />&nbsp;&nbsp;&nbsp;&nbsp;&#91;submit&#93;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">Place&nbsp;bid<br /></span><span style="color: #007700">)&nbsp;</span>
</span>
</code></div>

<p>We can see that the inputs <code>entry_id</code> and <code>bid_amount</code> that we defined in the form are there, along with a few others. The <code>ACT</code> value denotes the id of the action that we set up previously (ie, the <code>place_bids</code> function) and ExpressionEngine has added the <code>XID</code> and <code>site_id</code> values. The <code>site_id</code> is used to determine which site is being used when <a href="http://expressionengine.com/user_guide/cp/sites/index.html">Multiple Site Manager</a> is installed (by default it uses the value &#8216;1&#8217;). We&#8217;ll explain the <code>XID</code> value in another part.</p>

<h3 id="fetchingthedata">Fetching the data</h3>

<p>While we can use the <code>$_POST</code> array to get the submitted values, EE provides a better alternative using the <a href="http://expressionengine.com/user_guide/development/usage/input.html">Input</a> class.</p>

<p>The class is loaded by EE and automatically cleans up any input data (eg, POST/GET/COOKIE) provided. It also provides a handful of helper functions. </p>

<p>The <code>$this-&gt;EE-&gt;input-&gt;post()</code> function is a function to access data POSTed to the system by a form, eg: </p>

<div class="codeblock"><code><span style="color: #000000">
<span style="color: #0000BB">$entry_id&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">input</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">post</span><span style="color: #007700">(</span><span style="color: #DD0000">"entry_id"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">TRUE</span><span style="color: #007700">);&nbsp;</span>
</span>
</code></div>

<p>will look for <code>entry_id</code> element of the <code>$_POST</code> array and return it, or FALSE it is does not exist. The optional second parameter (<code>TRUE</code>) tells the function to also apply the built-in <code>xss_clean()</code> function to the data to remove any potential <a href="http://en.wikipedia.org/wiki/Cross-site_scripting">cross-site scripting (XSS)</a> attempts.</p>

<h3 id="addingthebidtothedatabase">Adding the bid to the database</h3>

<p>If we recall <a href="http://ee-recipes.com/articles/details/our-first-module-part-3-creating-a-database-table">in part 3 we created a database</a> to store the details of all the bids. The database had 5 fields:</p>

<ul>
<li>id</li>
<li>entry_id</li>
<li>member_id</li>
<li>bid_amount</li>
<li>bid_date</li>
</ul>

<p>We have the <code>entry_id</code> and <code>bid_amount</code> from the form, and the <code>id</code> field will be automatically generated as a primary key for the row, which leaves just <code>member_id</code> and <code>bid_date</code> to find.</p>

<p>The form to place a bid was only available to registered and logged-in users, so we can be confident that the bid was placed by a valid member. The <a href="http://expressionengine.com/user_guide/development/usage/session.html">Session</a> class stores information about the current member and the <code>userdata</code> function can be used to retrieve the visitor&#8217;s <code>member_id</code>:</p>

<div class="codeblock"><code><span style="color: #000000">
<span style="color: #0000BB">$member_id&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">session</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">userdata</span><span style="color: #007700">(</span><span style="color: #DD0000">'member_id'</span><span style="color: #007700">);&nbsp;</span>
</span>
</code></div>

<p>The <a href="http://expressionengine.com/user_guide/development/usage/localization.html">Localization</a> class allows you to fetch the current time:</p>

<div class="codeblock"><code><span style="color: #000000">
<span style="color: #0000BB">$bid_date&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">localize</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">now</span><span style="color: #007700">;&nbsp;</span>
</span>
</code></div>

<p>CodeIgniter&#8217;s <a href="http://codeigniter.com/user_guide/database/active_record.html#insert">Active Record Database</a> class makes inserting this bid as simple as adding the data to an array and passing it (with the table name) to the <code>insert</code> function:</p>

<div class="codeblock"><code><span style="color: #000000">
<span style="color: #0000BB">$data&nbsp;</span><span style="color: #007700">=&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"entry_id"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$entry_id</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"member_id"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$member_id</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"bid_amount"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$bid_amount</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"bid_date"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$bid_date<br /></span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">insert</span><span style="color: #007700">(</span><span style="color: #DD0000">'auction'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$data</span><span style="color: #007700">);&nbsp;</span>
</span>
</code></div>

<p>(Note: we&#8217;d normally want to do some validation of the data before adding it to the database - we&#8217;ll come back to that in a future article).</p>

<h3 id="whatnext">What next?</h3>

<p>After submitting the form, it&#8217;s likely we&#8217;d want to display a success message and return the user back to the site.</p>

<p>For the time being, we&#8217;ll keep this simple, again using some of EE&#8217;s built-in functions:</p>

<div class="codeblock"><code><span style="color: #000000">
<span style="color: #0000BB">$ret&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">functions</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fetch_site_index</span><span style="color: #007700">();<br /><br /></span><span style="color: #0000BB">$data&nbsp;</span><span style="color: #007700">=&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'title'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'Thanks&nbsp;for&nbsp;your&nbsp;bid'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'heading'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'Thanks&nbsp;for&nbsp;your&nbsp;bid'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'content'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"Your&nbsp;bid&nbsp;has&nbsp;been&nbsp;successfully&nbsp;placed"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'link'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(</span><span style="color: #0000BB">$ret</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"Back&nbsp;to&nbsp;site"</span><span style="color: #007700">)<br />);<br /></span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">output</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">show_message</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">);&nbsp;</span>
</span>
</code></div>

<p>The <a href="http://expressionengine.com/user_guide/development/reference/functions.html">Function</a> class&#8217;s <a href="http://expressionengine.com/user_guide/development/reference/functions.html#fetch-site-index-add-slash-int-session-id-int"><code>fetch_site_index()</code></a> method will return the URL of the site&#8217;s homepage. This is where we&#8217;ll send users (for now) once the have placed their bid.</p>

<p>The Output class has a <code>show_message()</code> function that will display a &#8216;standard&#8217; EE message box. </p>

<p><img src="http://ee-recipes.com/images/article/output.png" alt="ExpressionEngine output message" width="552" height="152"  /></p>

<p>It takes 4 parameters:</p>

<ul>
<li><code>title</code> sets the browser window&#8217;s title</li>
<li><code>heading</code> sets the message box&#8217;s heading</li>
<li><code>content</code> provides the message to display</li>
<li><code>link</code> is an array containing the URL to return to, and the return link&#8217;s text</li>
</ul>

<h3 id="summary">Summary</h3>

<p>There&#8217;s still some work to do on the form processing before we can be happy with it (eg, validation), but we now have a form that accepts the visitor&#8217;s bid and stores it in the database.</p>

<p>&nbsp;</p>]]></description>
      <dc:subject><![CDATA[Modules, Our first module,]]></dc:subject>
      <dc:date>2012-05-29T12:30:49+00:00</dc:date>
    </item>

    <item>
      <title><![CDATA[Our first module, part 5: adding a form]]></title>
      <link>http://ee-recipes.com/articles/details/adding-a-form</link>
      <guid>http://ee-recipes.com/articles/details/adding-a-form#When:21:56:07Z</guid>
      <description><![CDATA[<p>Next we need to allow users to place their bids for an auction item. First, let&#8217;s remember how our module has been specified to work:</p>

<ul>
<li>each auction item is associated with a channel entry which stores all the the information about the item. Our module will store details about any bids for the item.</li>
<li>only registered and logged in members can place a bid</li>
</ul>

<p>In our module class (mod.auction.php) we need to add a tag to create the form to handle the bid: <code>&#123;exp:auction:form}</code>. In our <code>mod.auction.php</code> file we need to a <code>form()</code> method (remember, the module&#8217;s function name matches the tag&#8217;s third segment).</p>

<div class="codeblock"><code><span style="color: #000000">
<span style="color: #007700">public&nbsp;function&nbsp;</span><span style="color: #0000BB">form</span><span style="color: #007700">()&nbsp;</span><span style="color: #0000BB">&#123;<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&#125;&nbsp;</span>
</span>
</code></div>

<p>ExpressionEngine forms (for example, the <a href="http://expressionengine.com/user_guide/modules/comment/index.html#comment-submission-form">comment form</a>) assume nothing about how the form is laid out. All we need to do is add the form&#8217;s opening and closing tags and assume that the developer using the form includes all the necessary form elements (with the correct names)</p>

<div class="codeblock"><code><span style="color: #000000">
<span style="color: #0000BB">&#123;exp</span><span style="color: #007700">:</span><span style="color: #0000BB">auction</span><span style="color: #007700">:</span><span style="color: #0000BB">form&#125;<br /></span><span style="color: #007700">&lt;</span><span style="color: #0000BB">p</span><span style="color: #007700">&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&amp;</span><span style="color: #0000BB">pound</span><span style="color: #007700">;&nbsp;&lt;</span><span style="color: #0000BB">input&nbsp;type</span><span style="color: #007700">=</span><span style="color: #DD0000">"text"&nbsp;</span><span style="color: #0000BB">name</span><span style="color: #007700">=</span><span style="color: #DD0000">"bid_amount"&nbsp;</span><span style="color: #0000BB">value</span><span style="color: #007700">=</span><span style="color: #DD0000">"0.00"&nbsp;</span><span style="color: #0000BB">size</span><span style="color: #007700">=</span><span style="color: #DD0000">"5"&nbsp;</span><span style="color: #007700">/&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color: #0000BB">input&nbsp;type</span><span style="color: #007700">=</span><span style="color: #DD0000">"submit"&nbsp;</span><span style="color: #0000BB">name</span><span style="color: #007700">=</span><span style="color: #DD0000">"submit"&nbsp;</span><span style="color: #0000BB">value</span><span style="color: #007700">=</span><span style="color: #DD0000">"Place&nbsp;bid"&nbsp;</span><span style="color: #007700">/&gt;<br />&lt;/</span><span style="color: #0000BB">p</span><span style="color: #007700">&gt;<br /></span><span style="color: #0000BB">&#123;</span><span style="color: #007700">/</span><span style="color: #0000BB">exp</span><span style="color: #007700">:</span><span style="color: #0000BB">auction</span><span style="color: #007700">:</span><span style="color: #0000BB">form&#125;&nbsp;</span>
</span>
</code></div>

<h3 id="makingsurewehaveallthedata">Making sure we have all the data</h3>

<p>Let&#8217;s make sure we have all the data we need to save the bid. When we designed the exp_auction database table earlier we decided we&#8217;d need:</p>

<ul>
<li>which entry the bid is for</li>
<li>the current user</li>
<li>the bid amount</li>
<li>the current time</li>
</ul>

<p>The bid amount is an element of the form, so will be passed when the form is submitted. </p>

<p>The current user and current time we&#8217;ll set when the bid is actually submitted (rather than when the form is created) so we&#8217;ll find these later.</p>

<p>The only data we still need to add to the form is the item&#8217;s <code>entry_id</code>. There are 2 ways we could do this:</p>

<ol>
<li>we could nest the <code>&#123;exp:auction:form}</code> tag within the <code>&#123;exp:channel:entries}</code> tag and supply it as a parameter to form tag (like we did with the <code>&#123;exp:auction:summary}</code> tag)</li>
<li>we could get the <code>&#123;exp:auction:form}</code> tag to work out which entry it should use from the current URL. This will only work if the form is used on a <em>single-entry</em> page, but this is often the case and is a useful technique to use.</li>
</ol>

<p>For simplicity&#8217;s sake we&#8217;ll use the first option and pass it as a parameter. We&#8217;ll look at the second option later.</p>

<p>As with the <code>summary</code> tag we can now use <code>$entry_id = $this-&gt;EE-&gt;TMPL-&gt;fetch_param(&#8216;entry_id&#8217;);</code> to fetch the entry id.</p>

<p>So we have all the data we require for now, so let&#8217;s build the form.</p>

<h3 id="buildingtheform">Building the form</h3>

<p>To add the opening <code>&lt;form&gt;</code> tag we use the function: <a href="http://expressionengine.com/user_guide/development/reference/functions.html#form-declaration-data-array"><code>$this-&gt;EE-&gt;functions-&gt;form_declaration()</code></a>. Using the provided function has a few advantages that we&#8217;ll see later.</p>

<p>This is how our <code>form</code> method in <code>mod.auction.php</code> will now look:</p>

<div class="codeblock"><code><span style="color: #000000">
<span style="color: #007700">public&nbsp;function&nbsp;</span><span style="color: #0000BB">form</span><span style="color: #007700">()&nbsp;</span><span style="color: #0000BB">&#123;<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Find&nbsp;the&nbsp;entry_id&nbsp;of&nbsp;the&nbsp;auction&nbsp;to&nbsp;add&nbsp;the&nbsp;form&nbsp;for<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$entry_id&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">TMPL</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fetch_param</span><span style="color: #007700">(</span><span style="color: #DD0000">'entry_id'</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;if(&nbsp;</span><span style="color: #0000BB">$entry_id&nbsp;</span><span style="color: #007700">===&nbsp;</span><span style="color: #0000BB">FALSE&nbsp;</span><span style="color: #007700">)&nbsp;</span><span style="color: #0000BB">&#123;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">return&nbsp;</span><span style="color: #DD0000">""</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">&#125;<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Build&nbsp;an&nbsp;array&nbsp;to&nbsp;hold&nbsp;the&nbsp;form's&nbsp;hidden&nbsp;fields<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$hidden_fields&nbsp;</span><span style="color: #007700">=&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"entry_id"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$entry_id<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Build&nbsp;an&nbsp;array&nbsp;with&nbsp;the&nbsp;form&nbsp;data<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$form_data&nbsp;</span><span style="color: #007700">=&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"id"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">TMPL</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">form_id</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"class"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">TMPL</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">form_class</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"hidden_fields"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$hidden_fields<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Fetch&nbsp;contents&nbsp;of&nbsp;the&nbsp;tag&nbsp;pair,&nbsp;ie,&nbsp;the&nbsp;form&nbsp;contents<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$tagdata&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">TMPL</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">tagdata</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$form&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">functions</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">form_declaration</span><span style="color: #007700">(</span><span style="color: #0000BB">$form_data</span><span style="color: #007700">)&nbsp;.&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$tagdata&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"&lt;/form&gt;"</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$form</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">&#125;&nbsp;</span>
</span>
</code></div>

<p>We pass the <code>$form_data</code> array to the <code>form_declaration()</code> function to construct the <code>&lt;form&gt;</code> tag. The <code>$form_data</code> array itself contains an array of hidden form variables (as if we&#8217;d used `<input type="hidden" /> tags) which stores the value of the entry_id that we fetched from the parameter.</p>

<p>If we look at the HTML that this generates, we&#8217;ll see:</p>

<div class="codeblock"><code><span style="color: #000000">
<span style="color: #007700">&lt;</span><span style="color: #0000BB">form&nbsp;method</span><span style="color: #007700">=</span><span style="color: #DD0000">"post"&nbsp;</span><span style="color: #0000BB">action</span><span style="color: #007700">=</span><span style="color: #DD0000">"http://site.com/index.php"</span><span style="color: #007700">&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color: #0000BB">div&nbsp;</span><span style="color: #007700">class=</span><span style="color: #DD0000">'hiddenFields'</span><span style="color: #007700">&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color: #0000BB">input&nbsp;type</span><span style="color: #007700">=</span><span style="color: #DD0000">"hidden"&nbsp;</span><span style="color: #0000BB">name</span><span style="color: #007700">=</span><span style="color: #DD0000">"XID"&nbsp;</span><span style="color: #0000BB">value</span><span style="color: #007700">=</span><span style="color: #DD0000">"564dfaeed1f70b0ae983d3f..."&nbsp;</span><span style="color: #007700">/&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color: #0000BB">input&nbsp;type</span><span style="color: #007700">=</span><span style="color: #DD0000">"hidden"&nbsp;</span><span style="color: #0000BB">name</span><span style="color: #007700">=</span><span style="color: #DD0000">"entry_id"&nbsp;</span><span style="color: #0000BB">value</span><span style="color: #007700">=</span><span style="color: #DD0000">"5"&nbsp;</span><span style="color: #007700">/&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color: #0000BB">input&nbsp;type</span><span style="color: #007700">=</span><span style="color: #DD0000">"hidden"&nbsp;</span><span style="color: #0000BB">name</span><span style="color: #007700">=</span><span style="color: #DD0000">"site_id"&nbsp;</span><span style="color: #0000BB">value</span><span style="color: #007700">=</span><span style="color: #DD0000">"1"&nbsp;</span><span style="color: #007700">/&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;/</span><span style="color: #0000BB">div</span><span style="color: #007700">&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color: #0000BB">p</span><span style="color: #007700">&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;</span><span style="color: #0000BB">pound</span><span style="color: #007700">;&nbsp;&lt;</span><span style="color: #0000BB">input&nbsp;type</span><span style="color: #007700">=</span><span style="color: #DD0000">"text"&nbsp;</span><span style="color: #0000BB">name</span><span style="color: #007700">=</span><span style="color: #DD0000">"bid_amount"&nbsp;</span><span style="color: #0000BB">value</span><span style="color: #007700">=</span><span style="color: #DD0000">"0.00"&nbsp;</span><span style="color: #0000BB">size</span><span style="color: #007700">=</span><span style="color: #DD0000">"5"&nbsp;</span><span style="color: #007700">/&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color: #0000BB">input&nbsp;type</span><span style="color: #007700">=</span><span style="color: #DD0000">"submit"&nbsp;</span><span style="color: #0000BB">name</span><span style="color: #007700">=</span><span style="color: #DD0000">"submit"&nbsp;</span><span style="color: #0000BB">value</span><span style="color: #007700">=</span><span style="color: #DD0000">"Place&nbsp;bid"&nbsp;</span><span style="color: #007700">/&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;/</span><span style="color: #0000BB">p</span><span style="color: #007700">&gt;<br />&lt;/</span><span style="color: #0000BB">form</span><span style="color: #007700">&gt;&nbsp;</span>
</span>
</code></div>

<p>The <code>form_declaration()</code> function has added another 2 hidden fields: <code>XID</code> and <code>site_id</code></p>

<p>The <code>site_id</code> refers to site if you are using the <a href="http://expressionengine.com/user_guide/cp/sites/index.html">Multi Site Manager</a></p>

<p>The <code>XID</code> variable is used to prevent spam and protect against Cross Site Request Forgery (CSRF) - we&#8217;ll look at this in more detail later.</p>

<p>The other thing to note is that the form&#8217;s <code>action</code> parameter has been set to the site&#8217;s <code>index.php</code> page. Obviously, at the moment, this page is not expecting to handle the auction&#8217;s form submission. We need to tell the form <em>where</em> to send its data by adding an <em>action</em>.</p>

<h3 id="actions">Actions</h3>

<p>An action allows us to set up a URL that will call a specific module&#8217;s function. The URL will all have the format: <code>http://site.com/index.php?ACT=id</code> where <code>id</code> will tell EE which action to use.</p>

<p>Actions are stored in the <code>exp_actions</code> table which has a simple structure containing 3 fields: id, class, method.</p>

<p>When EE receives a request with the <code>ACT=</code> query string, it will look up the id in the <code>exp_actions</code> table and (if it exists) call the appropriate class method it finds.</p>

<p>We need to add an action for our module so let&#8217;s go back to our <code>upd.auction.php</code> file and modify the <code>install</code> method to add it when the module is installed.</p>

<div class="codeblock"><code><span style="color: #000000">
<span style="color: #007700">public&nbsp;function&nbsp;</span><span style="color: #0000BB">install</span><span style="color: #007700">()<br /></span><span style="color: #0000BB">&#123;<br />&nbsp;&nbsp;&nbsp;&nbsp;$mod_data&nbsp;</span><span style="color: #007700">=&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'module_name'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">module_name</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'module_version'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">version</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'has_cp_backend'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"y"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'has_publish_fields'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'n'<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">insert</span><span style="color: #007700">(</span><span style="color: #DD0000">'modules'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$mod_data</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">load</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">dbforge</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$fields&nbsp;</span><span style="color: #007700">=&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'id'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'type'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'int'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'constraint'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'10'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'unsigned'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">TRUE</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'auto_increment'</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">TRUE<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'entry_id'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'type'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'int'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'constraint'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'10'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'unsigned'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">TRUE</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'null'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">FALSE<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'member_id'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'type'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'int'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'constraint'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'10'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'unsigned'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">TRUE</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'null'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">FALSE<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'bid_amount'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'type'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'decimal'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'constraint'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'7,2'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'default'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'0.00'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'null'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">FALSE<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'bid_date'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'type'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'int'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'constraint'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'10'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'unsigned'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">TRUE</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'default'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'0'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'null'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">FALSE<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">dbforge</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">add_field</span><span style="color: #007700">(</span><span style="color: #0000BB">$fields</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">dbforge</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">add_key</span><span style="color: #007700">(</span><span style="color: #DD0000">'id'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">TRUE</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">dbforge</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">create_table</span><span style="color: #007700">(</span><span style="color: #DD0000">'auction'</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$data&nbsp;</span><span style="color: #007700">=&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'class'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'Auction'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'method'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'place_bid'<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">insert</span><span style="color: #007700">(</span><span style="color: #DD0000">'actions'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$data</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">TRUE</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">&#125;&nbsp;</span>
</span>
</code></div>

<p>When we&#8217;ve added this, remove and install the module again to create the action. This will remove any existing data (if we had any) which would obviously be a bad thing to do in a live environment, but as we are still in development it is not a problem yet. We&#8217;ll look at how to safely make updates in a later article.</p>

<p>This should have added a line to the <code>exp_actions</code> table.</p>

<p>This new action points to the <code>Auction</code> class&#8217;s method <code>place_bid</code> so we need to this function to the <code>mod.auction.php</code> file:</p>

<div class="codeblock"><code><span style="color: #000000">
<span style="color: #007700">public&nbsp;function&nbsp;</span><span style="color: #0000BB">place_bid</span><span style="color: #007700">()&nbsp;</span><span style="color: #0000BB">&#123;<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&#125;&nbsp;</span>
</span>
</code></div>

<p>Finally, to tell the form to send its data to this action we need to update the <code>form()</code> method and we do this by adding another hidden variable:</p>

<div class="codeblock"><code><span style="color: #000000">
<span style="color: #007700">public&nbsp;function&nbsp;</span><span style="color: #0000BB">form</span><span style="color: #007700">()&nbsp;</span><span style="color: #0000BB">&#123;<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Find&nbsp;the&nbsp;entry_id&nbsp;of&nbsp;the&nbsp;auction&nbsp;to&nbsp;add&nbsp;the&nbsp;form&nbsp;for<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$entry_id&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">TMPL</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fetch_param</span><span style="color: #007700">(</span><span style="color: #DD0000">'entry_id'</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;if(&nbsp;</span><span style="color: #0000BB">$entry_id&nbsp;</span><span style="color: #007700">===&nbsp;</span><span style="color: #0000BB">FALSE&nbsp;</span><span style="color: #007700">)&nbsp;</span><span style="color: #0000BB">&#123;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">return&nbsp;</span><span style="color: #DD0000">""</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">&#125;<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Build&nbsp;an&nbsp;array&nbsp;to&nbsp;hold&nbsp;the&nbsp;form's&nbsp;hidden&nbsp;fields<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$hidden_fields&nbsp;</span><span style="color: #007700">=&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"entry_id"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$entry_id</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"ACT"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">functions</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fetch_action_id</span><span style="color: #007700">(&nbsp;</span><span style="color: #DD0000">'Auction'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'place_bid'&nbsp;</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;);<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Build&nbsp;an&nbsp;array&nbsp;with&nbsp;the&nbsp;form&nbsp;data<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$form_data&nbsp;</span><span style="color: #007700">=&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"id"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">TMPL</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">form_id</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"class"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">TMPL</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">form_class</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"hidden_fields"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$hidden_fields<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Fetch&nbsp;contents&nbsp;of&nbsp;the&nbsp;tag&nbsp;pair,&nbsp;ie,&nbsp;the&nbsp;form&nbsp;contents<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$tagdata&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">TMPL</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">tagdata</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$form&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">functions</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">form_declaration</span><span style="color: #007700">(</span><span style="color: #0000BB">$form_data</span><span style="color: #007700">)&nbsp;.&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$tagdata&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"&lt;/form&gt;"</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$form</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">&#125;&nbsp;</span>
</span>
</code></div>

<p>The <a href="http://expressionengine.com/user_guide/development/reference/functions.html#fetch-action-id-class-str-method-str">$this-&gt;EE-&gt;functions-&gt;fetch_action_id()</a> provides an easy way to fetch the action id given the class and method name (the action id will change if the module is re-installed, whereas the class and method name won&#8217;t).</p>

<h3 id="summary">Summary</h3>

<p>To recap, we&#8217;ve created a template tag to build a form. We then added an action and told the form to post its results to it. On receiving the form the action then tells the system which class and method should handle the response.</p>

<p>In the next article we&#8217;ll add the data from the form to the auction database table.</p>]]></description>
      <dc:subject><![CDATA[Modules, Our first module,]]></dc:subject>
      <dc:date>2012-05-03T21:56:07+00:00</dc:date>
    </item>

    <item>
      <title><![CDATA[Resources]]></title>
      <link>http://ee-recipes.com/articles/details/resources</link>
      <guid>http://ee-recipes.com/articles/details/resources#When:13:08:40Z</guid>
      <description><![CDATA[<p>Here are some useful ExpressionEngine resources:</p>]]></description>
      <dc:subject><![CDATA[]]></dc:subject>
      <dc:date>2012-02-03T13:08:40+00:00</dc:date>
    </item>

    <item>
      <title><![CDATA[Our first module, part 4: adding a tag to our module]]></title>
      <link>http://ee-recipes.com/articles/details/adding-a-tag-to-our-module</link>
      <guid>http://ee-recipes.com/articles/details/adding-a-tag-to-our-module#When:13:15:00Z</guid>
      <description><![CDATA[<p>Now let&#8217;s turn to the front-end of the website and remind ourselves what we are trying to do.</p>

<p><img src="http://ee-recipes.com/images/article/module_wireframe1.png" alt="Module concept"   /></p>

<p>On the listing page, we want to display a list of all the auction items. All the basic information about the item will be stored in a normal channel entry, so we need to add a channel to store the items that we are auctioning. </p>

<h3 id="createachannel">Create a channel</h3>

<p>In <code>Admin &gt; Channel Administration</code> create a channel and a field group. We don&#8217;t need this to be fancy - in fact we don&#8217;t really need any custom fields - but we&#8217;ll add an image and a description (with field names &#123;auction_image} and &#123;auction_description}). We&#8217;ll add a few test item to auction too.</p>

<p><img src="http://ee-recipes.com/images/article/auction_entries.png" alt="Some dummy auction entries"  /></p>

<p>We can now display a list of all the items with a basic <code>&#123;exp:channel:entries}</code> tag:</p>

<div class="codeblock"><code><span style="color: #000000">
<span style="color: #0000BB">&#123;exp</span><span style="color: #007700">:</span><span style="color: #0000BB">channel</span><span style="color: #007700">:</span><span style="color: #0000BB">entries&nbsp;channel</span><span style="color: #007700">=</span><span style="color: #DD0000">"auction"</span><span style="color: #0000BB">&#125;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">&lt;</span><span style="color: #0000BB">h3</span><span style="color: #007700">&gt;</span><span style="color: #0000BB">&#123;title&#125;</span><span style="color: #007700">&lt;/</span><span style="color: #0000BB">h3</span><span style="color: #007700">&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;</span><span style="color: #0000BB">img&nbsp;src</span><span style="color: #007700">=</span><span style="color: #DD0000">"&#123;auction_image&#125;"&nbsp;</span><span style="color: #007700">/&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">&#123;auction_description&#125;<br /><br />&#123;</span><span style="color: #007700">/</span><span style="color: #0000BB">exp</span><span style="color: #007700">:</span><span style="color: #0000BB">channel</span><span style="color: #007700">:</span><span style="color: #0000BB">entries&#125;&nbsp;</span>
</span>
</code></div>

<p>In addition to the basic channel entry data we also want to display the item&#8217;s current auction price and the number of bids it has received. This is where our module comes in.</p>

<h3 id="addatagtoourmodule">Add a tag to our module</h3>

<p>The first tag we need our add to our module is to display the auction status: the current bid and how many bids have been placed.</p>

<p>Module tags are composed the same way as plugin tags and are made up of 3 segments, eg, &#123;exp:short_name:function}. The first segment is always exp which tells the system it has encountered a tag. The second segment is the module&#8217;s short name and tells EE which module to use. The third segment is the tag&#8217;s function - it explains what the tag will do - and its name exactly corresponds to a method in the module&#8217;s class.</p>

<p>We&#8217;re going to call our tag &#123;exp:auction:summary} so in our mod.auction.php file we need to add a <code>summary</code> function:</p>

<div class="codeblock"><code><span style="color: #000000">
<span style="color: #007700">public&nbsp;function&nbsp;</span><span style="color: #0000BB">summary</span><span style="color: #007700">()&nbsp;</span><span style="color: #0000BB">&#123;<br />&#125;&nbsp;</span>
</span>
</code></div>

<p>Like <a href="http://ee-recipes.com/articles/details/developing_our_first_plugin_part_2">plugins</a>, module tags can be single tags or tag pairs. If you use a tag pair, the content between the opening and closing tags is available to the module. Often this content will contain variables that can be replaced by the module.</p>

<p>We&#8217;re going to use the summary tag as a tag pair, like this:</p>

<div class="codeblock"><code><span style="color: #000000">
<span style="color: #0000BB">&#123;exp</span><span style="color: #007700">:</span><span style="color: #0000BB">auction</span><span style="color: #007700">:</span><span style="color: #0000BB">summary&#125;<br /></span><span style="color: #007700">&lt;</span><span style="color: #0000BB">p</span><span style="color: #007700">&gt;</span><span style="color: #0000BB">Current&nbsp;bid</span><span style="color: #007700">:&nbsp;&amp;</span><span style="color: #0000BB">pound</span><span style="color: #007700">;</span><span style="color: #0000BB">&#123;current_bid&#125;&nbsp;</span><span style="color: #007700">(</span><span style="color: #0000BB">&#123;total_bids&#125;&nbsp;bids</span><span style="color: #007700">)&lt;/</span><span style="color: #0000BB">p</span><span style="color: #007700">&gt;&nbsp;<br /></span><span style="color: #0000BB">&#123;</span><span style="color: #007700">/</span><span style="color: #0000BB">exp</span><span style="color: #007700">:</span><span style="color: #0000BB">auction</span><span style="color: #007700">:</span><span style="color: #0000BB">summary&#125;&nbsp;</span>
</span>
</code></div>

<p>And the tag will have 2 variables &#123;current_bid} and &#123;total_bids}.</p>

<h3 id="usingthetemplateclass">Using the Template class</h3>

<p>We need to fetch the text between the opening and closing tags. EE&#8217;s <a href="http://expressionengine.com/user_guide/development/usage/template.html">Template class</a> provides a simple variable to do this:</p>

<div class="codeblock"><code><span style="color: #000000">
<span style="color: #0000BB">$tagdata&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">TMPL</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">tagdata</span><span style="color: #007700">;&nbsp;</span>
</span>
</code></div>

<p>We then need to replace the variables (<code>current_bid</code> and <code>total_bids</code>) in <code>$tagdata</code> with the required values. We could simply use the PHP function <code>str_replace</code> to do this. For example:</p>

<div class="codeblock"><code><span style="color: #000000">
<span style="color: #007700">public&nbsp;function&nbsp;</span><span style="color: #0000BB">summary</span><span style="color: #007700">()&nbsp;</span><span style="color: #0000BB">&#123;<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;$tagdata&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">TMPL</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">tagdata</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$tagdata&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">str_replace</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">LD</span><span style="color: #007700">.</span><span style="color: #DD0000">"current_bid"</span><span style="color: #007700">.</span><span style="color: #0000BB">RD</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"0.00"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$tagdata&nbsp;</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$tagdata&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">str_replace</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">LD</span><span style="color: #007700">.</span><span style="color: #DD0000">"total_bids"</span><span style="color: #007700">.</span><span style="color: #0000BB">RD</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"0"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$tagdata&nbsp;</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$tagdata</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">&#125;&nbsp;</span>
</span>
</code></div>

<p>(Note that EE adds an <code>LD</code> and <code>RD</code> constants that correspond to the <code>&#123;</code> and <code>}</code> characters of the tag.)</p>

<p>This function will return currently hard-coded values of &#8220;0.00&#8221; and &#8220;0&#8221; for the <code>current_bid</code> and <code>total_bids</code> variables.</p>

<p>However, the <a href="http://expressionengine.com/user_guide/development/usage/template.html">Template class</a> offers a more powerful way to do this.</p>

<h3 id="abetterway">A better way</h3>

<p>By providing the <code>$this-&gt;EE-&gt;TMPL-&gt;parse_variables()</code> method with the tagdata and an array of variables, it will:</p>

<ul>
<li>replace all the variable tags with their values</li>
<li>handle any date formatting that is required (eg, <code>&#123;date format=&quot;%d/%m/%Y&quot;}</code>)</li>
<li>automatically prepare conditionals for any variable you provide (eg, <code>&#123;if total_bids &gt; 5}Hot!&#123;/if}</code>)</li>
</ul>

<p>If you are returning multiple rows of data (we&#8217;ll come to that later), it will also provide </p>

<ul>
<li><code>&#123;count}</code></li>
<li><code>&#123;total_results}</code></li>
<li><code>&#123;switch}</code></li>
</ul>

<p>variables for you automatically.</p>

<p>There is more information about the <a href="http://expressionengine.com/user_guide/development/usage/template.html#parsing-variables"><code>parse_variables()</code> function here</a>, and we will be using it to display most of our tags.</p>

<p>Adding this to our function gives us:</p>

<div class="codeblock"><code><span style="color: #000000">
<span style="color: #007700">public&nbsp;function&nbsp;</span><span style="color: #0000BB">summary</span><span style="color: #007700">()&nbsp;</span><span style="color: #0000BB">&#123;<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;$tagdata&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">TMPL</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">tagdata</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Build&nbsp;array&nbsp;of&nbsp;our&nbsp;variables<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$data&nbsp;</span><span style="color: #007700">=&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"current_bid"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"0.00"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"total_bids"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">0<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Construct&nbsp;$variables&nbsp;array&nbsp;for&nbsp;use&nbsp;in&nbsp;parse_variables&nbsp;method<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$variables&nbsp;</span><span style="color: #007700">=&nbsp;array();<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$variables&#91;&#93;&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$data</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">TMPL</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">parse_variables</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$tagdata</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$variables&nbsp;</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">&#125;&nbsp;</span>
</span>
</code></div>

<p>The one thing to note here is that the <code>parse_variables</code> function can be used to output multiple rows of data. Our tag only requires a single row so out <code>$data</code> array is stored as an element of the <code>$variables</code> array.</p>

<h3 id="tagparameters">Tag parameters</h3>

<p>We&#8217;ll look at one more <a href="http://expressionengine.com/user_guide/development/usage/template.html">Template class</a> function that we&#8217;ll need to use: <code>fetch_param()</code>.</p>

<p>Our <code>&#123;exp:auction:summary}</code> tag will need to know which auction item we are looking at. If we were on a &#8216;single-entry&#8217; page (ie, a page that is only displaying the details of one entry) we could fetch the entry information from the URL., but as we will be using this tag on listing pages (where multiple entries will be shown) we&#8217;ll need to tell it which entry we are displaying the information for.</p>

<p>To do this we&#8217;ll add an <code>entry_id=</code> parameter to the tag:</p>

<div class="codeblock"><code><span style="color: #000000">
<span style="color: #0000BB">&#123;exp</span><span style="color: #007700">:</span><span style="color: #0000BB">channel</span><span style="color: #007700">:</span><span style="color: #0000BB">entries&nbsp;channel</span><span style="color: #007700">=</span><span style="color: #DD0000">"auction"&nbsp;</span><span style="color: #007700">...&nbsp;</span><span style="color: #0000BB">&#125;<br /></span><span style="color: #007700">...<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">&#123;exp</span><span style="color: #007700">:</span><span style="color: #0000BB">auction</span><span style="color: #007700">:</span><span style="color: #0000BB">summary&nbsp;entry_id</span><span style="color: #007700">=</span><span style="color: #DD0000">"&#123;entry_id&#125;"</span><span style="color: #0000BB">&#125;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">&lt;</span><span style="color: #0000BB">p</span><span style="color: #007700">&gt;</span><span style="color: #0000BB">Current&nbsp;bid</span><span style="color: #007700">:&nbsp;&amp;</span><span style="color: #0000BB">pound</span><span style="color: #007700">;</span><span style="color: #0000BB">&#123;current_bid&#125;&nbsp;</span><span style="color: #007700">(</span><span style="color: #0000BB">&#123;total_bids&#125;&nbsp;bids</span><span style="color: #007700">)&lt;/</span><span style="color: #0000BB">p</span><span style="color: #007700">&gt;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">&#123;</span><span style="color: #007700">/</span><span style="color: #0000BB">exp</span><span style="color: #007700">:</span><span style="color: #0000BB">auction</span><span style="color: #007700">:</span><span style="color: #0000BB">summary&#125;<br /></span><span style="color: #007700">...<br /></span><span style="color: #0000BB">&#123;</span><span style="color: #007700">/</span><span style="color: #0000BB">exp</span><span style="color: #007700">:</span><span style="color: #0000BB">channel</span><span style="color: #007700">:</span><span style="color: #0000BB">entries&#125;&nbsp;</span>
</span>
</code></div>

<p>We can then use the <code>fetch_param()</code> function to find the item&#8217;s <code>entry_id</code>:</p>

<div class="codeblock"><code><span style="color: #000000">
<span style="color: #007700">public&nbsp;function&nbsp;</span><span style="color: #0000BB">summary</span><span style="color: #007700">()&nbsp;</span><span style="color: #0000BB">&#123;<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Find&nbsp;the&nbsp;entry_id&nbsp;of&nbsp;the&nbsp;auction&nbsp;to&nbsp;display<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$entry_id&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">TMPL</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fetch_param</span><span style="color: #007700">(</span><span style="color: #DD0000">'entry_id'</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;if(&nbsp;</span><span style="color: #0000BB">$entry_id&nbsp;</span><span style="color: #007700">===&nbsp;</span><span style="color: #0000BB">FALSE&nbsp;</span><span style="color: #007700">)&nbsp;</span><span style="color: #0000BB">&#123;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">return&nbsp;</span><span style="color: #DD0000">""</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">&#125;<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;$tagdata&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">TMPL</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">tagdata</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Build&nbsp;array&nbsp;of&nbsp;our&nbsp;variables<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$data&nbsp;</span><span style="color: #007700">=&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"current_bid"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"0.00"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"total_bids"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">0<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Construct&nbsp;$variables&nbsp;array&nbsp;for&nbsp;use&nbsp;in&nbsp;parse_variables&nbsp;method<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$variables&nbsp;</span><span style="color: #007700">=&nbsp;array();<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$variables&#91;&#93;&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$data</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">TMPL</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">parse_variables</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$tagdata</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$variables&nbsp;</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">&#125;&nbsp;</span>
</span>
</code></div>

<p>If the <code>entry_id=</code> parameter is not provided in the tag, the <code>fetch_param()</code> method will return <code>FALSE</code>. If this happens in our <code>summary()</code> function, we will return an empty string to replace the tag (ie, we will not display anything). On other occasions you might want to display an error or an alternative result. You can also make the <code>fetch_param()</code> function return a default value, by using a <a href="http://ee-recipes.com/articles/details/developing_our_first_plugin_part_2#parameters">second, optional parameter</a>.</p>

<h3 id="summary">Summary</h3>

<p>Our module&#8217;s <code>summary</code> tag is now replacing its variables and displaying, but we&#8217;re currently hard-coding the auction item&#8217;s details.</p>

<p>In the next article, we&#8217;ll look at adding the form to allow users to place a bid, before returning to our summary function to finish it off by displaying the correct results from the database.</p>

<p>&nbsp;</p>]]></description>
      <dc:subject><![CDATA[Modules, Our first module,]]></dc:subject>
      <dc:date>2011-10-28T13:15:00+00:00</dc:date>
    </item>

    <item>
      <title><![CDATA[Our first module, part 3: creating a database table]]></title>
      <link>http://ee-recipes.com/articles/details/our-first-module-part-3-creating-a-database-table</link>
      <guid>http://ee-recipes.com/articles/details/our-first-module-part-3-creating-a-database-table#When:11:12:28Z</guid>
      <description><![CDATA[<p>Now we have <a href="http://ee-recipes.com/articles/details/our-first-module-part-2-setting-up-the-file-structure">the add-on&#8217;s files created</a>, and we can install and remove it, lets started adding the functionality.</p>

<p>In order to store details of who has bid for what, the module will need its own database table. This table needs to store:</p>

<ul>
<li>who has made the bid</li>
<li>which item the bid was for</li>
<li>the value of the bid</li>
<li>what time the bid was placed</li>
</ul>

<p><a href="http://ee-recipes.com/articles/details/our-first-module-part-1">Remember</a>, that we will be re-using as much built-in EE functionality as we can, so all the data about the auction item is going to be stored in a channel entry, and only logged-in members can bid. </p>

<p>This means we the data need to store is:</p>

<ul>
<li>the member id of the currently logged in user</li>
<li>the entry id of the relevant channel entry</li>
<li>the value of the bid</li>
<li>the current time</li>
</ul>

<h3 id="creatingthedatabasetable">Creating the database table</h3>

<p>This table needs to be created when the module is installed, so we&#8217;ll add it to the <code>install</code> method of our <code>upd.auction.php</code> file.</p>

<p>We&#8217;ll modify the <code>install</code> function of <code>upd.auction.php</code> to create the database table:</p>

<div class="codeblock"><code><span style="color: #000000">
<span style="color: #007700">public&nbsp;function&nbsp;</span><span style="color: #0000BB">install</span><span style="color: #007700">()<br /></span><span style="color: #0000BB">&#123;<br />&nbsp;&nbsp;&nbsp;&nbsp;$mod_data&nbsp;</span><span style="color: #007700">=&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'module_name'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">module_name</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'module_version'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">version</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'has_cp_backend'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"y"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'has_publish_fields'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'n'<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">insert</span><span style="color: #007700">(</span><span style="color: #DD0000">'modules'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$mod_data</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">load</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">dbforge</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$fields&nbsp;</span><span style="color: #007700">=&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'id'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'type'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'int'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'constraint'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'10'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'unsigned'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">TRUE</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'auto_increment'</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">TRUE<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'entry_id'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'type'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'int'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'constraint'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'10'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'unsigned'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">TRUE</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'null'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">FALSE<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'member_id'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'type'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'int'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'constraint'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'10'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'unsigned'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">TRUE</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'null'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">FALSE<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'bid_amount'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'type'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'decimal'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'constraint'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'7,2'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'default'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'0.00'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'null'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">FALSE<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'bid_date'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'type'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'int'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'constraint'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'10'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'unsigned'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">TRUE</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'default'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'0'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'null'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">FALSE<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">dbforge</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">add_field</span><span style="color: #007700">(</span><span style="color: #0000BB">$fields</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">dbforge</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">add_key</span><span style="color: #007700">(</span><span style="color: #DD0000">'id'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">TRUE</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">dbforge</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">create_table</span><span style="color: #007700">(</span><span style="color: #DD0000">'auction'</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">TRUE</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">&#125;&nbsp;</span>
</span>
</code></div>

<p>This sets up an array of fields that we want to add. As well as the 4 fields we identified above, its good practise to include a <em>primary key</em> field to uniquely identify each record.</p>

<p>CodeIgniter provides a <a href="http://codeigniter.com/user_guide/database/forge.html">Database Forge</a> class to make it easier to manage databases. Note, we don&#8217;t need to add the prefix that EE uses at the start of the table (by default, <code>exp_</code>) - although we could - the EE database functions are intelligent enough to do the right thing.</p>

<h3 id="howdoweknowwhattypeoffieldweneed">How do we know what type of field we need?</h3>

<p>In our case three of our fields - the member id, the entry id and the time - are all common fields in the EE database. The member id and entry id are primary keys in the exp_members and exp_channel_titles tables so it makes sense to use the same definitions as those tables:</p>

<div class="codeblock"><code><span style="color: #000000">
<span style="color: #007700">`</span><span style="color: #DD0000">entry_id</span><span style="color: #007700">`&nbsp;</span><span style="color: #0000BB">int</span><span style="color: #007700">(</span><span style="color: #0000BB">10</span><span style="color: #007700">)&nbsp;</span><span style="color: #0000BB">unsigned&nbsp;NOT&nbsp;NULL<br /></span><span style="color: #007700">`</span><span style="color: #DD0000">member_id</span><span style="color: #007700">`&nbsp;</span><span style="color: #0000BB">int</span><span style="color: #007700">(</span><span style="color: #0000BB">10</span><span style="color: #007700">)&nbsp;</span><span style="color: #0000BB">unsigned&nbsp;NOT&nbsp;NULL&nbsp;</span>
</span>
</code></div>

<p>The bid amount will be a currency value, so we can use:</p>

<div class="codeblock"><code><span style="color: #000000">
<span style="color: #007700">`</span><span style="color: #DD0000">bid_amount</span><span style="color: #007700">`&nbsp;</span><span style="color: #0000BB">decimal</span><span style="color: #007700">(</span><span style="color: #0000BB">7</span><span style="color: #007700">,</span><span style="color: #0000BB">2</span><span style="color: #007700">)&nbsp;</span><span style="color: #0000BB">NOT&nbsp;NULL&nbsp;</span><span style="color: #007700">DEFAULT&nbsp;</span><span style="color: #DD0000">'0.00'&nbsp;</span>
</span>
</code></div>

<p>for this field.</p>

<p>In EE, dates and times are stored as UTC (Universal Coordinated Time, or GMT, Greenwich Mean Time) <em>timestamps</em>. The timestamp is the number of seconds after midnight January 1st 1970. Again, by inspecting the existing database tables, we can find a suitable definition for this field:</p>

<div class="codeblock"><code><span style="color: #000000">
<span style="color: #007700">`</span><span style="color: #DD0000">bid_date</span><span style="color: #007700">`&nbsp;</span><span style="color: #0000BB">int</span><span style="color: #007700">(</span><span style="color: #0000BB">10</span><span style="color: #007700">)&nbsp;</span><span style="color: #0000BB">unsigned&nbsp;NOT&nbsp;NULL&nbsp;</span><span style="color: #007700">DEFAULT&nbsp;</span><span style="color: #DD0000">'0'&nbsp;</span>
</span>
</code></div>

<h3 id="removingthedatabasetable">Removing the database table</h3>

<p>Just as we create the table when installing the module, we must remove it when we un-install the module.</p>

<p>We do that by modifying the <code>uninstall</code> function of <code>upd.auction.php</code>:</p>

<div class="codeblock"><code><span style="color: #000000">
<span style="color: #007700">public&nbsp;function&nbsp;</span><span style="color: #0000BB">uninstall</span><span style="color: #007700">()<br /></span><span style="color: #0000BB">&#123;<br />&nbsp;&nbsp;&nbsp;&nbsp;$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">select</span><span style="color: #007700">(</span><span style="color: #DD0000">'module_id'</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$query&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">get_where</span><span style="color: #007700">(</span><span style="color: #DD0000">'modules'</span><span style="color: #007700">,&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array(&nbsp;</span><span style="color: #DD0000">'module_name'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">module_name&nbsp;</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;);<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">where</span><span style="color: #007700">(</span><span style="color: #DD0000">'module_id'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$query</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">row</span><span style="color: #007700">(</span><span style="color: #DD0000">'module_id'</span><span style="color: #007700">));<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">delete</span><span style="color: #007700">(</span><span style="color: #DD0000">'module_member_groups'</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">where</span><span style="color: #007700">(</span><span style="color: #DD0000">'module_name'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">module_name</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">delete</span><span style="color: #007700">(</span><span style="color: #DD0000">'modules'</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">where</span><span style="color: #007700">(</span><span style="color: #DD0000">'class'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">module_name</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">delete</span><span style="color: #007700">(</span><span style="color: #DD0000">'actions'</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">where</span><span style="color: #007700">(</span><span style="color: #DD0000">'class'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">module_name</span><span style="color: #007700">.</span><span style="color: #DD0000">'_mcp'</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">delete</span><span style="color: #007700">(</span><span style="color: #DD0000">'actions'</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">load</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">dbforge</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">dbforge</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">drop_table</span><span style="color: #007700">(</span><span style="color: #DD0000">'auction'</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">TRUE</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">&#125;&nbsp;</span>
</span>
</code></div>

<p>Again, we are using the CI Database Forge class to &#8216;drop&#8217; the table.</p>

<h3 id="testingthemodule">Testing the module</h3>

<p>Let&#8217;s make sure this works before we proceed by installing and removing the module a couple of times. After you have installed the module, you should check and see the table has been created. If this has worked, remove the module and check the table has been removed too.</p>

<h3 id="summary">Summary</h3>

<p>The module now creates a database table to store our auction data when the module is installed, and cleans up after itself if the module is removed. </p>

<p>&nbsp;</p>]]></description>
      <dc:subject><![CDATA[Modules, Our first module,]]></dc:subject>
      <dc:date>2011-10-17T11:12:28+00:00</dc:date>
    </item>

    <item>
      <title><![CDATA[Our first module, part 2: setting up the file structure]]></title>
      <link>http://ee-recipes.com/articles/details/our-first-module-part-2-setting-up-the-file-structure</link>
      <guid>http://ee-recipes.com/articles/details/our-first-module-part-2-setting-up-the-file-structure#When:11:13:28Z</guid>
      <description><![CDATA[<p>In future it will probably be easier to <a href="http://ee-recipes.com/articles/details/add_on_development_utilities">use a tool to create the add-on package</a>, but as this is our first module, we’ll do it manually this time.</p>

<p>Firstly, we’ll need to name the module and think of a <em>short name</em> that will be used within the module’s template tags and file and folder structure. A logical name for our creation is ‘Auction’ with the short name ‘auction’. </p>

<p>It is worth reading <a href="http://expressionengine.com/user_guide/development/guidelines/general.html#class_and_file_names_using_common_words">EllisLab’s Style guidelines</a> regarding naming. It is unlikely that another add-on called Auction will be installed, but if you are using a common term it may be advisable to add a prefix unique to you/your company, eg, <code>xyz_auction</code>.</p>

<p>With our name decided, let’s create the folder and file structure of the add-on package:</p>

<p><img src="http://ee-recipes.com/images/article/module_file_structure.png" alt="The Auction module file structure" width="283" height="192"  /></p>

<p>At its most simple level, a module package has 4 php files and a language folder (which contains a <em>default</em> english folder).</p>

<p>Let’s see what each file does and add the <em>stub</em> contents:</p>

<h3 id="upd.auction.php">upd.auction.php</h3>

<p>The upd file defines what happens when the module is installed, updated or removed.</p>

<div class="codeblock"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php&nbsp;&nbsp;</span><span style="color: #007700">if&nbsp;(&nbsp;!&nbsp;</span><span style="color: #0000BB">defined</span><span style="color: #007700">(</span><span style="color: #DD0000">'BASEPATH'</span><span style="color: #007700">))&nbsp;exit(</span><span style="color: #DD0000">'No&nbsp;direct&nbsp;script&nbsp;access&nbsp;allowed'</span><span style="color: #007700">);<br /><br />class&nbsp;</span><span style="color: #0000BB">Auction_upd&nbsp;&#123;<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">public&nbsp;</span><span style="color: #0000BB">$version&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'1.0'</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;</span><span style="color: #0000BB">$module_name&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"Auction"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;</span><span style="color: #0000BB">$EE</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Constructor<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">public&nbsp;function&nbsp;</span><span style="color: #0000BB">__construct</span><span style="color: #007700">()<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">&#123;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE&nbsp;</span><span style="color: #007700">=&amp;&nbsp;</span><span style="color: #0000BB">get_instance</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">&#125;<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/**<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;Install&nbsp;the&nbsp;module<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@return&nbsp;boolean&nbsp;TRUE<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">public&nbsp;function&nbsp;</span><span style="color: #0000BB">install</span><span style="color: #007700">()<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">&#123;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$mod_data&nbsp;</span><span style="color: #007700">=&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'module_name'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">module_name</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'module_version'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">version</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'has_cp_backend'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"y"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'has_publish_fields'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'n'<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">insert</span><span style="color: #007700">(</span><span style="color: #DD0000">'modules'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$mod_data</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">TRUE</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">&#125;<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/**<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;Uninstall&nbsp;the&nbsp;module<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@return&nbsp;boolean&nbsp;TRUE<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">public&nbsp;function&nbsp;</span><span style="color: #0000BB">uninstall</span><span style="color: #007700">()<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">&#123;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">select</span><span style="color: #007700">(</span><span style="color: #DD0000">'module_id'</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$query&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">get_where</span><span style="color: #007700">(</span><span style="color: #DD0000">'modules'</span><span style="color: #007700">,&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array(&nbsp;</span><span style="color: #DD0000">'module_name'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">module_name&nbsp;</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">where</span><span style="color: #007700">(</span><span style="color: #DD0000">'module_id'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$query</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">row</span><span style="color: #007700">(</span><span style="color: #DD0000">'module_id'</span><span style="color: #007700">));<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">delete</span><span style="color: #007700">(</span><span style="color: #DD0000">'module_member_groups'</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">where</span><span style="color: #007700">(</span><span style="color: #DD0000">'module_name'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">module_name</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">delete</span><span style="color: #007700">(</span><span style="color: #DD0000">'modules'</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">where</span><span style="color: #007700">(</span><span style="color: #DD0000">'class'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">module_name</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">delete</span><span style="color: #007700">(</span><span style="color: #DD0000">'actions'</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">where</span><span style="color: #007700">(</span><span style="color: #DD0000">'class'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">module_name</span><span style="color: #007700">.</span><span style="color: #DD0000">'_mcp'</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">delete</span><span style="color: #007700">(</span><span style="color: #DD0000">'actions'</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">TRUE</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">&#125;<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/**<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;Update&nbsp;the&nbsp;module<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@return&nbsp;boolean<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">public&nbsp;function&nbsp;</span><span style="color: #0000BB">update</span><span style="color: #007700">(</span><span style="color: #0000BB">$current&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">''</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">&#123;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">$current&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">version</span><span style="color: #007700">)&nbsp;</span><span style="color: #0000BB">&#123;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;No&nbsp;updates<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">return&nbsp;</span><span style="color: #0000BB">FALSE</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">&#125;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">return&nbsp;</span><span style="color: #0000BB">TRUE</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">&#125;<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&#125;<br /><br /></span><span style="color: #FF8000">/*&nbsp;End&nbsp;of&nbsp;file&nbsp;upd.auction.php&nbsp;*/<br />/*&nbsp;Location:&nbsp;/system/expressionengine/third_party/auction/upd.auction.php&nbsp;*/&nbsp;</span>
</span>
</code></div>

<p>There are 3 fairly self-explanatory methods: install, uninstall and update.</p>

<p><code>install</code> is run when the module is installed from the Control Panel. This adds a record in the <code>exp_modules</code> table to let the system know that the module exists. Along with the module&#8217;s name and version, we can configure 2 &#8216;flags&#8217; for the module: </p>

<ul>
<li><code>has_cp_backend</code> is set to <code>&#8216;y&#8217;</code> if we want the module to have an interface in the Control Panel&#8217;s Module section. If your module does not need any back-end configuration or reporting you can set this to <code>&#8216;n&#8217;</code>.</li>
<li><code>has_publish_fields</code> is set to <code>&#8216;n&#8217;</code> unless the module we are building adds custom fields or tabs to the Publish screen.</li>
</ul>

<p>Finally, the method returns the value <code>TRUE</code>.</p>

<h3 id="mod.auction.php">mod.auction.php</h3>

<p>The mod file handles the front-end interation and is where the template tag parsing occurs.</p>

<div class="codeblock"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php&nbsp;&nbsp;</span><span style="color: #007700">if&nbsp;(&nbsp;!&nbsp;</span><span style="color: #0000BB">defined</span><span style="color: #007700">(</span><span style="color: #DD0000">'BASEPATH'</span><span style="color: #007700">))&nbsp;exit(</span><span style="color: #DD0000">'No&nbsp;direct&nbsp;script&nbsp;access&nbsp;allowed'</span><span style="color: #007700">);<br /><br />class&nbsp;</span><span style="color: #0000BB">Auction&nbsp;&#123;<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">public&nbsp;</span><span style="color: #0000BB">$return_data</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Constructor<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">public&nbsp;function&nbsp;</span><span style="color: #0000BB">__construct</span><span style="color: #007700">()<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">&#123;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE&nbsp;</span><span style="color: #007700">=&amp;&nbsp;</span><span style="color: #0000BB">get_instance</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">&#125;<br /><br />&#125;<br /></span><span style="color: #FF8000">/*&nbsp;End&nbsp;of&nbsp;file&nbsp;mod.auction.php&nbsp;*/<br />/*&nbsp;Location:&nbsp;/system/expressionengine/third_party/auction/mod.auction.php&nbsp;*/&nbsp;</span>
</span>
</code></div>

<p>At this stage of the module, this class is empty apart from the classes&#8217; constructor which creates an instance of the EE superglobal (we&#8217;ll inevitably use this later).</p>

<h3 id="mcp.auction.php">mcp.auction.php</h3>

<p>The mcp file handles the Control Panel side of the module.</p>

<div class="codeblock"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php&nbsp;&nbsp;</span><span style="color: #007700">if&nbsp;(&nbsp;!&nbsp;</span><span style="color: #0000BB">defined</span><span style="color: #007700">(</span><span style="color: #DD0000">'BASEPATH'</span><span style="color: #007700">))&nbsp;exit(</span><span style="color: #DD0000">'No&nbsp;direct&nbsp;script&nbsp;access&nbsp;allowed'</span><span style="color: #007700">);<br /><br />class&nbsp;</span><span style="color: #0000BB">Auction_mcp&nbsp;&#123;<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">public&nbsp;</span><span style="color: #0000BB">$return_data</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Constructor<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">public&nbsp;function&nbsp;</span><span style="color: #0000BB">__construct</span><span style="color: #007700">()<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">&#123;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">EE&nbsp;</span><span style="color: #007700">=&amp;&nbsp;</span><span style="color: #0000BB">get_instance</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">&#125;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/**<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;The&nbsp;module's&nbsp;Control&nbsp;Panel&nbsp;default&nbsp;controller<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@return&nbsp;void<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">public&nbsp;function&nbsp;</span><span style="color: #0000BB">index</span><span style="color: #007700">()<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">&#123;<br />&nbsp;&nbsp;&nbsp;&nbsp;&#125;<br /><br />&#125;<br /></span><span style="color: #FF8000">/*&nbsp;End&nbsp;of&nbsp;file&nbsp;mcp.auction.php&nbsp;*/<br />/*&nbsp;Location:&nbsp;/system/expressionengine/third_party/auction/mcp.auction.php&nbsp;*/&nbsp;</span>
</span>
</code></div>

<p>At this stage of the module, this class does not do anything. We&#8217;ve included an empty <code>index</code> method - this is the controller that will be called if the Module&#8217;s Control Panel is accessed.</p>

<h3 id="lang.auction.php">lang.auction.php</h3>

<p>EE allows users to localize the system to their own language. Module’s should put all of their static text in the language file so that non-English speaking users can create and add their own translated language file.</p>

<p>A module’s language file <em>must</em> contain 2 elements which are used in the Control Panel’s Modules page to display the title and description. In this case <code>auction_module_name</code> and <code>auction_module_description</code>.</p>

<div class="codeblock"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php&nbsp;&nbsp;</span><span style="color: #007700">if&nbsp;(&nbsp;!&nbsp;</span><span style="color: #0000BB">defined</span><span style="color: #007700">(</span><span style="color: #DD0000">'BASEPATH'</span><span style="color: #007700">))&nbsp;exit(</span><span style="color: #DD0000">'No&nbsp;direct&nbsp;script&nbsp;access&nbsp;allowed'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$lang&nbsp;</span><span style="color: #007700">=&nbsp;array(<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;Required&nbsp;for&nbsp;ADD-ONS&nbsp;&gt;&nbsp;MODULES&nbsp;page&nbsp;*/<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'auction_module_name'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'Auction'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'auction_module_description'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'A&nbsp;simple&nbsp;module&nbsp;to&nbsp;allow&nbsp;entries&nbsp;to&nbsp;be&nbsp;auctioned'</span><span style="color: #007700">,<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;Required&nbsp;for&nbsp;CONTROL&nbsp;PANEL&nbsp;pages&nbsp;*/<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Required&nbsp;for&nbsp;FRONT&nbsp;END&nbsp;module&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">''&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">''<br /></span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*&nbsp;End&nbsp;of&nbsp;file&nbsp;lang.auction.php&nbsp;*/<br />/*&nbsp;Location:&nbsp;/system/expressionengine/third_party/auction/language/english/lang.auction.php&nbsp;*/&nbsp;</span>
</span>
</code></div>

<h3 id="summary">Summary</h3>

<p>We now have a (very) basic module that we can install and remove. </p>

<p>Project files can be downloaded from <a href="https://bitbucket.org/ee_recipes/auction">bitbucket </a> (<a href="https://bitbucket.org/ee_recipes/auction/get/4e1422262d1e.zip">Download</a>)</p>

<p>&nbsp;</p>]]></description>
      <dc:subject><![CDATA[Modules, Our first module,]]></dc:subject>
      <dc:date>2011-10-15T11:13:28+00:00</dc:date>
    </item>

    <item>
      <title><![CDATA[Our first module, part 1: the concept]]></title>
      <link>http://ee-recipes.com/articles/details/our-first-module-part-1</link>
      <guid>http://ee-recipes.com/articles/details/our-first-module-part-1#When:11:59:56Z</guid>
      <description><![CDATA[<p>For our first module, we’ll build something that introduces some of the most commonly needed aspects of module development:</p>

<ul>
<li>a database to store module-specific data</li>
<li>front end template tags</li>
<li>a form and corresponding <em>action</em> to handle the processing of the form</li>
<li>a Control Panel interface for managing/monitoring  data</li>
</ul>

<p>With this in mind - and a quick scan of <a href="http://devot-ee.com/">devot:ee</a> to see that one does not exist - let’s build an auction module. </p>

<p>Initially, we’ll keep the module as simple as possible so that the main concepts don’t get lost (we won’t be building the next ebay quite yet). Additional features can be added at a later stage.</p>

<p>This is what we want the module to do:</p>

<p><img src="http://ee-recipes.com/images/article/module_wireframe1.png" alt="Auction module wireframe"  /></p>

<ul>
<li>display an entry with its current auction price and other summary data (eg, the number of bids placed, the most recent bid).</li>
<li>display a form to allow logged in site members to place a bid.</li>
<li>display a list of previous bids for the item.</li>
</ul>

<p>One thing to to note: where possible, we’ll use the pre-existing functionality of EE. In this case: </p>

<ul>
<li>items will be created as normal channel entries</li>
<li>the module will use the existing EE membership functionality to control who can place a bid</li>
</ul>

<p>We’ll also provide some tools for the site administrator to:</p>

<ul>
<li>View the bids</li>
<li>Add/edit/delete bids manually</li>
</ul>

<p>In the next step, we&#8217;ll start creating the module.</p>

<p>&nbsp;</p>]]></description>
      <dc:subject><![CDATA[Modules, Our first module,]]></dc:subject>
      <dc:date>2011-10-02T11:59:56+00:00</dc:date>
    </item>

    
    </channel>
</rss>