<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="wordpress/2.3.3" -->
<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/"
	>

<channel>
	<title>Montreal Internet Marketing</title>
	<link>http://www.ftwmarketing.com/blog</link>
	<description>Target your market with Fetch the Web</description>
	<pubDate>Sat, 31 May 2008 04:48:53 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.3.3</generator>
	<language>en</language>
			<item>
		<title>Data Caching in PHP</title>
		<link>http://www.ftwmarketing.com/blog/programming/data-caching-in-php</link>
		<comments>http://www.ftwmarketing.com/blog/programming/data-caching-in-php#comments</comments>
		<pubDate>Sat, 31 May 2008 04:43:47 +0000</pubDate>
		<dc:creator>Charles</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.ftwmarketing.com/blog/programming/data-caching-in-php</guid>
		<description><![CDATA[
When writing more advanced PHP based online applications the need to improve performance is often desirable. By caching arrays of data that have been queried from a database such as MYSQL you can save substantial CPU cycles, particularly with data that is needed frequently but seldom changes.


Instead of querying the database each time a page [...]]]></description>
			<content:encoded><![CDATA[<p>
When writing more advanced PHP based online applications the need to improve performance is often desirable. By <em>caching arrays of data</em> that have been queried from a database such as MYSQL you can save substantial CPU cycles, particularly with data that is needed frequently but seldom changes.
</p>
<p>
Instead of querying the database each time a page is called, we can read the file contents and have the data at our fingertips. For simple queries this will only be necessary in high traffic websites but in more complicated queries using full text index and multiple table joins, caching data can drastically reduce load with even a medium volume of traffic.
</p>
<p>
Below is a <strong>PHP data caching class</strong> I wrote that caches variables using file handle read and write to cache data with or without an expiration time.  I used file_get_contents() rather than fread to speed up the application even further. Rather than run a query each time the data is needed, the default is to cache it for 3600 seconds or an hour.
</p>
<p>
Configurations you may wish to change:</p>
<ul>
<li>Remove the appended words to the cached filename “_ObjectCache”.</li>
<li>Change the location of the tmp folder. The tmp folder may not be secure on a shared host.</li>
<li>Change the expiration time.</li>
</ul>
<p>
You can download the <a href="http://www.ftwmarketing.com/examples/ObjectCache.class.phps">ObjectCache.class.php</a> PHP5 cache here.
</p>

<div class="wp_codebox_msgheader"><span class="codebox_right"><a href="javascript:;" onclick="toggle_collapse('113');">[<span id="113_symbol">-</span>]</a></span><span class="codebox_left"><span id="l11code3"><a href="javascript:;" onclick="javascript:showCodeTxt('11code3'); return false;">View Code</a> PHP</span></span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="113"><td class="code" id="11code3"><pre class="php">&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #808080; font-style: italic;">/**
 * Caches manipulated variables into files for future use
 * @author		Charles Weiss &lt; c w e i s s [ a t ] f t w m a r k e t i n g . c o m &gt;
 * @copyright	Copyright (C) Fetch The Web 2006-2008
 * @version 0.1
 */</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> ObjectCache <span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #ff0000">$data</span><span style="color: #66cc66;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #ff0000">$ext</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'_ObjectCache.php'</span><span style="color: #66cc66;">;</span>	<span style="color: #808080; font-style: italic;">// The group of the cache file ( appended to end of filename ) </span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #ff0000">$path</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'/tmp/'</span><span style="color: #66cc66;">;</span>		<span style="color: #808080; font-style: italic;">// The FULL path to the cached file </span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #ff0000">$this</span><span style="color: #66cc66;">-&gt;</span><span style="color: #006600;">data</span> <span style="color: #66cc66;">=</span> <span style="color: #000066;">array</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	 <span style="color: #808080; font-style: italic;">/**
	 * Fetches variable from the cache if cache exists and data has not expired
	 * @param  $id The id of that variable for use in the filename
	 * @param  $lock Optional parameter instructing the class to lock the file.
	 * @return the cache contents OR false on error
	 */</span>
	 <span style="color: #000000; font-weight: bold;">function</span> get<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000">$id</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000066;">isset</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000">$this</span><span style="color: #66cc66;">-&gt;</span><span style="color: #006600;">data</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000">$id</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #ff0000">$this</span><span style="color: #66cc66;">-&gt;</span><span style="color: #006600;">data</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000">$id</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">;</span> <span style="color: #808080; font-style: italic;">// Already set, return to sender</span>
		<span style="color: #ff0000">$path</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000">$this</span><span style="color: #66cc66;">-&gt;</span><span style="color: #006600;">path</span><span style="color: #66cc66;">.</span><span style="color: #000066;">base64_encode</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000">$id</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">.</span><span style="color: #ff0000">$this</span><span style="color: #66cc66;">-&gt;</span><span style="color: #006600;">ext</span><span style="color: #66cc66;">;</span>
&nbsp;
		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000066;">file_exists</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000">$path</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&amp;&amp;</span> <span style="color: #000066;">is_readable</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000">$path</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #808080; font-style: italic;">// Check if the cache file exists</span>
			<span style="color: #b1b100;">include</span> <span style="color: #ff0000">$path</span><span style="color: #66cc66;">;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000066;">isset</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000">$expires</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&amp;&amp;</span> <span style="color: #ff0000">$expires</span> <span style="color: #66cc66;">&lt;=</span> <span style="color: #000066;">time</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
				<span style="color: #ff0000">$this</span><span style="color: #66cc66;">-&gt;</span><span style="color: #006600;">clear</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000">$id</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
				<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">;</span>
			<span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
				<span style="color: #ff0000">$cache</span> <span style="color: #66cc66;">=</span> <span style="color: #000066;">file_get_contents</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000">$path</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">;</span>
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	 <span style="color: #808080; font-style: italic;">/**
	 * Sets variable into the cache
	 * @param  $id The id of that variable for use in the filename
	 * @param  $cache The data to be stored
	 * @param  $lifetime The expiration time  (in seconds) from file creation
	 * @return the cache contents OR false on error
	 */</span>
	<span style="color: #000000; font-weight: bold;">function</span> put<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000">$id</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000">$cache</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000">$lifetime</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #ff0000">$this</span><span style="color: #66cc66;">-&gt;</span><span style="color: #006600;">data</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000">$id</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000">$cache</span><span style="color: #66cc66;">;</span>
&nbsp;
		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000066;">is_resource</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000">$cache</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #ff0000;">&quot;Can't cache resource.&quot;</span><span style="color: #66cc66;">;</span>
&nbsp;
		<span style="color: #ff0000">$path</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000">$this</span><span style="color: #66cc66;">-&gt;</span><span style="color: #006600;">path</span><span style="color: #66cc66;">.</span><span style="color: #000066;">base64_encode</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000">$id</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">.</span><span style="color: #ff0000">$this</span><span style="color: #66cc66;">-&gt;</span><span style="color: #006600;">ext</span><span style="color: #66cc66;">;</span>
		<span style="color: #ff0000">$fp</span> <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">@</span><span style="color: #000066;">fopen</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000">$path</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'w'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">!</span><span style="color: #ff0000">$fp</span><span style="color: #66cc66;">&#41;</span> <span style="color: #000066;">echo</span> <span style="color: #ff0000;">'Unable to open file for writing.'</span><span style="color: #66cc66;">.</span><span style="color: #ff0000">$path</span><span style="color: #66cc66;">;</span>
		<span style="color: #66cc66;">@</span><span style="color: #000066;">flock</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000">$fp</span><span style="color: #66cc66;">,</span> LOCK_EX<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
		<span style="color: #66cc66;">@</span><span style="color: #000066;">fwrite</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000">$fp</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'&lt;?php $cache='</span><span style="color: #66cc66;">.</span><span style="color: #000066;">var_export</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000">$data</span><span style="color: #66cc66;">,</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">.</span><span style="color: #ff0000;">';'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000">$lifetime</span> <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">@</span><span style="color: #000066;">fwrite</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000">$fp</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'$expires='</span><span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066;">time</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">+</span><span style="color: #ff0000">$lifetime</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">.</span><span style="color: #ff0000;">';'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
		<span style="color: #66cc66;">@</span><span style="color: #000066;">fwrite</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000">$fp</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">' ?&gt;'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
		<span style="color: #66cc66;">@</span><span style="color: #000066;">flock</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000">$fp</span><span style="color: #66cc66;">,</span> LOCK_UN<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
		<span style="color: #66cc66;">@</span><span style="color: #000066;">fclose</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000">$fp</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000066;">file_exists</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000">$path</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #000066;">chmod</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000">$path</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">666</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
		<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">;</span>
&nbsp;
		<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">;</span>
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	 <span style="color: #808080; font-style: italic;">/**
	 * Deletes the cache file
	 * @param  $id The id of that variable for use in the filename
	 * @param  $lock Optional parameter instructing the class to lock the file.
	 * @return the true or descriptive string on error
	 */</span>
	<span style="color: #000000; font-weight: bold;">function</span> clear<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000">$id</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000066;">isset</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000">$this</span><span style="color: #66cc66;">-&gt;</span><span style="color: #006600;">data</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000">$id</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #000066;">unset</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000">$this</span><span style="color: #66cc66;">-&gt;</span><span style="color: #006600;">data</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000">$id</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
		<span style="color: #ff0000">$pretty_id</span> <span style="color: #66cc66;">=</span> <span style="color: #000066;">base64_encode</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000">$id</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
		<span style="color: #ff0000">$path</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000">$this</span><span style="color: #66cc66;">-&gt;</span><span style="color: #006600;">path</span><span style="color: #66cc66;">.</span><span style="color: #ff0000">$pretty_id</span><span style="color: #66cc66;">.</span><span style="color: #ff0000">$this</span><span style="color: #66cc66;">-&gt;</span><span style="color: #006600;">ext</span><span style="color: #66cc66;">;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000066;">file_exists</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000">$path</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&amp;&amp;</span> <span style="color: #000066;">unlink</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000">$path</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">;</span>
		<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">return</span> <span style="color: #ff0000;">'Cache could not be cleared.'</span><span style="color: #66cc66;">;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Example usage:</p>

<div class="wp_codebox_msgheader"><span class="codebox_right"><a href="javascript:;" onclick="toggle_collapse('114');">[<span id="114_symbol">-</span>]</a></span><span class="codebox_left"><span id="l11code4"><a href="javascript:;" onclick="javascript:showCodeTxt('11code4'); return false;">View Code</a> PHP</span></span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="114"><td class="code" id="11code4"><pre class="php"><span style="color: #b1b100;">include</span> <span style="color: #ff0000;">'./ObjectCache.class.php'</span><span style="color: #66cc66;">;</span>
<span style="color: #ff0000">$data</span> <span style="color: #66cc66;">=</span> <span style="color: #000066;">array</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'a'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'b'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'c'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'d'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'e'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'f'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'g'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
<span style="color: #ff0000">$distinct_name</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'lala'</span><span style="color: #66cc66;">;</span>
<span style="color: #ff0000">$cache</span> <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ObjectCache<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
<span style="color: #808080; font-style: italic;">// Cache will last 3600 seconds or 1 hr</span>
<span style="color: #ff0000">$cache</span><span style="color: #66cc66;">-&gt;</span><span style="color: #006600;">put</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000">$distinct_name</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000">$data</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">3600</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
<span style="color: #ff0000">$data2</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000">$cache</span><span style="color: #66cc66;">-&gt;</span><span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000">$distinct_name</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
<span style="color: #808080; font-style: italic;">// Forcibly clear the cache (on data update via admin perhaps</span>
<span style="color: #808080; font-style: italic;">//$cache-&gt;clear($distinct_name);</span>
<span style="color: #000066;">print_r</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000">$data2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
<span style="color: #000066;">unset</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000">$cache</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span></pre></td></tr></table></div>

<p>
I would like to have run this example live but unfortunately the server this site is hosted on is php4 at the moment so you will have to run it on your own box.
</p>
<p>
Hope this helps some of you. Feel free to give credit or not; whatever makes your socks go up and down! <img src='http://www.ftwmarketing.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ftwmarketing.com/blog/programming/data-caching-in-php/feed</wfw:commentRss>
		</item>
		<item>
		<title>Javascript Cascading Form Values - part 2</title>
		<link>http://www.ftwmarketing.com/blog/programming/javascript-cascading-form-values-part-2</link>
		<comments>http://www.ftwmarketing.com/blog/programming/javascript-cascading-form-values-part-2#comments</comments>
		<pubDate>Sat, 19 Jan 2008 16:36:02 +0000</pubDate>
		<dc:creator>charles</dc:creator>
		
		<category><![CDATA[JavaScript]]></category>

		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.ftwmarketing.com/blog/uncategorized/javascript-cascading-form-values-part-2</guid>
		<description><![CDATA[A few weeks ago, I started a simple tutorial on how to cascade JavaScript check boxes from one row to the next in order to simplify the manipulation of large forms. This week, I’ll take it a step further by adding the same ability to both drop downs and text fields.
I’ve created a small example [...]]]></description>
			<content:encoded><![CDATA[<p>A few weeks ago, I started a <a href="http://www.ftwmarketing.com/examples/casca1.html" title="Simple Cascading CheckBoxes">simple tutorial on how to cascade</a> JavaScript check boxes from one row to the next in order to simplify the manipulation of large forms. This week, I’ll take it a step further by adding the same ability to both drop downs and text fields.</p>
<p>I’ve created a small example to help <a href="http://www.ftwmarketing.com/examples/casca2.html" title="Cascading Form Values">illustrate the functionality of the cascade</a>. Once you load the cascade example page and click the down arrow next to a text field. It should change all the values in the text fields below to the same value as appears in the text box you clicked. The same can be done in the active column. Change one of the values in the active column to True and then click the down arrow next to it and all the rows below will change to True.</p>
<p>Without further discussion, here’s the Javascript code:<br />
<code><br />
function cascSelect(theElement) {<br />
var theForm = theElement.form;<br />
var startBox = theElement.name;<br />
for(z=0; z</code></p>
<theform.length;z++){>if(theForm[z].type == &#8216;checkbox&#8217; &amp;&amp; theForm[z].name != &#8216;checkall&#8217;){<br />
var checkNames = theForm[z].name;<br />
if(checkNames == startBox.replace(/casca/, &#8216;checkeddelete&#8217;)) {<br />
var startValue = (theForm[z].checked);<br />
var startKey = z;<br />
}<br />
if (z &gt; startKey) {<br />
theForm[z].checked = startValue;<br />
}<br />
}<br />
}<br />
}<br />
function cascScore(theElement) {<br />
var theForm = theElement.form;<br />
var startBox = theElement.name;<br />
for(z=0; z</theform.length;z++){>
<theform.length;z++){>if(theForm[z].type == &#8216;text&#8217;){<br />
var checkNames = theForm[z].name;<br />
var tmp = startBox.replace(/cascScore/, &#8221;);<br />
tmp += &#8217;score&#8217;;<br />
if(checkNames == tmp) {<br />
var startValue = (theForm[z].value);<br />
var startKey = z;<br />
}<br />
if(theForm[z].name.match(/[0-9]score/)) {<br />
if (z &gt; startKey) {<br />
theForm[z].value = startValue;<br />
}<br />
}<br />
}<br />
}<br />
}<br />
function selectTrueFalse(theElement) {<br />
var theForm = theElement.form;<br />
var startBox = theElement.name;<br />
for(z=0; z</theform.length;z++){>
<theform.length;z++){>if(theForm[z].type == &#8217;select-one&#8217;){<br />
var checkNames = theForm[z].name;<br />
var tmp = startBox.replace(/selectTrueFalse/, &#8221;);<br />
tmp += &#8216;active&#8217;;<br />
if(checkNames == tmp) {<br />
var startValue = (theForm[z].value);<br />
var startKey = z;<br />
}<br />
if(theForm[z].name.match(/[0-9]active/)) {<br />
if (z &gt; startKey) {<br />
theForm[z].value = startValue;<br />
}<br />
}<br />
}<br />
}<br />
} </theform.length;z++){>The only thing you have to watch for is the naming scheme. I was pretty lazy when I put all of this together and so it doesn’t follow any clean schema so to speak. Make sure the form fields have the same name as the JavaScript functions and you should be set.</p>
<p>Hope this helps some of you out, like I said before it may have saved me a few hours of work had I known it existed previously. It can be cleaned up quite a bit and I know all three functions could be lumped together into a more dynamic function if you like, but I’m not going to trouble with that here.</p>
<p>Cheers! Charles <img src='http://www.ftwmarketing.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ftwmarketing.com/blog/programming/javascript-cascading-form-values-part-2/feed</wfw:commentRss>
		</item>
		<item>
		<title>Creating visual styles for OpenAds text advertisements</title>
		<link>http://www.ftwmarketing.com/blog/maketing/creating-visual-styles-for-openads-text-advertisements</link>
		<comments>http://www.ftwmarketing.com/blog/maketing/creating-visual-styles-for-openads-text-advertisements#comments</comments>
		<pubDate>Mon, 07 Jan 2008 18:05:18 +0000</pubDate>
		<dc:creator>charles</dc:creator>
		
		<category><![CDATA[Marketing]]></category>

		<guid isPermaLink="false">http://www.ftwmarketing.com/blog/maketing/creating-visual-styles-for-openads-text-advertisements</guid>
		<description><![CDATA[Recently I had a project where I was aked to add visual styls to Openads in an effort to create text that were similar in style to Google&#8217;s Adsense ads. Openads is an ads server or an extended banner rotator which allows for stats and all kinds of other features. I won&#8217;t get into it, [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I had a project where I was aked to add visual styls to <a href="http://www.openads.org/">Openads</a> in an effort to create text that were similar in style to Google&#8217;s <a href="http://www.google.com/adsense">Adsense</a> ads. Openads is an ads server or an extended banner rotator which allows for stats and all kinds of other features. I won&#8217;t get into it, if you need an adserver or want to know more, visit the <a href="http://www.openads.org/">Openads</a> site directly.</p>
<p>Setting up the text ad is fairly simple. Click on Inventory, Advertisers, Choose the advertiser, Choose or Create a new campaign and then click on add a new banner. I&#8217;m hoping you know how to get to that point, otherwise give me a shout and I&#8217;ll be happy walk you through it.</p>
<p>Choose &#8220;Text Ad&#8221; from the dropdown box and you the page will look something like this:<img src="http://www.ftwmarketing.com/images/Analytics/openads.jpg" alt="openads text banner " align="middle" height="425" width="500" /></p>
<p>Ok, so now in the big trextarea, lets insert the following:<br />
<code><br />
&lt;font color="blue"&gt;&lt;strong&gt;How To Kill Bed Bugs&lt;/strong&gt;&lt;/font&gt;<br />
Kill Bed Bugs with Strei-Fab Spray Buy Now from Nixalite.com<br />
&lt;font color="green"&gt;www.nixalite.com/bedbugs.asp&lt;/font&gt;<br />
</code></p>
<p>This should end up looking like this when all is said and done:<br />
<font color="blue"><strong>How To Kill Bed Bugs</strong></font><br />
Kill Bed Bugs with Strei-Fab Spray Buy Now from Nixalite.com<br />
<font color="green">www.nixalite.com/bedbugs.asp</font></p>
<p>Now when we publish the text link, just place it in 200px div and voila, you have a sudo adsense text advertisement.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ftwmarketing.com/blog/maketing/creating-visual-styles-for-openads-text-advertisements/feed</wfw:commentRss>
		</item>
		<item>
		<title>Analytics changes urchin.js to the new and improved ga.js</title>
		<link>http://www.ftwmarketing.com/blog/maketing/analytics-changes-urchinjs-to-the-new-and-improved-gajs</link>
		<comments>http://www.ftwmarketing.com/blog/maketing/analytics-changes-urchinjs-to-the-new-and-improved-gajs#comments</comments>
		<pubDate>Sat, 05 Jan 2008 04:03:20 +0000</pubDate>
		<dc:creator>Charles</dc:creator>
		
		<category><![CDATA[Marketing]]></category>

		<guid isPermaLink="false">http://www.ftwmarketing.com/blog/maketing/analytics-changes-urchinjs-to-the-new-and-improved-gajs</guid>
		<description><![CDATA[First off, I&#8217;d like to wish a happy new year to all of you. I hope that this year is even better than last, and I must admit that last was very good for me!
Alrighty, so one of my clients took my advice and asked me to implement the new ga.js tracking cookie on his [...]]]></description>
			<content:encoded><![CDATA[<p>First off, I&#8217;d like to wish a happy new year to all of you. I hope that this year is even better than last, and I must admit that last was very good for me!</p>
<p>Alrighty, so one of my clients took my advice and asked me to implement the new ga.js tracking cookie on his site. I didn&#8217;t pay much attention to what had changed, and just uploaded the new code without thinking about it. Google offers upgrade, they must insure backwards compatibility right?!?! Naw, that would be way too simple, instead they decide to take Microsoft&#8217;s lead and nerf backwards compatibility.</p>
<p>Ok, great, it doesn&#8217;t work so how do we fix it? Well, in this case, my client re-directs links to other sites. These redirects are driven via database driven backend and tend to change often enough that they shouldn&#8217;t be (and aren&#8217;t) hardcoded. To capture these clicks in analytics (which we compare to our own tracking system and notice a less than 5% difference) we used the following in our links:<br />
<code><br />
onClick="javascript:urchinTracker('/outgoing/uniqueID_or_Title');"<br />
</code><br />
and then changed it to<br />
<code><br />
onClick="javascript:pageTracker._trackPageview('/outgoing/uniqueID_or_Title');"<br />
</code></p>
<p>So how will it work out? Hopefully all will  be fine and our data will once again be tracked by analytics&#8230; if not I&#8217;ll let you guys know. BTW: You can find the information on google here: (for both versions of the tracking code)</p>
<p><a href="http://www.google.com/support/googleanalytics/bin/answer.py?hl=en&#038;answer=55520">GA Tracker</a> (new)</p>
<p><a href="http://www.google.com/support/googleanalytics/bin/answer.py?answer=74979">Urchin Tracker</a> (old)</p>
<p> <img src='http://www.ftwmarketing.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> Charles</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ftwmarketing.com/blog/maketing/analytics-changes-urchinjs-to-the-new-and-improved-gajs/feed</wfw:commentRss>
		</item>
		<item>
		<title>Merry Christmas</title>
		<link>http://www.ftwmarketing.com/blog/news/merry-christmas</link>
		<comments>http://www.ftwmarketing.com/blog/news/merry-christmas#comments</comments>
		<pubDate>Tue, 25 Dec 2007 14:13:03 +0000</pubDate>
		<dc:creator>charles</dc:creator>
		
		<category><![CDATA[News]]></category>

		<guid isPermaLink="false">http://www.ftwmarketing.com/blog/news/merry-christmas</guid>
		<description><![CDATA[Merry Christmas and Happy Holidays to all!
]]></description>
			<content:encoded><![CDATA[<p>Merry Christmas and Happy Holidays to all!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ftwmarketing.com/blog/news/merry-christmas/feed</wfw:commentRss>
		</item>
		<item>
		<title>Javascript Cascades - part 1</title>
		<link>http://www.ftwmarketing.com/blog/programming/javascript/javascript-cascades-part-1</link>
		<comments>http://www.ftwmarketing.com/blog/programming/javascript/javascript-cascades-part-1#comments</comments>
		<pubDate>Sat, 22 Dec 2007 19:58:55 +0000</pubDate>
		<dc:creator>charles</dc:creator>
		
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://www.ftwmarketing.com/blog/programming/javascript/javascript-cascades-part-1</guid>
		<description><![CDATA[In the ten years that I’ve been doing online programming, I’ve spent at least half of that time working on the backends or administrative portions of content management systems. Content Management Systems or CMS as they’re often called are dynamically driven websites. Great, but what the hell does that mean? A CMS is an online [...]]]></description>
			<content:encoded><![CDATA[<p>In the ten years that I’ve been doing online programming, I’ve spent at least half of that time working on the backends or administrative portions of content management systems. <a href="http://en.wikipedia.org/wiki/Content_management_system">Content Management System</a>s or CMS as they’re often called are dynamically driven websites. Great, but what the hell does that mean? A CMS is an online tool that allows users to build very large websites without needing knowledge of the workings behind the text.</p>
<p>Take WordPress for instance, it’s a blog publishing tool (and happens to be the tool I chose to publish this blog entry). It’s also a variation of a CMS. It allows users to enter text in a similar way to writing in <a href="http://office.microsoft.com/en-ca/word/default.aspx">Microsoft Word</a> and have it appear on a website following a specific template. Once the CMS templates are setup, the display will have the same formatting even though the content is different. The user just writes the content and chooses when to display it. The programming behind the scenes decides how to display it and how to manage all the rest of the workings.</p>
<p>One of my clients wanted a way to edit multiple entries at a time which is fairly simple, you output the listings into a grid display in a table; I won’t bore you with how to do that. The complex part was finding a solution to choose multiple blocks of entries to edit, kind of like a select all but not quite, more of a select some but faster than selecting them one by one.</p>
<p>Either he or one of his programmers decided top create a <a href="http://www.ftwmarketing.com/examples/casca1.html">cascading tool</a> that would allow users to select a row, click on the submit button in that row and each of the rows below that one selected. By contrast, deselecting the row further down the page and hitting that row’s submit button would deselect all the rows below that one.</p>
<p><a href="http://www.ftwmarketing.com/examples/casca1.html">Here’s an example </a>and below is the Javscript I created to do this:<br />
<code>// JavaScript Document<br />
function cascSelect(theElement) {<br />
var theForm = theElement.form;<br />
var startBox = theElement.name;<br />
for(z=0; z&lt;theForm.length;z++){<br />
if(theForm[z].type == 'checkbox' &amp;&amp; theForm[z].name != 'checkall'){<br />
var checkNames = theForm[z].name;<br />
if(checkNames == startBox.replace(/pos/, 'chk')) {<br />
var startValue = (theForm[z].checked);<br />
var startKey = z;<br />
}<br />
if (z &gt; startKey) {<br />
theForm[z].checked = startValue;<br />
}<br />
}<br />
}<br />
}</code></p>
<p>You might be wondering what the usefulness of the cascade might be. Think about having  200 rows on the page instead of the six I placed on the examples. What if you wanted to change the values for twenty of those rows without having to select each one individually. This will facilitate that goal.</p>
<p>I&#8217;ve used similar solutions which were stored in session cookies and selected via submit rather than javascript functions however using javascript will allow us to cascade values as I will show in my next entry. I hope this is helpful to someone, I might have saved a bunch of hours had I known this solution existed.</p>
<p><a href="http://www.ftwmarketing.com/blog/wp-content/uploads/2007/12/casca1.html" title="Cascades Select Example 1"></a></p>
<p> <img src='http://www.ftwmarketing.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> Charles</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ftwmarketing.com/blog/programming/javascript/javascript-cascades-part-1/feed</wfw:commentRss>
		</item>
		<item>
		<title>Bonjour</title>
		<link>http://www.ftwmarketing.com/blog/news/bonjour</link>
		<comments>http://www.ftwmarketing.com/blog/news/bonjour#comments</comments>
		<pubDate>Sat, 22 Dec 2007 17:56:49 +0000</pubDate>
		<dc:creator>charles</dc:creator>
		
		<category><![CDATA[News]]></category>

		<guid isPermaLink="false">http://www.ftwmarketing.com/blog/?p=3</guid>
		<description><![CDATA[Alrighty then, where to begin? Well, as some of you reading this may know, my name is Charles and I&#8217;m currently working as a Programming and Marketing consultant from my home in Montreal. In February, it&#8217;s likely that I will take on a full time position with a fairly large review site. I guess all [...]]]></description>
			<content:encoded><![CDATA[<p>Alrighty then, where to begin? Well, as some of you reading this may know, my name is Charles and I&#8217;m currently working as a Programming and Marketing consultant from my home in Montreal. In February, it&#8217;s likely that I will take on a full time position with a fairly large review site. I guess all the writings around me is contagious as I feel compelled to write my own musings now. So without further a due, welcome to the Fetch the Web blog where I might post things from time to time of programming, marketing or just the general BS variety. I&#8217;m not so foolish as to commit myself to writing here often hehe.</p>
<p>Thanks for stopping by</p>
<p>-Charles</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ftwmarketing.com/blog/news/bonjour/feed</wfw:commentRss>
		</item>
	</channel>
</rss>
