<?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>willoller.com &#187; php programming</title>
	<atom:link href="http://www.willoller.com/category/php-programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.willoller.com</link>
	<description>Learning to match the beat of the Old World man.</description>
	<lastBuildDate>Thu, 02 Feb 2012 23:34:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.1</generator>
		<item>
		<title>Counting Characters php vs javascript</title>
		<link>http://www.willoller.com/counting-characters-php-vs-javascript/</link>
		<comments>http://www.willoller.com/counting-characters-php-vs-javascript/#comments</comments>
		<pubDate>Wed, 03 Dec 2008 04:13:05 +0000</pubDate>
		<dc:creator>will</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[php programming]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.willoller.com/?p=159</guid>
		<description><![CDATA[On a recent project, an issue cropped up where we were exporting xml reports to a third party, who had rules about line lengths in Description fields. So, I wrote up a quick-and-dirty character counter for the description field so the client can keep their descriptions under the 1500-word limit. Here&#8217;s how it was done: [...]]]></description>
			<content:encoded><![CDATA[	<p>On a recent project, an issue cropped up where we were exporting xml reports to a third party, who had rules about line lengths in Description fields.</p>
	<p>So, I wrote up a quick-and-dirty character counter for the description field so the client can keep their descriptions under the 1500-word limit.</p>
	<p>Here&#8217;s how it was done:</p>
	<div class="code php" style="font-family: monospace;"><br />
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>!<span style="color: #0000ff;">$fdata</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'description'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #0000ff;">$errors</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #ff0000;">&quot;You must enter a Description&quot;</span>;<br />
<span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">elseif</span><span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/strlen"><span style="color: #000066;">strlen</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$fdata</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'description'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span> &gt; <span style="color: #cc66cc;">1500</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #0000ff;">$len</span> = <a href="http://www.php.net/strlen"><span style="color: #000066;">strlen</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$fdata</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'description'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp;<span style="color: #0000ff;">$errors</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #ff0000;">&quot;Descriptions are limited to 1500 characters ($len)&quot;</span>;<br />
<span style="color: #66cc66;">&#125;</span><br />
<br />
&nbsp;</div>
	<p>Now the html and javascript:</p>
	<div class="code html" style="font-family: monospace;"><br />
&lt;li class=&quot;description&quot;&gt;<br />
&nbsp; &nbsp;&lt;label for=&quot;htmlarea&quot; class=&quot;required&quot;&gt;Description *&lt;/label&gt;<br />
&nbsp; &nbsp;&lt;textarea name=&quot;description&quot; id=&quot;htmlarea&quot; cols=&quot;80&quot; rows=&quot;20&quot; class=&quot;textarea&quot;&gt;&lt; ?= $ff['description'] ?&gt;&lt;/textarea&gt;<br />
&nbsp; &nbsp;&lt;p class=&quot;hint&quot; id=&quot;desc_char_count&quot;&gt;1500 character limit&lt;/p&gt;<br />
&lt;/li&gt;<br />
&nbsp;</div>
	<div class="code javascript" style="font-family: monospace;"><br />
$<span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp;$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'#htmlarea'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">keyup</span><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> charLength = $<span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">val</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">length</span>;<br />
&nbsp; &nbsp; &nbsp; $<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'#desc_char_count'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">html</span><span style="color: #66cc66;">&#40;</span>charLength + <span style="color: #3366CC;">' of 1500 characters used'</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span><span style="color: #66cc66;">&#40;</span>$<span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">val</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">length</span> &gt; <span style="color: #CC0000;">1500</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'#desc_char_count'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">removeClass</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'positive'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">addClass</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'negative'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">html</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'&lt;strong&gt;You have '</span> + $<span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">val</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">length</span> + <span style="color: #3366CC;">' characters. You may only have up to 1500 characters.&lt;/strong&gt;'</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'#desc_char_count'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">addClass</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'positive'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">removeClass</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'negative'</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp;$<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'#htmlarea'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">keyup</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;<br />
<br />
&nbsp;</div>
	<p>All was working well, except I underestimated the descriptions. The next morning, the thing was broken. The php side was kicking validation errors (&#8220;over 1500 characters&#8221;), while the javascript was counting a mere 1480. Why?</p>
	<p>Turns out the data was full of bullets and other multibyte characters. To make the counts match, I needed multibyte-safe php:</p>
	<div class="code php" style="font-family: monospace;"><br />
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>!<span style="color: #0000ff;">$fdata</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'description'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #0000ff;">$errors</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #ff0000;">&quot;You must enter a Description&quot;</span>;<br />
<span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">elseif</span><span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/mb_strlen"><span style="color: #000066;">mb_strlen</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$fdata</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'description'</span><span style="color: #66cc66;">&#93;</span>,<span style="color: #ff0000;">'UTF-8'</span><span style="color: #66cc66;">&#41;</span> &gt; <span style="color: #cc66cc;">1500</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #0000ff;">$len</span> = <a href="http://www.php.net/mb_strlen"><span style="color: #000066;">mb_strlen</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$fdata</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'description'</span><span style="color: #66cc66;">&#93;</span>,<span style="color: #ff0000;">'UTF-8'</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp;<span style="color: #0000ff;">$errors</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #ff0000;">&quot;Descriptions are limited to 1500 characters ($len)&quot;</span>;<br />
<span style="color: #66cc66;">&#125;</span><br />
<br />
&nbsp;</div>
	<p>Hooray! Now the counts match!</p>

 ]]></content:encoded>
			<wfw:commentRss>http://www.willoller.com/counting-characters-php-vs-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Generating amazing reports using Basecamp (Part 1 &#8211; Tagging)</title>
		<link>http://www.willoller.com/use-sql-to-query-your-basecamp-account/</link>
		<comments>http://www.willoller.com/use-sql-to-query-your-basecamp-account/#comments</comments>
		<pubDate>Sat, 11 Aug 2007 20:36:23 +0000</pubDate>
		<dc:creator>will</dc:creator>
				<category><![CDATA[Basecamp]]></category>
		<category><![CDATA[Hacks]]></category>
		<category><![CDATA[php programming]]></category>

		<guid isPermaLink="false">http://www.willoller.com/use-sql-to-query-your-basecamp-account/</guid>
		<description><![CDATA[We&#8217;ve been looking for good ways to produce advanced reports using Basecamp. We have hit upon a system in which we implement a rudimentary tagging system, and add additional metadata for time estimation. In this first part, I will explain the simple tagging system. Next time, I will tell you all about the Big XML [...]]]></description>
			<content:encoded><![CDATA[	<p>We&#8217;ve been looking for good ways to produce advanced reports using Basecamp.  We have hit upon a system in which we implement a rudimentary tagging system, and add additional metadata for time estimation.  </p>
	<p>In this first part, I will explain the simple tagging system.  Next time, I will tell you all about the Big XML Dump.</p>
<h2>Tagging System</h2>
	<p>Tagging will be implemented using { curly braces } around a comma-delimited list.  We will be using our regular work categories as tags, so a typical todo-list might look like:</p>
<blockquote>
   <h4>Implement new feature {Programming}</h4>
   <ul>
      <li>Add new processing loop</li>
      <li>Create new template {HTML Production}</li>
      <li>Connect to pre-existing API</li>
</blockquote> 
	<p>We want our tagging parser to be smart, and change an item&#8217;s tag list on the fly based on it&#8217;s parent item&#8217;s tag list.  That sounds complicated; here&#8217;s an example using the list above.</p>
	<p><h4>Computed Tags</h4><br />
<blockquote>
   <h4>Implement new feature {Programming}</h4>
   <ul>
      <li>Add new processing loop {<em>Programming</em>}</li>
      <li>Create new template {<em>Programming</em>, HTML Production}</li>
      <li>Connect to pre-existing API {<em>Programming</em>}</li><br />
</blockquote> </p>
	<p>The <em>italicized</em> tags are derived from the list&#8217;s name (Implement new feature) and will be programmatically added. </p>
<h2>Time Estimation</h2>
	<p>Time estimates are handled in much the same way as the tags, just using straight brackets instead ([, ]).</p>
	<p>Adding time estimation to our list:</p>
<blockquote>
   <h4>Implement new feature {Programming}</h4>
   <ul>
      <li>Add new processing loop [1]</li>
      <li>Create new template [6.5] {HTML Production}</li>
      <li>Connect to pre-existing API [9]</li>
</blockquote> 
	<p>This way, each todo-item has a time value associated with it.  We will use this data later on to test our estimation skills and to quantify our productivity.</p>
	<p>Next: Part 2 &#8211; The Big XML Dump</p>


 ]]></content:encoded>
			<wfw:commentRss>http://www.willoller.com/use-sql-to-query-your-basecamp-account/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Limiting title and excerpt length with WordPress</title>
		<link>http://www.willoller.com/limiting-title-and-excerpt-length-with-wordpress/</link>
		<comments>http://www.willoller.com/limiting-title-and-excerpt-length-with-wordpress/#comments</comments>
		<pubDate>Sat, 14 Apr 2007 00:55:42 +0000</pubDate>
		<dc:creator>will</dc:creator>
				<category><![CDATA[Hacks]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[php programming]]></category>
		<category><![CDATA[Wordpress]]></category>

		<guid isPermaLink="false">http://www.willoller.com/limiting-title-and-excerpt-length-with-wordpress/</guid>
		<description><![CDATA[Sometimes clients have very specific requirements. Sometimes they want autometic controls on simple things they have total control over anyway. I digress. In this case, blog titles needed to be of any length, but have their lengths truncated under certain circumstances. Furthermore, excerpts need to be able to be the default length (50 words), but [...]]]></description>
			<content:encoded><![CDATA[	<p>Sometimes clients have very specific requirements. Sometimes they want autometic controls on simple things they have total control over anyway.  I digress.</p>
	<p>In this case, blog titles needed to be of any length, but have their lengths truncated under certain circumstances.  Furthermore, excerpts need to be able to be the default length (50 words), but shorter (10 words) under those same certain circumstances.</p>
	<p>Fortunately for me, the excerpt was easily fixed using a plugin called <a href="http://guff.szub.net/2005/02/26/the-excerpt-reloaded/">the excerpt reloaded</a>.</p>
	<p>http://guff.szub.net/2005/02/26/the-excerpt-reloaded/</p>
	<p>Next, for the title length requirements, you will need to modifiy a core file: <code>wp-includes/post-template.php</code>.</p>
	<p>[php]<br />
function the_title($before = &#8216;&#8217;, $after = &#8216;&#8217;, $echo = true, $length = false) {
   $title = get_the_title();
    if ( $length &#38;&#38; is_numeric($length) ) {
       $title = substr( $title, 0, $length );
    }
    if ( strlen($title) > 0 ) {
       $title = apply_filters(&#8216;the_title&#8217;, $before . $title . $after, $before, $after);
       if ( $echo )
          echo $title;
       else
          return $title;
    }<br />
}<br />
[/php]</p>
	<p>Finally, when in <strong>The Loop</strong>, call the_title() like this:</p>
	<p>[php]< ?php the_title('', '', true, '40') ?>[/php]</p>
	<p>Now, the title will be a mere 40 characters long, and the excerpt (using <code>the_excerpt_reloaded()</code>) can be any desired length.</p>

 ]]></content:encoded>
			<wfw:commentRss>http://www.willoller.com/limiting-title-and-excerpt-length-with-wordpress/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Become a Bushido Coder</title>
		<link>http://www.willoller.com/sayings-for-the-bushido-coder-to-live-by/</link>
		<comments>http://www.willoller.com/sayings-for-the-bushido-coder-to-live-by/#comments</comments>
		<pubDate>Fri, 05 Jan 2007 04:57:48 +0000</pubDate>
		<dc:creator>will</dc:creator>
				<category><![CDATA[Books I Have Read]]></category>
		<category><![CDATA[History]]></category>
		<category><![CDATA[php programming]]></category>

		<guid isPermaLink="false">http://www.willoller.com/11-sayings-for-the-bushido-coder-to-live-by/</guid>
		<description><![CDATA[From Hagakure. Remeber, harmony in all aspects of your project is difficult. Great patience is required to overcome this. Master Ittei said, &#8220;In calligraphy it is progress when the paper, brush, and ink are in harmony.&#8221; Yet they are so wont to be disjointed! Don&#8217;t reinvent the wheel Take a lesson from me; don&#8217;t get [...]]]></description>
			<content:encoded><![CDATA[	<p>From <em>Hagakure</em>.</p>
<ol>
<li>
<h3>Remeber, harmony in all aspects of your project is difficult.</h3>
<p>Great patience is required to overcome this.</p><br />
<blockquote><p>Master Ittei said, &#8220;In calligraphy it is progress when the paper, brush, and ink are in harmony.&#8221; Yet they are so wont to be disjointed!</p><br />
</blockquote><br />
</li><br />
<li><br />
<h3>Don&#8217;t reinvent the wheel</h3><br />
<p>Take a lesson from me; don&#8217;t get stuck pursuing a tricky solution.  Look to what others have done and try to improve on it, but don&#8217;t reinvent the wheel (yeah, I know you hear that all the time).</p><br />
<blockquote><p>Learning is a good thing, but more often it leads to mistakes.  It is like the admonition of the priest K?nan. It is worthwhile just looking at the deeds of accomplished persons for the purpose of knowing our own insufficiencies.  But often this does not happen. For the most part, we admire our own opinions and become fond of arguing.</p><br />
</blockquote><br />
</li><br />
<li><br />
<h3>Listen to old people</h3><br />
<p>This is less about coding and more about life. Listen to old people. They have done things you will never do in times you could never see.  Pay attention.  There are less than 50 WWI vets alive today.  1100 WWII vets die every day.  When they are gone, they are gone forever.</p><br />
<blockquote><p>When you are listening to the stories of accomplished men and the like, you should listen with deep sincerity, even if it&#8217;s something about which you already know.  If in listening to the same thing ten or twenty times it happens that you come to an unexpected understanding, that moment will be very special. Within the tedious talk of old folks are there meritorious deeds.</p><br />
</blockquote><br />
</li><br />
<li><br />
<h3>Keep your eyes on the prize</h3><br />
<p>Planning is essential to a succesful project, but when the planning phase is complete and the battle begins, you must not take your eye off of your goal.  See it through completely and swiftly; be dilligent and unfailing in your aim. If you want great word-of-mouth advertising, turn a client&#8217;s project around ahead of schedule and on budget.  Your profit margin increases, as does the satisfaction of the client.</p><br />
<blockquote><br />
<p>According to what one of the elders said, taking an enemy on the battlefield is like a hawk taking a bird. Even though it enters into the midst of a thousand of them, it gives no attention to any bird other than the one that it has first marked .<br />
</p><br />
<p>Moreover, what is called a <em>tezuke no kubi</em> is a head that one has taken after having made the declaration, &#8220;I will take that warrior wearing such and such armor.&#8221;</p><br />
</blockquote><br />
</li><br />
<li><br />
<h3>Youth is the time for the hardest work</h3><br />
<p>Work hard while you&#8217;re young, folks.  No amount of caffiene will fuel a 16-hour code binge when you&#8217;re old.</p><br />
<p>Save your money. Invest! Your retirement should not consist of asking if I want fries.</p><br />
<blockquote><p>Shida Kichinosuke said, &#8220;At first it is an oppressive thing to run until one is breathless. But it is an extraordinarily good feeling when one is standing around after the running. More than that, it is even better to sit down. More than that, it is even better to lie down. And more than that, to put down a pillow and sleep soundly is even better. A man&#8217;s whole life should be like this. To exert oneself to a great extent when one is young and then to sleep when he is old or at the point of death is the way it should be. But to first sleep and then exert oneself . . . To exert oneself to the end, and to end one&#8217;s whole life in toil is regrettable.&#8221; Shimomura Rokurouemon told this story.</p><br />
<p>A saying of Kichinosuke&#8217;s that is similar to this is, &#8220;A man&#8217;s life should be as toilsome as possible.&#8221;</p><br />
</blockquote><br />
</li><br />
<li><br />
<h3>Honesty is the best policy</h3><br />
<p>Clients should know up front the benifits and drawbacks to the changes they are requesting.  Don&#8217;t assume clients understand the ramifications of a new cart checkout process on the user&#8217;s experience, or the irrelevance of a new XML-based export. Let them know <em>why</em> the latest round of changes will put the project behind schedule 2 weeks. If they know up front, they won&#8217;t feel like you were hiding it when a deadline gets pushed back (unless they are jerks).</p><br />
<blockquote><p><br />
Hirose Denzaemon was then about twelve or thirteen years old. When he sat down in front of the physiognomist, he said in a bristling voice , &#8216;&#8217;if you read cowardice in me, I&#8217;ll cut you down with a single blow !&#8221;</p><br />
<p>When there is something to be said, it is better if it is said right away. If it is said later, it will sound like an excuse. Moreover, it is occasionally good to really overwhelm your opponent. Also, in addition to having spoken sufficiently it is the highest sort of victory to teach your opponent some- thing that will be to his benefit. This is in accordance with the Way. </p><br />
</blockquote><br />
</li><br />
<li><br />
<h2>Plan ahead</h2><br />
<blockquote><p>The phrase, &#8216;&#8217;Win first, fight later, &#8216;&#8217; can be summed up in the two words, &#8220;Win beforehand.&#8221; The resourcefulness of times of peace is the military preparation for times of war. With five hundred allies one can defeat an enemy force of ten thousand. </p><br />
</blockquote><br />
<p>For me, this speaks to the benifits of a good code archive. If you alredy have an app or killer solution to a problem waiting in the wings, you have already completed much of your task.</p><br />
</li><br />
<li><br />
<blockquote><p>Continue to spur a running horse.</p><br />
</blockquote><br />
<p>Managers: push your developers, no matter how well they are doing.  Reward them well when they excel, and then push again.</p><br />
</li><br />
<li><br />
<h3>Learn by asking questions.</h3><br />
<blockquote><p>To ask when you already know is politeness. To ask when you don&#8217;t know is the rule.</p><br />
</blockquote><br />
</li><br />
<li><br />
<h3>Be skeptical</h3><br />
<blockquote><p>People with intelligence will use it to fashion things both true and false and will try to push through whatever they want with their clever reasoning. This is injury from intelligence . Nothing you do will have effect if you do not use truth.</p><br />
</blockquote><br />
<p>If something sounds too good to be true, it probably is (except Gmail &#8211; Gmail rocks). </p><br />
</li><br />
<li><br />
<h3>Be quick</h3><br />
<blockquote><p>Lord Katsushige always used to say that there are four kinds of retainers. They are the &#8220;quick, then lapping,&#8221; the &#8220;lagging, then quick,&#8221; the &#8220;continually quick,&#8221; and the &#8216;&#8217;continually lagging.&#8217;&#8216;</p><br />
<p>The &#8220;continually quick&#8221; are men who when given orders will undertake their execution quickly and settle the matter well. Fukuchi Kichizaemon and the like resemble this type.</p><br />
<p>The &#8220;lagging, then quick&#8221; are men who, though lacking in understanding when given orders, prepare quickly and bring the matter to a conclusion. I suppose that Nakano Kazuma and men similar are like this.</p><br />
<p>The &#8220;quick, then lagging&#8221; are men who when given orders seem to be going to settle things but in their preparation take time and procrastinate. There are many people like this.</p><br />
<p>Other than these, one could say that the rest are &#8216;&#8217;continually lagging.&#8221; </p><br />
</blockquote><br />
</li><br />
<p>May your tasks fall before your sword like weeds before the farmer&#8217;s hoe.</p>
<h2>References:</h2>
<ul>
<li><a href="http://www.worldebooklibrary.com/eBooks/WorldeBookLibrary.com/hagakure.htm">Hagakure EBook</a></li>
<li><a href="http://en.wikipedia.org/wiki/Bushido">Wikipedia: Bushido</a></li>
<li><a href="http://www.realultimatepower.net/">Ninjas are freakin&#8217; cool</a></li>
</ul>
</ol>


 ]]></content:encoded>
			<wfw:commentRss>http://www.willoller.com/sayings-for-the-bushido-coder-to-live-by/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sorting Algorithms: Laser Sort</title>
		<link>http://www.willoller.com/sorting-algorithms-laser-sort/</link>
		<comments>http://www.willoller.com/sorting-algorithms-laser-sort/#comments</comments>
		<pubDate>Fri, 14 Jul 2006 20:51:19 +0000</pubDate>
		<dc:creator>will</dc:creator>
				<category><![CDATA[php programming]]></category>

		<guid isPermaLink="false">http://wp.willoller.com/?p=11</guid>
		<description><![CDATA[Sorting There are lots of sorting algorithms out there. Bubble, Shell, Heap, Quick, Library come readily to mind, and still the list goes on. So, of course, I had to come across a sorting problem that I couldn&#8217;t solve with my Beginning Programming textbook. It&#8217;s not a particularly compex problem, and the solution is even [...]]]></description>
			<content:encoded><![CDATA[<h2>Sorting</h2>
<p>There are lots of sorting algorithms out there.  Bubble, Shell, Heap, Quick, Library come readily to mind, and still the list goes on.</p>
	<p><p>So, of course, I had to come across a sorting problem that I couldn&#8217;t solve with my Beginning Programming textbook.  It&#8217;s not a particularly compex problem, and the solution is even simpler, but coming to that simple solution was not.</p><br />
<span id="more-11"></span><br />
<h2>The Setup</h2></p>
<p>First, here is an example of the type of data I am working with.  This is the simplest part, because the data is as average run-of-the-mill as data can be.</p>
<table><tbody><tr><th>ID</th><th>Article</th><th>Date Published</th><th>Order</th></tr><tr><td>1</td><td>Willoller.com</td><td>12-July-2006</td><td><input name="Willoller.com" value="1" size="3"/></td></tr><tr><td>2</td><td>Wikipedia.com</td><td>31-Jan-2004</td><td><input name="Wikipedia.com" value="2" size="3"/></td></tr><tr><td>3</td><td>Redvsblue.com</td><td>14-Aug-2003</td><td><input name="Redvsblue.com" value="3" size="3"/></td></tr></tbody></table>
	<p><p>The $_POST is then passed to a function that works like this:<code style="white-space: pre;">function supersort( $post_in ) { 
   foreach ( $post_in as $k => $i ) {    // Iterate through the $_POST
      if ( is_numeric( $k ) ) {          // If the key is a number
         $new[$k] = $i;                  // Push it onto $new
      }
   }
   asort($new);                          // Sort the array by the values</p>
   $counter = 1;                         // Start with 1
   foreach ( $new as $k => $i ) {        // Iterate through $new
      $update[$k] = $count;              // Populate $update using $count, to be sure
      $count++;                          //    we get a nice sequence in the UPDATE
   }
}
</code></p>
<p>Then it gets inserted into the DB using the <code>$new[$key]</code> and its value to create the <code>UPDATE</code>.</p>
<h2>The Problem</h2>
<p>&#8220;It appears to be working&#8221; you may say.  &#8220;Don&#8217;t reinvent the wheel!&#8221; says the voice in my head.  </p>
<p>And yes, all is well and good indeed, unless the user repeats numbers.  That is, rather than inputting 2,1,3, they lazily input 1,1,3 with the expectation that the 2nd item will become the new 1st item, like so:</p>
<table><tbody><tr><th>ID</th><th>Article</th><th>Date Published</th><th>Order</th></tr><tr><td>2</td><td>Wikipedia.com</td><td>31-Jan-2004</td><td><input value="1" size="3"/></td></tr><tr><td>1</td><td>Willoller.com</td><td>12-July-2006</td><td><input value="2" size="3"/></td></tr><tr><td>3</td><td>Redvsblue.com</td><td>14-Aug-2003</td><td><input value="3" size="3"/></td></tr></tbody></table>
<p>In practice, the <code>asort()</code>&#8217;s outcome will be unreliable, and the new value will only swap places with the old value approximately half the time.  So, how to force the new change to take precedence over the old change?  To accomplish this, we will need to retrieve the previous values from the database, and use an array of commands.  </p>
<h2>The Solution.</h2>
<p>Not everybody likes to dive right into the code. But, if you&#8217;re still reading this, you are not everybody.</p>
<code style="white-space: pre;">
   /*
      $old is populated from the database
      $new comes from the form submission
      Both are of the format $primary_key => $sort_value
   */
   asort($new);
   $prev = false;
   foreach ($new as $k=>$i) {
      if ($new[$k]  1 &amp;&amp; $prev&lt;sup&gt;&lt;a href="#fn9082155794f30a45d2ac85"&gt;1&lt;/a&gt;&lt;/sup&gt;  1) {
         $update[] = array($prev<sup><a href="#fn11175337554f30a45d2b06e">0</a></sup>, 2);
         $update[] = array($k, 1);
      }
      if($new[$k]  $old[$k] &amp;&amp; $prev &amp;&amp; prev&lt;sup&gt;&lt;a href="#fn9082155794f30a45d2ac85"&gt;1&lt;/a&gt;&lt;/sup&gt;  $new[$k]) {
         $update[] = array($k, $i-1);
      }
      $prev = array($k, $i);
   }
   foreach ($update as $i) {
      $new[$i<sup><a href="#fn11175337554f30a45d2b06e">0</a></sup>] = $i<sup><a href="#fn9082155794f30a45d2ac85">1</a></sup>;
   }
   $count = 1;
   foreach ($new as $k => $i) {
      $new[$k] = $count;
      $count++;
   }
</code>
<p>Couldn&#8217;t be easier.  But, for those of you not madly in love with php&#8217;s various array-handling methods, I will happily give a quick overview of what&#8217;s going on here.</p>
<p>First, we <code>asort($new)</code>. Why not $old as well?  Becaouse we never actually iterate on $old.  Besides, you already ordered by sort in the query that generated $old, right?</p>
<p>Second, I found that the standard algorithm I devised choked when the first values were both 1, because the $prev variable has not been set yet and also because I don&#8217;t like negative numbers.  So, there is a little bit of logic in there to make double-sure that the ordering goes smoothly when 1&#8217;s are the doubled number.</p>
<p>Third, the real good part.  The algorithm checks to see if the sort value of a particular item has been updated, and if it has, it takes priority in the numbering, pushing the old number up to get out of the way.  If 2 2&#8217;s are submitted, the newest 2 gets the spot and the old 2 becomes a 3.</p>
<p>These updates are passed by means of the update array.  After the whole $new array is run through, the update array is traversed, and the $new array is augmented with the replacement values.</p>
<p>Finally, the numbers in the $new array are recalculated, starting from 1.  This is also a double-check to be sure every number is in its place and no sort is ambiguous.</p>

 ]]></content:encoded>
			<wfw:commentRss>http://www.willoller.com/sorting-algorithms-laser-sort/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

