<?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>code-diesel &#187; php</title>
	<atom:link href="http://www.codediesel.com/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codediesel.com</link>
	<description>/* PHP &#38; MySQL Journal */</description>
	<lastBuildDate>Thu, 02 Feb 2012 13:19:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>&#8216;isset&#8217; construct and multiple parameters</title>
		<link>http://www.codediesel.com/php/isset-construct-and-multiple-parameters/</link>
		<comments>http://www.codediesel.com/php/isset-construct-and-multiple-parameters/#comments</comments>
		<pubDate>Sat, 30 Jan 2010 10:44:09 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/php/isset-construct-and-multiple-parameters/</guid>
		<description><![CDATA[how the isset construct takes multiple parameters]]></description>
			<content:encoded><![CDATA[<p>PHP programmers are frequently into using the various <em>is_x</em> group of functions: is_int, is_null etc. All of this functions take a single parameter which leads you to believe that other similar constructs take only a single parameter. Like the <em>isset</em> and <em>unset</em> constructs, which by the way take multiple parameters as shown below, which might help you save some keystrokes.<br />
<span id="more-2368"></span><br />
So instead of writing:</p>

<div class="wp_codebox"><table><tr id="p23681"><td class="code" id="p2368code1"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$var1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'a'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$var2</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'b'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$var3</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'c'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$var4</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'d'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$var1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$var2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$var3</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$var4</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #009933; font-style: italic;">// All variables are set, do something</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>We can write:</p>

<div class="wp_codebox"><table><tr id="p23682"><td class="code" id="p2368code2"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$var1</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'a'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$var2</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'b'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$var3</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'c'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$var4</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'d'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$var1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$var2</span><span style="color: #339933;">,</span> <span style="color: #000088;">$var3</span><span style="color: #339933;">,</span> <span style="color: #000088;">$var4</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #009933; font-style: italic;">// Will return TRUE only if all of the parameters are set</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Same with the unset() construct:</p>

<div class="wp_codebox"><table><tr id="p23683"><td class="code" id="p2368code3"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$var1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$var2</span><span style="color: #339933;">,</span> <span style="color: #000088;">$var3</span><span style="color: #339933;">,</span> <span style="color: #000088;">$var4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/isset-construct-and-multiple-parameters/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Creating custom stream filters</title>
		<link>http://www.codediesel.com/php/creating-custom-stream-filters/</link>
		<comments>http://www.codediesel.com/php/creating-custom-stream-filters/#comments</comments>
		<pubDate>Mon, 15 Jun 2009 13:49:47 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[streams]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=1001</guid>
		<description><![CDATA[php custom stream filter tutorial]]></description>
			<content:encoded><![CDATA[<p>In this post we will see how to create a custom stream filter. <em>Streams</em>, first introduced in PHP 4.3, provide an abstration layer for file access. A number of different resources besides files &#8211; like network connections, compression protocols etc. can be regarded as &#8220;streams&#8221; of data which can be serially read and written to.<br />
<span id="more-1001"></span></p>
<p>By default there are a number of filters registered with PHP. You can get the list of filters registered on your system by the following call:</p>

<div class="wp_codebox"><table><tr id="p10014"><td class="code" id="p1001code4"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">print_r</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">stream_get_filters</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>On my system it returns the following registered filters.</p>

<div class="wp_codebox"><table><tr id="p10015"><td class="code" id="p1001code5"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">Array</span>
<span style="color: #009900;">&#40;</span>
    <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> convert<span style="color: #339933;">.</span><span style="color: #990000;">iconv</span><span style="color: #339933;">.*</span>
    <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> string<span style="color: #339933;">.</span>rot13
    <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> string<span style="color: #339933;">.</span>toupper
    <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> string<span style="color: #339933;">.</span>tolower
    <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> string<span style="color: #339933;">.</span><span style="color: #990000;">strip_tags</span>
    <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> convert<span style="color: #339933;">.*</span>
    <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">6</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> consumed
    <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">7</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> zlib<span style="color: #339933;">.*</span>
    <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">8</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> bzip2<span style="color: #339933;">.*</span>
<span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>For example to strip html tags from a input string you add the &#8216;string.strip_tags&#8217; filter to the stream resource as below.</p>

<div class="wp_codebox"><table><tr id="p10016"><td class="code" id="p1001code6"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$html</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'The &lt;b&gt;World&lt;/b&gt; is safe &lt;i&gt;again&lt;/i&gt;.'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$fp</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;php://output&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;r&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">stream_filter_prepend</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;string.strip_tags&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">fwrite</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #339933;">,</span> <span style="color: #000088;">$html</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">fclose</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Which will than output:</p>

<div class="wp_codebox"><table><tr id="p10017"><td class="code" id="p1001code7"><pre class="html" style="font-family:monospace;">The World is safe again.</pre></td></tr></table></div>

<p><strong>Creating a custom filter</strong><br />
In this section we will create a filter to replace urls in the input stream with a<br />
 &#8216;[----URL----]&#8216; string. Now this may not look like much of a helpful filter, but this is just for illustrative purpose. You can use any replacement string you like. The complete source for the filter class is shown below.</p>

<div class="wp_codebox"><table><tr id="p10018"><td class="code" id="p1001code8"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> URLFilter <span style="color: #000000; font-weight: bold;">extends</span> PHP_User_Filter
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_data</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/* Called when the filter is initialized */</span>
    <span style="color: #000000; font-weight: bold;">function</span> onCreate<span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_data <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/* This is where the actual stream data conversion takes place */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> filter<span style="color: #009900;">&#40;</span><span style="color: #000088;">$in</span><span style="color: #339933;">,</span> <span style="color: #000088;">$out</span><span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span><span style="color: #000088;">$consumed</span><span style="color: #339933;">,</span> <span style="color: #000088;">$closing</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #009933; font-style: italic;">/* We read all the stream data and store it in 
           the '$_data' variable 
        */</span>
        <span style="color: #000000; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$bucket</span> <span style="color: #339933;">=</span> <span style="color: #990000;">stream_bucket_make_writeable</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$in</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_data <span style="color: #339933;">.=</span> <span style="color: #000088;">$bucket</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bucket</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$bucket</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$consumed</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #009933; font-style: italic;">/* Now that we have read all the data from the stream we process 
          it and save it again to the bucket.
        */</span>
        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$closing</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$consumed</span> <span style="color: #339933;">+=</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000088;">$pattern</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;#http(s)?://([\w-]+\.)+[\w-]+(/[\w-./?%&amp;=]*)?#&quot;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$str</span> <span style="color: #339933;">=</span> <span style="color: #990000;">preg_replace</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pattern</span><span style="color: #339933;">,</span>
                                <span style="color: #0000ff;">'[----URL----]'</span><span style="color: #339933;">,</span>
                                <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bucket</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$str</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bucket</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">datalen</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bucket</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
                <span style="color: #990000;">stream_bucket_append</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$out</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">bucket</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000000; font-weight: bold;">return</span> PSFS_PASS_ON<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">return</span> PSFS_FEED_ME<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>All the action takes place in the <em>filter()</em> method, where we collect all the data from the buckets. The data is retrieved in the fashion of a <a target="_blank" href="http://en.wikipedia.org/wiki/Bucket_brigade">bucket brigade</a>. The <em>stream_bucket_make_writable()</em> function reads a portion of the data from the input bucket and converts it to a PHP bucket object. The data property of the bucket object is a string holding the bucket&#8217;s data, whereas datalen stores its length. We could convert the data in the filter() function itself, but rather we store all the data retrieved in the $_data variable and convert it at a single go. </p>
<p>Once all the data from the stream is read the $closing parameter is set to <em>true</em>. Now we can check for that and than process the data in any fashion we like. Once the data processing is done we add the processed data to the bucket and return a <em>PSFS_PASS_ON</em> to imply that we have successfully processed the stream data.</p>
<p><strong>An Example</strong><br />
Before we use the filter we have to register the filter and append it to the stream. A example using the above class is shown below, which gets the content from Google.com and than replaces all the urls in the source with the &#8216;[----URL----]&#8216; string.</p>

<div class="wp_codebox"><table><tr id="p10019"><td class="code" id="p1001code9"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #990000;">stream_filter_register</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'myFilter'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'URLFilter'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$handle</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://www.google.com/&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;r&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">stream_filter_append</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$handle</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;myFilter&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">feof</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$handle</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$contents</span> <span style="color: #339933;">.=</span> <span style="color: #990000;">fread</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$handle</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">8192</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #990000;">fclose</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$handle</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #000088;">$contents</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/creating-custom-stream-filters/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Validating POST fields the easy way</title>
		<link>http://www.codediesel.com/php/validating-post-fields-the-easy-way/</link>
		<comments>http://www.codediesel.com/php/validating-post-fields-the-easy-way/#comments</comments>
		<pubDate>Fri, 12 Dec 2008 15:47:44 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[tip]]></category>
		<category><![CDATA[html]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=142</guid>
		<description><![CDATA[Validating POST data from a form is a common requirement for a developer. If the number of form fields are few than the validation is a small matter. But the case is different when the form contains more than 15 or 20 fields and some of the fields are mandatory. The following code will give you an idea [...]]]></description>
			<content:encoded><![CDATA[<p>Validating POST data from a form is a common requirement for a developer. If the number of form fields are few than the validation is a small matter. But the case is different when the form contains more than 15 or 20 fields and some of the fields are mandatory. The following code will give you an idea of how to easily validate mandatory fields, whatever the number of fields.<br />
<span id="more-142"></span><br />
The first step is to prefix a &#8216;c_&#8217; or any other character of your choice to a form field that is mandatory. For example I&#8217;ve prefixed a &#8216;c_&#8217; for the email field below.</p>

<div class="wp_codebox"><table><tr id="p14212"><td class="code" id="p142code12"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>input name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;c_email&quot;</span> id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;c_email&quot;</span> maxlength<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;40&quot;</span> type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text&quot;</span> <span style="color: #339933;">/&gt;</span></pre></td></tr></table></div>

<p>Once we have added the required prefix we can check for them using the following code.</p>

<div class="wp_codebox"><table><tr id="p14213"><td class="code" id="p142code13"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/* Initialize $_SESSION variables to hold errors and form variables */</span>
<span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'formVars'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'errors'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Copy all $_POST variables to $_SESSION['formVars'] */</span>
<span style="color: #000000; font-weight: bold;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span> <span style="color: #000000; font-weight: bold;">as</span> <span style="color: #000088;">$varname</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> 
<span style="color: #009900;">&#123;</span>
    <span style="color: #009933; font-style: italic;">/**
     * For form elements that are compulsory check 
     * to see if they are empty. To ease this ,form 
     * elements that are compulsory have a 'c_' prefix added,
     * we can use that to parse the compulsory fields.
     **/</span>
&nbsp;
     <span style="color: #009933; font-style: italic;">/* Get the field prefix */</span>
    <span style="color: #000088;">$prefix</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$varname</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'formVars'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$varname</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$prefix</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">&quot;c_&quot;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'errors'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$varname</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$varname</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$varname</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; field cannot be empty&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Now it doesn&#8217;t matter how many form fields you have, the code remains the same. You can extend the idea for other validation purpose. For example you can add a double prefix like &#8216;ci_&#8221; to indicate that the field is mandatory with a integer datatype and make the corresponding change in the validation code.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/validating-post-fields-the-easy-way/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Creating a Figlet text in php</title>
		<link>http://www.codediesel.com/php/creating-a-figlet-text-in-php/</link>
		<comments>http://www.codediesel.com/php/creating-a-figlet-text-in-php/#comments</comments>
		<pubDate>Tue, 18 Nov 2008 12:21:27 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[zend]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=130</guid>
		<description><![CDATA[Zend_Text_Figlet is a Zend component which enables developers to create a FIGlet text. A Figlet is nothing but a technique of creating large letters using ordinary text. I don&#8217;t know how to make a use of this in my daily matters, but its a nice recreation. A short history on Figlet can be found here. [...]]]></description>
			<content:encoded><![CDATA[<p>Zend_Text_Figlet is a Zend component which enables developers to create a <a title="figlet" href="http://en.wikipedia.org/wiki/FIGlet" target="_blank">FIGlet</a> text. A Figlet is nothing but a technique of creating large letters using ordinary text. I don&#8217;t know how to make a use of this in my daily matters, but its a nice recreation. A short history on Figlet can be found <a target="_blank" href="http://www.figlet.org/figlet_history.html">here</a>. An example and the code to generate it is shown below.<br />
<span id="more-130"></span></p>
<pre>
  ____     __   _     ____
 |  _ \\  | || | ||  |  _ \\
 | |_| || | '--' ||  | |_| ||
 | .__//  | .--. ||  | .__//
 |_|--`   |_|| |_||  |_|--`
 `-`      `-`  `-`   `-`
</pre>

<div class="wp_codebox"><table><tr id="p13017"><td class="code" id="p130code17"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;zend/text/figlet.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$figlet</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Text_Figlet<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #000088;">$figlet</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">render</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'PHP'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>If you are displaying the output on a web page make sure to use the &#8216;pre&#8217; tags around the Figlet. The Figlet looks good in a pure text format.<br />
You can also define various options for the Figlet by passing the function an array of options, like say changing the font.</p>

<div class="wp_codebox"><table><tr id="p13018"><td class="code" id="p130code18"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;zend/text/figlet.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009933; font-style: italic;">/* use a different font */</span>
<span style="color: #000088;">$options</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'font'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'graffiti.flf'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>               
&nbsp;
<span style="color: #000088;">$figlet</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Text_Figlet<span style="color: #009900;">&#40;</span><span style="color: #000088;">$options</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #000088;">$figlet</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">render</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'PHP'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<pre>
__________   ___ ___  __________
\______   \ /   |   \ \______   \
 |     ___//    ~    \ |     ___/
 |    |    \    Y    / |    |
 |____|     \___|_  /  |____|
                  \/
</pre>
<p>You can download more fonts from <a target="_blank" href="http://www.figlet.org/fontdb.cgi">here</a>.<br />
The default width of the Figlet output is set to 80 characters, which you can change with the &#8216;outputWidth&#8217; option.</p>

<div class="wp_codebox"><table><tr id="p13019"><td class="code" id="p130code19"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">.</span>
<span style="color: #339933;">.</span>
<span style="color: #000088;">$options</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'outputWidth'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">120</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>For more options you can take a look at the <a target="_blank" href="http://framework.zend.com/manual/en/zend.text.html">documentation</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/creating-a-figlet-text-in-php/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Easy way to build GET query strings in php</title>
		<link>http://www.codediesel.com/php/building-get-query-strings-php/</link>
		<comments>http://www.codediesel.com/php/building-get-query-strings-php/#comments</comments>
		<pubDate>Thu, 13 Nov 2008 14:10:58 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[functions]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=127</guid>
		<description><![CDATA[Passing variables with a url is such a frequent thing programmers do that most of you may think this post is unwarranted. We call this method of passing variables as GET, the other being POST. It is one of those things which can be easily done in php. Lets take an example. You are to [...]]]></description>
			<content:encoded><![CDATA[<p>Passing variables with a url is such a frequent thing programmers do that most of you may think this post is unwarranted. We call this method of passing variables as GET, the other being POST. It is one of those things which can be easily done in php. Lets take an example. You are to query a database and for that you need to send three variables via GET &#8211; city, id, paid.<br />
The common way to pass them via GET is to construct a query string as below:<br />
<span id="more-127"></span></p>

<div class="wp_codebox"><table><tr id="p12726"><td class="code" id="p127code26"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/* assume we want to pass this variables */</span>
<span style="color: #000088;">$city_name</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;new york&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$invoice_id</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">3456</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$paid</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$query_string</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;city=<span style="color: #006699; font-weight: bold;">{$city_name}</span>&amp;id=<span style="color: #006699; font-weight: bold;">{$invoice_id}</span>&amp;paid=<span style="color: #006699; font-weight: bold;">{$paid}</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://www.example.com?&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$query_string</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The problem is that most php programmers get stuck using the above method. The method is fine for three to four variables, but for more than that the code gets hard to read and maintain, introducing subtle bugs in the constructed query.</p>
<p>The best way to pass GET variables is to use the <em>http_build_query()</em> function available from php version 5; which takes an array of variables and builds a nice URL encoded string which you can append to a url. And example is shown below.</p>

<div class="wp_codebox"><table><tr id="p12727"><td class="code" id="p127code27"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$city_name</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;new york&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$invoice_id</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">3456</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$paid</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$fields</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'city'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$city_name</span><span style="color: #339933;">,</span>
                <span style="color: #0000ff;">'id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$invoice_id</span><span style="color: #339933;">,</span>
                <span style="color: #0000ff;">'paid'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$paid</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://www.example.com?&quot;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">http_build_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fields</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;&amp;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>In the above example the array contained the variable names and their values. You can also pass an array containing only values and let the function fill the variable name using the array index and a variable you supply (as a second parameter). For example if you want to pass an array of six cities than you can do the following, the second parameter of the function being the query variable.</p>

<div class="wp_codebox"><table><tr id="p12728"><td class="code" id="p127code28"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$fields</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'paris'</span><span style="color: #339933;">,</span>
                <span style="color: #0000ff;">'new york'</span><span style="color: #339933;">,</span>
                <span style="color: #0000ff;">'florence'</span><span style="color: #339933;">,</span>
                <span style="color: #0000ff;">'london'</span><span style="color: #339933;">,</span>
                <span style="color: #0000ff;">'berlin'</span><span style="color: #339933;">,</span>
                <span style="color: #0000ff;">'delhi'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://www.example.php?&quot;</span> <span style="color: #339933;">.</span>
        <span style="color: #990000;">http_build_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fields</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'city'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;&amp;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The generated url will look like this:</p>
<p>http://www.example.php/?city0=paris&#038;city1=new+york&#038;city2=florence&#038;city3=london&#038;city4=berlin&#038;city5=delhi</p>
<p>The third parameter to the function is not necessary as the function defaults to using &#8216;&#038;&#8217; as a separator. But I prefer making it explicit.<br />
You can also easily pass a complex array as below:</p>

<div class="wp_codebox"><table><tr id="p12729"><td class="code" id="p127code29"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$city_name</span>      <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;new york&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$invoice_id</span>     <span style="color: #339933;">=</span> <span style="color: #cc66cc;">3456</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$currency_name</span>  <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;euro&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$total</span>          <span style="color: #339933;">=</span> <span style="color: #cc66cc;">345</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$receipt_no</span>     <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;fgf44545&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$fields</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'city'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$city_name</span><span style="color: #339933;">,</span>
                <span style="color: #0000ff;">'id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$invoice_id</span><span style="color: #339933;">,</span>
                <span style="color: #0000ff;">'paid'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'currency'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$currency_name</span><span style="color: #339933;">,</span>
                                <span style="color: #0000ff;">'amount'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$total</span><span style="color: #339933;">,</span>
                                <span style="color: #0000ff;">'receipt'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$receipt_no</span><span style="color: #009900;">&#41;</span>                
                <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://www.example.php?&quot;</span> <span style="color: #339933;">.</span>
        <span style="color: #990000;">http_build_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fields</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;&amp;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Which will create a url encoded query as following:</p>
<p>http://www.example.com?city=new+york&#038;id=3456&#038;paid%5Bcurrency%5D=euro&#038;paid%5Bamount%5D=345&#038;paid%5Breceipt%5D=fgf44545</p>
<p>To go the reverse way, we can parse the encoded url above using the code below:</p>

<div class="wp_codebox"><table><tr id="p12730"><td class="code" id="p127code30"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$url_to_parse</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://www.example.com?city=new+york&amp;id=3456&amp;paid%5Bcurrency%5D=euro&amp;paid%5Bamount%5D=345&amp;paid%5Breceipt%5D=fgf44545&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$parsed_url</span> <span style="color: #339933;">=</span> <span style="color: #990000;">parse_url</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url_to_parse</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$parsed_url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* This will output the following array */</span>
<span style="color: #990000;">Array</span>
<span style="color: #009900;">&#40;</span>
    <span style="color: #009900;">&#91;</span>scheme<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> http
    <span style="color: #009900;">&#91;</span>host<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> www<span style="color: #339933;">.</span>example<span style="color: #339933;">.</span>com
    <span style="color: #009900;">&#91;</span>query<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> city<span style="color: #339933;">=</span><span style="color: #000000; font-weight: bold;">new</span><span style="color: #339933;">+</span>york<span style="color: #339933;">&amp;</span>id<span style="color: #339933;">=</span><span style="color: #cc66cc;">3456</span><span style="color: #339933;">&amp;</span>paid<span style="color: #339933;">%</span>5Bcurrency<span style="color: #339933;">%</span>5D<span style="color: #339933;">=</span>euro<span style="color: #339933;">&amp;</span>paid<span style="color: #339933;">%</span>5Bamount<span style="color: #339933;">%</span>5D<span style="color: #339933;">=</span><span style="color: #cc66cc;">345</span><span style="color: #339933;">&amp;</span>paid<span style="color: #339933;">%</span>5Breceipt<span style="color: #339933;">%</span>5D<span style="color: #339933;">=</span>fgf44545
<span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>Now we can further parse the url query string stored in the &#8216;query&#8217; variable above:</p>

<div class="wp_codebox"><table><tr id="p12731"><td class="code" id="p127code31"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$url_query</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$parsed_url</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'query'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">parse_str</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url_query</span><span style="color: #339933;">,</span> <span style="color: #000088;">$out</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$out</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* This will output the following array */</span>
<span style="color: #990000;">Array</span>
<span style="color: #009900;">&#40;</span>
    <span style="color: #009900;">&#91;</span>city<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">new</span> york
    <span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">3456</span>
    <span style="color: #009900;">&#91;</span>paid<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
        <span style="color: #009900;">&#40;</span>
            <span style="color: #009900;">&#91;</span>currency<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> euro
            <span style="color: #009900;">&#91;</span>amount<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">345</span>
            <span style="color: #009900;">&#91;</span>receipt<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> fgf44545
        <span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>As can be seen the http_build_query() function really makes constructing GET queries simple.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/building-get-query-strings-php/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Simple Pagination in PHP tutorial</title>
		<link>http://www.codediesel.com/php/simple-pagination-in-php/</link>
		<comments>http://www.codediesel.com/php/simple-pagination-in-php/#comments</comments>
		<pubDate>Fri, 24 Oct 2008 05:03:48 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[pear]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[functions]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=118</guid>
		<description><![CDATA[PHP pagination tutorial using the pears pager library]]></description>
			<content:encoded><![CDATA[<p>Pagination is a frequent requirement in web development projects. Most PHP developers must have already implementated paging in one form or other in their projects. In this post we will see how to add pagination the easy way using PEAR&#8217;s Pager class. Note that in all the posts I use PHP 5.x.x, so if you are still stuck at version 4.x.x, its already time to upgrade.<br />
<span id="more-118"></span></p>
<p>Its always nice to see some working code first, before getting into the details. So here goes.</p>

<div class="wp_codebox"><table><tr id="p11832"><td class="code" id="p118code32"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">require_once</span> <span style="color: #0000ff;">'Pager/Pager.php'</span><span style="color: #339933;">;</span>
<span style="color: #009933; font-style: italic;">/* We will bypass the database connection code ... */</span>
<span style="color: #000088;">$sqlQuery</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SOME SQL QUERY&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sqlQuery</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$totalRows</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_num_rows</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$pager_options</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
<span style="color: #0000ff;">'mode'</span>       <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Sliding'</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'perPage'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'delta'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'totalItems'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$totalRows</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$pager</span> <span style="color: #339933;">=</span> Pager<span style="color: #339933;">::</span><span style="color: #004000;">factory</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pager_options</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #000088;">$pager</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">links</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The above code will return the following pagination links.</p>
<p>1   |   2   |   3   |   4   |   5   |   6   |   7   |   8   |   9      »      [10]</p>
<p>Assuming the page where the above code is included is named &#8216;dataPage.php&#8217;; the pager links will have a url like follows:</p>
<p>dataPage.php?pageID=<em>n</em></p>
<p>where <em>n</em> is the page number.</p>
<p>You can see how easy it is using the Pager class. All the link building is handled by the class. Now lets go into the details.</p>
<h4>1. Installation</h4>
<p>If you are installing using the Pear installer, enter the following at the command prompt to install Pager.</p>

<div class="wp_codebox"><table><tr id="p11833"><td class="code" id="p118code33"><pre class="php" style="font-family:monospace;">c<span style="color: #339933;">:</span>\pear install Pager</pre></td></tr></table></div>

<p>Or else you can just download the module from <a href="http://pear.php.net/package/Pager/download">here</a> and then copy it to your Pear includes directory, which will probably be &#8216;c:\<em>your php installation directory</em>\Pear&#8217;.</p>
<h4>2. A simple example</h4>
<p>In the following example we will create a pagination for a &#8216;logs&#8217; table.</p>

<div class="wp_codebox"><table><tr id="p11834"><td class="code" id="p118code34"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/* Include the Pear::Pager file */</span>
<span style="color: #000000; font-weight: bold;">require_once</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Pager/Pager.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Replace this with your database details */</span>
<span style="color: #000088;">$connection</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;localhost&quot;</span><span style="color: #339933;">,</span> username<span style="color: #339933;">,</span> password<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">mysql_select_db</span><span style="color: #009900;">&#40;</span>database name<span style="color: #339933;">,</span> <span style="color: #000088;">$connection</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* First we need to get the total rows in the table */</span>
<span style="color: #000088;">$result</span><span style="color: #339933;">=</span><span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT count(*) AS total FROM logs&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$connection</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Total number of rows in the logs table */</span>
<span style="color: #000088;">$totalItems</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'total'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Set some options for the Pager */</span>
<span style="color: #000088;">$pager_options</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
<span style="color: #0000ff;">'mode'</span>       <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Sliding'</span><span style="color: #339933;">,</span>   <span style="color: #009933; font-style: italic;">// Sliding or Jumping mode. See below.</span>
<span style="color: #0000ff;">'perPage'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span>   <span style="color: #009933; font-style: italic;">// Total rows to show per page</span>
<span style="color: #0000ff;">'delta'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span>   <span style="color: #009933; font-style: italic;">// See below</span>
<span style="color: #0000ff;">'totalItems'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$totalItems</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Initialize the Pager class with the above options */</span>
<span style="color: #000088;">$pager</span> <span style="color: #339933;">=</span> Pager<span style="color: #339933;">::</span><span style="color: #004000;">factory</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pager_options</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Display the links */</span>
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #000088;">$pager</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">links</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* The following code will retreive the result using the pager options */</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* The function below will get the page offsets to be used with
the database query. For e.g if we are on the third page then the
$from variable will have the value of '21' (we are showing 10 items per 
page, remember) and the $to variable will have the value of '30'.
*/</span>
<span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$from</span><span style="color: #339933;">,</span> <span style="color: #000088;">$to</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$pager</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getOffsetByPageId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009933; font-style: italic;">/* The MySQL 'LIMIT' clause index starts from '0', 
    so decrease the $from by 1 */</span>
<span style="color: #000088;">$from</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$from</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* The number of rows to get per query */</span>
<span style="color: #000088;">$perPage</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$pager_options</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'perPage'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT * FROM alogs LIMIT <span style="color: #006699; font-weight: bold;">$from</span> , <span style="color: #006699; font-weight: bold;">$perPage</span>&quot;</span><span style="color: #339933;">,</span>
<span style="color: #000088;">$connection</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #009933; font-style: italic;">/* Do something with the query results */</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>In the Pager class there are two modes to display the pagination: Sliding &amp; Jumping.</p>
<p>With Pager in &#8220;Sliding&#8221; mode the pagination links change smoothly, and the current page is always shown at the center of the &#8220;window&#8221; (except for the start and end pages). When in Sliding mode the delta option implies how many links to show on the left and right of the current page link.<br />
For e.g with a delta set to &#8217;3&#8242; the links are displayed as shown below, page 10 being the current page:</p>
<p>[1]      «      7   |   8   |   9   |   <strong>10</strong> |   11   |   12   |   13      »      [13]</p>
<p>With delta option set to &#8217;4&#8242; the links are shown as below. As you can see there are four links to the left and right of the current page.</p>
<p>[1]      «      5   |   6   |   7   |   8   |   <strong>9</strong> |   10   |   11   |   12   |   13      »      [13]</p>
<p>With the Pager in &#8220;Jumping&#8221; mode and delta set to &#8217;4&#8242; the Pager always shows the same 4 page links while you are on one of these pages.<br />
For e.g with a delta set to &#8217;4&#8242; the links are displayed as shown below, page 5 being the current page:</p>
<p>[1] &lt;&lt; Back <strong>5</strong> 6 7 8  Next &gt;&gt; [13]</p>
<p>If you are on page &#8217;9&#8242; the links displayed are as below:</p>
<p>[1] &lt;&lt; Back <strong>9</strong> 10 11 12  Next &gt;&gt; [13]</p>
<h4>3. More options</h4>
<p>You can also change the default separator to any character or image you like.</p>

<div class="wp_codebox"><table><tr id="p11835"><td class="code" id="p118code35"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$pager_options</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
<span style="color: #0000ff;">'mode'</span>       <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Sliding'</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'perPage'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'delta'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'separator'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">','</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'totalItems'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$totalItems</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>This will output the following:<br />
[1]      «      1   ,   2   ,   3   ,   4   ,   5   ,   6   ,   7   ,   8   ,   9      »      [13]</p>
<p>If you would like to add images to the &#8216;next&#8217; and &#8216;previous&#8217; links, use the following options:</p>

<div class="wp_codebox"><table><tr id="p11836"><td class="code" id="p118code36"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$pager_options</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
<span style="color: #0000ff;">'mode'</span>       <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Sliding'</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'perPage'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'delta'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'nextImg'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'&lt;img src=&quot;next.png&quot;  /&gt;'</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'prevImg'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'&lt;img src=&quot;prev.png&quot;  /&gt;'</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'totalItems'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$totalItems</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Which will display the links as shown below:<br />
<a href="http://www.codediesel.com/wp-content/uploads/2008/10/pager2.gif"><img class="aligncenter size-full wp-image-121" style="border:1px solid #c0c0c0;" title="pager2" src="http://www.codediesel.com/wp-content/uploads/2008/10/pager2.gif" alt="" width="472" height="32" /></a></p>
<p>The default number of spaces before each separator is three. To change the number of spaces use the &#8216;spacesBeforeSeparator&#8217; option. For e.g:</p>

<div class="wp_codebox"><table><tr id="p11837"><td class="code" id="p118code37"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">.</span>
<span style="color: #339933;">.</span>
<span style="color: #0000ff;">'spacesBeforeSeparator'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span>
<span style="color: #339933;">.</span></pre></td></tr></table></div>

<h4>4. Styling</h4>
<p>You can style the links by putting them in a div tag and applying a css. A example css and its output is shown below.</p>
<p>Add a div tag:</p>

<div class="wp_codebox"><table><tr id="p11838"><td class="code" id="p118code38"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">.</span>
<span style="color: #339933;">.</span>
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #0000ff;">'&lt;div class=&quot;pager&quot;&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #000088;">$pager</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">links</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #0000ff;">'&lt;/div&gt;'</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Apply CSS:</p>

<div class="wp_codebox"><table><tr id="p11839"><td class="code" id="p118code39"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.pager</span> <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">font-family</span><span style="color: #00AA00;">:</span> Arial<span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span> <span style="color: #933;">14px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.pager</span> a <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">font-family</span><span style="color: #00AA00;">:</span> Arial<span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span> <span style="color: #933;">14px</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">text-decoration</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">17px</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">17px</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #993333;">solid</span> <span style="color: #cc00cc;">#000</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#0C74BA</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">text-align</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">center</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#fff</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #6666ff;">.pager</span> a<span style="color: #3333ff;">:hover </span><span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#c0c0c0</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p>The output:<br />
<a href="http://www.codediesel.com/wp-content/uploads/2008/10/pager.gif"><img class="aligncenter size-full wp-image-119" style="border:1px solid #c0c0c0;" title="pager" src="http://www.codediesel.com/wp-content/uploads/2008/10/pager.gif" alt="pager" width="376" height="35" /></a></p>
<p>That&#8217;s it. There are many other options you can use with the Pager class, the details which you can find in the Pager <a href="http://pear.php.net/manual/en/package.html.pager.php" target="_blank">documentation</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/simple-pagination-in-php/feed/</wfw:commentRss>
		<slash:comments>29</slash:comments>
		</item>
		<item>
		<title>WHOIS directory lookup in PHP</title>
		<link>http://www.codediesel.com/php/whois-directory-lookup-in-php/</link>
		<comments>http://www.codediesel.com/php/whois-directory-lookup-in-php/#comments</comments>
		<pubDate>Sat, 13 Sep 2008 12:23:11 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[functions]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=104</guid>
		<description><![CDATA[Net_Whois lets you query a internet name directory service in a easy to use manner. WHOIS is a protocol which is widely used to determine the owner of a domain name, an IP address by querying an official name database. One possible application I can think of is to see if DNS servers are set [...]]]></description>
			<content:encoded><![CDATA[<p><a target="_blank" href="http://pear.php.net/package/Net_Whois">Net_Whois</a> lets you query a internet name directory service in a easy to use manner. <a target="_blank" href="http://www.faqs.org/rfcs/rfc3912.html">WHOIS</a> is a protocol which is widely used to determine the owner of a domain name, an IP address by querying an official name database. One possible application I can think of is to see if DNS servers are set correctly for various domains I handle regularly.<br />
<span id="more-104"></span></p>
<h4>Installation</h4>
<p>Net_Whois being a Pear package we will use the Pear installer as below. I recommend to always use the Pear installer to download packages rather than downloading it manually as the Pear installer automatically downloads any dependent packages.</p>

<div class="wp_codebox"><table><tr id="p10442"><td class="code" id="p104code42"><pre class="text" style="font-family:monospace;">pear install Net_Whois-1.0.2</pre></td></tr></table></div>

<h4>Usage</h4>
<p>The current library uses the following servers to execute the query:<br />
whois.crsnic.net<br />
whois.networksolutions.com<br />
whois.nic.mil<br />
whois.nic.gov<br />
whois.arin.net<br />
whois.ripe.net<br />
whois.apnic.net<br />
whois.ripn.net<br />
whois.ra.net<br />
.whois-servers.net<br />
whois.6bone.net<br />
whois.registro.br</p>
<p>A example code is shown below.</p>

<div class="wp_codebox"><table><tr id="p10443"><td class="code" id="p104code43"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">require_once</span> <span style="color: #0000ff;">&quot;Net/Whois.php&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$whois</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Net_Whois<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009933; font-style: italic;">/* You can add other NIC server as a second parameter. */</span>
    <span style="color: #000088;">$info</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$whois</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'example.com'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">echo</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$info</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Note that the class returns a string, so you would need to use regular expressions to get to the relevant information. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/whois-directory-lookup-in-php/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Ping a server using PHP</title>
		<link>http://www.codediesel.com/php/ping-a-server-using-php/</link>
		<comments>http://www.codediesel.com/php/ping-a-server-using-php/#comments</comments>
		<pubDate>Sat, 13 Sep 2008 11:58:05 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[functions]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=103</guid>
		<description><![CDATA[PEAR&#8217;s Net_Ping is a niffty wrapper class for executing ping calls from PHP. You can use it to check if a remote server is responding correctly. The library can be download from  here. Installation Net_Ping being a Pear package we will use the Pear installer to download and install it. I recommend to always use [...]]]></description>
			<content:encoded><![CDATA[<p>PEAR&#8217;s Net_Ping is a niffty wrapper class for executing ping calls from PHP. You can use it to check if a remote server is responding correctly. The library can be download from  <a target="_blank" href="http://pear.php.net/package/Net_Ping">here</a>. </p>
<h4>Installation</h4>
<p>Net_Ping being a Pear package we will use the Pear installer to download and install it. I recommend to always use the Pear installer to download packages rather than downloading it manually as the Pear installer automatically downloads any dependent packages.</p>

<div class="wp_codebox"><table><tr id="p10347"><td class="code" id="p103code47"><pre class="text" style="font-family:monospace;">pear install Net_Ping-2.4.4</pre></td></tr></table></div>

<h4>Usage</h4>
<p>A example of using Net_Ping is given below.<br />
<span id="more-103"></span></p>

<div class="wp_codebox"><table><tr id="p10348"><td class="code" id="p103code48"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">require_once</span> <span style="color: #0000ff;">&quot;Net/Ping.php&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$ping</span> <span style="color: #339933;">=</span> Net_Ping<span style="color: #339933;">::</span><span style="color: #004000;">factory</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>PEAR<span style="color: #339933;">::</span><span style="color: #004000;">isError</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ping</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
      <span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #000088;">$ping</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">else</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #009933; font-style: italic;">/* Number of packets to send */</span>
      <span style="color: #000088;">$ping</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setArgs</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'count'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$rawData</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$ping</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ping</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'google.com'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$rawData</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>The output of the same is shown below.</p>

<div class="wp_codebox"><table><tr id="p10349"><td class="code" id="p103code49"><pre class="php" style="font-family:monospace;">Net_Ping_Result Object
<span style="color: #009900;">&#40;</span>
    <span style="color: #009900;">&#91;</span>_icmp_sequence<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
        <span style="color: #009900;">&#40;</span>
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">310</span>
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">300</span>
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">318</span>
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">301</span>
        <span style="color: #009900;">&#41;</span>
&nbsp;
    <span style="color: #009900;">&#91;</span>_target_ip<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> 64<span style="color: #339933;">.</span>233<span style="color: #339933;">.</span>167<span style="color: #339933;">.</span>99
    <span style="color: #009900;">&#91;</span>_bytes_per_request<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">32</span>
    <span style="color: #009900;">&#91;</span>_bytes_total<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">128</span>
    <span style="color: #009900;">&#91;</span>_ttl<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">238</span>
    <span style="color: #009900;">&#91;</span>_raw_data<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
        <span style="color: #009900;">&#40;</span>
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> 
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Pinging google<span style="color: #339933;">.</span>com <span style="color: #009900;">&#91;</span>64<span style="color: #339933;">.</span>233<span style="color: #339933;">.</span>167<span style="color: #339933;">.</span>99<span style="color: #009900;">&#93;</span> with <span style="color: #cc66cc;">32</span> bytes of data<span style="color: #339933;">:</span>
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> 
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Reply from 64<span style="color: #339933;">.</span>233<span style="color: #339933;">.</span>167<span style="color: #339933;">.</span>99<span style="color: #339933;">:</span> bytes<span style="color: #339933;">=</span><span style="color: #cc66cc;">32</span> <span style="color: #990000;">time</span><span style="color: #339933;">=</span>310ms TTL<span style="color: #339933;">=</span><span style="color: #cc66cc;">238</span>
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Reply from 64<span style="color: #339933;">.</span>233<span style="color: #339933;">.</span>167<span style="color: #339933;">.</span>99<span style="color: #339933;">:</span> bytes<span style="color: #339933;">=</span><span style="color: #cc66cc;">32</span> <span style="color: #990000;">time</span><span style="color: #339933;">=</span>300ms TTL<span style="color: #339933;">=</span><span style="color: #cc66cc;">238</span>
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Reply from 64<span style="color: #339933;">.</span>233<span style="color: #339933;">.</span>167<span style="color: #339933;">.</span>99<span style="color: #339933;">:</span> bytes<span style="color: #339933;">=</span><span style="color: #cc66cc;">32</span> <span style="color: #990000;">time</span><span style="color: #339933;">=</span>318ms TTL<span style="color: #339933;">=</span><span style="color: #cc66cc;">238</span>
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">6</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Reply from 64<span style="color: #339933;">.</span>233<span style="color: #339933;">.</span>167<span style="color: #339933;">.</span>99<span style="color: #339933;">:</span> bytes<span style="color: #339933;">=</span><span style="color: #cc66cc;">32</span> <span style="color: #990000;">time</span><span style="color: #339933;">=</span>301ms TTL<span style="color: #339933;">=</span><span style="color: #cc66cc;">238</span>
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">7</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> 
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">8</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Ping statistics <span style="color: #000000; font-weight: bold;">for</span> 64<span style="color: #339933;">.</span>233<span style="color: #339933;">.</span>167<span style="color: #339933;">.</span>99<span style="color: #339933;">:</span>
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span>     Packets<span style="color: #339933;">:</span> Sent <span style="color: #339933;">=</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span> Received <span style="color: #339933;">=</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span> Lost <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">%</span> loss<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Approximate <span style="color: #990000;">round</span> trip times in milli<span style="color: #339933;">-</span>seconds<span style="color: #339933;">:</span>
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">11</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span>     Minimum <span style="color: #339933;">=</span> 300ms<span style="color: #339933;">,</span> Maximum <span style="color: #339933;">=</span> 318ms<span style="color: #339933;">,</span> Average <span style="color: #339933;">=</span> 307ms
        <span style="color: #009900;">&#41;</span>
&nbsp;
    <span style="color: #009900;">&#91;</span>_sysname<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> windows
    <span style="color: #009900;">&#91;</span>_round_trip<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
        <span style="color: #009900;">&#40;</span>
            <span style="color: #009900;">&#91;</span><span style="color: #990000;">min</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">300</span>
            <span style="color: #009900;">&#91;</span><span style="color: #990000;">max</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">318</span>
            <span style="color: #009900;">&#91;</span>avg<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">307</span>
        <span style="color: #009900;">&#41;</span>
&nbsp;
    <span style="color: #009900;">&#91;</span>_transmitted<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">4</span>
    <span style="color: #009900;">&#91;</span>_received<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">4</span>
    <span style="color: #009900;">&#91;</span>_loss<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">0</span>
<span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/ping-a-server-using-php/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Reading raw POST data in PHP</title>
		<link>http://www.codediesel.com/php/reading-raw-post-data-in-php/</link>
		<comments>http://www.codediesel.com/php/reading-raw-post-data-in-php/#comments</comments>
		<pubDate>Fri, 12 Sep 2008 07:00:55 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[functions]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=101</guid>
		<description><![CDATA[A quick tip for reading raw http POST data in PHP. For example if we have a xml posted to a page, we can read the raw data with the following code. $xml = file_get_contents&#40;'php://input'&#41;; We could use $HTTP_RAW_POST_DATA instead, but many times it does not work due to some php.ini settings. Note that &#8216;php://input&#8217; [...]]]></description>
			<content:encoded><![CDATA[<p>A quick tip for reading raw http POST data in PHP. For example if we have a xml posted to a page, we can read the raw data with the following code.</p>

<div class="wp_codebox"><table><tr id="p10151"><td class="code" id="p101code51"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$xml</span> <span style="color: #339933;">=</span> <span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'php://input'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>We could use <em>$HTTP_RAW_POST_DATA</em> instead, but many times it does not work due to some php.ini settings. Note that &#8216;php://input&#8217; does not work with <em>enctype=&#8221;multipart/form-data&#8221;</em>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/reading-raw-post-data-in-php/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>converting MySQL queries to xml</title>
		<link>http://www.codediesel.com/php/converting-mysql-queries-to-xml/</link>
		<comments>http://www.codediesel.com/php/converting-mysql-queries-to-xml/#comments</comments>
		<pubDate>Tue, 05 Aug 2008 13:00:51 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=79</guid>
		<description><![CDATA[There frequently arises a need to return mySQL query results in xml. Maybe you need to send the xml data to the browser or you want to use it as a xml request to a web service; whatever the application, the following function will return the result of a sql query in xml format. The [...]]]></description>
			<content:encoded><![CDATA[<p>There frequently arises a need to return mySQL query results in xml. Maybe you need to send the xml data to the browser or you want to use it as a xml request to a web service; whatever the application, the following function will return the result of a sql query in xml format. </p>
<p>The function accepts 3 parameters. The first is the mysql query result resource, the second is the name of the root element and the third the name of the first child of the root. All the fields from the mySQL table will be sub elements of this child.</p>
<p>The function is shown below:<br />
<span id="more-79"></span></p>

<div class="wp_codebox"><table><tr id="p7955"><td class="code" id="p79code55"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * @param mysql_resource - $queryResult - mysql query result
 * @param string - $rootElementName - root element name
 * @param string - $childElementName - child element name
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> sqlToXml<span style="color: #009900;">&#40;</span><span style="color: #000088;">$queryResult</span><span style="color: #339933;">,</span> <span style="color: #000088;">$rootElementName</span><span style="color: #339933;">,</span> <span style="color: #000088;">$childElementName</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span> 
    <span style="color: #000088;">$xmlData</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&lt;?xml version=<span style="color: #000099; font-weight: bold;">\&quot;</span>1.0<span style="color: #000099; font-weight: bold;">\&quot;</span> encoding=<span style="color: #000099; font-weight: bold;">\&quot;</span>ISO-8859-1<span style="color: #000099; font-weight: bold;">\&quot;</span> ?&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span> 
    <span style="color: #000088;">$xmlData</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;&lt;&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$rootElementName</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$record</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_object</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$queryResult</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span> 
        <span style="color: #009933; font-style: italic;">/* Create the first child element */</span>
        <span style="color: #000088;">$xmlData</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;&lt;&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$childElementName</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #990000;">mysql_num_fields</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$queryResult</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span> 
            <span style="color: #000088;">$fieldName</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_field_name</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$queryResult</span><span style="color: #339933;">,</span> <span style="color: #000088;">$i</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
            <span style="color: #009933; font-style: italic;">/* The child will take the name of the table column */</span>
            <span style="color: #000088;">$xmlData</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;&lt;&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$fieldName</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #009933; font-style: italic;">/* We set empty columns with NULL, or you could set 
                it to '0' or a blank. */</span>
            <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$record</span><span style="color: #339933;">-&gt;</span><span style="color: #000088;">$fieldName</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
                <span style="color: #000088;">$xmlData</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$record</span><span style="color: #339933;">-&gt;</span><span style="color: #000088;">$fieldName</span><span style="color: #339933;">;</span> 
            <span style="color: #000000; font-weight: bold;">else</span>
                <span style="color: #000088;">$xmlData</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;null&quot;</span><span style="color: #339933;">;</span> 
&nbsp;
            <span style="color: #000088;">$xmlData</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;&lt;/&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$fieldName</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&gt;&quot;</span><span style="color: #339933;">;</span> 
        <span style="color: #009900;">&#125;</span> 
        <span style="color: #000088;">$xmlData</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;&lt;/&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$childElementName</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&gt;&quot;</span><span style="color: #339933;">;</span> 
    <span style="color: #009900;">&#125;</span> 
    <span style="color: #000088;">$xmlData</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;&lt;/&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$rootElementName</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&gt;&quot;</span><span style="color: #339933;">;</span> 
&nbsp;
    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000088;">$xmlData</span><span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><strong>How to use?</strong><br />
For example if we have a &#8216;company&#8217; table with the following three fields:<br />
<em>id</em>, <em>companyid</em>, <em>name</em>.</p>
<p>You can use the above function in the following manner to convert the table rows into xml;</p>

<div class="wp_codebox"><table><tr id="p7956"><td class="code" id="p79code56"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">.</span>
<span style="color: #339933;">.</span>
<span style="color: #009933; font-style: italic;">/* Sql query */</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT * from company&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* If you want to process the returned xml rather than send it
    to the browser, remove the below line.
*/</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-Type: application/xml&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">echo</span> sqlToXml<span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;companies&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;company&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>which returns the following xml.</p>

<div class="wp_codebox"><table><tr id="p7957"><td class="code" id="p79code57"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;ISO-8859-1&quot;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;companies<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;company<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;companyid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/companyid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>microsoft<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/company<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
.
.
.
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/companies<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/converting-mysql-queries-to-xml/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

