<?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>Ask About PHP &#187; PHP Tutorials</title>
	<atom:link href="http://www.askaboutphp.com/category/tutorials/feed" rel="self" type="application/rss+xml" />
	<link>http://www.askaboutphp.com</link>
	<description></description>
	<lastBuildDate>Wed, 09 Jun 2010 07:17:33 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>PHP and jQuery: Submit a form without refreshing the page</title>
		<link>http://www.askaboutphp.com/213/php-and-jquery-submit-a-form-without-refreshing-the-page.html</link>
		<comments>http://www.askaboutphp.com/213/php-and-jquery-submit-a-form-without-refreshing-the-page.html#comments</comments>
		<pubDate>Wed, 09 Jun 2010 06:55:39 +0000</pubDate>
		<dc:creator>Eldee</dc:creator>
				<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[forms]]></category>
		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://www.askaboutphp.com/?p=213</guid>
		<description><![CDATA[Lately, I&#8217;ve been chancing on quite a number of posts at various places asking about how to perform a web action without the webpage reloading/refreshing? For example, dynamically updating a list on a page without a reload or submitting a form to PHP without leaving the webpage the form sits on.
I guess this is as [...]]]></description>
			<content:encoded><![CDATA[<p>Lately, I&#8217;ve been chancing on quite a number of posts at various places asking about how to perform a web action without the webpage reloading/refreshing? For example, dynamically updating a list on a page without a reload or submitting a form to PHP without leaving the webpage the form sits on.</p>
<p>I guess this is as good a time to start branching into some posts on PHP and Javascript, or more specifically, jQuery.</p>
<p><span id="more-213"></span><br />
To be honest, I&#8217;ve never been partial to using Javascript (ie client-side programming) in my web applications, prefering instead to rely on server-side processing for all my programming needs. I guess this has to do with my frustrations of Javascript incompatibility between different browsers back in the day. However, with a javascript library (or framework) like jQuery, I&#8217;m starting to use more client-side actions to complement all the server-side PHP stuff. Here&#8217;s <a href="http://davidwalsh.name/6-reasons-to-use-javascript-libraries-frameworks">6 reasons why you should use JavaScript Libraries &#038; Frameworks</a>.</p>
<p>You can download my example source code if it helps you understand how the whole thing works.</p>
<p>Download: <a onclick="_gaq.push(['_trackPageview','/downloads/jquery_example.zip']);" href="http://www.askaboutphp.com/wp-post-images/213/jquery_example.zip"><img src="http://www.askaboutphp.com/wp-post-images/213/jquery_example.png" border="0" alt="download icon" align="middle" /></a></p>
<p>And here&#8217;s a demo of what we&#8217;re trying to do.<br />
<iframe src="http://www.askaboutphp.com/examples/jquery_form/form.php" width="500" height="120"></iframe></p>
<p><b>The traditional way of doing form submissions</b><br />
Back in the &#8220;old&#8221; days, in order to perform a form submission, you need to do something like this.</p>
<p><img src="http://www.askaboutphp.com/wp-post-images/213/jquery-php-form1.png"></p>
<p>When you click the &#8220;Submit&#8221; button on <code>form.php</code>, the web server knows to send the name and email values using the POST method to the php script called <code>process.php</code>. The web server will load <code>process.php</code> page and the PHP script will start to read in the form values posted from <code>form.php</code>, maybe perform some validation to check that the information received is correct, then perhaps do something with the form values such as storing it in a database or sending it via email and output a &#8220;thank you&#8221; message.</p>
<p>What if something goes wrong, for example, the person submitting the form wrote in a badly-formed email address, the validation at <code>process.php</code> will throw a nicely worded error message and stop running the rest of the script. The person then would need to go back to the form to correct the email address and he would submit the form again.</p>
<p>This might have been an OK way of doing things 10 years ago, but in this era of Facebook and Twitter, that&#8217;s a pretty antiquated and tedious method.</p>
<p><b>The jQuery way of doing form submissions</b><br />
Here&#8217;s the jQuery way, which is made possible by something called AJAX (which stands for <a href="http://webdesign.about.com/od/ajax/a/aa101705.htm">Asynchronous JavaScript and XML</a>). AJAX is bascially the technology that makes it possible to exchange data with a server, and update parts of a web page &#8211; without reloading the whole page. jQuery includes the necessary functions to implement AJAX into your web application.</p>
<p><img src="http://www.askaboutphp.com/wp-post-images/213/jquery-php-form2.png"></p>
<p>The diagram looks quite similar to the &#8220;old&#8221; way of doing things, except that <code>process.php</code> is now represented by a &#8216;circle&#8217;. This is because process.php is now hidden for lack of a better word. In the traditional method, when you click the submit, your browser will load <code>process.php</code> and it will jump from <code>form.php</code> to <code>process.php</code>. </p>
<p>With the jQuery/AJAX way, <code>process.php</code> becomes a background process which the web server will call on, and when you click on the &#8220;Submit&#8221; button, <code>form.php</code> pushes the data to <code>process.php</code>, but your browser will remain showing <code>form.php</code>. Once the <code>process.php</code> has done what it needs to, it will return to <code>form.php</code> some output to be displayed.</p>
<p>Here&#8217;s the source code for <code>form.php</code></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt; 
&lt;html&gt;
&lt;head&gt;
	&lt;title&gt;JQuery Form Example&lt;/title&gt; 
	&lt;script type=&quot;text/javascript&quot; src=&quot;http://code.jquery.com/jquery-1.4.2.min.js&quot;&gt;&lt;/script&gt;
	&lt;script type=&quot;text/javascript&quot; src=&quot;http://ajax.microsoft.com/ajax/jquery.validate/1.7/jquery.validate.min.js&quot;&gt;&lt;/script&gt;
	&lt;script type=&quot;text/javascript&quot;&gt;
	$(document).ready(function(){
		$(&quot;#myform&quot;).validate({
			debug: false,
			rules: {
				name: &quot;required&quot;,
				email: {
					required: true,
					email: true
				}
			},
			messages: {
				name: &quot;Please let us know who you are.&quot;,
				email: &quot;A valid email will help us get in touch with you.&quot;,
			},
			submitHandler: function(form) {
				// do other stuff for a valid form
				$.post('process.php', $(&quot;#myform&quot;).serialize(), function(data) {
					$('#results').html(data);
				});
			}
		});
	});
	&lt;/script&gt;
	&lt;style&gt;
	label.error { width: 250px; display: inline; color: red;}
	&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form name=&quot;myform&quot; id=&quot;myform&quot; action=&quot;&quot; method=&quot;POST&quot;&gt;  
&lt;!-- The Name form field --&gt;
    &lt;label for=&quot;name&quot; id=&quot;name_label&quot;&gt;Name&lt;/label&gt;  
    &lt;input type=&quot;text&quot; name=&quot;name&quot; id=&quot;name&quot; size=&quot;30&quot; value=&quot;&quot;/&gt;  
	&lt;br&gt;
&lt;!-- The Email form field --&gt;
    &lt;label for=&quot;email&quot; id=&quot;email_label&quot;&gt;Email&lt;/label&gt;  
    &lt;input type=&quot;text&quot; name=&quot;email&quot; id=&quot;email&quot; size=&quot;30&quot; value=&quot;&quot;/&gt; 
	&lt;br&gt;
&lt;!-- The Submit button --&gt;
	&lt;input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Submit&quot;&gt; 
&lt;/form&gt;
&lt;!-- We will output the results from process.php here --&gt;
&lt;div id=&quot;results&quot;&gt;&lt;div&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></td></tr></table></div>

<p>and the source code for <code>process.php</code></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
	<span style="color: #b1b100;">print</span> <span style="color: #0000ff;">&quot;Form submitted successfully: &lt;br&gt;Your name is &lt;b&gt;&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;/b&gt; and your email is &lt;b&gt;&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'email'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;/b&gt;&lt;br&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p><b>Here&#8217;s what&#8217;s happening</b><br />
I included jQuery&#8217;s form validation because it provides a more real world example. Usually, most sites will perform processes like form validation at the client-side before the form is submitted. It&#8217;s just more intuitive and a much better user experience. In fact if you can offload as much workload as you can to the browser, it will greatly reduce the hit on your server.</p>
<p>So here&#8217;s a quick high-level walk through what&#8217;s happening on the jQuery part. </p>
<p><img src="http://www.askaboutphp.com/wp-post-images/213/jquery-php-source1.png"></p>
<p>I call the validate method (make sure you have included the jquery validate plugin &#8211; see <code>form.php</code> Line 6) on my form #myform, and proceed to define what to validate. In this case, we&#8217;re checking that the name field is &#8216;required&#8217;, and we&#8217;re checking the email field is not only &#8216;required&#8217;, but must be an email format. I also define the custom error message I want to display if the validation fails.</p>
<p>Lastly, the submitHandler is what gets processed if the validation is successful. In this case, I&#8217;m doing an ajax post method, sending my form data to process.php, and get back any results as HTML data which will be displayed into #results.</p>
<p>Over at <code>process.php</code>, I&#8217;m simply printing out the data received to show you the information was sent and returned.</p>
<p>That&#8217;s basically it. If you want to know more, do check out the <a href="http://jquery.com/">jQuery</a> website to get more details on how to do <a href="http://api.jquery.com/category/ajax/">AJAX</a> and <a href="http://docs.jquery.com/Plugins/Validation">Validation</a>.</p>
<p>Feel free to comment your thoughts or suggestions.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.askaboutphp.com/213/php-and-jquery-submit-a-form-without-refreshing-the-page.html/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>PHP Basics: Sorting Arrays</title>
		<link>http://www.askaboutphp.com/191/php-basics-sorting-arrays.html</link>
		<comments>http://www.askaboutphp.com/191/php-basics-sorting-arrays.html#comments</comments>
		<pubDate>Sun, 14 Mar 2010 14:56:44 +0000</pubDate>
		<dc:creator>Eldee</dc:creator>
				<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[arrays]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.askaboutphp.com/?p=191</guid>
		<description><![CDATA[Arrays are wonderful things. I can&#8217;t imagine using a programming language that does not have support for arrays. For newbies, arrays might be something that&#8217;s difficult to understand at first, but once you get used to it, it&#8217;s almost impossible to live without. Guaranteed!
If you are not familiar with what arrays are, visit the PHP [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.askaboutphp.com/wp-post-images/191/sorting-arrays.jpg" align="right">Arrays are wonderful things. I can&#8217;t imagine using a programming language that does not have support for arrays. For newbies, arrays might be something that&#8217;s difficult to understand at first, but once you get used to it, it&#8217;s almost impossible to live without. Guaranteed!</p>
<p>If you are not familiar with what arrays are, visit the <a href="http://php.net/manual/en/language.types.array.php">PHP manual</a>. Alternatively, there&#8217;s a <a href="http://www.developer.com/lang/php/article.php/977371/Arrays-in-PHP.htm">introduction to arrays</a> at developer.com.</p>
<p>In this post, I&#8217;m not going to cover the same ground but instead touch on sorting of arrays.</p>
<p><span id="more-191"></span><br />
PHP provides a number of function which you can use to <a href="http://sg2.php.net/manual/en/array.sorting.php">sort arrays</a>. </p>
<p><b>Sorting by its values</b><br />
Sorting an array by its values is probably the most common way of performing a sort, and the most straighfoward. PHP provides the <code>sort()</code> function for doing this.</p>
<p>Example 1:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$nums</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">14</span><span style="color: #339933;">,</span> <span style="color:#800080;">3.2</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span> <span style="color:#800080;">1.3</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">sort</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$nums</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;">$nums</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* Outputs:
Array 
(
    [0] =&gt; -5
    [1] =&gt; 0
    [2] =&gt; 1.3
    [3] =&gt; 3.2
    [4] =&gt; 14
)
*/</span></pre></td></tr></table></div>

<p>Example 2:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$movies</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'A Fish Called Wanda'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'The Godfather'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Finding Nemo'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Star Wars'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Grease'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">sort</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$movies</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;">$movies</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* Outputs:
Array
(
    [0] =&gt; A Fish Called Wanda
    [1] =&gt; Finding Nemo
    [2] =&gt; Grease
    [3] =&gt; Star Wars
    [4] =&gt; The Godfather
)
*/</span></pre></td></tr></table></div>

<p>Example 3:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$movies</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Comedy'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'A Fish Called Wanda'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Drama'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'The Godfather'</span><span style="color: #339933;">,</span> 
		<span style="color: #0000ff;">'Animation'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Finding Nemo'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Sci-Fi'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Star Wars'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Musical'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Grease'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">sort</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$movies</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;">$movies</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* Outputs:
Array
(
    [0] =&gt; A Fish Called Wanda
    [1] =&gt; Finding Nemo
    [2] =&gt; Grease
    [3] =&gt; Star Wars
    [4] =&gt; The Godfather
)
*/</span></pre></td></tr></table></div>

<p>So, it all looks pretty straightfoward right? The arrays are sorted in ascending order. But what has happened to the keys for the last example for $movies? <code>sort()</code> will reset all the indexes, so you have lost your original keys. To get around this you  can use the <code>asort()</code> function instead, which works the same as sort except that it preserves the original key/value mapping.</p>
<p>Example 4:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$movies</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Comedy'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'A Fish Called Wanda'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Drama'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'The Godfather'</span><span style="color: #339933;">,</span> 
		<span style="color: #0000ff;">'Animation'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Finding Nemo'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Sci-Fi'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Star Wars'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Musical'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Grease'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">asort</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$movies</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;">$movies</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* Outputs:
Array
(
    [Comedy] =&gt; A Fish Called Wanda
    [Animation] =&gt; Finding Nemo
    [Musical] =&gt; Grease
    [Sci-Fi] =&gt; Star Wars
    [Drama] =&gt; The Godfather
)
*/</span></pre></td></tr></table></div>

<p>So how about sorting in descending order? Well, the functions to in this case are <code>rsort()</code> and <code>arsort()</code></p>
<p>Example 5:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$movies</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Comedy'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'A Fish Called Wanda'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Drama'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'The Godfather'</span><span style="color: #339933;">,</span> 
		<span style="color: #0000ff;">'Animation'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Finding Nemo'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Sci-Fi'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Star Wars'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Musical'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Grease'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">arsort</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$movies</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;">$movies</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* Outputs:
Array
(
    [Drama] =&gt; The Godfather
    [Sci-Fi] =&gt; Star Wars
    [Musical] =&gt; Grease
    [Animation] =&gt; Finding Nemo
    [Comedy] =&gt; A Fish Called Wanda
)
*/</span></pre></td></tr></table></div>

<p><b>Sorting by its keys</b><br />
For associative arrays, it is just as important to be able to sort the array by its keys. The <code>ksort()</code> and <code>krsort()</code> functions does exactly that.</p>
<p>Example 6:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$movies</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Comedy'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'A Fish Called Wanda'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Drama'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'The Godfather'</span><span style="color: #339933;">,</span> 
		<span style="color: #0000ff;">'Animation'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Finding Nemo'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Sci-Fi'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Star Wars'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Musical'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Grease'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">ksort</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$movies</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;">$movies</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* Outputs:
Array
(
    [Animation] =&gt; Finding Nemo
    [Comedy] =&gt; A Fish Called Wanda
    [Drama] =&gt; The Godfather
    [Musical] =&gt; Grease
    [Sci-Fi] =&gt; Star Wars
)
*/</span></pre></td></tr></table></div>

<p>Example 7:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$movies</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Comedy'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'A Fish Called Wanda'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Drama'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'The Godfather'</span><span style="color: #339933;">,</span> 
		<span style="color: #0000ff;">'Animation'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Finding Nemo'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Sci-Fi'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Star Wars'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Musical'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Grease'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">krsort</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$movies</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;">$movies</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* Outputs:
Array
(
    [Sci-Fi] =&gt; Star Wars
    [Musical] =&gt; Grease
    [Drama] =&gt; The Godfather
    [Comedy] =&gt; A Fish Called Wanda
    [Animation] =&gt; Finding Nemo
)
*/</span></pre></td></tr></table></div>

<p><b>Sorting using custom comparison functions</b><br />
It is also possible to create your own sort criteria and apply it to an array. Three functions are provided for you to do just that &#8211; <code>usort()</code>, <code>uasort()</code> and <code>uksort()</code>. The following example, I&#8217;m going to sort my fruits array by arranging the quantity with odd numbers first followed by even numbers, in descending order. I&#8217;m going to use uasort() because more often than not, you want to preserve your keys, which <code>usort()</code> will not do.</p>
<p>Example 8:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> oddfirst<span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">,</span> <span style="color: #000088;">$j</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$value</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;"># default return value (do nothing)
</span>	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">%</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$value</span><span style="color: #339933;">--;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$j</span> <span style="color: #339933;">%</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$value</span><span style="color: #339933;">++;</span>	
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$value</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$j</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$value</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$fruits</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'apples'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">37</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'mangoes'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">72</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'oranges'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">92</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'bananas'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">15</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'grapes'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">52</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'kiwis'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">43</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">uasort</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fruits</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'oddfirst'</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;">$fruits</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* Outputs:
Array
(
    [bananas] =&gt; 15
    [apples] =&gt; 37
    [kiwis] =&gt; 43
    [grapes] =&gt; 52
    [mangoes] =&gt; 72
    [oranges] =&gt; 92
)
*/</span></pre></td></tr></table></div>

<p>So how does this actually works? The <code>uasort()</code> function takes in 2 arguments &#8211; the first argument defines the array to be sorted, the second argument states the name of the function to call. In my example, that would be &#8216;oddfirst&#8217;.</p>
<p>This comparison function needs to take in 2 parameters (in my case $i and $j) and return one of three possible values &#8211; a negative number, a positive number and zero. In most cases, returning -1, 0, 1, would be sufficient. This number tells <code>uasort()</code> how $i compares with $j as follows.</p>
<ul>
<li>If a negative number (-1) is returned, then $i is less than $j</li>
<li>If a positive number (1) is returned, then $i is greater than $j</li>
<li>If zero (0) is returned, then $i is equal to $j</li>
</ul>
<p>For the oddfirst() function, my algorithm goes like this:</p>
<ul>
<li>If $i is odd, $j is even then return a negative number ($value=-1) because I want $i to be sorted first</li>
<li>If $i is even, $j is odd then return a positive number ($value=1) because I want $j to be sorted first</li>
<li>if both $i and $j are even or both are odd, then $value is zero, and I need to make a further comparison to sort the numbers in ascending order. Hence, $j < $i tests which number is greater and return $value=1 when $i is greater than $j.</li>
</ul>
<p><b>Sorting multi-dimensional arrays</b><br />
So far, we&#8217;ve only dealt with single dimensional arrays. What about multi-dimensional arrays? PHP provides only one function to this multiple array sorting &#8211; <code>array_multisort()</code>. The function takes in a number of arrays as arguments, each followed optionally by one or more sort flags. Let&#8217;s take a look at the following example of movies with genre and global box office revenue, and attempt to sort the movies by revenue.</p>
<p>Example 9:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$moviedata</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
	<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'movie'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Home Alone'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'genre'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Comedy'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'revenue'</span><span style="color: #339933;">=&gt;</span><span style="color: #cc66cc;">285</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'movie'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'The Godfather'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'genre'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Drama'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'revenue'</span><span style="color: #339933;">=&gt;</span><span style="color: #cc66cc;">268</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'movie'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Finding Nemo'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'genre'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Animation'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'revenue'</span><span style="color: #339933;">=&gt;</span><span style="color: #cc66cc;">866</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'movie'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Star Wars'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'genre'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Sci-Fi'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'revenue'</span><span style="color: #339933;">=&gt;</span><span style="color: #cc66cc;">797</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'movie'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Grease'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'genre'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Musical'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'revenue'</span><span style="color: #339933;">=&gt;</span><span style="color: #cc66cc;">387</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'movie'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Ghostbusters'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'genre'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Comedy'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'revenue'</span><span style="color: #339933;">=&gt;</span><span style="color: #cc66cc;">238</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'movie'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Titanic'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'genre'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Drama'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'revenue'</span><span style="color: #339933;">=&gt;</span><span style="color: #cc66cc;">600</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$cols</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: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$moviedata</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</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: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cols</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
		<span style="color: #000088;">$cols</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$key</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;">$cols</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$value</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$cols</span><span style="color: #339933;">;</span>
<span style="color: #990000;">array_multisort</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'revenue'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'movie'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'genre'</span><span style="color: #009900;">&#93;</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;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* Outputs:
Array
(
    [movie] =&gt; Array
        (
            [0] =&gt; Ghostbusters
            [1] =&gt; The Godfather
            [2] =&gt; Home Alone
            [3] =&gt; Grease
            [4] =&gt; Titanic
            [5] =&gt; Star Wars
            [6] =&gt; Finding Nemo
        )
&nbsp;
    [genre] =&gt; Array
        (
            [0] =&gt; Comedy
            [1] =&gt; Drama
            [2] =&gt; Comedy
            [3] =&gt; Musical
            [4] =&gt; Drama
            [5] =&gt; Sci-Fi
            [6] =&gt; Animation
        )
&nbsp;
    [revenue] =&gt; Array
        (
            [0] =&gt; 238
            [1] =&gt; 268
            [2] =&gt; 285
            [3] =&gt; 387
            [4] =&gt; 600
            [5] =&gt; 797
            [6] =&gt; 866
        )
&nbsp;
)
*/</span></pre></td></tr></table></div>

<p>To be effective, <code>array_multisort()</code> requires that multidimensional arrays be arranged in a columnar fashion, which is done by the foreach block at line 12 in the example. After that, just call <code>array_multisort()</code> to sort the movie data, I define which columns I want to sort first followed by the next and so on. </p>
<p>You may notice that revenue is sorted in ascending order. If you want sort by descending order, just call <code>array_multisort()</code> with sort flags like so:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">array_multisort</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'revenue'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> SORT_DESC<span style="color: #339933;">,</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'movie'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'genre'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Well, that&#8217;s it. I hope this post helps you get started with how to sort arrays. Remember to check out PHP&#8217;s page on <a href="http://sg2.php.net/manual/en/array.sorting.php">sorting arrays</a>. Feel free to comment and contribute your tips on sorting arrays.</p>
<p>QGYMRRM2NK7K</p>
]]></content:encoded>
			<wfw:commentRss>http://www.askaboutphp.com/191/php-basics-sorting-arrays.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Codeigniter: Handling errors</title>
		<link>http://www.askaboutphp.com/172/codeigniter-handling-errors.html</link>
		<comments>http://www.askaboutphp.com/172/codeigniter-handling-errors.html#comments</comments>
		<pubDate>Tue, 09 Feb 2010 04:19:53 +0000</pubDate>
		<dc:creator>Eldee</dc:creator>
				<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[codeigniter]]></category>
		<category><![CDATA[errors]]></category>

		<guid isPermaLink="false">http://www.askaboutphp.com/?p=172</guid>
		<description><![CDATA[As coders, I&#8217;m sure we all know the value of good error handling. So I thought a quick post about how Codeigniter deals with those pesky errors would be a good post. 
The way I see it, there are 2 types of errors we have to work with. The ones which are displayed out in [...]]]></description>
			<content:encoded><![CDATA[<p>As coders, I&#8217;m sure we all know the value of good error handling. So I thought a quick post about how Codeigniter deals with those pesky errors would be a good post. </p>
<p>The way I see it, there are 2 types of errors we have to work with. The ones which are displayed out in nice friendly text to your users, and the type which are hidden and shown only to you as the coder to figure out where something has gone wrong. CI provides the means to do both, thankfully.</p>
<p><span id="more-172"></span></p>
<p><strong>System/Application Errors</strong><br />
Let&#8217;s start with having a look at how CI deals with PHP errors. By default, on a clean install, CI will display ALL php errors of all severity. So if you make a mistake in your PHP coding, you&#8217;ll see plenty of these&#8230;</p>
<p><img src="http://www.askaboutphp.com/wp-post-images/172/ci-error-1.png"></p>
<p>CI uses PHP&#8217;s <code>error_reporting()</code> function to define the level of error reporting, and you can find this in the main <code>index.php</code> file in the root of your CI install. It&#8217;s the first function you see.</p>
<p><img src="http://www.askaboutphp.com/wp-post-images/172/ci-error-2.png"></p>
<p>Instead of E_ALL, you can change it to any of the <a href="http://sg2.php.net/manual/en/errorfunc.constants.php">predefined error constant</a> that PHP understands to suit your needs. Obviously, once your site goes live, you should change from E_ALL to E_ERROR to show only messages for fatal run time problems. This will hide all php errors except those that will halt your script execution.</p>
<p>So, if the error messages are hidden, how else can we tell if something is not working right? Well, CI can writes out any errors it encounters into its log file. You can configure your CI log file by going to the <code>config.php</code> file in your <code>applications/config</code> directory. </p>
<p><img src="http://www.askaboutphp.com/wp-post-images/172/ci-error-3.png"></p>
<p>By default, the logs are turned off, so you should turn it on by setting the <code>$config['log_threshold']</code> variable. Once enabled, the logs will appear in the <code>system/logs</code> folder of your CI install. You can set the location of your log file if you wish to keep your logs elsewhere by defining a path for the <code>$config['log_path']</code> variable.</p>
<p>You may notice that the <code>$config['log_threshold']</code> allows a few settings.</p>
<ul>
<li> <strong>1 &#8211; ERROR</strong><br />This will log down all E_ALL level php generated errors, HTML status code errors (like 404s) and any user-defined errors.</li>
<li> <strong>2 &#8211; DEBUG</strong><br />In addition to ERROR messages, this will show messages which helps you to debug your CI application.</li>
<li> <strong>3 &#8211; INFO</strong><br />This level will show all ERROR &#038; DEBUG messages as well as any user-defined informational messages. CI doesn&#8217;t natively generate any INFO messages but you can do so in your application.</li>
<li> <strong>4 &#8211; All Messages</strong><br />This will show all log messages. I could not see any difference between this level and level 3 setting. If you do know a difference, post it into the comments.</li>
</ul>
<p>In addition CI&#8217;s natively generated messages, you can define your own messages by calling the <code>log_message()</code> function. You can call this anywhere in your code, and you can set what level of messaging (error, debug, or info) you want logged. (<a href="http://codeigniter.com/user_guide/general/errors.html" target="_blank">See CI&#8217;s guide on how to use the log_message() function</a>.)</p>
<p><strong>Displaying Errors</strong><br />
Next, let&#8217;s look at what options are available if you want to display error messages to your user. CI provides 2 functions for this purpose &#8211; <code>show_error()</code> and <code>show_404()</code>. (<a href="http://codeigniter.com/user_guide/general/errors.html" target="_blank">See CI&#8217;s guide on how to use the show_error() and show_404() functions</a>.)</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>SHOW_ERROR<span style="color: #339933;">==</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	show_error<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'This is where your error message will appear surrounded by this default template.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>When you call <code>show_error()</code>, you will generate page that looks like this. The layout can changed by modifying this file <code>application/errors/error_general.php</code>)</p>
<p><img src="http://www.askaboutphp.com/wp-post-images/172/ci-error-5.png"></p>
<p>show_404() works in a similar way, except it will display the <code>application/errors/error_404.php</code> layout and will throw a HTTP status code of 404.</p>
<p>Both of these functions are breakpoints and will stop the execution of your script, so any functions that follows these will not be processed.</p>
<p>That&#8217;s it. Feel free to comment.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.askaboutphp.com/172/codeigniter-handling-errors.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Codeigniter: Intergrating OpenFlashCharts</title>
		<link>http://www.askaboutphp.com/113/codeigniter-intergrating-openflashcharts.html</link>
		<comments>http://www.askaboutphp.com/113/codeigniter-intergrating-openflashcharts.html#comments</comments>
		<pubDate>Fri, 06 Nov 2009 05:12:34 +0000</pubDate>
		<dc:creator>Eldee</dc:creator>
				<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[codeigniter]]></category>
		<category><![CDATA[openflashcharts]]></category>

		<guid isPermaLink="false">http://www.askaboutphp.com/?p=113</guid>
		<description><![CDATA[Yes I know, it has been some time since I last posted anything on the site. Things have just been crazy at work, trying to get up to speed on using Pentaho for a major project. In fact, I might start posting some Pentaho related topics in the future. There&#8217;s definitely a need for more [...]]]></description>
			<content:encoded><![CDATA[<p>Yes I know, it has been some time since I last posted anything on the site. Things have just been crazy at work, trying to get up to speed on using <a href="http://community.pentaho.com/" target="_blank">Pentaho</a> for a major project. In fact, I might start posting some Pentaho related topics in the future. There&#8217;s definitely a need for more help and guides on using Pentaho for beginners.</p>
<p>Anyway, one of the things on the project was to pull data from Pentaho and display it inside OpenFlashCharts on a CI platform. If you don&#8217;t know what <a href="http://teethgrinder.co.uk/open-flash-chart-2/" target="_blank">OpenFlashCharts</a> is, go visit the website. It&#8217;s a pretty awesome kit.</p>
<p>There&#8217;s been quite a bit of chatter on the net about integrating CI with OpenFlashCharts, but ever since version 2 came out there have been more questions about how to do it.<br />
<span id="more-113"></span><br />
In the new version of OpenFlashCharts, it uses the JSON format to describe what type of chart to render in the flash object. OFC comes with a bunch of libraries (in various programming languages) which will generate the JSON format for the flash object. For PHP, the kit comes with the generic PHP version and a PHP5 version.</p>
<p>Somebody by the name of Thomas did managed to stitch <a href="http://codeigniter.com/wiki/Open_Flash_Chart_2/" target="_blank">OpenFlashCharts 2 with CI</a> and he posted it up on the CI wiki. So kudos to Thomas (whoever you are) for making my life easier. If you like you can download his library and give it a spin.</p>
<p>For Thomas, he used the PHP5 libraries of OpenFlashCharts, which unfortunately is a bit incomplete. I wasn&#8217;t able to generate more advance graphs such as Hollow Areas, Dotted lines etc. The flash object gave me an &#8216;infinity&#8217; error. After some investigation, I found that the OFC PHP5 libraries are not generating the same JSON as the generic PHP version. Taking a page out of his book, I modified his library to work with the generic PHP version instead. </p>
<p>One advantage of doing this is that suddenly, you can apply all the tutorial codes on the OFC website because they were written for the generic PHP library and not the PHP5 version.</p>
<p>Download: <a onclick="_gaq.push(['_trackPageview','/downloads/ci-ofc2.zip']);" href="http://www.askaboutphp.com/wp-post-images/113/ci-ofc2.zip"><img src="http://www.askaboutphp.com/wp-post-images/113/ci-ofc2-download.gif" border="0" alt="download icon" align="middle" /></a></p>
<p>So, download the zip file and extract the files to the relevant folders of your CI install. The assets folder should go to wherever you put your stuff like images and javascript files. Just make sure you change the view to reflect the correct path. I put my assets in the root of my webserver folder, and I access my CI through <code>http://dev.ci/</code>.</p>
<p>When I run the <code>http://dev.ci/charts</code>, I would get this graph with 3 data lines.</p>
<p><img src="http://www.askaboutphp.com/wp-post-images/113/ofc2-test-chart.gif" alt="OFC Test Chart" /></p>
<p><strong>So how does it all work? </strong><br />
The OFC library, <code>php-ofc-library</code> and the <code>OpenFlashChartLib.php</code> can now be accessed by calling the load library call.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">load</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">library</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'OpenFlashChartLib'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'OFCL'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>If you look inside the view folder for <code>chart_view.php</code>, you can see where the OFC flash object is getting its JSON feed from, <code>http://dev.ci/charts/get_data</code>.</p>
<p>Inside <code>get_data()</code> function of the charts controller, it&#8217;s basically the same code as the tutorial at <a href="http://teethgrinder.co.uk/open-flash-chart-2/data-lines-2.php">http://teethgrinder.co.uk/open-flash-chart-2/data-lines-2.php</a> </p>
<p>The difference here is that instead of calling the</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$chart</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> open_flash_chart<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>I&#8217;m now doing</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$chart</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">OFCL</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">create</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'open_flash_chart'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>to perform the same instantiation but through the OpenFlashChartLib (OFCL) library.</p>
<p>So, just change all the object instantiation call accordingly, and the graph should show up nice and neat.</p>
<p>Here&#8217;s Dilbert to close out&#8230;</p>
<p><img src="http://www.askaboutphp.com/wp-post-images/113/dilbert-pie-chart.png" alt="Dilbert Pie Chart" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.askaboutphp.com/113/codeigniter-intergrating-openflashcharts.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Codeigniter: Setting up multiple sites on one install</title>
		<link>http://www.askaboutphp.com/88/codeigniter-setting-up-multiple-sites-on-one-install.html</link>
		<comments>http://www.askaboutphp.com/88/codeigniter-setting-up-multiple-sites-on-one-install.html#comments</comments>
		<pubDate>Wed, 19 Aug 2009 08:19:40 +0000</pubDate>
		<dc:creator>Eldee</dc:creator>
				<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[Apache]]></category>
		<category><![CDATA[codeigniter]]></category>
		<category><![CDATA[website]]></category>

		<guid isPermaLink="false">http://www.askaboutphp.com/?p=88</guid>
		<description><![CDATA[It&#8217;s been awhile since I last posted about Codeigniter. In this post, I&#8217;m going to walkthrough how I setup a CI install that can support multiple sites. This is by no means the only way to do it, but it&#8217;s what works for me.
CI&#8217;s default folder structure is really geared towards one site domain, and [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s been awhile since I <a href="http://www.askaboutphp.com/tutorials/58/codeigniter-mixing-segment-based-url-with-querystrings.html">last posted about Codeigniter</a>. In this post, I&#8217;m going to walkthrough how I setup a CI install that can support multiple sites. This is by no means the only way to do it, but it&#8217;s what works for me.</p>
<p>CI&#8217;s default folder structure is really geared towards one site domain, and having both the <code>application</code> folder and the <code>system</code> folder visible to the browser is not exactly a secure approach.</p>
<p>By configuring your install to support multiple sites, not only do you make the updating of the CI core easier, but also make the CI install more secure as a whole. So without delay, let&#8217;s get started&#8230;</p>
<p><span id="more-88"></span></p>
<p>In this walkthrough, I&#8217;m setting up my multiple CI sites on my local XAMPP install, which is on my <code>M:\ drive</code>. Your Apache setup might be different, so do take note of my settings and adjust according to your own web server environment.</p>
<p>I&#8217;m planning to setup two sites just for illustration &#8211; <code>http://dev.ci/</code> and <code>http://live.ci/</code>.</p>
<p><strong>Codeigniter default folder structure</strong><br />
When you first unzip the downloaded CI file, you would get something like this:</p>
<p><img src="http://www.askaboutphp.com/wp-post-images/88/ci-sites-1.jpg"></p>
<p>You would have set your Apache&#8217;s configuration to point to your CI folder (which is <code>M:\xampplite\CodeIgniter_1.7.1</code> for me) as the document root. Once you hit the site in your browser, you would get the standard welcome page.</p>
<p>If you have setup the default CI before, you may know that within the <code>system</code> folder are a bunch of core folders which you don&#8217;t normally need to touch and the <code>application</code> folder which will house all your codes for your site. You may also have noticed the <code>index.php</code> file in the root of the CI directory, which you didn&#8217;t really need to touch before but will become crucial in setting up multiple sites.</p>
<p><strong>Adjusting the application folders</strong><br />
First thing I do is to move the <code>application</code> folder out of the <code>system</code> folder to the same level as the <code>system</code> folder.</p>
<p><img src="http://www.askaboutphp.com/wp-post-images/88/ci-sites-2.jpg"></p>
<p>Since I&#8217;m setting up to run two distinct sites sharing the same CI core, I will make another copy of the <code>application</code> folder and rename both folders to reflect the different sites.</p>
<p><img src="http://www.askaboutphp.com/wp-post-images/88/ci-sites-3.jpg"></p>
<p><strong>Create the web root folders</strong><br />
Next, we would need two folders which will be the website&#8217;s root folders. This is how I setup mine.</p>
<p><img src="http://www.askaboutphp.com/wp-post-images/88/ci-sites-4.jpg"></p>
<p>Make copies of the <code>index.php</code> file located in the CI&#8217;s root folder (ie <code>M:\xampplite\CodeIgniter_1.7.1</code>) into each of the htdocs folder. In this case, I would have one <code>index.php</code> in each of my <code>htdocs_ci_dev</code> and <code>htdocs_ci_live</code> folders.</p>
<p>Open <code>index.php</code> file for each site and edit the following lines.</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">At Line 26:
	$system_folder = &quot;system&quot;;
Change to 
	$system_folder = &quot;../system&quot;;
&nbsp;
At Line 43:
	$application_folder = &quot;application&quot;;
Change to 
	$application_folder = &quot;../app_ci_dev&quot;; (for the htdocs_ci_dev)
or 
	$application_folder = &quot;../app_ci_live&quot;; (for the htdocs_ci_live)</pre></div></div>

<p><strong>Configure Apache virtual hosts</strong><br />
Once that&#8217;s done, the last thing to do is to setup the Apache virtual host configuration of my XAMPP install. </p>
<p>My Apache virtual host configuration looks like this:</p>
<p><img src="http://www.askaboutphp.com/wp-post-images/88/ci-sites-5.png"></p>
<p>Don&#8217;t forget to edit your local hosts file so that your system recognizes the dev.ci and live.ci domains. </p>
<p>Once you fire up your Apache web server, you should be able to load either site accordingly (*fingers crossed*)</p>
<p><strong>Renaming the <code>system</code> folder</strong><br />
If you want to go further you can rename the <code>system</code> folder to reflect the different CI versions. This way, when a new version of the CI core is released, you can configure your sites easily by just changing <code>$system_folder</code> parameter in the index.php file (Line 26) for each site.</p>
<p><img src="http://www.askaboutphp.com/wp-post-images/88/ci-sites-6.jpg"></p>
<p><strong>Conclusion</strong><br />
I think you will agree that with this setup, you can easily expand the CI install to more sites. There is also no doubt that this is more secure than the default CI setup, as the only folders which are exposed to the browser are the htdocs folders &#8211; all the <code>application</code> and <code>system</code> folders are no longer accessible through the web server. </p>
<p>Of course, this is not the only way you can configure CI for multiple sites/applications, check out some of these alternatives:<br />
- <a href="http://codeigniter.com/wiki/Multiple_Applications/" target="_blank">CodeIgniter Wiki &#8211; Multiple Applications</a><br />
- <a href="http://www.michaelwales.com/codeigniter/secure-application-starting-point" target="_blank">Michael Wales &#8211; CodeIgniter Advent: Day 1</a><br />
- <a href="http://www.codeofficer.com/blog/entry/multiple_codeigniter_sites_using_symlinks/" target="_blank">Multiple codeigniter sites, using symlinks</a></p>
<p>Please feel free to give your thoughts and feedback. Thanks!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.askaboutphp.com/88/codeigniter-setting-up-multiple-sites-on-one-install.html/feed</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>PHP Basics: Accessing Remote URLs using cURL</title>
		<link>http://www.askaboutphp.com/65/php-basics-accessing-remote-urls-using-curl.html</link>
		<comments>http://www.askaboutphp.com/65/php-basics-accessing-remote-urls-using-curl.html#comments</comments>
		<pubDate>Fri, 19 Jun 2009 08:40:52 +0000</pubDate>
		<dc:creator>Eldee</dc:creator>
				<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[curl]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.askaboutphp.com/?p=65</guid>
		<description><![CDATA[In this post, I&#8217;m going back to basics to talk about accessing remote URLs with cURL.
In PHP, there are actually four ways to access a remote URL &#8211; fopen() fsockopen(), cURL extensions and HTTP_Request class from the PEAR library. Now, choosing one way over another really depends on your needs for simplicity, control, and portability.

Generally, [...]]]></description>
			<content:encoded><![CDATA[<p>In this post, I&#8217;m going back to basics to talk about accessing remote URLs with cURL.</p>
<p>In PHP, there are actually four ways to access a remote URL &#8211; <code>fopen()</code> <code>fsockopen()</code>, cURL extensions and <code>HTTP_Request</code> class from the PEAR library. Now, choosing one way over another really depends on your needs for simplicity, control, and portability.<br />
<span id="more-65"></span></p>
<p>Generally, I prefer using cURL simply because it&#8217;s easy to understand and it&#8217;s really powerful. cURL supports a number of different protocols and gives your access to the response headers. It even has the capability to do parallel access if you call the &#8216;curl_multi&#8217; group of functions (I&#8217;ll save this for another post one day).</p>
<p>To use cURL, you need to make sure that your PHP install has the library enabled. Read this posting on <a href="http://www.wallpaperama.com/forums/how-to-find-out-if-php-is-compiled-with-curl-extension-installed-enabled-t1576.html" target="_blank">how to enable cURL for your PHP</a>.</p>
<p><strong>Fetching a URL with the GET Method</strong><br />
The simplest way to start using cURL is to fetch a URL with the GET method.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_init</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'http://www.askaboutphp.com/robots.txt'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #339933;">,</span> CURLOPT_RETURNTRANSFER<span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$page</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_exec</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>So what is going on? The simple example above really maps out 4 stages of using cURL. You start by calling the <code>curl_init()</code> to get a handle on the URL you wan to access. Then, you set some options using <code>curl_setopt()</code>.After that, you execute what you want to do with <code>curl_exec()</code>. Lastly, you close the handle using <code>curl_close()</code>.</p>
<p>The control of the curl functions is all to do with what options you set using <code>curl_setopt()</code>. PHP lists out all the options available at <a href="http://sg.php.net/manual/en/function.curl-setopt.php"><code>curl_setopt()</code></a> page. Frankly, this is probably the hardest part about using the cURL library &#8211; understanding what all these options do. It was pretty daunting when I first started using.</p>
<p>Following on, here are some of the more commonly used (at least by me anyway) options for cURL.</p>
<p><strong>Fetching a protected URL with the GET Method</strong><br />
To do this, you need to set the CURLOPT_USERPWD option, like so:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_init</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'http://www.askaboutphp.com/protected-page.html'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #339933;">,</span> CURLOPT_RETURNTRANSFER<span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #339933;">,</span> CURLOPT_USERPWD<span style="color: #339933;">,</span> <span style="color: #0000ff;">'askme:anything'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #000088;">$page</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_exec</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p><strong>Submitting data to a URL with the POST Method</strong><br />
In this case, you need to set the CURLOPT_POST and CURLOPT_POSTFIELDS option.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_init</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'http://www.askaboutphp.com/submit.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #339933;">,</span> CURLOPT_POST<span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #339933;">,</span> CURLOPT_POSTFIELDS<span style="color: #339933;">,</span> <span style="color: #0000ff;">'father=dad&amp;mother=mum'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #339933;">,</span> CURLOPT_RETURNTRANSFER<span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_exec</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>The parameters you want to pass should be formatted like a querystring. If you are expecting some response after posting, then you need to enable the CURLOPT_RETURNTRANSFER option.</p>
<p><strong>Fetching a URL with Custom Headers</strong><br />
You can also retrieve URLs that requires specific headers to be sent with the request. You can do so with CURLOPT_HTTPHEADER option.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_init</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'http://www.askaboutphp.com/custom-header.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #339933;">,</span> CURLOPT_RETURNTRANSFER<span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #339933;">,</span> CURLOPT_HTTPHEADER<span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'My-Custom-Header: PHPISCOOL'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$page</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_exec</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>cURL has special options for setting the Referer and User-Agent request headers —<br />
CURLOPT_REFERER and CURLOPT_USERAGENT:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_init</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'http://www.askaboutphp.com/fetch.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #339933;">,</span> CURLOPT_VERBOSE<span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #339933;">,</span> CURLOPT_RETURNTRANSFER<span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #339933;">,</span> CURLOPT_REFERER<span style="color: #339933;">,</span> <span style="color: #0000ff;">'http://www.askaboutphp.com/form.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #339933;">,</span> CURLOPT_USERAGENT<span style="color: #339933;">,</span> <span style="color: #0000ff;">'CURL via PHP'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$page</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_exec</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>I&#8217;m going to stop here. If you&#8217;re new to cURL, hopefully this has been a good broad tutorial to get you started. In the Web 2.0 age, online services constantly pop-up to allow you to programmatically connect to them, cURL is the best tool you can have to help you access those services.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.askaboutphp.com/65/php-basics-accessing-remote-urls-using-curl.html/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Google Analytics API class for PHP</title>
		<link>http://www.askaboutphp.com/63/google-analytics-api-class-for-php.html</link>
		<comments>http://www.askaboutphp.com/63/google-analytics-api-class-for-php.html#comments</comments>
		<pubDate>Fri, 29 May 2009 09:13:56 +0000</pubDate>
		<dc:creator>Eldee</dc:creator>
				<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[google analytics]]></category>
		<category><![CDATA[php class]]></category>

		<guid isPermaLink="false">http://www.askaboutphp.com/?p=63</guid>
		<description><![CDATA[About a month back, Google opened the Google Analytics API service to all Analytics users. the API allows developers to integrate the GA reports into their own applications or websites, or even access the reports from a phone!
I&#8217;ve been thinking about how I can make use of this API to enhance the sites I&#8217;m working [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.askaboutphp.com/wp-post-images/63/ga-marbles.jpg" alt="GA marbles" align="left"/>About a month back, Google <a href="http://analytics.blogspot.com/2009/04/attention-developers-google-analytics.html" target="_blank">opened the Google Analytics API service</a> to all Analytics users. the API allows developers to integrate the GA reports into their own applications or websites, or even access the reports from a phone!</p>
<p>I&#8217;ve been thinking about how I can make use of this API to enhance the sites I&#8217;m working on. But before kicking off ideas, I had to find out how to access and use the API. I finally came up with a PHP class that will do all the grunt work of calling the API, you just need to supply your report&#8217;s parameter and the PHP class will return you an array of Analytics data.</p>
<p><span id="more-63"></span><br />
<strong>Getting Started</strong><br />
Ok, first thing&#8217;s first, you need to have Google Analytics (obviously) and login credentials to GA. Then you should download my PHP class available at the end of this post.</p>
<p>To access the API is basically a 2 step process. First is to authenticate against Google with your GA account credentials to get an authentication code. After that you can use the authentication code to access your list of website profiles in GA and extract out the data you want.</p>
<p><strong>Google Analytics PHP Class</strong><br />
Here&#8217;s how you access a report showing pageviews and visits, by date and country, filtered by &#8216;Australia&#8217;, sorted by pageviews in descending order.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">// include the Google Analytics PHP class</span>
<span style="color: #b1b100;">include</span> <span style="color: #0000ff;">&quot;googleanalytics.class.php&quot;</span><span style="color: #339933;">;</span>
try <span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// create an instance of the GoogleAnalytics class using your own Google {email} and {password}</span>
	<span style="color: #000088;">$ga</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> GoogleAnalytics<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'{email}'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'{password}'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// set the Google Analytics profile you want to access - format is 'ga:123456';</span>
	<span style="color: #000088;">$ga</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setProfile</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'{GA Profile ID}'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// set the date range we want for the report - format is YYYY-MM-DD</span>
	<span style="color: #000088;">$ga</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setDateRange</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'2009-04-01'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'2009-04-07'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// get the report for date and country filtered by Australia, showing pageviews and visits</span>
	<span style="color: #000088;">$report</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$ga</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getReport</span><span style="color: #009900;">&#40;</span>
		<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'dimensions'</span><span style="color: #339933;">=&gt;</span>urlencode<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ga:date,ga:country'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">'metrics'</span><span style="color: #339933;">=&gt;</span>urlencode<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ga:pageviews,ga:visits'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">'filters'</span><span style="color: #339933;">=&gt;</span>urlencode<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ga:country=@Australia'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">'sort'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'-ga:pageviews'</span>
			<span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">//print out the $report array</span>
	<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$report</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>Exception <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
	<span style="color: #b1b100;">print</span> <span style="color: #0000ff;">'Error: '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$e</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: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Hopefully, the code is quite self-explanatory. <code>setProfile()</code> requires your website profile id number. To get the profile id number, you go to your GA dashboard, click on &#8216;View Report&#8217; for your website, and look at the URL. There should be an &#8216;id=xxxxxx&#8217; in the URL. That is your id number. You have to specify it as &#8216;ga:xxxxxxx&#8217; in <code>setProfile()</code>.</p>
<p><img src="http://www.askaboutphp.com/wp-post-images/63/ga-url.jpg" alt="Profile ID"/> Alternatively, included within the class is a function called <code>getWebsiteProfiles()</code>, if you call this function, after authenticating, you will get an array of all the website profiles available under your account. You can find the id number from there also.</p>
<p>The only major function you need to really pay attention to is the <code>getReport()</code> function. This is where you craft the report parameters that you want results from.</p>
<p>If you&#8217;re familiar with GA, you will know that GA basically works with 2 field types &#8211; dimensions and metrics. If you&#8217;re not sure of what these these things are, I recommend reading the <a href="http://code.google.com/apis/analytics/docs/gdata/gdataReference.html#dimensionsAndMetrics" target="_blank">Google documentation</a> on them. Simplistically, you could think of metrics as the columns of your GA reports, and dimensions as the rows. </p>
<p>Google has also listed out the <a href="http://code.google.com/apis/analytics/docs/gdata/gdataReferenceDimensionsMetrics.html" target="_blank">available dimensions and metrics</a> you can use in the API.</p>
<p>With the <code>getReport()</code> function, you are basically doing the same thing as the Custom Reports feature of GA. You decide which dimension and metrics you want and the API will return you the data.</p>
<p>What you will get from <code>getReport()</code> is an array that looks something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">Array
(
    [20090401~~Australia] =&gt; Array
        (
            [ga:pageviews] =&gt; 6
            [ga:visits] =&gt; 3
        )
&nbsp;
    [20090402~~Australia] =&gt; Array
        (
            [ga:pageviews] =&gt; 4
            [ga:visits] =&gt; 3
        )
&nbsp;
    [20090407~~Australia] =&gt; Array
        (
            [ga:pageviews] =&gt; 4
            [ga:visits] =&gt; 4
        )
&nbsp;
    [20090403~~Australia] =&gt; Array
        (
            [ga:pageviews] =&gt; 3
            [ga:visits] =&gt; 3
        )
&nbsp;
    [20090405~~Australia] =&gt; Array
        (
            [ga:pageviews] =&gt; 3
            [ga:visits] =&gt; 3
        )
&nbsp;
    [20090406~~Australia] =&gt; Array
        (
            [ga:pageviews] =&gt; 3
            [ga:visits] =&gt; 3
        )
&nbsp;
    [20090404~~Australia] =&gt; Array
        (
            [ga:pageviews] =&gt; 2
            [ga:visits] =&gt; 2
        )
)</pre></div></div>

<p>To keep the array simple, I&#8217;ve basically pushed the dimensions as the 1st array dimension keys, separated by &#8216;~~&#8217; pattern so that you can parse the keys if you require. The metrics are listed in the sub-array, with the metric as the key and value of the metric.</p>
<p>Do know that not every dimension has a corresponding metric. Google has put up a compatibility chart for you to verify which <a href="http://code.google.com/apis/analytics/docs/gdata/gdataReferenceDimensionsMetrics.html#explore2Pair" target="_blank">combination of dimensions and metrics are valid</a>.</p>
<p>This class requires the use of the cURL function in PHP. The functions are normally enabled, but if its not, here a <a href="http://www.wallpaperama.com/forums/how-to-find-out-if-php-is-compiled-with-curl-extension-installed-enabled-t1576.html" target="_blank">guide on how you enable cURL in PHP</a>.</p>
<p>That&#8217;s basically it. Feel free to download the class and play with it. Do bear in mind that this is not a &#8216;production&#8217; ready class. <del datetime="2009-06-16T04:49:17+00:00">There&#8217;s hardly any validation and error checking and <code>die()</code> is not the best fail-safe method for the class to fail gracefully. When I have more time, I will look at improving the class.</del>. The class has been updated with some exception catching, so hopefully this will give more meaningful error messages for those of you facing problems.  </p>
<p>Download: <a onclick="_gaq.push(['_trackPageview','/downloads/googleanalytics.class.zip']);" href="http://www.askaboutphp.com/wp-post-images/63/googleanalytics.class.zip"><img src="http://www.askaboutphp.com/wp-post-images/63/icon-googleanalytics.jpg" border="0" alt="download icon" align="middle" /></a></p>
<p>Here are some more examples of reports you can create:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// what browsers visitors to your using?</span>
<span style="color: #000088;">$report</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$ga</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getReport</span><span style="color: #009900;">&#40;</span>
	<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'dimensions'</span><span style="color: #339933;">=&gt;</span>urlencode<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ga:browser'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'metrics'</span><span style="color: #339933;">=&gt;</span>urlencode<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ga:visits'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'sort'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'-ga:visits'</span>
		<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// which are your top landing pages and how long they spent on the page?</span>
<span style="color: #000088;">$report</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$ga</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getReport</span><span style="color: #009900;">&#40;</span>
	<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'dimensions'</span><span style="color: #339933;">=&gt;</span>urlencode<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ga:landingPagePath,ga:pageTitle'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'metrics'</span><span style="color: #339933;">=&gt;</span>urlencode<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ga:entrances,ga:timeOnPage'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'sort'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'-ga:entrances'</span>
		<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// which are your top internal search keywords by pageviews?</span>
<span style="color: #000088;">$report</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$ga</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getReport</span><span style="color: #009900;">&#40;</span>
	<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'dimensions'</span><span style="color: #339933;">=&gt;</span>urlencode<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ga:searchKeyword'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'metrics'</span><span style="color: #339933;">=&gt;</span>urlencode<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ga:pageview'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'sort'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'-ga:pageviews'</span>
		<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><strong>UPDATE 2009-07-15:</strong> Google has posted <a href="http://analytics.blogspot.com/2009/07/fun-and-helpful-updates-to-api.html" target="_blank">new updates to the Google Analytics API</a>. The changes includes new limit on the amount of data returned, and relaxed restrictions on combinations of dimensions and metrics.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.askaboutphp.com/63/google-analytics-api-class-for-php.html/feed</wfw:commentRss>
		<slash:comments>72</slash:comments>
		</item>
		<item>
		<title>PHP&#8217;s alternative syntax and why it&#8217;s useful</title>
		<link>http://www.askaboutphp.com/59/phps-alternative-syntax-and-why-its-useful.html</link>
		<comments>http://www.askaboutphp.com/59/phps-alternative-syntax-and-why-its-useful.html#comments</comments>
		<pubDate>Thu, 16 Apr 2009 06:34:17 +0000</pubDate>
		<dc:creator>Eldee</dc:creator>
				<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[codeigniter]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[syntax]]></category>

		<guid isPermaLink="false">http://www.askaboutphp.com/?p=59</guid>
		<description><![CDATA[PHP offers an alternative way to write their control structures for as long as I&#8217;ve remembered. It basically does away with the curly brackets and replaces the opening curly with a colon (:) and the closing with &#8216;end&#8217;-whatever. I have to be honest and say I&#8217;ve never really found a need to use the alternative [...]]]></description>
			<content:encoded><![CDATA[<p>PHP offers an <a href="http://www.php.net/alternative_syntax" target="_blank">alternative way to write their control structures</a> for as long as I&#8217;ve remembered. It basically does away with the curly brackets and replaces the opening curly with a colon (:) and the closing with &#8216;end&#8217;-whatever. I have to be honest and say I&#8217;ve never really found a need to use the alternative syntax, simply because I&#8217;m so used to using the curly braces after so many years of using PHP. Plus, it&#8217;s less typing!</p>
<p>But having spend time working on CodeIgniter projects, I have found myself adopting this alternative syntax when it comes to buidling the CI&#8217;s views templates. I appreciate that there is place in the PHP universe for this alternative coding style.</p>
<p><span id="more-59"></span><br />
The best time to use this alternative style is when you need to mix PHP codes with HTML. In CI, this normally happens in views. Have a look at the IF example below of a typical HTML table layout:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">table</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>Site Name<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>Site URL<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>Site Owner<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
<span style="color: #009900;">&lt;?php foreach <span style="color: #66cc66;">&#40;</span>$sites as $site<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> ?&gt;</span>
	<span style="color: #009900;">&lt;?php if <span style="color: #66cc66;">&#40;</span>$site<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'name'</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">==</span> <span style="color: #ff0000;">'AskAboutPHP'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> ?&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;&lt;?php echo $site<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'name'</span><span style="color: #66cc66;">&#93;</span>;?&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;&lt;?php echo $site<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'url'</span><span style="color: #66cc66;">&#93;</span>;?&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;&lt;?php echo $site<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'owner'</span><span style="color: #66cc66;">&#93;</span>;?&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
	<span style="color: #009900;">&lt;?php <span style="color: #66cc66;">&#125;</span> else <span style="color: #66cc66;">&#123;</span> ?&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span> - <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span> - <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span> - <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
	<span style="color: #009900;">&lt;?php <span style="color: #66cc66;">&#125;</span> ?&gt;</span>
<span style="color: #009900;">&lt;?php <span style="color: #66cc66;">&#125;</span> ?&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">table</span>&gt;</span></pre></td></tr></table></div>

<p>The same table can be written in the alternative style as:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">table</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>Site Name<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>Site URL<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span>Site Owner<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
<span style="color: #009900;">&lt;?php foreach <span style="color: #66cc66;">&#40;</span>$sites as $site<span style="color: #66cc66;">&#41;</span>: ?&gt;</span>
	<span style="color: #009900;">&lt;?php if <span style="color: #66cc66;">&#40;</span>$site<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'name'</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">==</span> <span style="color: #ff0000;">'AskAboutPHP'</span><span style="color: #66cc66;">&#41;</span> : ?&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;&lt;?php echo $site<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'name'</span><span style="color: #66cc66;">&#93;</span>;?&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;&lt;?php echo $site<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'url'</span><span style="color: #66cc66;">&#93;</span>;?&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;&lt;?php echo $site<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'owner'</span><span style="color: #66cc66;">&#93;</span>;?&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
	<span style="color: #009900;">&lt;?php else: ?&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span> - <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span> - <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span>&gt;</span> - <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
	<span style="color: #009900;">&lt;?php endif; ?&gt;</span>
<span style="color: #009900;">&lt;?php endforeach; ?&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">table</span>&gt;</span></pre></td></tr></table></div>

<p>Personally, I find the alternative syntax gels better with HTML even though it&#8217;s more verbose. But because this form is more verbose, if a HTML developer was to look at this, I would imagine the second example would make more sense and easier to read and understand than the first table.</p>
<p>This alternative form works for PHP&#8217;s &#8216;if&#8217;, &#8217;switch&#8217;, &#8216;while&#8217;, &#8216;for&#8217; and &#8216;foreach&#8217; control statements.</p>
<p>See <a href="http://blog.bluefur.com/2009/03/17/php-alternative-syntax-control-structures/" target="_blank">Everything PHP&#8217;s article on Alternative Syntax for Control Structures</a> for more examples.</p>
<p><strong>Alternative short form &#8216;if-else&#8217; statement</strong><br />
There is also another shorter way of writing the &#8216;if-else&#8217; statements which I also found very useful in my CI views. Take for example the following:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> 
<span style="color: #b1b100;">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;">$a</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #339933;">;</span>	
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>This can be written as:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$a</span> <span style="color: #339933;">=</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;">$a</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #009900; font-weight: bold;">TRUE</span> <span style="color: #339933;">:</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #339933;">;</span> 
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>The portion before the &#8216;?&#8217; is the condition, followed by the &#8216;true&#8217; outcome, then colon, then the &#8216;false&#8217; outcome.</p>
<p>This helps to keep the coding of &#8216;if-else&#8217; to the minimum, especially when you have to perform a lot of validation type check which could be a whole stack of &#8216;if-else&#8217; statements.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.askaboutphp.com/59/phps-alternative-syntax-and-why-its-useful.html/feed</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>Codeigniter: Mixing segment-based URL with querystrings</title>
		<link>http://www.askaboutphp.com/58/codeigniter-mixing-segment-based-url-with-querystrings.html</link>
		<comments>http://www.askaboutphp.com/58/codeigniter-mixing-segment-based-url-with-querystrings.html#comments</comments>
		<pubDate>Mon, 09 Mar 2009 09:58:16 +0000</pubDate>
		<dc:creator>Eldee</dc:creator>
				<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[codeigniter]]></category>
		<category><![CDATA[querystring]]></category>
		<category><![CDATA[segments]]></category>
		<category><![CDATA[url]]></category>

		<guid isPermaLink="false">http://www.askaboutphp.com/?p=58</guid>
		<description><![CDATA[Codeigniter, by default crafts it&#8217;s URL in a search engine friendly format. It uses a segment-based approach and does away with the all familiar querystring format that developers have been using for many years to pass parameters via the URL into their applications. 
Codeigniter does allow you to turn on the querystring capability, but that [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.codeigniter.com" target="_blank">Codeigniter</a>, by default crafts it&#8217;s URL in a search engine friendly format. It uses a segment-based approach and does away with the all familiar querystring format that developers have been using for many years to pass parameters via the URL into their applications. </p>
<p>Codeigniter does allow you to turn on the querystring capability, but that would mean you have to use a pure querystring approach, foregoing the segment-based approached.</p>
<p>So, is it possible to mix segments and querystring?<br />
<span id="more-58"></span><br />
Thankfully, Codeigniter can be configured to mix the segments and querystring, and all it takes are some tweaks in your <code>application/config.php</code> file and perhaps an additional line of code.</p>
<p>Say you would like to have a URL like so:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">example.com/product/search/?pname=test&amp;pid=2</pre></div></div>

<p>By default, Codeigniter would prefer you to craft the equivalent segmented URL, and it would probably look like this.</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">example.com/product/search/test/2</pre></div></div>

<p>One of the limitations of this is that the order of the segments becomes important. In your code, you have to remember that the segment after /search/ is the &#8216;pname&#8217; variable and the segment after that is the &#8216;pid&#8217; variable. You won&#8217;t have this issue if you used a query string.</p>
<p>In most cases, this is not really a show stopper. But there are times when you have no choice but to include querystrings into your URL. For example, a legacy application that interfaces with your application perhaps?</p>
<p>If you follow the (excellent) <a href="http://codeigniter.com/user_guide/general/urls.html"  target="_blank">Codeigniter user guide</a>, and you turn on the querystring capability , Codeigniter will now function with a URL like this:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">example.com/?c=product&amp;m=search&amp;pname=test&amp;pid=2</pre></div></div>

<p>Which is also not the most ideal.</p>
<p>This is what you can do if you need to have a mixed segment and querystring approach. You can enable this either globally for your whole application or just locally within a controller.</p>
<p><strong>Option 1: Mixing Globally</strong><br />
To enable this globally, go to your <code>application/config.php</code> file, and look for <code>$config['uri_protocol']</code> variable and change it to:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$config</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'uri_protocol'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;PATH_INFO&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p>After that, find the <code>$config['enable_query_strings']</code> variable and change it to:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$config</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'enable_query_strings'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #339933;">;</span></pre></div></div>

<p>Your application should now accept both segments and querystrings.</p>
<p><strong>Option 2: Mixing Locally</strong><br />
If you don&#8217;t want to enable this capability across your whole application, you can restrict it to only the controllers you want.</p>
<p>You can do this by going to the <code>application/config.php</code> file and switching <code>$config['enable_query_strings']</code> back to &#8216;FALSE&#8217;.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$config</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'enable_query_strings'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #339933;">;</span></pre></div></div>

<p>Leave the <code>$config['uri_protocol']</code> as PATH_INFO</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$config</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'uri_protocol'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;PATH_INFO&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p>and then you can add this line to your controller&#8217;s contructor method:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">parse_str</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'QUERY_STRING'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>So if you do a test now, only the controller with the above line of code will be able to accept mixed segment and querystring URLs.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.askaboutphp.com/58/codeigniter-mixing-segment-based-url-with-querystrings.html/feed</wfw:commentRss>
		<slash:comments>26</slash:comments>
		</item>
		<item>
		<title>Codeigniter: Helpers, Plugins and Libraries</title>
		<link>http://www.askaboutphp.com/51/codeigniter-helpers-plugins-and-libraries.html</link>
		<comments>http://www.askaboutphp.com/51/codeigniter-helpers-plugins-and-libraries.html#comments</comments>
		<pubDate>Wed, 03 Dec 2008 03:31:44 +0000</pubDate>
		<dc:creator>Eldee</dc:creator>
				<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[codeigniter]]></category>
		<category><![CDATA[helpers]]></category>
		<category><![CDATA[libraries]]></category>
		<category><![CDATA[plugins]]></category>

		<guid isPermaLink="false">http://www.askaboutphp.com/?p=51</guid>
		<description><![CDATA[Having used Codeigniter for a few months now, this framework is really turning out to be a joy. My last post, I talked about how to modify native CI Libraries.
With so many published PHP classes and functions, it would be a shame if we couldn&#8217;t use them in CI. Fortunately CI (like all good frameworks) [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.askaboutphp.com/wp-post-images/51/cookie-cutter-jigsaw.png" alt="Codeigniter Jigsaw" align="right" />Having used Codeigniter for a few months now, this framework is really turning out to be a joy. My last post, I talked about <a href="http://www.askaboutphp.com/beginners/50/codeigniter-extending-the-native-model-and-make-it-your-own.html">how to modify native CI Libraries</a>.</p>
<p>With so many published PHP classes and functions, it would be a shame if we couldn&#8217;t use them in CI. Fortunately CI (like all good frameworks) provides not one but three ways to integrate 3rd code, by using Helpers, Plugins and Libraries.</p>
<p><span id="more-51"></span><br />
Of course, these &#8216;hooks&#8217; are not just for integrating 3rd party codes. They are also a great way for you to organise your own PHP codes. The CI user guide has done a great job to show how to use Helpers, Plugins and Libraries, so if you haven&#8217;t already read those sections of the user guide, I would strongly encourage you to do so.</p>
<p>As I found out during the course of my CI project, Helpers, Plugins and Libraries are nothing more than glorified includes. I can pretty much take any 3rd party code and integrate into my application using any of the 3 methods.</p>
<p>Let me demonstrate by integrating the <a href="http://www.phpclasses.org/browse/package/4299.html" target="_blank">Google Graph</a> class (by Ryon Sherman which I wrote about in my <a href="http://www.askaboutphp.com/tutorials/26/creating-google-graphs.html">previous post</a>) as a Helper, a Plugin and a Library, and all achieving the same result.</p>
<p><strong>As a Helper</strong><br />
Once you download a copy of the Google Graph class (or any 3rd party class of your choice), to use it as a helper in your application. This is what you do:</p>
<ol>
<li>Put the <code><strong>GoogleGraph.php</strong></code> file into <code><strong>application/helpers</strong></code> folder.</li>
<li>Rename the GoogleGraph.php file as <code><strong>GoogleGraph_helper.php</strong></code></li>
</ol>
<p>Create a new controller, like so:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> DrawGraph <span style="color: #000000; font-weight: bold;">extends</span> Controller <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> index<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// include the 3rd party code as a helper</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">load</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">helper</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'googlegraph'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000088;">$graph</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> GoogleGraph<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">// set the Graph type to 'line'</span>
		<span style="color: #000088;">$graph</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Graph</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setType</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'line'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">// set the Size of the chart, in this case its 300x150</span>
		<span style="color: #000088;">$graph</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Graph</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setSize</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">300</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">150</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">// define which axis we want, which is x,y only.</span>
		<span style="color: #666666; font-style: italic;">// I don't need the top (t) or right (r) axis.</span>
		<span style="color: #000088;">$graph</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Graph</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setAxis</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'x'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'y'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
		<span style="color: #666666; font-style: italic;">// set the AxisLabels for the x axis		</span>
		<span style="color: #000088;">$graph</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Graph</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addAxisLabel</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'A'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'B'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'C'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'D'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'E'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'F'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">// set the Axis Range for the y axis </span>
		<span style="color: #000088;">$graph</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Graph</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setAxisRange</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">40</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">// define the data points as an array</span>
		<span style="color: #000088;">$graph</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Data</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addData</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">20</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">15</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">30</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">40</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">// finally draw the graph.	</span>
		<span style="color: #000088;">$graph</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">printGraph</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>You should get a graph like this:<br />
<img src="http://chart.apis.google.com/chart?cht=lc&#038;chs=300x150&#038;chd=t:5.0,10.0,20.0,15.0,30.0,40.0&#038;chxt=x,y&#038;chxl=0:|A|B|C|D|E|F&#038;chxr=1,0,40"/></p>
<p><strong>As a Plugin</strong><br />
You can do the same thing by incorporating the GoogleGraph class as a Plugin.</p>
<ol>
<li>Take your <code><strong>GoogleGraph.php</strong></code> file and put it into either the <code><strong>system/plugins</strong></code> folder or your own <code><strong>application/plugins</strong></code> folder (if you don&#8217;t have the folder, just create it)</li>
<li>Rename the <code><strong>GoogleGraph.php</strong></code> file as <code><strong>GoogleGraph_pi.php</strong></code></li>
</ol>
<p>Now, instead of loading a helper, change the controller above by changing the load helper statement:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">Change
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">load</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">helper</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'googlegraph'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
To
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">load</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">plugin</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'googlegraph'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>You should still be able to redraw the graph.</p>
<p><strong>As a Library</strong><br />
The same goes for using GoogleGraph class as a Library.</p>
<ol>
<li>Take your <code><strong>GoogleGraph.php</strong></code> file and put it into your <code><strong>application/libraries </strong></code>folder.</li>
<li>This time you don&#8217;t need to rename it.</li>
</ol>
<p>Now, instead of loading a plugin, change the controller code as follow:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">Change
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">load</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">plugin</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'googlegraph'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
To
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">load</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">library</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'googlegraph'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Again, you should be able to redraw the same graph without any problems.</p>
<p><strong>So, when to use what?</strong><br />
Since, all three methods achieve the same ends. The question then is when do you use what? Fortunately, CI has also provided that distinction in their user guide which you can go read it yourself.</p>
<p>For me, these are my guidelines on when to use what:</p>
<ol>
<li>Plugins &#8211; I put all 3rd party codes I&#8217;m using in my application as Plugins. I would, as best as I can, try to use classes rather than straight function calls.</li>
<li>Helpers &#8211; Any standalone straight functions calls, which are repetitive in nature, I classify them as Helpers. For example, sorting functions, my own calculation functions, etc</li>
<li>Libraries &#8211; I classify my own classes as &#8216;Libraries&#8217;. Normally, if I&#8217;m already writing a class in my application, it would then to be the core logic of the application, as such, I group them all in the Library folder.</li>
</ol>
<p>Of course all my own or 3rd party codes, I would put them in their respective folders under the <code><strong>application/</strong></code> folder. I would not mix them into the <code><strong>system/</strong></code> which is reserved for CI.</p>
<p>Whether you may have your own scheme to organize your own code, or you follow what CI user guide defined, one thing I do know, you should have a scheme to organize your codes. Without which, as your application grows in number of functions and classes, it would become a real mess, real fast! </p>
]]></content:encoded>
			<wfw:commentRss>http://www.askaboutphp.com/51/codeigniter-helpers-plugins-and-libraries.html/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>
