<?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>Not A n00b &#187; array</title>
	<atom:link href="http://notan00b.com/tag/array/feed/" rel="self" type="application/rss+xml" />
	<link>http://notan00b.com</link>
	<description>Tutorials, Scripts, and Rants</description>
	<lastBuildDate>Wed, 12 May 2010 08:08:35 +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>WhosOnline updated again!</title>
		<link>http://notan00b.com/2009/09/whosonline-updated-again/</link>
		<comments>http://notan00b.com/2009/09/whosonline-updated-again/#comments</comments>
		<pubDate>Fri, 25 Sep 2009 00:11:19 +0000</pubDate>
		<dc:creator>pyr0t3chnician</dc:creator>
				<category><![CDATA[Post]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[count]]></category>
		<category><![CDATA[update]]></category>
		<category><![CDATA[whosonline]]></category>

		<guid isPermaLink="false">http://notan00b.com/?p=217</guid>
		<description><![CDATA[The biggest feature is the ability to show WHO is online, not just a count!!!  Read more]]></description>
			<content:encoded><![CDATA[<p>Due to popular response, I updated WhosOnline.  I changed a few things.  I took out the lazy connection because it was giving to many errors.  You now connect, set the table, update, and then can display the data.  I know, I know, that&#8217;s TWO whole lines more code.  Sorry.</p>
<p>The biggest feature is the ability to show WHO is online, not just a count!!!  You will need to have a variable that has the users login name, which most coders store as a session variable, or a global variable.  Both are perfectly acceptable.  You simply set the users name, update it, and then retrieve the data as an ARRAY.  The array will show who is online, the number of &#8220;Guests&#8221; online, and the number of members, and the overall count of people.    PRETTY SLICK.</p>
<p><a href="http://notan00b.com/code-bin/">Head over to the code bin to check it out!</a></p>
]]></content:encoded>
			<wfw:commentRss>http://notan00b.com/2009/09/whosonline-updated-again/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Nifty PHP/AJAX tricks</title>
		<link>http://notan00b.com/2009/07/nifty-phpajax-tricks/</link>
		<comments>http://notan00b.com/2009/07/nifty-phpajax-tricks/#comments</comments>
		<pubDate>Sun, 26 Jul 2009 21:03:52 +0000</pubDate>
		<dc:creator>pyr0t3chnician</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[eval]]></category>
		<category><![CDATA[evaluate]]></category>
		<category><![CDATA[exit]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[tricks]]></category>

		<guid isPermaLink="false">http://notan00b.com/?p=166</guid>
		<description><![CDATA[This brief tutorial covers usage of the exit function to help with AJAX scripts as well as how to return both a numeric and associative array via AJAX.]]></description>
			<content:encoded><![CDATA[<p>These are a couple awesome AJAX/PHP tricks I learned to make coding just slightly easier on myself.</p>
<h1>Exit</h1>
<p>Lets say we had an index page, and when the user click a button, it would display the time.  Typically, in the past I would have it go to another &#8220;remote procedure call&#8221; (RPC) page, like &#8220;time.php&#8221; where the main portion of the script would be &#8220;echo time();&#8221;.  This tends to clutter my folders with miscellaneous scripts if I have multiple AJAX events.  I may have a different RPC for every page, which could mean I have like 50 php files in one folder and I&#8217;m not sure which ones are the RPC and which ones are the main files.</p>
<p>I then started condensing them, making one large script for a few files, and using a $_POST['action'] to decide what to do.  So if I had an ajax.php page it might look something like this:</p>

<div class="wp_syntax"><div 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: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'action'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">==</span><span style="color: #0000ff;">'time'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
   <span style="color: #b1b100;">echo</span> <span style="color: #990000;">time</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: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'action'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">==</span><span style="color: #0000ff;">'hello'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
   <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;hello!&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>So now it will execute the portion of the script that it is told.  So if I point my AJAX to ajax.php and pass a post variable called &#8220;action=time&#8221;, it will return the time.  We have now condensed it slightly.</p>
<p>We can condense it even further by using the php function &#8220;exit();&#8221;.  This function completely stops (exits) the script.  If my script looked like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;hello &quot;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;world!&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>The output would look like this: <code>hello </code><br />
Exit will abort the script and not run anything after it.</p>
<p>Exit also has the capability to display (echo) a message as to why it is quitting, so you could essentially use exit() and let people know they aren&#8217;t logged in if you wanted.  Here is an example:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #990000;">session_start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$notlogged</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&lt;a href=<span style="color: #000099; font-weight: bold;">\&quot;</span>login.php<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;Please log in to view this page&lt;/a&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'loggedin'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
   <span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$notlogged</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">/* Main Content Starts Here */</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>So if we had never logged into the site, all we would see is &#8220;Please log in to view this page&#8221; as a link that we could click.</p>
<p>Lets put this all together.  The exit function allows you to point the ajax script to the current page, as it will &#8220;exit&#8221; and echo the data you want without running the main content of the page over again.  Let me show you what I mean, using jQuery ajax, with a time.php.</p>

<div class="wp_syntax"><div 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;">//time.php</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'do'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'do'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">==</span><span style="color: #0000ff;">'time'</span><span style="color: #009900;">&#41;</span>
   <span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'g:i A'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'do'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'do'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">==</span><span style="color: #0000ff;">'hello'</span><span style="color: #009900;">&#41;</span>
   <span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Hello World!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&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 xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
&lt;head&gt;
&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot; /&gt;
&lt;title&gt;AJAX Example&lt;/title&gt;
&lt;link type=&quot;text/css&quot; href=&quot;css/custom-theme/jquery-ui-1.7.2.custom.css&quot; rel=&quot;stylesheet&quot; /&gt;	
&lt;script type=&quot;text/javascript&quot; src=&quot;js/jquery-1.3.2.min.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;js/jquery-ui-1.7.2.custom.min.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;js/jquery.geturlparams.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
$(function() {
	$('#checktime').click(function(){
		$.get('time.php',
			{do:'time'},
			function(data){
				$('#timeorhello').html(data);
			});								 
	});
	$('#sayhello').click(function(){
		$.get('time.php',
			{do:'hello'},
			function(data){
				$('#timeorhello').html(data);
			});								 
	});
});
&lt;/script&gt;
&lt;body&gt;
&lt;div id=&quot;timeorhello&quot;&gt;&amp;nbsp;&lt;/div&gt;
&lt;input type=&quot;button&quot; id=&quot;checktime&quot; value=&quot;Get Time&quot; /&gt;
&lt;input type=&quot;button&quot; id=&quot;sayhello&quot; value=&quot;Say Hello&quot; /&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></div></div>

<p>Pretty simple stuff right?  You can reduce the number of extra files by simply putting the AJAX commands at the top of the file.  If you put &#8220;echo(time());&#8221;, it would continue to run the entire page and mess up your AJAX.  This has made my life easier and my code a little more succinct.</p>
<h1>Arrays</h1>
<p>Because AJAX only accepts strings to be echo, and returned to the AJAX function, so a lot of people struggle with returning an array to Javascript through AJAX.  There are 2 ways to do this that don&#8217;t require JSON.</p>
<p>The first is simply to break the array up and make it into a string using the &#8220;implode&#8221; function:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;hello&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;world&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;!&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$newarray</span><span style="color: #339933;">=</span><span style="color: #990000;">implode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;::&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$newarray</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>On the Javascript end, just split it up into an array again!</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> myArray<span style="color: #339933;">=</span>data.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'::'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>That is by far the easiest way to return arrays.</p>
<p>There is one problem.  What if in PHP, your arrays didn&#8217;t have numerical keys, but strings: <code>$array['name']='bob'</code>.  This is where we run into a problem.  There is a way around this fairly easy.  Most languages have an &#8220;evaluate&#8221; command, which will take a string, and execute it as though it were code.  In PHP and Javascript, it is eval().  Here is a PHP example: <code>eval("echo 'Hello World!';");</code>.  This would output &#8220;Hello World!&#8221;.  In Javascript, it is exactly the same thing.  So lets use eval() to return a Javascript array!</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$array</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;">'bob'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'age'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'96'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'sex'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'male'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$return</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'var myArray={&quot;name&quot;:&quot;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$array</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;, &quot;age&quot;:&quot;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'age'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&quot;, &quot;sex&quot;:&quot;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'sex'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&quot;};
echo $return;
?&gt;</span></pre></div></div>

<p>And on the Javascript side, all you need to do is evaluate the string:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> handler<span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
   <span style="color: #000066; font-weight: bold;">eval</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>myArray<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Its a bit lengthy to do it this way, but will definitely get the job done.  One thing to watch out for would be quotes, plus signs, slashes, especially if the user gets to define some of the stuff that is being returned.</p>
<p>I hope this has helped out a little with using PHP and AJAX.  I spent hours trying to figure this stuff out the first time, but when I did, it made my life a lot simpler. </p>
]]></content:encoded>
			<wfw:commentRss>http://notan00b.com/2009/07/nifty-phpajax-tricks/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
