<?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>Christian Riesen &#187; PHP</title>
	<atom:link href="http://christianriesen.com/category/development/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://christianriesen.com</link>
	<description>Life and work in the information and communication age</description>
	<lastBuildDate>Thu, 29 Dec 2011 17:52:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Programming helping the writer and getting more exposure</title>
		<link>http://christianriesen.com/2010/03/programming-helping-the-writer-and-getting-more-exposure/</link>
		<comments>http://christianriesen.com/2010/03/programming-helping-the-writer-and-getting-more-exposure/#comments</comments>
		<pubDate>Mon, 15 Mar 2010 13:37:28 +0000</pubDate>
		<dc:creator>Christian Riesen</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Toreas]]></category>
		<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[MediaWiki]]></category>
		<category><![CDATA[Twitter]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[XML-RPC]]></category>
		<category><![CDATA[ZendFramework]]></category>
		<category><![CDATA[Zend_Http_Client]]></category>

		<guid isPermaLink="false">http://christianriesen.com/?p=160</guid>
		<description><![CDATA[Writing is fun, but takes up a lot of time. Something that also takes up a lot of time is the things around writing. Those other things take time away from the more fun part of the general just writing the story. Now wouldn't it be great if that was no longer the case?

Taking ...]]></description>
			<content:encoded><![CDATA[<p>Writing is fun, but takes up a lot of time. Something that also takes up a lot of time is the things around writing. Those other things take time away from the more fun part of the general just writing the story. Now wouldn&#8217;t it be great if that was no longer the case?</p>
<p>Taking my little experiment Toreas, the <a href="http://toreas.com/" onclick="pageTracker._trackPageview('/outgoing/toreas.com/?referer=');">free fantasy book</a> or novel if you like, as a sample, I was in updating hell. The page itself where the book is readable from is an instance of <a href="http://www.mediawiki.org/" onclick="pageTracker._trackPageview('/outgoing/www.mediawiki.org/?referer=');">MediaWiki</a>. I chose it for the ease of adding new things, a good and stable code base, but also because I had a lot of experience in extending it. Then I wanted to add notes to each release and of course that looks like a blog. So my number one choice was simply <a href="http://wordpress.org/" onclick="pageTracker._trackPageview('/outgoing/wordpress.org/?referer=');">WordPress</a> (which also powers this site), for a lot of the same reasons that MediaWiki was the page software. Twitter is not a software for me, but a web service as such and I wanted to add tweets as soon as there is a new chapter out (even though there probably is nobody reading those). I also have plans to include more services, like Facebook, but I have not yet come around to those.</p>
<p>So, a new chapter is out. I have to edit 4 MediaWiki pages (3 of those templates), one of them I have to calculate newly with a script I have written. So for calculating the <a href="http://toreas.com/Statistics" onclick="pageTracker._trackPageview('/outgoing/toreas.com/Statistics?referer=');">statistics</a> I have to open the template (which requires editing the main article first to get the edit link) run from a website a script that calls up each and every page already created and creates the stats out of that in wikitext. Now I copy paste this over from my script to the template and save it. Next I have to login to twitter, post the comment in the right format. And last but not least post the blog post on the page, again logging in, selecting the right category and tags and so on and so forth. All of this made it a horror to update and I frankly found myself thinking a few times to delay an update just because of this horrible (admittedly self induced) process.</p>
<p>Then I stumbled across an artifact in my memory. <a href="http://www.mediawiki.org/wiki/API" onclick="pageTracker._trackPageview('/outgoing/www.mediawiki.org/wiki/API?referer=');">MediaWiki has an API</a>. Looking at it <a href="http://en.wikipedia.org/w/api.php" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/w/api.php?referer=');">closely</a> I found that you could login, open pages and edit them. You could do pretty much anything with it, but those were my only requirements. After looking around a bit, I found a class named Snoopy that is supposed to take care of it, but it was still too much &#8220;manual labor&#8221; on my side. So A quick check on ZendFramework and in the Http Client library I found all the parts that I needed to make this work. I could define one client and use it like a browser. It would even keep the cookies for me and do all that handling.</p>
<p>So I whipped up my own MediaWiki API class, based on Zend_Http_Client. It actually uses the autoloader as well, but that&#8217;s because I&#8217;m lazy. So now I instantiate it by handing it the api url, a user name and password, and the whole thing is ready for duty. A get function gives me the content (needed for the latest changes, where I kill the last line in the box and add a new one on top) and an edit function that allows me to simply overwrite the current version. All the magic is done behind the scenes and no worries for me. Twitter of course also has an API to which Zend already has Zend_Service_Twitter. Again, three lines and the new status is posted. And lastly, WordPress has an XMLPRC interface, which has had me quiet confused for a while. Now that I understand it (or at least I think I do) I am able to post news in there by just supplying a title and description, while the tags and categories are preset (for this particular thing).</p>
<p>My 20 minutes of monkeying around with everything, plus additional time for writing a blog post if I was so inclined to do, has been reduced to copy pasting the story (then write the blog post) and hit a button. So for the last eight chapters I have lost two and a half hours of time, which no longer happens. Not only do I gain time, but the step to actually publish the next chapter has become pretty much nothing, as opposed to the big process it has been before.</p>
<p>Once I clean up the API class a little, I might even release my code for the MediaWiki class.</p>
<p class="wp-flattr-button"></p>]]></content:encoded>
			<wfw:commentRss>http://christianriesen.com/2010/03/programming-helping-the-writer-and-getting-more-exposure/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Testing missing translations in Zend Framework</title>
		<link>http://christianriesen.com/2009/12/testing-missing-translations-in-zend-framework/</link>
		<comments>http://christianriesen.com/2009/12/testing-missing-translations-in-zend-framework/#comments</comments>
		<pubDate>Thu, 10 Dec 2009 10:03:29 +0000</pubDate>
		<dc:creator>Christian Riesen</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[Zend_Translate]]></category>

		<guid isPermaLink="false">http://christianriesen.com/?p=110</guid>
		<description><![CDATA[Zend_Translate is a wonderful tool to translate your page. While your backends may vary, you have to ensure you call it everywhere in your code. Unfortunately, there is no easy way to spot any untranslated strings by default. But fear not, you can easy make one yourself.

Translate uses an adapter to actually do the ...]]></description>
			<content:encoded><![CDATA[<p>Zend_Translate is a wonderful tool to translate your page. While your backends may vary, you have to ensure you call it everywhere in your code. Unfortunately, there is no easy way to spot any untranslated strings by default. But fear not, you can easy make one yourself.</p>
<p>Translate uses an adapter to actually do the heavy lifting. Translate itself is just a gateway to the adapter classes, so the magic happens there. Now my solution was to just create a new adapter and have it give back a string that&#8217;s fixed every single time. So any request to the translate method would return the same string. With this you can just look on your page, see where it doesn&#8217;t display your special string and there you have your missing spots. You could set it to be an empty string, but if you use it for links, then you wont see the link anymore.</p>
<p>Well used, you will only see the data strings, like user names, the configured replacement string and of course any string you missed.<br />
<span id="more-110"></span><br />
I use the App prefix for such very common bits of code that I can share easy between all my applications and of course with others, but there is nothing preventing you from changing this of course.</p>
<p>First create a file called Testing.php in the folder library/App/Translate/Adapter filled with the content below.</p>
<pre class="brush: php; title: ; notranslate">&lt;?php

/** Zend_Translate_Adapter */
require_once 'Zend/Translate/Adapter.php';

/**
 * @category   App
 * @package    App_Translate_Adapter_Testing
 * @copyright  Copyright (c) 2009 Christian Riesen &lt;chris.riesen@gmail.com&gt;
 * @license    Public Domain
 */
class App_Translate_Adapter_Testing extends Zend_Translate_Adapter
{
    private $_data = array();
    private $_teststring = 'XtestX';

    /**
     * Generates the adapter
     *
     * @param  array               $data     Translation data
     * @param  string|Zend_Locale  $locale   OPTIONAL Locale/Language to set, identical with locale identifier,
     *                                       see Zend_Locale for more information
     * @param  array               $options  OPTIONAL Options to set
     */
    public function __construct($data, $locale = null, array $options = array())
    {
        $this-&gt;_loadTranslationData($data, $locale, $options);
    }

    /**
     * Load translation data
     *
     * @param  string|array  $data
     * @param  string        $locale  Locale/Language to add data for, identical with locale identifier,
     *                                see Zend_Locale for more information
     * @param  array         $options OPTIONAL Options to use
     * @return array
     */
    protected function _loadTranslationData($data, $locale, array $options = array())
    {
        $this-&gt;_data = array();
        if (!is_null($data)) {
        	$this-&gt;_teststring = $data;
        }
        return $this-&gt;_data;
    }

    /**
     * Translates the given string
     * returns the translation
     *
     * @param  string|array       $messageId Translation string, or Array for plural translations
     * @param  string|Zend_Locale $locale    (optional) Locale/Language to use, identical with
     *                                       locale identifier, @see Zend_Locale for more information
     * @return string
     */
    public function translate($messageId, $locale = null)
    {
        return $this-&gt;_teststring;
    }

    /**
     * returns the adapters name
     *
     * @return string
     */
    public function toString()
    {
        return &quot;Testing&quot;;
    }
}</pre>
<p>So now it&#8217;s ready for use. To use it you have a line that starts something like this.</p>
<pre class="brush: php; title: ; notranslate">$translate = new Zend_Translate('array', ...</pre>
<p>Now this specific one would be for the array adapter. Just comment this line and add this line here.</p>
<pre class="brush: php; title: ; notranslate">$translate = new Zend_Translate('App_Translate_Adapter_Testing', 'XtestingX');</pre>
<p>All the rest can stay the same as you have used it before.</p>
<p>Load your application and all the translated strings will have changed to XtestingX and whatever is left over points you in the right direction. I checked and could not find anything this class would break, in case you use other functions with translate or the adapter directly. Of course some classes will not return something, like a list of all translations, but they should not break, should you attempt to call them.</p>
<p>Of course once you tested, you have to reverse this, or you could use a configuration option or a switch to enable it. Personally, I prefer to comment it and uncomment when really needed and testing is done. Test on a none live server, or your page will become useless in an instant (or at the least very confusing).</p>
<p>Feel free to use it, anyway you wish. I specified it as <a href="http://en.wikipedia.org/wiki/Public_domain" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Public_domain?referer=');">Public Domain</a> which I&#8217;m trying out as a license now for a while and see how feedback and experiences are with it. Yes I know that public domain is not a license per se, as it&#8217;s a free for all thing, but maybe that&#8217;s a &#8220;really good thing&#8221; &#8482;.</p>
<p class="wp-flattr-button"></p>]]></content:encoded>
			<wfw:commentRss>http://christianriesen.com/2009/12/testing-missing-translations-in-zend-framework/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>wget in php</title>
		<link>http://christianriesen.com/2009/10/wget-in-php/</link>
		<comments>http://christianriesen.com/2009/10/wget-in-php/#comments</comments>
		<pubDate>Sun, 25 Oct 2009 13:12:16 +0000</pubDate>
		<dc:creator>Christian Riesen</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[System Administration]]></category>
		<category><![CDATA[wget]]></category>

		<guid isPermaLink="false">http://christianriesen.com/?p=92</guid>
		<description><![CDATA[Ok, this is far from what wget is capable of, but most people do only need one function for wget. Enter url, get content. Especially during the setup phase this is often needed and sometimes wget is not available or libraries are missing. For those that have a working php installation but no working ...]]></description>
			<content:encoded><![CDATA[<p>Ok, this is far from what wget is capable of, but most people do only need one function for wget. Enter url, get content. Especially during the setup phase this is often needed and sometimes wget is not available or libraries are missing. For those that have a working php installation but no working wget, this little script does exactly that, download a file and save it locally. This little helper has done valuable services for me now a few times, so here it is for anyone to use.</p>
<pre class="brush: php; title: ; notranslate">&lt;?php
// wget substitute
if ($argc != 2) {
?&gt;
This works a bit like wget. Just run it with the url as argumnet
&lt;?php
    exit;
}

$url = $argv[1];
$filename = basename($url);

file_put_contents($filename,file_get_contents($url));

echo PHP_EOL. 'Written file ' . $filename . PHP_EOL;</pre>
<p>Usage is rather simple now:<br />
<code>php wget.php URL</code></p>
<p class="wp-flattr-button"></p>]]></content:encoded>
			<wfw:commentRss>http://christianriesen.com/2009/10/wget-in-php/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Ant and Zend Studio</title>
		<link>http://christianriesen.com/2009/09/ant-and-zend-studio/</link>
		<comments>http://christianriesen.com/2009/09/ant-and-zend-studio/#comments</comments>
		<pubDate>Wed, 23 Sep 2009 11:47:16 +0000</pubDate>
		<dc:creator>Christian Riesen</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Ant]]></category>
		<category><![CDATA[Eclipse]]></category>
		<category><![CDATA[Integration]]></category>
		<category><![CDATA[Zend Studio]]></category>

		<guid isPermaLink="false">http://christianriesen.com/?p=84</guid>
		<description><![CDATA[For whatever reason Ant has been disabled in Zend Studio. Thankfully some rather nice guy by the name of Max Horvath has already stumbled upon this and solved it. In his post, dating back a year now, he describes how you first need to have Eclipse Java Development Tools installed. Once that is installed, ...]]></description>
			<content:encoded><![CDATA[<p>For whatever reason Ant has been disabled in Zend Studio. Thankfully some rather nice guy by the name of Max Horvath has already stumbled upon this and solved it. <a href="http://www.maxhorvath.com/2008/08/how-to-enable-the-ant-plugin-in-zend-studio-for-eclipse.html" onclick="pageTracker._trackPageview('/outgoing/www.maxhorvath.com/2008/08/how-to-enable-the-ant-plugin-in-zend-studio-for-eclipse.html?referer=');">In his post</a>, dating back a year now, he describes how you first need to have Eclipse Java Development Tools installed. Once that is installed, you start a new &#8220;Java Project from Existing Ant Buildfile&#8221;. It will ask you if it should enable Ant, which you should say yes, and with that done, you can already cancel the wizard for this. Voila, Ant is installed.</p>
<p class="wp-flattr-button"></p>]]></content:encoded>
			<wfw:commentRss>http://christianriesen.com/2009/09/ant-and-zend-studio/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Making lists easier and more beautiful</title>
		<link>http://christianriesen.com/2009/08/making-lists-easier-and-more-beautiful/</link>
		<comments>http://christianriesen.com/2009/08/making-lists-easier-and-more-beautiful/#comments</comments>
		<pubDate>Thu, 27 Aug 2009 10:57:07 +0000</pubDate>
		<dc:creator>Christian Riesen</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://christianriesen.com/?p=76</guid>
		<description><![CDATA[There is a small dilemma on how to create a neat listing such as this: a, b, c, d

Sometimes you need it for programming reasons, SQL is often pretty unhelpful if you got an extra comma there. Sometimes you just want it that way because it looks nice. Usually you have values in an ...]]></description>
			<content:encoded><![CDATA[<p>There is a small dilemma on how to create a neat listing such as this: a, b, c, d</p>
<p>Sometimes you need it for programming reasons, SQL is often pretty unhelpful if you got an extra comma there. Sometimes you just want it that way because it looks nice. Usually you have values in an array to begin with.</p>
<pre class="brush: php; title: ; notranslate">$array = ('a', 'b', 'c', 'd');</pre>
<p>Now you need to do soemthing with each entry, well, that screams foreach.</p>
<pre class="brush: php; title: ; notranslate">$array = ('a', 'b', 'c', 'd');
$out = '';
foreach ($array as $value)
{
	$out .= $value . ', ';
}</pre>
<p>But that gives you the dangling comma and a space at the end. So you could now trim that off the end (substr) or already do it the right way in the foreach. That could look something like this.<br />
<span id="more-76"></span></p>
<pre class="brush: php; title: ; notranslate">$array = ('a', 'b', 'c', 'd');
$out = '';
$first = true;
foreach ($array as $value)
{
	if (true == $first)
	{
		$first = false;
	}
	else
	{
		$out .= ', ';
	}
	$out .= $value;
}</pre>
<p>Now we also need to add a link around it.</p>
<pre class="brush: php; title: ; notranslate">$array = ('a', 'b', 'c', 'd');
$out = '';
$first = true;
foreach ($array as $value)
{
	if (true == $first)
	{
		$first = false;
	}
	else
	{
		$out .= ', ';
	}
	$out .= '&lt;a href=&quot;/' . $value . '/&quot;&gt;' . $value . '&lt;/a&gt;';
}</pre>
<p>A lot of code and a lot of extra checks for not much gain really. Isn&#8217;t there a better way? After all there is only a need for this addition in between the parts of the array. If only there was a function that puts something between the pieces of an array. Ah but yes there is, and it&#8217;s called implode. If we had the first simple array and list this could be over rather quick like this.</p>
<pre class="brush: php; title: ; notranslate">$array = ('a', 'b', 'c', 'd');
$out = implode(', ', $array);</pre>
<p>But we have this extra data we need around the array, so what do we do? Well first generate a new array, then implode of course, like this.</p>
<pre class="brush: php; title: ; notranslate">$array = ('a', 'b', 'c', 'd');
$temp = array();
foreach ($array as $value)
{
	$temp[] = '&lt;a href=&quot;/' . $value . '/&quot;&gt;' . $value . '&lt;/a&gt;';
}
$out = implode(', ', $temp);
unset($temp);</pre>
<p>Much more comfortable and also much more readable now. First one round preparing, then gluing the pieces into the string. And of course, if you don&#8217;t need it anymore, unset the array or you will carry that data around the application for no reason.</p>
<p class="wp-flattr-button"></p>]]></content:encoded>
			<wfw:commentRss>http://christianriesen.com/2009/08/making-lists-easier-and-more-beautiful/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Comparing folders over system boundaries</title>
		<link>http://christianriesen.com/2009/08/comparing-folders-over-system-boundaries/</link>
		<comments>http://christianriesen.com/2009/08/comparing-folders-over-system-boundaries/#comments</comments>
		<pubDate>Wed, 26 Aug 2009 08:09:39 +0000</pubDate>
		<dc:creator>Christian Riesen</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://christianriesen.com/?p=80</guid>
		<description><![CDATA[Following scenario: Two servers, one Linux, one windows, and some months ago someone copied from windows to Linux a code base. As things happen, the windows server was still in use so changes were made and supposed to be ported to the Linux server as well. And again, as it happens, that did, well, ...]]></description>
			<content:encoded><![CDATA[<p>Following scenario: Two servers, one Linux, one windows, and some months ago someone copied from windows to Linux a code base. As things happen, the windows server was still in use so changes were made and supposed to be ported to the Linux server as well. And again, as it happens, that did, well, not happen. Now under Linux you would have a few nice tools to do the job, but Windows, not so much.</p>
<p>So, using a windows workstation I connected to both locations and did a search in the windows explorer. Don&#8217;t worry, this insanity has a method. Since I knew the date of when the full copy was made, I could simply search for all files on the windows server that are modified after said date. Only problem now, how to get the list into something usable? I mean you can&#8217;t just copy that 6000 files list into notepad or your favorite spreadsheet.</p>
<p>For that there is a free little tool called <a href="http://www.nirsoft.net/utils/sysexp.html" onclick="pageTracker._trackPageview('/outgoing/www.nirsoft.net/utils/sysexp.html?referer=');">SysExporter</a>. It will allow you to copy not only the content of any list under windows, but also have it as a tab delimited list.</p>
<p>That saved and a second file for the Linux server in the same format (for easy comparison), now all I had to do was run a PHP script that parses both files as CSV files, using the tabulator character as delimiter and voila, you have two arrays in the same structure that you can compare. There I compared file name and the folder (after normalizing both so the beginning is the same for each). On no match, it&#8217;s written in a new file. All those entries in the new file are then my list of files I have to copy and check for needed changes. Of course it&#8217;s still the same tab delimited format so you can just import it into a spreadsheet for easier sorting and display.</p>
<p>As a small caveat, windows likes to also search in ZIP files. If you do not really need that, you should disable it. Just go to start, run, then enter this: regsvr32 /u %windir%\system32\zipfldr.dll<br />
A small box appears telling you all went well and now you no longer search for ZIP files. You should maybe get yourself some replacement like <a href="http://www.7-zip.org/" onclick="pageTracker._trackPageview('/outgoing/www.7-zip.org/?referer=');">7zip</a> while you are at it.</p>
<p class="wp-flattr-button"></p>]]></content:encoded>
			<wfw:commentRss>http://christianriesen.com/2009/08/comparing-folders-over-system-boundaries/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cycling between strings</title>
		<link>http://christianriesen.com/2009/06/cycling-between-strings/</link>
		<comments>http://christianriesen.com/2009/06/cycling-between-strings/#comments</comments>
		<pubDate>Mon, 15 Jun 2009 07:47:36 +0000</pubDate>
		<dc:creator>Christian Riesen</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://christianriesen.com/?p=29</guid>
		<description><![CDATA[I'm certain every programmer who wanted to something a bit more fancy came across this problem. You want to cycle through 2 or more values, and then start from the beginning again, but all of this easy and fast. The standard implementation I have seen so far is to define a variable, then count ...]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m certain every programmer who wanted to something a bit more fancy came across this problem. You want to cycle through 2 or more values, and then start from the beginning again, but all of this easy and fast. The standard implementation I have seen so far is to define a variable, then count it up until it has reached a also predefined maximum value, and reset it to the start value. Rinse repeat.</p>
<p>Often this is paired with an array and keys to find the correct content. Sometimes a function is used as a wrapper (using static variables more often than not) and similar rather ugly things.</p>
<p>Well, what does keep a state in itself very well? A class of course.</p>
<p>On Aidan Lister&#8217;s page I cam across a very interesting piece of code that <a href="http://aidanlister.com/2004/04/cycling-between-strings-a-novel-implementation/" onclick="pageTracker._trackPageview('/outgoing/aidanlister.com/2004/04/cycling-between-strings-a-novel-implementation/?referer=');">cycles between strings</a>. You instantiate the class with the values to cycle through given to the constructor. Now from here on out all you do is use the variable, holding the object directly like it would contain the value you need directly, say an integer or a string. Through the magic method __toString() it then dispenses the current value and advances the pointer, or wraps it around to the start.</p>
<p>Very simple yet powerful and also some food for thought, as this exact method might work well on other problems.</p>
<p class="wp-flattr-button"></p>]]></content:encoded>
			<wfw:commentRss>http://christianriesen.com/2009/06/cycling-between-strings/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mini caching in classes</title>
		<link>http://christianriesen.com/2009/06/mini-caching-in-classes/</link>
		<comments>http://christianriesen.com/2009/06/mini-caching-in-classes/#comments</comments>
		<pubDate>Fri, 12 Jun 2009 08:26:11 +0000</pubDate>
		<dc:creator>Christian Riesen</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Caching]]></category>

		<guid isPermaLink="false">http://christianriesen.com/?p=25</guid>
		<description><![CDATA[I just found myself a bit in a bind on what to do. I have a list, generated from the database that is returned by a method. Now this method is called multiple times from two different places (out of scope) so I either have to globalize the result somehow, like make it an ...]]></description>
			<content:encoded><![CDATA[<p>I just found myself a bit in a bind on what to do. I have a list, generated from the database that is returned by a method. Now this method is called multiple times from two different places (out of scope) so I either have to globalize the result somehow, like make it an attribute in a higher up class or pass it around. Both are very unappealing and rather ugly to maintain.</p>
<p>Instead, I opted for a mini cache in the originating class. The setup is quiet simple, so I now also use this in base classes that get implemented down the line.<br />
<span id="more-25"></span></p>
<pre class="brush: php; title: ; notranslate">private $_localCache = NULL;

public function myFunc()
{
	if (!isset($this-&gt;_localCache[__METHOD__]))
	{
		$value = time(); // Create value here
		$this-&gt;_localCache[__METHOD__] = $value;
		return $value;
	}
	else
	{
		return $this-&gt;_localCache[__METHOD__];
	}
}</pre>
<p>The code is as simple as it looks. First you create a private attribute _localCache which is used for caching. Each cache is identified by a key in the _localCache array. That way you can reuse this multiple times very fast and easy. As key, I use the method name. This is especially hand because you don&#8217;t have to change any code if you refactor the method or the class.</p>
<p>As with usual caching, it checks if a cache is set, then returns that, otherwise, grabs the real data and caches it. This works of course without any additional cache tools installed, so even though memcached and xcache are wonderful for caching things like this often, they might not be available or simply not really usable.</p>
<p>A prime example is when you know that the value does not change in the execution of this script, but might change between reloads. This way you know for certain you will always use the same data inside the script, while also being sure the next reload fetches the data new.</p>
<p>For static things that get read more often than written to between loads, are better of in a real cache system. If the data changes even less than that, say every few months or so and it&#8217;s not a lot, you could also optimize by just hard coding it as an array or class.</p>
<p class="wp-flattr-button"></p>]]></content:encoded>
			<wfw:commentRss>http://christianriesen.com/2009/06/mini-caching-in-classes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>2009 has 53 weeks</title>
		<link>http://christianriesen.com/2009/06/2009-has-53-weeks/</link>
		<comments>http://christianriesen.com/2009/06/2009-has-53-weeks/#comments</comments>
		<pubDate>Wed, 10 Jun 2009 07:58:58 +0000</pubDate>
		<dc:creator>Christian Riesen</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://christianriesen.com/?p=17</guid>
		<description><![CDATA[I had to write a tool that will add weeks to a database, where records that get added in the future will have to be linked with those week records. So a cron job runs and creates about 150 weeks in advance, running once a month, leaving the previously inserted weeks alone.

Upon testing though ...]]></description>
			<content:encoded><![CDATA[<p>I had to write a tool that will add weeks to a database, where records that get added in the future will have to be linked with those week records. So a cron job runs and creates about 150 weeks in advance, running once a month, leaving the previously inserted weeks alone.</p>
<p>Upon testing though there was a weird little snag.<br />
<span id="more-17"></span><br />
What happened was that the insert scripts failed to find a particular week. Debug revealed that they tried to enter a week 53. But a year has only 52 weeks, right? Wrong. 2009 has actually 53 calendar weeks. So to make it future proof for when such a thing might happen again, here my little code that determines the number of possible weeks in the current year. You need nothing else than the date function to make it work.</p>
<pre class="brush: php; title: ; notranslate">$currentYear = date('Y');
$weeksInYear = date('W',mktime(23,59,59,12,31,$currentYear));</pre>
<p>This should run fine until someone decides to change the number of days in December, number of months in a year, number of hours in a day or something equally constant.</p>
<p class="wp-flattr-button"></p>]]></content:encoded>
			<wfw:commentRss>http://christianriesen.com/2009/06/2009-has-53-weeks/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Large MySQL Inserts with PHP</title>
		<link>http://christianriesen.com/2009/06/large-mysql-inserts-with-php/</link>
		<comments>http://christianriesen.com/2009/06/large-mysql-inserts-with-php/#comments</comments>
		<pubDate>Wed, 10 Jun 2009 07:10:27 +0000</pubDate>
		<dc:creator>Christian Riesen</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Zend Framework]]></category>

		<guid isPermaLink="false">http://christianriesen.com/?p=7</guid>
		<description><![CDATA[You have to insert 1 Million entries into a database. You know there is a structure to it (I'll supply one as a sample) so the easiest is to write a PHP script to do this task for you. Said and done, the script is finished and it does the job. But slow as ...]]></description>
			<content:encoded><![CDATA[<p>You have to insert 1 Million entries into a database. You know there is a structure to it (I&#8217;ll supply one as a sample) so the easiest is to write a PHP script to do this task for you. Said and done, the script is finished and it does the job. But slow as hell. Not only does it trash your hard disk like mad, it also times out on a browser since you don&#8217;t want to do the shell rumba. You have only 20k entries in your database, wasted 30 seconds (while the database still tries to catch up with the sent queries) and now you realize you need a few more rows anyways.</p>
<p>To make life easy, I&#8217;ll use Zend Framework for this. You could use anything else that sends queries, but since I already used it for this project, there was little sense in doing something else.  So, with all useless stuff stripped out, here the code in it&#8217;s first, rather infancy way.</p>
<p><span id="more-7"></span></p>
<pre class="brush: php; title: ; notranslate">$db = Zend_Registry::get('db');
$x = 0;
$y = 0;
$z = 0;

while($z &lt; 100)
{
	while($y &lt; 100)
	{
		while($x &lt; 100)
		{
			$query = 'INSERT INTO location (locx, locy, locz) VALUES ';
			$query .= '('.$x.', '.$y.', '.$z.')';
			$db-&gt;query($query);
			$x++;
		}
		$y++;
		$x = 0;
	}
	$z++;
	$y = 0;
}</pre>
<p>This sample creates rows for a location in a 3D space in case you wonder. So these lines represent single points in a cube with the side length of 100 whatever units you want to imagine here. It starts at 0,0,0 and ends at 99,99,99. So this gives us our nice 1 million entries.</p>
<p>The $db variable up there is a Zend_Db_Adapter instance, so I can now run queries directly. Yes I know you could write the query differently, but I left it like that to make life easier for the next steps. So instead of firing off 1 Million queries, how about grouping 100 queries together, into one large one? I left the top part out, so I will only post the while loop. The result looks like this.</p>
<pre class="brush: php; title: ; notranslate">while($z &lt; 100)
{
	while($y &lt; 100)
	{
		$query = 'INSERT INTO location (locx, locy, locz) VALUES ';
		$first = true;
		while($x &lt; 100)
		{
			if ($first == TRUE)
			{
				$first = false;
			}
			else
			{
				$query .= ', ';
			}
			$query .= '('.$x.', '.$y.', '.$z.')';
			$x++;
		}

		$db-&gt;query($query);
		$y++;
		$x = 0;
	}
	$z++;
	$y = 0;
}</pre>
<p>It&#8217;s faster already, but it still does a lot of trashing around on the DB. While we now have &#8220;only&#8221; 10&#8217;000 queries instead of 1 Million, it still is far from &#8220;nice&#8221;. I could extended this and create larger queries, so I end up with 100 queries, each of them having 10&#8217;000 entries, but that might by too large queries and it does not solve much of my problem if I have even more data. Since I&#8217;m using MySQL for this, I have transactions at my disposal as well. So instead of writing it all with every query, I start a transaction, run 100 queries then commit that batch. But wait isn&#8217;t that the same thing than just writing 100 queries? In theory, yes, practically though this changes a few things. Since those queries are small, the system can keep them in RAM and write them in one big write, combined together, instead of 100 small changes, it makes one big one, lightning fast. Here how this looks like.</p>
<pre class="brush: php; title: ; notranslate">while($z &lt; 100)
{
	$db-&gt;beginTransaction();
	while($y &lt; 100)
	{
		$query = 'INSERT INTO location (locx, locy, locz) VALUES ';
		$first = true;
		while($x &lt; 100)
		{
			if ($first == TRUE)
			{
				$first = false;
			}
			else
			{
				$query .= ', ';
			}
			$query .= '('.$x.', '.$y.', '.$z.')';
			$x++;
		}

		$db-&gt;query($query);
		$y++;
		$x = 0;
	}
	$db-&gt;commit();
	$z++;
	$y = 0;
}</pre>
<p>Now PHP will write all 1 Million rows in under 30 seconds even on a relative small system.</p>
<p>You could of course optimize this, maybe even wrap the whole process in a transaction instead of single steps, but that&#8217;s up to you and your ingenuity. For my needs this did the job so I had no need to go even further with it. Either way you should be able to take it and run with it fast and far for your problem of inserting large amounts of data.</p>
<p>Again, I used Zend Framework, so in your case the beginTransaction and commit function might not exist or named differently.</p>
<p class="wp-flattr-button"></p>]]></content:encoded>
			<wfw:commentRss>http://christianriesen.com/2009/06/large-mysql-inserts-with-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

