<?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>Kristo Vaher &#187; tutorial</title>
	<atom:link href="http://waher.net/archives/tag/tutorial/feed" rel="self" type="application/rss+xml" />
	<link>http://waher.net</link>
	<description>inventas vitam iuvat excoluisse per artes</description>
	<lastBuildDate>Fri, 13 Apr 2012 19:07:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Smart AJAX/JSON/PHP solution with jQuery</title>
		<link>http://waher.net/archives/319</link>
		<comments>http://waher.net/archives/319#comments</comments>
		<pubDate>Tue, 01 Jun 2010 21:45:57 +0000</pubDate>
		<dc:creator>Kristo Vaher</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[user interface]]></category>

		<guid isPermaLink="false">http://waher.net/?p=319</guid>
		<description><![CDATA[I have seen a number of complicated solutions for front-end AJAX development and it has become quite clear such solutions are difficult thing to grasp for someone not that experienced with AJAX. Today I am offering a very simple and easy to extend solution that not only is less than two hundred lines of code [...]]]></description>
			<content:encoded><![CDATA[<p>I have seen a number of complicated solutions for front-end AJAX development and it has become quite clear such solutions are difficult thing to grasp for someone not that experienced with AJAX. Today I am offering a very simple and easy to extend solution that not only is less than two hundred lines of code in total &#8211; together with detailed comments &#8211; but also gives the ropes to jQuery AJAX development and using JSON as the preferred type of returned data from the server. Fear not, this is actually easier than it sounds.</p>
<p><span id="more-319"></span></p>
<p>For those of you looking for the files directly, they can grab them <a title="smart-json-ajax-solution.rar" href="http://waher.net/blog/smart-json-ajax-solution.rar">here</a>. Individual source code can be viewed here: <a href="http://waher.net/blog/smart-ajax/index.htm.txt">index.htm</a>, <a href="http://waher.net/blog/smart-ajax/javascript.js.txt">javascript.js</a>, <a href="http://waher.net/blog/smart-ajax/ajax.php.txt">ajax.php</a>. Note that jquery.js file is also required (1.4.2), should you copy-paste from the source code instead of downloading the archive.</p>
<p>The code is licensed under LGPL, so you can use it for personal as well as commercial purposes. All files are commented and a working example is provided within. You do not need a firm grasp of jQuery in order to understand this example and you do not need experience with JSON data format. PHP and jQuery handle the conversions for you. I will not explain everything step by step &#8211; for that refer to the files and the example provided &#8211; but I will give a general overview of what is happening here.</p>
<p>Front-end user-interface has the various actions built through a uiAction() javascript function. This is a general-use function that takes two variables, an &#8216;action&#8217; that acts as an action identifier, and &#8216;data&#8217; variable. Data variable can be whatever you want, it can be a string, integer, array or an object, depending on your needs. Since you will control how the &#8216;data&#8217; variable will be used, it is entirely up to you. In the example files I am calling the uiAction() on a &lt;button&gt; element like this:</p>
<p><em>uiAction(&#8216;alertLowercaseString&#8217;,$(&#8216;#example&#8217;).val());</em></p>
<p>For those of you who know jQuery, know that I am sending the value of an input field with an id of &#8216;example&#8217; to the function. Action&#8217;s name itself is &#8216;alertLowercaseString&#8217; in that case. Then, within uiAction() function in the &#8216;action&#8217;-switch I am executing the function that sends data to AJAX, like this:</p>
<p><em>input['string']=data;<br />
executeAjaxAction(action,input);</em></p>
<p>Action is, by default, the same as action identifier used to call uiAction(). executeAjaxAction() is a function that submits data to PHP backend (but you can customize it, such as have different uiAction() calls execute same function in back-end). &#8216;input&#8217; is an uiAction() specific javascript object. This is defined at the start of the uiAction() function and is used for convenience in order to send specific data to AJAX. &#8216;executeAjaxAction()&#8217; expects &#8216;input&#8217; to be an object or an array, since this is what will be sent to PHP backend. After submitting data to executeAjaxAction() the front-end will connect PHP back-end with AJAX. In this case I am sending an &#8216;action&#8217; of alertLowercaseString. PHP backend will solve the &#8216;input&#8217; into a variable named $inputVariables and again &#8211; as it was in front-end, within a switch you can define what you want to take place in the back-end and what will be returned. In this example it is:</p>
<p><em>$return['action']=&#8217;alert&#8217;;<br />
$return['message']=&#8217;Your string lowercased: &#8216;.strtolower($inputVariables['string']);</em></p>
<p>&#8216;$return&#8217; is an array that is used to create a JSON object from. &#8216;action&#8217; for &#8216;$return&#8217; needs to be always defined, but every other variable can be whatever you want, since &#8211; as before &#8211; you get to decide what data you need to use later on in front end. Basically everything you want to return to front-end should be placed in &#8216;$return&#8217; array. PHP then creates a JSON string of the &#8216;$return&#8217; array and returns it to the browser. executeAjaxAction() function will then call a javascript function parseAjaxReturn(), which will check what action PHP defined for callback (if any) and then &#8211; in this example of &#8216;alert&#8217; as the action &#8211; does the following:</p>
<p><em>alert(json['message']);</em></p>
<p>And this alerts to the user the lowercased string.</p>
<p>This quick explanation may be difficult to understand at first, but I strongly suggest that you take a look at the fully working examples provided within. Files are fully commented and examples provided within are minimal, so you can take this into production use right away should you want.</p>
<p><a title="smart-json-ajax-solution.rar" href="http://waher.net/blog/smart-json-ajax-solution.rar">Download fully working example files here</a></p>
<p>If you have any questions, go ahead and contact me directly or comment this article. I will get back to you as soon as possible.</p>
<p>All the best!</p>
]]></content:encoded>
			<wfw:commentRss>http://waher.net/archives/319/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A simple PHP pathfinder class &#8211; as promised</title>
		<link>http://waher.net/archives/313</link>
		<comments>http://waher.net/archives/313#comments</comments>
		<pubDate>Tue, 25 May 2010 20:55:58 +0000</pubDate>
		<dc:creator>Kristo Vaher</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://waher.net/?p=313</guid>
		<description><![CDATA[I recently shared a simple PHP and jQuery animated pathfinding tech-demo through my Twitter account and promised to share the simple pathfinding class used for pathfinding calculations. The example shared in Twitter is still available here. This example has a few limits, that is you can easily confuse it by clicking on two tile locations [...]]]></description>
			<content:encoded><![CDATA[<p>I recently shared a simple PHP and jQuery animated pathfinding tech-demo through my Twitter account and promised to share the simple pathfinding class used for pathfinding calculations. The example shared in Twitter is still available <a href="http://waher.net/pathfinder/" target="_blank">here</a>.</p>
<p><span id="more-313"></span></p>
<p>This example has a few limits, that is you can easily confuse it by clicking on two tile locations in a row. But in general it works and what I will be using this for will be something a bit different, so this limitation was of no concern to me. However the entire pathfinding calculation takes place on PHP backend. Each map is essentially a square (currently a limitation of pathfinder, but can easily be tweaked) and stored as an associative array, every tile in that array like this:</p>
<p><strong>$map['1x1']['weight']=1.0;</strong></p>
<p>The first key is the coordinate of the single map tile and the weight key is used to store weight of this map tile for calculations. Nothing else is needed (and an example is provided in the files below). For actual game purposes or whatnot you can store additional information on that very map as long as the coordinate and weight are assigned as shown in the example. For example, in my own tech-demo I also store the graphic tile used in the map array.</p>
<p>Finding the actual path is just as simple:</p>
<p><strong>$map=getMap();<br />
require(&#8216;class.pathfinder.php&#8217;);<br />
$path=new PathFinder(); </strong><em>//Create new pathfinder object</em><strong><br />
$path-&gt;setOrigin(1,1); </strong><em>//Set the start location</em><strong><br />
$path-&gt;setDestination(20,20); </strong><em>//Set the destination</em><strong><br />
$path-&gt;setMap($map); </strong><em>//Assign the map to use for calculations</em><strong><br />
$result=$path-&gt;returnPath(); </strong><em>//Returns an array of all the walked steps</em></p>
<p>Working example, as well as commented pathfinder class is available below:<em><br />
</em></p>
<p><a href="http://www.waher.net/blog/pathfinder.rar">Download the class and example file here</a></p>
<p>Please note that this is not a definitive pathfinder. It only takes into account one square ahead and on very large maps I am sure there are more ideal solutions. But for your basic browser based game this is a good starting point and I learned a lot by creating it. This is also easy to expand based on your needs.</p>
<p>If you have any other questions, let me know.</p>
<p>The code is shared entirely free for personal or commercial use.</p>
<p>Cheers!</p>
]]></content:encoded>
			<wfw:commentRss>http://waher.net/archives/313/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

