<?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>Emanuele Feronato &#187; Ajax</title>
	<atom:link href="http://www.emanueleferonato.com/category/ajax/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.emanueleferonato.com</link>
	<description>italian geek and PROgrammer</description>
	<lastBuildDate>Tue, 16 Mar 2010 15:55:36 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=abc</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Creation of a sortable list with Ajax tutorial</title>
		<link>http://www.emanueleferonato.com/2008/04/21/creation-of-a-sortable-list-with-ajax-tutorial/</link>
		<comments>http://www.emanueleferonato.com/2008/04/21/creation-of-a-sortable-list-with-ajax-tutorial/#comments</comments>
		<pubDate>Mon, 21 Apr 2008 20:44:55 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Php]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=258</guid>
		<description><![CDATA[One interesting Web 2.0 effect is the sortable list, and we are going to make one in less than five minutes thanks to Prototype and script.aculo.us libraries.
Prototype is a JavaScript Framework that aims to ease development of dynamic web applications and can be downloaded here, while script.aculo.us (scriptaculous from now on) provides you with easy-to-use, [...]]]></description>
			<content:encoded><![CDATA[<p>One interesting Web 2.0 effect is the sortable list, and we are going to make one in less than five minutes thanks to <a target = "_blank" href="http://www.prototypejs.org/">Prototype</a> and <a target = "_blank" href="http://script.aculo.us/">script.aculo.us</a> libraries.</p>
<p><strong>Prototype</strong> is a JavaScript Framework that aims to ease development of dynamic web applications and can be downloaded <a target = "_blank" href="http://www.prototypejs.org/download">here</a>, while <strong>script.aculo.us</strong> (scriptaculous from now on) provides you with easy-to-use, cross-browser user interface JavaScript libraries to make your web sites and web applications fly and can be downloaded <a target = "_blank" href="http://script.aculo.us/downloads">here</a>.</p>
<p>This tutorial is based on zen of shen&#8217;s pseudocode found at <a target = "_blank" href="http://www.zenofshen.com/2008/01/08/scriptaculous-ajax-sortable-lists-tutorial/">script.aculo.us Ajax Sortable Lists Tutorial</a>.</p>
<p>This tutorial will show real code and a step by step guide through MySql, php and Ajax coding in order to create your sortable list.</p>
<p>Of course your hosting plan must allow you to use php and MySql, so refer to your hosting provider if you don&#8217;t know if your web space supports those features.</p>
<p><strong>MYSQL</strong></p>
<p>First we need to work on the database to create the table that will store our list. I made the simplest table ever, because it&#8217;s just an example, but you can add as many columns as you want, in order to fit the table to your needs.</p>
<p>My table only has three fields: the primary key <code>id</code>, the name of the list element <code>color</code> and the core column, the one where you will save the order of the elements, called, in a lack of creativity, <code>color_order</code>.<span id="more-258"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #ff0000;">`sortable`</span> <span style="color: #66cc66;">&#40;</span>
<span style="color: #ff0000;">`id`</span> INT <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span> <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">,</span>
<span style="color: #ff0000;">`color`</span> TEXT <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #66cc66;">,</span>
<span style="color: #ff0000;">`color_order`</span> INT <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> 
<span style="color: #66cc66;">&#41;</span> ENGINE <span style="color: #66cc66;">=</span> MYISAM ;</pre></td></tr></table></div>

<p>Once we have the table, let&#8217;s populate it. I am doing it directly with MySql, but again you are free to code your own application to populate it</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
</pre></td><td class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> <span style="color: #ff0000;">`sortable`</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">`color`</span> <span style="color: #66cc66;">&#41;</span> 
<span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span>
<span style="color: #ff0000;">'Red'</span>
<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #66cc66;">&#40;</span>
<span style="color: #ff0000;">'Yellow'</span>
<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #66cc66;">&#40;</span>
<span style="color: #ff0000;">'Green'</span>
<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #66cc66;">&#40;</span>
<span style="color: #ff0000;">'Blue'</span>
<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #66cc66;">&#40;</span>
<span style="color: #ff0000;">'Black'</span>
<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #66cc66;">&#40;</span>
<span style="color: #ff0000;">'White'</span>
<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #66cc66;">&#40;</span>
<span style="color: #ff0000;">'Orange'</span>
<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #66cc66;">&#40;</span>
<span style="color: #ff0000;">'Purple'</span>
<span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>Now the table looks like this one</p>
<p><img src="/images/colors.png" alt="" /></p>
<p>and our MySql task is completed. Now, let&#8217;s write some php and html</p>
<p><strong>PHP/HTML</strong></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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000088;">$connection</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_connect</span> <span style="color: #009900;">&#40;</span>localhost<span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;your_login&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;your_password&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">mysql_select_db</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;your_db&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$connection</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;no db found&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;select * from sortable order by color_order asc&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span>or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$number</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_num_rows</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span><span style="color: #000088;">$x</span><span style="color: #339933;">&lt;=</span><span style="color: #000088;">$number</span><span style="color: #339933;">;</span><span style="color: #000088;">$x</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$list</span><span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;			&lt;li id=<span style="color: #000099; font-weight: bold;">\&quot;</span>item_<span style="color: #006699; font-weight: bold;">$row[id]</span><span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;<span style="color: #006699; font-weight: bold;">$row[color]</span>&lt;/li&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;&gt;
&lt;html&gt;
	&lt;head&gt;
		&lt;script type=&quot;text/javascript&quot; src=&quot;prototype-1.6.0.2.js&quot;&gt;&lt;/script&gt;
		&lt;script type=&quot;text/javascript&quot; src=&quot;scriptaculous/src/scriptaculous.js&quot;&gt;&lt;/script&gt;
		&lt;style type = &quot;text/css&quot;&gt;
			ul#sortlist{list-style:none;padding:0px;margin:0px}
			ul#sortlist li{padding:5px;border:1px solid #666666;margin:1px;font:normal 10px verdana;width:200px;color:#666666;background-color:#f0f0f0;cursor:move}
			#output{font:bold 10px verdana}
		&lt;/style&gt;
	&lt;/head&gt;
	&lt;body&gt;
		&lt;ul id = &quot;sortlist&quot;&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$list</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
		&lt;/ul&gt;
		&lt;div id = &quot;output&quot;&gt;Drag to sort your favourite colors&lt;/div&gt;
		&lt;script type=&quot;text/javascript&quot;&gt;
			Sortable.create('sortlist',{
				onUpdate:function(){
					new Ajax.Updater('output','sort.php',{onComplete:function(request){}, parameters:Sortable.serialize('sortlist'), evalScripts:true, asynchronous:true})
				}
			})	
		&lt;/script&gt;
	&lt;/body&gt;
&lt;/html&gt;</pre></td></tr></table></div>

<p><strong>Lines 3-4</strong>: Connecting to the database. If you don&#8217;t know your username, password or database name, ask them to your hosting provider</p>
<p><strong>Lines 6-8</strong>: Query to get all the colors ordered by their color order</p>
<p><strong>Lines 10-13</strong>: Building the list from the result of the query just executed</p>
<p><strong>Lines 19-20</strong>: Including Prototype and scriptaculous sources. Remember to include Prototype before Scriptaculous and also remember to change the path if you placed the scripts in a different location</p>
<p><strong>Lines 21-25</strong>: Giving the list a touch of style</p>
<p><strong>Lines 28-30</strong>: Displaying the list</p>
<p><strong>Line 31</strong>: Creation of the <code>div</code> element to display Ajax output</p>
<p><strong>Lines 32-38</strong>: Calling the <code>Sortable.create</code> function to initialize the sortable list. You can find more information about <code>Sortable.create</code> function in the scriptaculous wiki at <a target = "_blank" href="http://wiki.script.aculo.us/scriptaculous/show/Sortable.create">this link</a></p>
<p>And the php/html is over.</p>
<p>Now, I should made the Ajax part. But it&#8217;s already made by Prototype thanks to the <code>Ajax.Updater</code> object, so I just have to make the <code>sort.php</code> script called in the <code>Ajax.Updater</code> object</p>
<p>You can find more information about the Ajax.Updater object in the API Docs pages at <a target = "_blank" href="http://www.prototypejs.org/api/ajax/updater">this link</a></p>
<p>So it&#8217;s basically php again, but I will call this part the Ajax part</p>
<p><strong>AJAX</strong></p>
<p>Create a file called <code>sort.php</code> and write:</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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000088;">$connection</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_connect</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;62.149.150.49&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Sql92833&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;0015025a&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$db</span><span style="color: #339933;">=</span> <span style="color: #990000;">mysql_select_db</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Sql92833_5&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$connection</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span>sortlist<span style="color: #009900;">&#93;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$varname</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$varvalue</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;update sortable set color_order = &quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$varname</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; where id = &quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$varvalue</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;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;You just updated it on &quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Y-m-d H:i:s&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p><strong>Lines 3-4</strong>: Connecting to the database, just like in the php/html part</p>
<p><strong>Lines 6-9</strong>: Scanning all the <code>sortlist</code> array passed with <code>POST</code> method by the <code>Sortable.create</code> and the <code>Ajax.Updater</code> and updating the <code>color_order</code> column of the <code>sortable</code> table according to <code>sortlist</code> names and values</p>
<p><strong>Line 11</strong>: displaying an output message.</p>
<p>And that&#8217;s it! Drag colors as you want, then <a target = "iframe" href="/stuff/sortable/index.php">reload</a> the page in the iframe and the colors will be in the same position you just ordered&#8230; unless someone else changed their order while the page was reloading</p>
<p><iframe name = "iframe" id = "iframe" width = "500" height = "300" src = "/stuff/sortable/index.php"></iframe></p>
<p>That&#8217;s all&#8230; hope you will find it useful as I did.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2008/04/21/creation-of-a-sortable-list-with-ajax-tutorial/feed/</wfw:commentRss>
		<slash:comments>25</slash:comments>
		</item>
	</channel>
</rss>
