<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>BinaryKitten&#039;s Development Dropbox &#187; code</title>
	<atom:link href="http://binarykitten.com/tag/code/feed" rel="self" type="application/rss+xml" />
	<link>http://binarykitten.com</link>
	<description>Curently a work in progress. Please be patient</description>
	<lastBuildDate>Thu, 27 Oct 2011 21:49:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>jQuery Keyz Plugin</title>
		<link>http://binarykitten.com/dev/jq-plugins/259-jquery-keyz-plugin.html</link>
		<comments>http://binarykitten.com/dev/jq-plugins/259-jquery-keyz-plugin.html#comments</comments>
		<pubDate>Wed, 24 Feb 2010 15:20:45 +0000</pubDate>
		<dc:creator>BinaryKitten</dc:creator>
				<category><![CDATA[jQuery Plugins]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[keypress]]></category>
		<category><![CDATA[plugins]]></category>

		<guid isPermaLink="false">http://binarykitten.me.uk/?p=259</guid>
		<description><![CDATA[The purpose of this plugin is to easily facilitate the end user to create and hook key presses for their own use. Usually you would need to know what key links with which keycode etc. Usage It&#8217;s as easy as these steps: Include jquery &#8211; either from CDN or local source Include jquery.keyz.js call the [...]]]></description>
			<content:encoded><![CDATA[<p>The purpose of this plugin is to easily facilitate the end user to  create and hook key presses for their own use. Usually you would need to know what key links with which keycode etc.</p>
<h3>Usage</h3>
<p>It&#8217;s as easy as these steps:</p>
<ol>
<li>Include jquery &#8211; either from CDN or local source</li>
<li>Include jquery.keyz.js</li>
<li>call the following within your document ready or after the item exists</li>
</ol>
<pre class="brush: javascript">
$(&#039;selector&#039;).keyz({
&quot;enter&quot;: function(ctl,sft,alt,event) {
alert(&#039;you pressed enter!&#039;);
}
});
</pre>
<p>this will hook the enter key and raise an alert when pressed.</p>
<p>If you  want to cancel the key either return false from the function or set the  value to false like so:</p>
<pre class="brush: javascript">
$(&#039;selector&#039;).keyz({
&quot;enter&quot;: function(ctl,sft,alt,event) {
return false;
}
});
</pre>
<p>OR</p>
<pre class="brush: javascript">
$(&#039;selector&#039;).keyz({
&quot;enter&quot;:false
});</pre>
<p>You can either use the key names in singular or in a grouping like so:</p>
<pre class="brush: javascript">
$(&#039;selector&#039;).keyz({
&quot;enter&quot;: function(ctl,sft,alt,event) {
/* single key */
return false;
},
&quot;F1 F2 F3 F4 F5 F6 F7 F8 F9 F10 F11 F12&quot;: function(ctl,sft,alt,event) {
/* mapped to all F-Keys */
return false;
}
});
</pre>
<p>Download jquery.keyz.js here: <a href="/keyzDemo/jquery.keyz.js">Full Source</a> &amp;&amp;  <a href="/keyzDemo/jquery.keyz.gc.js">Google Closure Compiled version</a></p>
<p>Visit the demo page here: <a href="/keyzDemo/">Full Source</a></p>
<h3>Planned Features</h3>
<ul>
<li>support for keypress chains .. passing a sequence of keys and an event firing upon completion</li>
<li><span style="text-decoration: line-through;">support for all three key states &#8211; presently only triggers on key down</span> &#8211; Added in 1.0.2</li>
<li>to add some <a href="http://paulirish.com/2010/duck-punching-with-jquery/" target="_blank">duck punching</a> to add the keyname to the event for the three standard events</li>
</ul>
<h3>Supported Keys</h3>
<p>The keys listed below are the current ones supported by the plugin.<br />
They also support the hyphenated name eg numpad-1 or in upper case like &#8220;F1&#8243; or &#8220;f1&#8243;</p>
<ul>
<li>enter</li>
<li>return</li>
<li>esc</li>
<li>escape</li>
<li>numerics</li>
<li>upper</li>
<li>lower</li>
<li>alphanumeric</li>
<li>tab</li>
<li>shift</li>
<li>alt</li>
<li>ctrl</li>
<li>f1</li>
<li>f2</li>
<li>f3</li>
<li>f4</li>
<li>f5</li>
<li>f6</li>
<li>f7</li>
<li>f8</li>
<li>f9</li>
<li>f10</li>
<li>f11</li>
<li>f12</li>
<li>caps</li>
<li>capslock</li>
<li>numlock</li>
<li>winflag</li>
<li>winkey</li>
<li>windows</li>
<li>scrolllock</li>
<li>left</li>
<li>up</li>
<li>right</li>
<li>down</li>
<li>volumeup</li>
<li>volumedown</li>
<li>menu</li>
<li>contextmenu</li>
<li>backspace</li>
<li>pause</li>
<li>break</li>
<li>pausebreak</li>
<li>pageup</li>
<li>pagedown</li>
<li>end</li>
<li>home</li>
<li>insert</li>
<li>del</li>
<li>delete</li>
<li>numpad0</li>
<li>numpad1</li>
<li>numpad2</li>
<li>numpad3</li>
<li>numpad4</li>
<li>numpad5</li>
<li>numpad6</li>
<li>numpad7</li>
<li>numpad8</li>
<li>numpad9</li>
<li>*</li>
<li>multiply</li>
<li>+</li>
<li>add</li>
<li>-</li>
<li>subtract</li>
<li>.</li>
<li>fullstop</li>
<li>decimal</li>
<li>/</li>
<li>divide</li>
<li>;</li>
<li>semicolon</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://binarykitten.com/dev/jq-plugins/259-jquery-keyz-plugin.html/feed</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Active Module Based Config with Zend Framework</title>
		<link>http://binarykitten.com/dev/zend-framework/177-active-module-based-config-with-zend-framework.html</link>
		<comments>http://binarykitten.com/dev/zend-framework/177-active-module-based-config-with-zend-framework.html#comments</comments>
		<pubDate>Mon, 04 Jan 2010 22:41:41 +0000</pubDate>
		<dc:creator>BinaryKitten</dc:creator>
				<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[Component]]></category>
		<category><![CDATA[Framework]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[Zend]]></category>

		<guid isPermaLink="false">http://binarykitten.me.uk/?p=177</guid>
		<description><![CDATA[I&#8217;ve recently taken to using Zend Framework for a project that I needed to bring up to date. I won&#8217;t go into the pros and cons of choosing a framework as there are many much more qualified people who have done a much better job of this subject than I would or could. So Instead [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve recently taken to using Zend Framework for a project that I needed to bring up to date. I won&#8217;t go into the pros and cons of choosing a framework as there are many much more qualified people who have done a much better job of this subject than I would or could. So Instead I bring to you How I managed to get Active Module Based Configuration within Zend Framework.</p>
<h2><strong>The Problem</strong></h2>
<p>The Concept I wanted to achieve was to have unique Configuration based upon the module that was active. The Issue with this is that the Bootstrap files and the _init functions for ALL modules are called with no bias as to which module is active. Thus if you created a 3 modules wanted to make menu alterations in one, those alterations will be applied to all. I also wanted to have a a system where if i added extra modules i could just add extra functions to the bootstrap file and it would work in a similar way.</p>
<p>With this in mind, I set about trying to figure out the solution.<br />
<span id="more-177"></span></p>
<h2><strong>New Version</strong></h2>
<p>Though this version still works and you should read through the code to see how to implement the plugin, there is a new version available at:<br />
<a href="http://binarykitten.me.uk/dev/zend-framework/296-active-module-config-v2.html">http://binarykitten.me.uk/dev/zend-framework/296-active-module-config-v2.html</a> <br />
Please refer to this as the latest version.. Thanks</p>
<p>&#8212;&#8211; Original Post &#8212;&#8211;</p>
<h2><strong>Solution 1 &#8211; Failure</strong></h2>
<p>With the Idea that no-body is perfect, including myself. My 1st attempt ended in failure. This attempt was to modify/extend the Bootstrap class to add extra functions to the resources list.. In the end I couldn&#8217;t determine if the Module bootstrap was the active one. ok So Attempt 1 was a failure, onto the next</p>
<h2>Solution 2 &#8211; Success!</h2>
<p>A quick conversation with Matthew Weier O&#8217;Phinney (<a href="http://twitter.com/weierophinney">@weierophinney</a>) pointed me in the direction of Controller plugins and the routeShutdown method, as after the route had been finished which module was active would be able to be discerned.<br />
At this point I must apologise to Pieter Kokx ( @kokxie )  who I had a small disagreement with in the #zftalk channel. Pieter had done his best to point me down this route to start with, though being a stubborn mule I am refused to see the quality and precision of his comments.<br />
Thank you both for your help here.</p>
<p>The way that this works is that is scans the active modules bootstrap for functions starting with <span style="text-decoration: underline">activeInit</span> or <span style="text-decoration: underline">modulenameInit</span> just like the <a href="http://is.gd/64s1y">_init</a> functions but these would only be called if the module is active.<br />
I was successfully able to create the plugin and trigger the functions, unfortunately it was triggering/calling them in a static sense.. which meant that standard _init style code wouldn&#8217;t work. Luckily with a little digging in the source of the framework i found a storage of the modules and their initiated bootstrap classes. Lucky Me! So finally we call the methods within the right context.</p>
<p>So Here it is the Final Code, Please do comment, I learn from people as I hope that others can learn from me.</p>
<p>I&#8217;ve used the &#8220;Namespace&#8221; of BinaryKitten here. If you want to use a different &#8220;Namespace&#8221; Replace BinaryKitten with what you want.<br />
The &#8220;Namespace&#8221; allows for the use of the BinaryKitten folder within the Libray Folder.<br />
Remember the Controller Plugin should go in the right place for your application, if you are using the autoloader you can add the &#8220;Namespace&#8221; to be autoloaded via your application.ini</p>
<pre class="brush: text">
autoloadernamespaces[] = &quot;BinaryKitten&quot;
</pre>
<p>First off we have the Controller Plugin.<br />
This should go into the &#8220;Namespace&#8221; folder within the Library Folder and should be called ModuleConfig.php</p>
<pre class="brush: php">
&lt;?php
class BinaryKitten_ModuleConfig extends Zend_Controller_Plugin_Abstract
{
    public function routeShutdown(Zend_Controller_Request_Abstract $request)
    {
        $frontController = Zend_Controller_Front::getInstance();
        $bootstrap =  $frontController-&gt;getParam(&#039;bootstrap&#039;);
        $activeModuleName = $request-&gt;getModuleName();
        $moduleList = $bootstrap-&gt;modules;

        $moduleInitName = strtolower($activeModuleName).&quot;Init&quot;;
        $moduleInitNameLength = strlen($moduleInitName);

        if (isset($moduleList[$activeModuleName])) {
            $activeModule = $moduleList[$activeModuleName];

            $bootstrapMethodNames = get_class_methods($bootstrap);
            foreach ($bootstrapMethodNames as $key=&gt;$method) {
                $runMethod = false;
                $methodNameLength = strlen($method);
                if ($moduleInitNameLength &lt; $methodNameLength &amp;&amp;
                    $moduleInitName == substr($method, 0, $moduleInitNameLength)) {
                    call_user_func(array($bootstrap,$method));
                }
            }
        } else {
            $activeModule = $bootstrap;
        }

        $methodNames = get_class_methods($activeModule);
        foreach ($methodNames as $key=&gt;$method) {
            $runMethod = false;
            $methodNameLength = strlen($method);
            if (10 &lt; $methodNameLength &amp;&amp; &#039;activeInit&#039; === substr($method, 0, 10)) {
                $runMethod = true;
            } elseif ($moduleInitNameLength &lt; $methodNameLength &amp;&amp;
                    $moduleInitName == substr($method, 0, $moduleInitNameLength)) {
                $runMethod = true;
            }
            if ($runMethod) {
                call_user_func(array($activeModule,$method));
            }
        }
    }
}
</pre>
<p>Next we need to make sure the Controller Plugin is loaded.<br />
We can do this in one of two ways. Either in the Application Bootstrap via an _init function</p>
<pre class="brush: php">
public function _initControllerPlugins()
{
    $plugin = Zend_Controller_Front::getInstance()-&gt;registerPlugin(
        new BinaryKitten_ModuleConfig()
    );
}
</pre>
<p>*&#8211; Or &#8211;*</p>
<p>We can add a line to the application.ini</p>
<pre class="brush: text">
resources.frontController.plugins.BKModuleConfig = &quot;BinaryKitten_ModuleConfig&quot;
</pre>
<p>Finally some example init code from the module bootstrap.<br />
Please remember that the activeInit*() Functions need to be public for this to work properly</p>
<pre class="brush: php">
class Default_Bootstrap extends Zend_Application_Module_Bootstrap {
    public function activeInitMenus() {
        $layout = $this
                    -&gt;bootstrap(&#039;layout&#039;)
                    -&gt;getResource(&#039;layout&#039;);

        $view = $layout-&gt;getView();
        $config = new Zend_Config_Xml(APPLICATION_PATH.&#039;/configs/navigation_default.xml&#039;,&quot;menu&quot;);
        $navigation = new Zend_Navigation($config);
        $view-&gt;navigation($navigation);
    }
    public function activeInitDoSomethingElse() {
        /* some other code */
    }
    public function defaultInitSomething() {
    	/* more code */
    }
}
</pre>
<p>We can also add module inits to the application bootstrap like so:</p>
<pre class="brush: php">
    public function modulenameInitFunction() {
    	/* place code here */
    }
</pre>
<p>Where modulename is the lowercase version of the Modules name, eg if you have the Admin module, then you would use:</p>
<pre class="brush: php">
    public function adminInitFunction() {
    	/* place code here */
    }
</pre>
<p>Hopefully someone will find this code useful.</p>
<p>[ Edit January 5th 2010 ]<br />
Thanks to:<br />
	Matthew Weier O&#8217;Phinney for pointing out places for update.<br />
	Rob Allen (@Akrabat) for the info that the plugin could be loaded via the application.ini<br />
	Elizabeth Marie Smith and Matthew Turland for questioning the use of the Reflection.</p>
<ul>
<li>Updated the Bootstrap code to properly define the functions as Public</li>
<li>Removed the reflection as this wasn&#8217;t actually required any more.</li>
<li>Updated Post to make clean up the order of things and to properly designate that the BinaryKitten is the &#8220;Namespace&#8221;<br />
&#8220;Namespace&#8221; is used to reference that we&#8217;re not using PHP5.3 Namespaces, but the Namespaces within the Zend Framework.</li>
</ul>
<p>[ Edit January 11th 2010 ]<br />
Thanks to:<br />
	septem for pointing out a typo where i had $boostrap instead of $bootstrap<br />
	Gerard Roche for pointing out that by default, the default module doesn&#8217;t require a module bootstrap (in my code it has one)</p>
<ul>
<li>Updated to fix the typos</li>
<li>Added in a quick check to see if the module exists in the modules list of bootstraps</li>
<li>Removed the _ from the function name that it searches for, this should please the people who are adamant over the Zend Coding Standards</li>
<li>Added in the functionality to have $modulenameInit() functions as well in both active module bootstrap and the application bootstrap</li>
</ul>
<p>[ Edit February 13th 2010 ]<br />
Ran the code through codesniffer against the Zend Standard supplied.. updated so no errors found. 3 Warnings are left .. they are as follows:<br />
22 | WARNING | Line exceeds 80 characters; contains 83 characters<br />
35 | WARNING | Line exceeds 80 characters; contains 84 characters<br />
38 | WARNING | Line exceeds 80 characters; contains 83 characters<br />
Don&#8217;t think it&#8217;s worth the change for 3/4 characters </p>
]]></content:encoded>
			<wfw:commentRss>http://binarykitten.com/dev/zend-framework/177-active-module-based-config-with-zend-framework.html/feed</wfw:commentRss>
		<slash:comments>33</slash:comments>
		</item>
		<item>
		<title>PHP &#8211; Chmod-ed to Windows hell and back</title>
		<link>http://binarykitten.com/dev/php/159-php-chmod-ed-to-windows-hell-and-back.html</link>
		<comments>http://binarykitten.com/dev/php/159-php-chmod-ed-to-windows-hell-and-back.html#comments</comments>
		<pubDate>Tue, 10 Mar 2009 21:05:22 +0000</pubDate>
		<dc:creator>BinaryKitten</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[chmod]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[files]]></category>

		<guid isPermaLink="false">http://binarykitten.jkrswebsolutions.co.uk/?p=159</guid>
		<description><![CDATA[Today has not been a good day. With various things going wrong, I was please that I had got the file upload code done for a site i&#8217;m working on. All I needed to do was make sure that the file had the right permissions and then edit it via GD (via an image editor [...]]]></description>
			<content:encoded><![CDATA[<p>Today has not been a good day.</p>
<p>With various things going wrong, I  was please that I had got the file upload code done for a site i&#8217;m working on. All I needed to do was make sure that the file had the right permissions and then edit it via GD (via an image editor class i have). Here&#8217;s where the trouble started..</p>
<p>I started off by chmoding the file to 777 and then trying to do the resize.. this threw an error with permissions.. so i looked at the file.. Read Only.. strange I thought.</p>
<p>Then I checked the folder and that had a weird green square instead of the checkmark in the checkbox. I tried to remove the read only attribute off of this and hte file.. and it kept coming back.. I went all around hte houses, trying all sorts of techniques .. to no avail.</p>
<p><a href="http://twitter.com/auroraeosrose">@auroraeosrose</a> in the PHPWomen IRC channel helped a lot as did Dreis.. (thanks muchly)..</p>
<p>After backing up the files and formatting the drive, to reinstalling the Apache/PHP/MySql stack I was no clearer to the answer as to why the file was always ending up with read only permissions..</p>
<p>Finally it came down to the 1 thing that I had overlooked.. the <a href="http://php.net/chmod">chmod</a> in the code .. the second parameter was 777. I thought this was correct. Aparrantly not. The second parameter of the chmod function requires an octal code.. thus prefixing the 777 with a 0 (zero) fixed the whole issue.</p>
<p>Boy did i really feel dumb today.</p>
<p>So to wrap up, remember to always pass a FOUR digit code through to chmod or face the annoyance and headache I got today.</p>
]]></content:encoded>
			<wfw:commentRss>http://binarykitten.com/dev/php/159-php-chmod-ed-to-windows-hell-and-back.html/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>MySQL – Conditional Joins</title>
		<link>http://binarykitten.com/dev/mysql/149-mysql-conditional-joins.html</link>
		<comments>http://binarykitten.com/dev/mysql/149-mysql-conditional-joins.html#comments</comments>
		<pubDate>Sat, 07 Mar 2009 12:02:30 +0000</pubDate>
		<dc:creator>BinaryKitten</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[conditional]]></category>
		<category><![CDATA[joins]]></category>
		<category><![CDATA[optional]]></category>

		<guid isPermaLink="false">http://binarykitten.jkrswebsolutions.co.uk/?p=149</guid>
		<description><![CDATA[For the most part I&#8217;ve used many JOINs in my queries since I leartn how to use them and what they were good for, I much prefer to use an Inner Join over the the Implied join of a WHERE statement. For those who haven&#8217;t yet learnt about JOINs you might be using an implied [...]]]></description>
			<content:encoded><![CDATA[<p>For the most part I&#8217;ve used many JOINs in my queries since I leartn how to use them and what they were good for, I much prefer to use an Inner Join over the the Implied join of a WHERE statement.</p>
<p>For those who haven&#8217;t yet learnt about JOINs you might be using an implied join in your queries even now&#8230; lets take this example:</p>
<pre class="brush: sql">
SELECT table1.field1,table2.field2
FROM table1,table2
WHERE table1.table2_id = table2.IDField
</pre>
<p>This is an Implied JOIN, you are telling MySQL to get the 2 fields from the 2 tables but only return the ones where the field in table1 is in the field in table2. This works, but using Joins we can improve it.</p>
<p>Ok,  Lets take the above example and apply the join mentality to it:</p>
<pre class="brush: sql">
SELECT table1.field1, table2.field2
FROM table1
INNER JOIN table2 on table1.table2_id = table2.IDField
</pre>
<p>This Links in only the rows that match the ON criteria which in theory should be less than the whole table, which again in theory should make the query faster.</p>
<p>Ok, now we have the JOIN theory out the way, we can get down to what this blog post is actually really all about, Conditional joins. Just Like the WHERE Clause in the SELECT statement you can use AND &amp; OR in the ON part of a JOIN. Usually it&#8217;s used to add more constraints to the join, but you can use it in interesting ways,</p>
<pre class="brush: sql">
SELECT table1.field1, table2.field2
FROM table1
INNER JOIN table2 on (table1.table2_id = table2.IDField) AND (table1.itemName = &#039;foo&#039;)
</pre>
<p>Only JOIN rows that have the matching ID and where the itemName from table1 is equal to foo;</p>
<pre class="brush: sql">
SELECT table1.field1, table2.field2
FROM table1
INNER JOIN table2 on ((table1.table2_id = table2.IDField) AND (table1.itemName = &#039;foo&#039;)) OR (table2.item = &#039;xx&#039;)
</pre>
<p>Only JOIN rows when the IDs are matching and the itemname is foo,  OR when the item in table2 is &#8216;xx&#8217;</p>
<p>Baring in mind that the more complicated you make it, the harder it will be to maintain IT still can be a great method of Reducing the amount of rows that need to be processed.</p>
<p>Hopefully this will help someone, if so or if you have anything to add/point out/criticise etc.. please do comment.. I Don&#8217;t Bite.</p>
]]></content:encoded>
			<wfw:commentRss>http://binarykitten.com/dev/mysql/149-mysql-conditional-joins.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Enhancing your listings with jQuery and QuickSearch</title>
		<link>http://binarykitten.com/dev/jq-plugins/118-enhancing-your-listings-with-jquery-and-quicksearch.html</link>
		<comments>http://binarykitten.com/dev/jq-plugins/118-enhancing-your-listings-with-jquery-and-quicksearch.html#comments</comments>
		<pubDate>Tue, 17 Feb 2009 21:37:52 +0000</pubDate>
		<dc:creator>BinaryKitten</dc:creator>
				<category><![CDATA[jQuery Plugins]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[enhance]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[quicksearch]]></category>

		<guid isPermaLink="false">http://binarykitten.jkrswebsolutions.co.uk/?p=118</guid>
		<description><![CDATA[Ever Wanted to have a way to filter/search a list without having to code extra to the serverside? Well maybe the jquery QuickSearch plugin from rikrikrik.com might be the answer Recently I came across the need to filter my results after they had been displayed. I really didn&#8217;t want to start messing with optional WHERE [...]]]></description>
			<content:encoded><![CDATA[<p>Ever Wanted to have a way to filter/search a list without having to code extra to the serverside? Well maybe the jquery QuickSearch plugin from rikrikrik.com might be the answer</p>
<p><span id="more-118"></span></p>
<p>Recently I came across the need to filter my results after they had been displayed. I really didn&#8217;t want to start messing with optional WHERE statements in teh php to control the output of the php as that was rather annoying.</p>
<p>Just at that moment, up popped a tweet fron jQuery telling me about a plugin called QuickSearch after taking a look at it, I found that the search options were nice and simple to use. So I went about implementing it.</p>
<p>The Plugin (available from http://rikrikrik.com/jquery/quicksearch/) was quickly installed and a few lines of js later it was up and working, nicely hiding the elements set up that didn&#8217;t match the search term, quick, simple. Nice.</p>
<p><strong>Setup</strong></p>
<ol>
<li>Download jQuery (is you don&#8217;t already have it) and Link it to your document</li>
<li>Download the plugin and place into the right place (like /js possibly)</li>
<li>Link to the file &lt;script type=&#8221;text/javascript&#8221; src=&#8221;/js/jquery.quicksearch.js&#8221;&gt;&lt;/script&gt;</li>
<li>Setup the JS to load the Quicksearch</li>
</ol>
<p>for Example, lets take the setup list we just had:<br />
Html:
<pre class="brush: html">
&lt;ul id=&quot;listings&quot;&gt;
	&lt;li&gt;Download jQuery (is you don&#039;t already have it) and Link it to your document&lt;/li&gt;
	&lt;li&gt;Download the plugin and place into the right place (like /js possibly)&lt;/li&gt;
	&lt;li&gt;Link to the file &lt;script type=&quot;text/javascript&quot; src=&quot;/js/jquery.quicksearch.js&quot;&gt;&lt;/script&gt;&lt;/li&gt;
	&lt;li&gt;Setup the JS to load the Quicksearch&lt;/li&gt;
&lt;/ul&gt;
</pre>
<p>JS</p>
<pre class="brush: javascript">
$(function(){ /* Wait for Document Ready */
/* take the items we want searching and call quicksearch on them

*/
$(&#039;ul#listings li&#039;).quicksearch({
position: &#039;before&#039;,
attached: &#039;#listings&#039;,
loaderText: &#039;&#039;,
delay: 100
});
});
</pre>
<p>This will create the demo you can see over at <a href="http://jsbin.com/atafi" target="_blank">http://jsbin.com/atafi</a> as you type into the box you can search the list.. only displaying items that match.</p>
<p>Now for the fun part.<br />
When you call the .quicksearch function.. it indexes the content of the items you have passed. So we can actively enhance this search by having hidden data in side each element..</p>
<p>Html:
<pre class="brush: html">
&lt;ul id=&quot;listings&quot;&gt;
	&lt;li&gt;Download jQuery (is you don&#039;t already have it) and Link it to your document&lt;span style=&quot;display:none&quot;&gt;1 first 1st&lt;/span&gt;&lt;/li&gt;
	&lt;li&gt;Download the plugin and place into the right place (like /js possibly)&lt;span style=&quot;display:none&quot;&gt;2 second 2nd&lt;/span&gt;&lt;/li&gt;
	&lt;li&gt;Link to the file &lt;script type=&quot;text/javascript&quot; src=&quot;/js/jquery.quicksearch.js&quot;&gt;&lt;/script&gt;&lt;span style=&quot;display:none&quot;&gt;3 third 3rd&lt;/span&gt;&lt;/li&gt;
	&lt;li&gt;Setup the JS to load the Quicksearch&lt;span style=&quot;display:none&quot;&gt;4 fourth 4th&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
</pre>
<p>Just using exactly the same JS as before we now can search to options by using 4  or third for example..<br />
<a href="http://jsbin.com/exumu">http://jsbin.com/exumu</a></p>
<p>Finally now we have the basics.. we can really go to town.<br />
1st we need to Hide the quicksearch with css </p>
<pre class="brush: css">
#quicksearch { display: none; }
</pre>
<p>This will hide the quicksearch form.</p>
<p>Next we take our html and add a couple of buttons</p>
<pre class="brush: html">
&lt;input type=&quot;button&quot; id=&quot;btnclear&quot; value=&quot;reset&quot; /&gt;
&lt;input type=&quot;button&quot; id=&quot;btn1&quot; value=&quot;show 1&quot; /&gt;
&lt;input type=&quot;button&quot; id=&quot;btn2&quot; value=&quot;show 2nd&quot; /&gt;
&lt;input type=&quot;button&quot; id=&quot;btn4&quot; value=&quot;show fourth&quot; /&gt;
&lt;ul id=&quot;listings&quot;&gt;
	&lt;li&gt;Download jQuery (is you don&#039;t already have it) and Link it to your document&lt;span style=&quot;display:none&quot;&gt;1 first 1st&lt;/span&gt;&lt;/li&gt;
	&lt;li&gt;Download the plugin and place into the right place (like /js possibly)&lt;span style=&quot;display:none&quot;&gt;2 second 2nd&lt;/span&gt;&lt;/li&gt;
	&lt;li&gt;Link to the file &lt;script type=&quot;text/javascript&quot; src=&quot;/js/jquery.quicksearch.js&quot;&gt;&lt;/script&gt;&lt;span style=&quot;display:none&quot;&gt;3 third 3rd&lt;/span&gt;&lt;/li&gt;
	&lt;li&gt;Setup the JS to load the Quicksearch&lt;span style=&quot;display:none&quot;&gt;4 fourth 4th&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
</pre>
<p>and update the js to:</p>
<pre class="brush: javascript">
$(function(){
  /* append to the end of the document */
  $(&#039;ul#listings li&#039;).quicksearch({
    position: &#039;append&#039;,
    attached: &#039;body&#039;,
    loaderText: &#039;&#039;,
    delay: 100
  });
  $(&#039;#btn1&#039;).click(function(){
    $(&#039;.qs_input&#039;).val(&#039;1&#039;).keydown(); /* Set key and trigger keydown, which is what quicksearch responds to */
  });
  $(&#039;#btn2&#039;).click(function(){    $(&#039;.qs_input&#039;).val(&#039;2nd&#039;).keydown(); });
  $(&#039;#btn4&#039;).click(function(){    $(&#039;.qs_input&#039;).val(&#039;fourth&#039;).keydown(); });
  $(&#039;#btnclear&#039;).click(function(){    $(&#039;.qs_input&#039;).val(&#039;&#039;).keydown(); });
});
</pre>
<p>This gives us clickable buttons that do our filtering using the quicksearch plugin. Demo is here: <a href="http://jsbin.com/obixa">http://jsbin.com/obixa</a></p>
<p>As you can see this makes for a interesting system.<br />
Hope you get as much use out of this as I am</p>
<p>~ Kat ~</p>
]]></content:encoded>
			<wfw:commentRss>http://binarykitten.com/dev/jq-plugins/118-enhancing-your-listings-with-jquery-and-quicksearch.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>jQuery image Preloader Plus Callbacks</title>
		<link>http://binarykitten.com/dev/jq-plugins/107-jquery-image-preloader-plus-callbacks.html</link>
		<comments>http://binarykitten.com/dev/jq-plugins/107-jquery-image-preloader-plus-callbacks.html#comments</comments>
		<pubDate>Wed, 11 Feb 2009 21:56:26 +0000</pubDate>
		<dc:creator>BinaryKitten</dc:creator>
				<category><![CDATA[jQuery Plugins]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[images]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[preloader]]></category>

		<guid isPermaLink="false">http://binarykitten.jkrswebsolutions.co.uk/?p=107</guid>
		<description><![CDATA[Hi all, After comments on the previous version by Roberto, I looked into creating callbacks within the code so that it can be used in the way that Roberto had outlined, also I used it as a way to improve my knowledge a little. After a while I got stuck and figured out a potential [...]]]></description>
			<content:encoded><![CDATA[<p>Hi all, After comments on the previous version by Roberto, I looked into creating callbacks within the code so that it can be used in the way that Roberto had outlined, also I used it as a way to improve my knowledge a little.</p>
<p>After a while I got stuck and figured out a potential method thanks to ai-a in #Javascript on UnderNet, then I spoke to Remy Sharp on Twitter and always being impressed with the work he does on his site (<a href="http://www.jqueryfordesigners.com" target="_blank">http://www.jqueryfordesigners.com</a>) I asked politely if he would give my code a quick look over. He did so, and so much more besides.</p>
<p>So I present to you now v0.95 of the Image Pre-loader which is now Split into 3 functions (rather than the 2 from before)<br />
<span id="more-107"></span><br />
So the main functionality is the same as the one from <a href="http://binarykitten.jkrswebsolutions.co.uk/2009/01/06/jquery-image-preloader/" target="_blank">http://binarykitten.jkrswebsolutions.co.uk/2009/01/06/jquery-image-preloader/</a> but now has the following functions</p>
<ol>
<li><strong>$.preLoadImages</strong> &#8211; Preload the passed list of images, calling the passed call back function when all images are preloaded</li>
<li><strong>$.preLoadCSSImages</strong> &#8211; Preload all images found within the stylesheets of the document, then call the passed call back function when all the images are preloaded</li>
<li><strong>$.preLoadAllImages</strong> &#8211; Processes the stylesheet images and then if passed additional images, will process them too. When all are complete will call the passed callback function.</li>
</ol>
<p>Here&#8217;s the actual code now for the plugin:</p>
<pre class="brush: javascript">
/* jQuery.preloader - v0.95 - K Reeve aka BinaryKitten
*
* v0.95
* 	# Note - keeping below v1 as really not sure that I consider it public usable.
* 	# But it saying that it does the job it was intended to do.
* 	Added Completion of loading callback.
* 	Main Reworking With Thanks to Remy Sharp of jQuery for Designers
*
*
* v0.9
* 	Fixed .toString being .toSteing
*
* v0.8
*		Fixed sheet.href being null error (was causing issues in FF3RC1)
*
* v0.7
*		Remade the preLoadImages from jQuery to DOM
*
* v0.6
* 		Fixed IE6 Compatability!
*		Moved from jQuery to DOM
*
* v0.5
* 		Shifted the additionalimages loader in the preLoadAllImages so it wasn&#039;t called multiple times
* 		Created secondary .preLoadImages to handle additionalimages and so it can be called on itself
*/

(function ($) {
	$.preLoadImages = function(imageList,callback) {
		var pic = [], i, total, loaded = 0;
		if (typeof imageList != &#039;undefined&#039;) {
			if ($.isArray(imageList)) {
				total = imageList.length; // used later
					for (i=0; i &lt; total; i++) {
						pic[i] = new Image();
						pic[i].onload = function() {
							loaded++; // should never hit a race condition due to JS&#039;s non-threaded nature
							if (loaded == total) {
								if ($.isFunction(callback)) {
									callback();
								}
							}
						};
						pic[i].src = imageList[i];
					}
			}
			else {
				pic[0] = new Image();
				pic[0].onload = function() {
					if ($.isFunction(callback)) {
						callback();
					}
				}
				pic[0].src = imageList;
			}
		}
		pic = undefined;
	};

	$.preLoadCSSImages = function(callback) {
		var pic = [], i, imageList = [], loaded = 0, total, regex = new RegExp(&quot;url\((.*)\)&quot;,&#039;i&#039;),spl;
		var cssSheets = document.styleSheets, path,myRules,Rule,match,txt,img,sheetIdx,ruleIdx;
		for (sheetIdx=0;sheetIdx &lt; cssSheets.length;sheetIdx++){
			var sheet = cssSheets[sheetIdx];
			if (typeof sheet.href == &#039;string&#039; &amp;&amp; sheet.href.length &gt; 0) {
				spl = sheet.href.split(&#039;/&#039;);spl.pop();path = spl.join(&#039;/&#039;)+&#039;/&#039;;
			}
			else {
				path = &#039;./&#039;;
			}
			myRules = sheet.cssRules ? sheet.cssRules : sheet.rules;
			for (ruleIdx=0;ruleIdx &lt; myRules.length;ruleIdx++) {
				Rule = myRules[ruleIdx];
				txt = Rule.cssText ? Rule.cssText : Rule.style.cssText;
				match = regex.exec(txt);
				if (match != null) {
					img = match[1].substring(1,match[1].indexOf(&#039;)&#039;,1));
					if (img.substring(0,4) == &#039;http&#039;) {
						imageList[imageList.length] = img;
					}
					else if ( match[1].substring(1,2) == &#039;/&#039;) {
						var p2 = path.split(&#039;/&#039;);p2.pop();p2.pop();p2x = p2.join(&quot;/&quot;);
						imageList[imageList.length] = p2x+img;
					}
					else {
						imageList[imageList.length] = path+img;
					}
				}
			};
		};

		total = imageList.length; // used later
		for (i=0; i &lt; total; i++) {
			pic[i] = new Image();
			pic[i].onload = function() {
				loaded++; // should never hit a race condition due to JS&#039;s non-threaded nature
				if (loaded == total) {
					if ($.isFunction(callback)) {
						callback();
					}
				}
			};
			pic[i].src = imageList[i];
		}

	};
	$.preLoadAllImages = function(imageList,callback) {
		if (typeof imageList != &#039;undefined&#039;) {
			if ($.isFunction(imageList)) {
				callback = imageList;
			}
			else if (!$.isArray(imageList)) {
				imageList = [imageList];
			}
		}
		$.preLoadCSSImages(function(){
			if (imageList.length &gt; 0) {
				$.preLoadImages(imageList,function(){
					callback();
				});
			}
			else {
				callback();
			}
		});
	}
})(jQuery);
</pre>
<p>So now with these in place we can call them as so:</p>
<pre class="brush: javascript">
     $.preLoadImages(
          [
               &#039;http://www.google.co.uk/intl/en_uk/images/logo.gif&#039;,
               &#039;http://l.yimg.com/eur.yimg.com/i/uk/hp/yahoo1.png&#039;,
               &#039;http://tk2.stc.s-msn.com/br/hp/11/en-us/css/i/msn_b2.gif&#039;
          ],function(){
               alert(&#039;All Passed Images Loaded&#039;);
          }
     );
</pre>
<pre class="brush: javascript">
     $.preLoadCSSImages(function(){
          alert(&#039;All CSS Images Loaded&#039;);
     });
</pre>
<pre class="brush: javascript">
     $.preLoadAllImages(
          [
               &#039;http://www.google.co.uk/intl/en_uk/images/logo.gif&#039;,
               &#039;http://l.yimg.com/eur.yimg.com/i/uk/hp/yahoo1.png&#039;,
               &#039;http://tk2.stc.s-msn.com/br/hp/11/en-us/css/i/msn_b2.gif&#039;
          ],function(){
               alert(&#039;All Passed Images and All CSS Images Loaded&#039;);
          }
     );
</pre>
<p>To match up with Robert&#8217;s request for a loader image&#8230;</p>
<pre class="brush: javascript">
$.preLoadImages(&#039;/images/loader.gif&#039;,function() {
  /* Pre Load the loader gif first */
  $(&#039;&lt;img /&gt;&#039;).attr({
  	src:&#039;/images/loader.gif&#039;,
  	id:&#039;loader&#039;
  }).appendTo(&#039;#position&#039;);
  /* now preload stuff */
  $.preLoadCSSImages(function() {
     $(&#039;#loader&#039;).remove();
  })
});
</pre>
<p>Hopefully you have found this useful, as usual.. comments gratefully received.</p>
<p>[edit]<br />
 Thanks to Roberto for pointing out that the source had become corrupted in the post </p>
]]></content:encoded>
			<wfw:commentRss>http://binarykitten.com/dev/jq-plugins/107-jquery-image-preloader-plus-callbacks.html/feed</wfw:commentRss>
		<slash:comments>40</slash:comments>
		</item>
		<item>
		<title>php: processing raw Post / Get Values</title>
		<link>http://binarykitten.com/dev/php/95-php-processing-raw-post-get-values.html</link>
		<comments>http://binarykitten.com/dev/php/95-php-processing-raw-post-get-values.html#comments</comments>
		<pubDate>Wed, 21 Jan 2009 09:03:53 +0000</pubDate>
		<dc:creator>BinaryKitten</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[head]]></category>
		<category><![CDATA[post]]></category>
		<category><![CDATA[raw post]]></category>
		<category><![CDATA[web development]]></category>

		<guid isPermaLink="false">http://binarykitten.jkrswebsolutions.co.uk/?p=95</guid>
		<description><![CDATA[I just added a jquery plugin to call head requests, but because you are not doing a get or post request php won&#8217;t convert the values that are sent back to their form. To get around this we have to use the php://input stream ( is that the right word?) which is handy for a [...]]]></description>
			<content:encoded><![CDATA[<p>I just added a <a href="http://binarykitten.jkrswebsolutions.co.uk/2009/01/21/jquery-plugin-ajax-head-request/">jquery plugin</a> to call head requests, but because you are not doing a get or post request php won&#8217;t convert the values that are sent back to their form.<br />
To get around this we have to use the php://input stream ( is that the right word?) which is handy for a lot of things.</p>
<pre class="brush: php">
$data = file_get_contents(&quot;php://input&quot;);
$lines = explode(&quot;&amp;&quot;,$data);
foreach($lines as $line) {
    list($key,$value) = explode(&quot;=&quot;,$line,2);
    $_REQUEST[$key] = $value;
}
</pre>
<p>basically this grabs the data from the php://input and then splits it up into it&#8217;s component parts and then stores that within the $_REQUEST superglobal array.<br />
Why $_REQUEST  well we&#8217;re calling a head request and not a post or get request, so where else should it go.</p>
<p>The php://input is handy when doing stuff like xmlRPC or jsonRPC etc .. </p>
<p>Hopefully this will benefit someone out there</p>
<p>&lt;edit&gt;<br />
Thanks to Mortal of #php on OFTC (irc) for pointing out the unlimited explode </p>
]]></content:encoded>
			<wfw:commentRss>http://binarykitten.com/dev/php/95-php-processing-raw-post-get-values.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>jQuery plugin:  ajax Head Request</title>
		<link>http://binarykitten.com/dev/jq-plugins/88-jquery-plugin-ajax-head-request.html</link>
		<comments>http://binarykitten.com/dev/jq-plugins/88-jquery-plugin-ajax-head-request.html#comments</comments>
		<pubDate>Wed, 21 Jan 2009 13:34:32 +0000</pubDate>
		<dc:creator>BinaryKitten</dc:creator>
				<category><![CDATA[jQuery Plugins]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[head]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[xmlhttprequest]]></category>

		<guid isPermaLink="false">http://binarykitten.jkrswebsolutions.co.uk/?p=88</guid>
		<description><![CDATA[I recently looked into the head request for pulling data without much overhead. PHP can send headers out to the browser in a response.. so I looked into a way to get these headers. I came across a page that outlined using head requests via the xmlhttprequest object. I looked inside jQuery for this functionality [...]]]></description>
			<content:encoded><![CDATA[<p>I recently looked into the head request for pulling data without much overhead. PHP can send headers out to the browser in a response.. so I looked into a way to get these headers. I came across <a href="http://www.jibbering.com/2002/4/httprequest.html" target="_blank">a page</a> that outlined using head requests via the xmlhttprequest object.</p>
<p>I looked inside jQuery for this functionality and noticed it wasn&#8217;t there so I copied the $.post functionality and modified it into $.head.</p>
<p>So I present to you my Plugin for jQuery</p>
<pre class="brush: javascript">
/* jQuery.head - v1.0.3 - K Reeve aka BinaryKitten
*
*	makes a Head Request via XMLHttpRequest (ajax) and returns an object/array of headers returned from the server
*	$.head(url, [data], [callback])
*		url			The url to which to place the head request
*		data		(optional) any data you wish to pass - see $.post and $.get for more info
*		callback	(optional) Function to call when the head request is complete.
*					This function will be passed an object containing the headers with
*					the object consisting of key/value pairs where the key is the header name and the
*					value it&#039;s corresponding value
*
*	for discussion and info please visit: http://binarykitten.me.uk/jQHead
*
* ------------ Version History -----------------------------------
* v1.0.3
* 	Fixed the zero-index issue with the for loop for the headers
* v1.0.2
* 	placed the function inside an enclosure
*
* v1.0.1
* 	The 1st version - based on $.post/$.get
*/

(function ($) {
  $.extend({
	head: function( url, data, callback ) {
	  if ( $.isFunction( data ) ) {
		  callback = data;
		  data = {};
	  }

	  return $.ajax({
		type: &quot;HEAD&quot;,
		url: url,
		data: data,
		complete: function (XMLHttpRequest, textStatus) {
		  var headers = XMLHttpRequest.getAllResponseHeaders().split(&quot;\n&quot;);
		  var new_headers = {};
		  var l = headers.length;
		  for (var key=0;key&lt;l;key++) {
			  if (headers[key].length != 0) {
				  header = headers[key].split(&quot;: &quot;);
				  new_headers[header[0]] = header[1];
			  }
		  }
		  if ($.isFunction(callback)) {
			callback(new_headers);
		  }
		}
	  });
	}
  });
})(jQuery);
</pre>
<p>The function calls the passed url, passing the data and then processes the headers on completion. It takes the long text passed and splits it up into each header line and then splits it into array. The passed array is sent back to the callback function where the key of the array is the header name and the value being the header value.</p>
<p>To call our new function, we use the form just like the basic functionality of $.post and $.get:</p>
<pre class="brush: javascript">
/* $.head(&quot;url&quot;,{data},callback_function(headers) { }); */
/* Example */
$.head(&quot;index.php&quot;,{&#039;a&#039;:5,&#039;bc&#039;:&#039;help&#039;},function(headers) {
	$.each(headers,function(key,header){ console.log(key+&#039;:--:&#039;+header);});
});
</pre>
<p>This will use firebug&#8217;s console.log to output the details of each header.</p>
<p>all straight forward yes?<br />
Hopefully this will help or do something for the people out there<br />
Enjoy</p>
<p><strong>&lt; Edit &gt;</strong><br />
This has now been placed on plugins.jquery.com   ->  <a href="http://plugins.jquery.com/project/jqHead">http://plugins.jquery.com/project/jqHead</a></p>
]]></content:encoded>
			<wfw:commentRss>http://binarykitten.com/dev/jq-plugins/88-jquery-plugin-ajax-head-request.html/feed</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>php: smarty assign content plugin</title>
		<link>http://binarykitten.com/dev/php/smarty-templates/83-php-smarty-assign-content-plugin.html</link>
		<comments>http://binarykitten.com/dev/php/smarty-templates/83-php-smarty-assign-content-plugin.html#comments</comments>
		<pubDate>Mon, 12 Jan 2009 19:27:18 +0000</pubDate>
		<dc:creator>BinaryKitten</dc:creator>
				<category><![CDATA[Smarty]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[smarty]]></category>

		<guid isPermaLink="false">http://binarykitten.jkrswebsolutions.co.uk/?p=83</guid>
		<description><![CDATA[I needed to assign a block of content to a variable instead of just a value.. so i set about creating this plugin function smarty_block_assign_content($params, $content, &#38;$smarty) { $smarty = clone($smarty); //Copy the original class, so there&#039;s no garbage variables after we finish if(!isset($content)) return; if (!isset($params[&#039;var&#039;])) { $smarty-&#62;trigger_error(&#34;assign_content: missing &#039;var&#039; parameter&#34;, E_USER_WARNING); return; } [...]]]></description>
			<content:encoded><![CDATA[<p>I needed to assign a block of content to a variable instead of just a value.. so i set about creating this plugin</p>
<pre class="brush: php">
function smarty_block_assign_content($params, $content, &amp;$smarty)
{
        $smarty = clone($smarty); //Copy the original class, so there&#039;s no garbage variables after we finish
        if(!isset($content))
                return;
        if (!isset($params[&#039;var&#039;])) {
                $smarty-&gt;trigger_error(&quot;assign_content: missing &#039;var&#039; parameter&quot;, E_USER_WARNING);
                return;
        }

        //Compile content to ensure all smarty tags get processed
        $smarty-&gt;assign($params[&#039;var&#039;], $content);
                $smarty-&gt;assign($k, $v);
        return ;
}
</pre>
<p>example of usage: </p>
<pre class="brush: html">
{assign_content var=&#039;menu&#039;}
  {foreach from=$menus.cats-&gt;children item=&#039;cat&#039;}
  &lt;div class=&quot;item{if $cat-&gt;active || $cat-&gt;expanded}_select{/if}&quot;&gt;
    &lt;div class=&quot;arrow&quot;&gt;&lt;/div&gt;
    {if !$cat-&gt;active}&lt;a href=&quot;{$cat-&gt;link}&quot;&gt;{/if}{$cat-&gt;text}{if !$cat-&gt;active}&lt;/a&gt;
    {/if}
  &lt;/div&gt;
  {if $cat-&gt;children|<a href="http://twitter.com/count">@count</a> ne 0 || $cat-&gt;expanded}
    {foreach from=$cat-&gt;children item=&#039;subCat&#039;}
      &lt;div class=&quot;subitem{if $subCat-&gt;active}_select{/if}&quot;&gt;
        &lt;div class=&quot;arrow&quot;&gt;&lt;/div&gt;
      {if !$subCat-&gt;active}&lt;a href=&quot;{$subCat-&gt;link}&quot;&gt;{/if}{$subCat-&gt;text}{if !$subCat-&gt;active}&lt;/a&gt;{/if}
      &lt;/div&gt;
    {/foreach}
  {/if}
  {/foreach}
{/assign_content}
</pre>
<p>This will set the code we used to generate the menu and assign it to $menu for later use.<br />
Hope you find as usefull</p>
]]></content:encoded>
			<wfw:commentRss>http://binarykitten.com/dev/php/smarty-templates/83-php-smarty-assign-content-plugin.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>php: Menu Classes</title>
		<link>http://binarykitten.com/dev/php/74-php-menu-classes.html</link>
		<comments>http://binarykitten.com/dev/php/74-php-menu-classes.html#comments</comments>
		<pubDate>Mon, 12 Jan 2009 18:52:10 +0000</pubDate>
		<dc:creator>BinaryKitten</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[classes]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[development]]></category>

		<guid isPermaLink="false">http://binarykitten.jkrswebsolutions.co.uk/?p=74</guid>
		<description><![CDATA[Recently I&#8217;ve been working on my own framework (we&#8217;ve all done it i&#8217;m sure) and part of this was a menu handling system. The basis really is simple, we have a menu item which really is just link and text. That menu item could have children, could be active and/or be selected. In this case [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I&#8217;ve been working on my own framework (we&#8217;ve all done it i&#8217;m sure) and part of this was a menu handling system.</p>
<p>The basis really is simple, we have a menu item which really is just link and text. That menu item could have children, could be active and/or be selected.</p>
<p>In this case Selected is the currently menu item, which is also Active, and all it&#8217;s parents in the chain upwards are also active.</p>
<p>We also have a menu controller which makes it easier to add to the menus.</p>
<pre class="brush: php">
class menuHandler {
    public $menus=array();
    public function NewMenu() {
        $k = time();
        $this-&gt;menus[$k] = new menu_class();
        return $k;
    }
    public function AddMenu($key,menu_class $menu) {
        if (!isset($this-&gt;menus[$key])) {
            $this-&gt;menus[$key] = $menu;
        }
        return $this;
    }
    public function ItemCount($menuKey) {
        $menu = $this-&gt;menus[$menuKey];
        if (!($menu instanceof menu_class)) {
            return -1;
        }
        else {
            return count($menu-&gt;children);
        }
    }
    public function AddItem($menuKey,$itemKey=null,menu_class $item) {
        if (isset($this-&gt;menus[$menuKey])) {
            $menu = $this-&gt;menus[$menuKey];
            if (!isset($itemKey) or (is_numeric($itemKey) &amp;amp;amp;amp;&amp;amp;amp;amp; $itemKey &lt;0)) {
                $itemKey = count($menu-&gt;children);
            }
            $menu-&gt;AddSubItem($item,$itemKey,$menuKey);
        }
        return $this;
    }
    public function SetActive($id,$from=null) {
        $menu = new menu_class();
        if ($from == null) {
            $from = $this-&gt;menus;
        }
        elseif($from instanceof menu_class) {
            $from = $from-&gt;children;
        }
        foreach ($from as $key=&gt;&amp;amp;amp;amp;$menu) {
            if ($key==$id) {
                $menu-&gt;active=true;
                return true;
            }
            elseif(count($menu-&gt;children) !=0) {
                $r = $this-&gt;SetActive($id,$menu-&gt;children);
                if ($r) {
                    $menu-&gt;expanded = true;
                    return $r;
                }
            }
        }
    }
}
class menu_class {
    public $children = array();
    public $myParent=null;
    public $active = false;
    public $expanded = false;
    public $link =&#039;&#039;;
    public $text = &#039;&#039;;
    public $additional = array();

    public function AddSubItem($item,$ID=null,$parentID=null) {
        if ($item instanceof menu_class) {
        }
        elseif(is_array($item)) {
            $item = new menu_class();
            $item-&gt;text = $details[0];
            $item-&gt;link = $details[1];
        }
        if (isset($ID)) {
            if (isset($this-&gt;children[$ID])) {
                if (is_numeric($ID)) {
                    $this-&gt;children = array_insert($this-&gt;children,$item,$ID);
                }
            }
            else {
                $this-&gt;children[$ID] = $item;
            }
        }
        else {
            $this-&gt;children[] = $item;
        }
        if (!isset($parentID)) {
            $parentID = &quot;main&quot;;
        }
        $this-&gt;myParent = $parentID;
        ksort($this-&gt;children);
    }

    public function __construct($text=null,$link=null) {
        if(isset($text) &amp;amp;amp;amp;&amp;amp;amp;amp; !is_null($text)) {
            $this-&gt;text = $text;
        }
        if(isset($link) &amp;amp;amp;amp;&amp;amp;amp;amp; !is_null($link)) {
            $this-&gt;link = $link;
        }
    } 

    public function Format($text) {
        if (!empty($this-&gt;link)) {
            $text= str_replace(&quot;%href%&quot;,$this-&gt;link);
        }
        if (!empty($this-&gt;text)) {
            $text = str_replace(&quot;%text%&quot;,$this-&gt;text);
        }
        return $text;
    }
}
</pre>
<p>You will notice that i&#8217;ve used the <a href="http://binarykitten.jkrswebsolutions.co.uk/2009/01/11/php-insert-element-and-shift/">array_insert function</a> from my previous posting. This was why it was created, so that i could insert without worry that i would overwrite the code</p>
<p>here&#8217;s an example of using the code</p>
<pre class="brush: php">
$menus = new menuHandler();
$menus-&gt;AddMenu(&quot;main&quot;,new menu_class());
 $menus-&gt;AddItem(&quot;main&quot;,-1,new menu_class(&quot;TEXT&quot;,&quot;link.php&quot;));
 $menus-&gt;AddItem(&quot;main&quot;,-1,new menu_class(&quot;TEXT2&quot;,&quot;link2.php&quot;));
//we want this item to be 1st!
 $menus-&gt;AddItem(&quot;main&quot;,0,new menu_class(&quot;1st Link&quot;,&quot;homelink.php&quot;));
</pre>
<p>So far I pass the $menus out to smarty in the usual assignment method and process like as follows</p>
<pre class="brush: html">
 {foreach from=$menus.main-&gt;children item=&quot;mItem&quot;}
  &lt;div class=&quot;mainbutton&quot;&gt;&lt;a href=&quot;{$mItem-&gt;link}&quot;&gt;{$mItem-&gt;text}&lt;/a&gt;&lt;/div&gt;
 {/foreach}
</pre>
<p>For a cascaded menu i&#8217;ve used the following </p>
<pre class="brush: html">
{foreach from=$menus.cats-&gt;children item=&#039;cat&#039;}
  &lt;div class=&quot;item{if $cat-&gt;active || $cat-&gt;expanded}_select{/if}&quot;&gt;
    &lt;div class=&quot;arrow&quot;&gt;&lt;/div&gt;
    {if !$cat-&gt;active}&lt;a href=&quot;{$cat-&gt;link}&quot;&gt;{/if}{$cat-&gt;text}{if !$cat-&gt;active}&lt;/a&gt;
    {/if}
  &lt;/div&gt;
  {if $cat-&gt;children|<a href="http://twitter.com/count">@count</a> ne 0 || $cat-&gt;expanded}
    {foreach from=$cat-&gt;children item=&#039;subCat&#039;}
      &lt;div class=&quot;subitem{if $subCat-&gt;active}_select{/if}&quot;&gt;
        &lt;div class=&quot;arrow&quot;&gt;&lt;/div&gt;
      {if !$subCat-&gt;active}&lt;a href=&quot;{$subCat-&gt;link}&quot;&gt;{/if}{$subCat-&gt;text}{if !$subCat-&gt;active}&lt;/a&gt;{/if}
      &lt;/div&gt;
    {/foreach}
  {/if}
  {/foreach}
</pre>
<p>As usual any comments gratefully recieved</p>
]]></content:encoded>
			<wfw:commentRss>http://binarykitten.com/dev/php/74-php-menu-classes.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

