<?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>Tricklin &#187; php</title>
	<atom:link href="http://www.patmullin.com/weblog/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.patmullin.com/weblog</link>
	<description></description>
	<lastBuildDate>Tue, 11 May 2010 15:13:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Sortable List Example With Scriptaculous and PHP/mySQL</title>
		<link>http://www.patmullin.com/weblog/2007/01/05/sortable-list-example-with-scriptaculous-and-phpmysql/</link>
		<comments>http://www.patmullin.com/weblog/2007/01/05/sortable-list-example-with-scriptaculous-and-phpmysql/#comments</comments>
		<pubDate>Thu, 04 Jan 2007 19:26:23 +0000</pubDate>
		<dc:creator>Patrick</dc:creator>
				<category><![CDATA[AJAX]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.patmullin.com/weblog/?p=74</guid>
		<description><![CDATA[After spending a considerable amount of time looking for a decent example of "how to store the newly sorted order of a list after dragging and dropping", I realized that there isn't a whole lot of examples and documentation out there. I've decided that it's my turn to try and come up with a full [...]]]></description>
			<content:encoded><![CDATA[<p>After spending a considerable amount of time looking for
a decent example of "how to store the newly sorted order of
a list after dragging and dropping", I realized that there
isn't a whole lot of examples and documentation out there. 
I've decided that it's my turn to try and come up with a
full blown, easy to understand example using XHTML,
Javascript, PHP, and mySQL.</p>
<p>First off, you'll need both the <a
href="http://script.aculo.us/downloads"
target="_blank">scriptaculous</a> and <a
href="http://script.aculo.us/downloads"
target="_blank">prototype</a> javascript libraries (both
available from <a href="http://script.aculo.us"
target="_blank">script.aulo.us</a>).  Once you've downloaded
these files, you'll need to reference them in the
<code>head</code> of your file:</p>
<p><span id="more-74"></span></p>
<pre class="brush: html">
&lt;script type=&quot;text/javascript&quot;
src=&quot;scripts/prototype.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot;
src=&quot;scripts/scriptaculous.js&quot;&gt;&lt;/script&gt;
</pre>
<p>Then, in your html file, make your list:</p>
<pre class="brush: html">
&lt;ul id=&quot;items_list&quot;&gt;
	&lt;li id=&quot;item_1&quot;&gt;Item 1&lt;/li&gt;
	&lt;li id=&quot;item_2&quot;&gt;Item 2&lt;/li&gt;
	&lt;li id=&quot;item_3&quot;&gt;Item 3&lt;/li&gt;
&lt;li id=&quot;item_4&quot;&gt;Item 4&lt;/li&gt;
</pre>
<p>Note that the <code>ul</code> and each <code>li</code>
have unique <code>id</code>s.  It is also important to note
that the <code>id</code>s of the list items must have "_#"
at the end (this number must be unique).</p>
<p>Next, add this after the list (because of IE):</p>
<pre class="brush: html">
&lt;script type=&quot;text/javascript&quot;
language=&quot;javascript&quot;&gt;
Sortable.create(&quot;items_list&quot;,{dropOnEmpty:true,con
straint:false});
&lt;/script&gt;
</pre>
<h3>Cool!  It works!  But...</h3>
<p>Now that you've done this, go ahead and check it out. 
Cool...  You can now sort your list by dragging and
dropping!  This is all fine and dandy, but how do I actually
store this new order in my database?</p>
<p>It's pretty simple actually.  In comes "serialize", to
make life easy.  This little gem creates a string containing
information on of the current state of the list being
sorted.  How do you use it?</p>
<pre class="brush: html">
var listorder = Sortable.serialize(&#039;items_list&#039;);
</pre>
<p>Let's say that you've sorted your list as follows:
</ul>
<ul>
<li>item 3</li>
<li>item 2</li>
<li>item 1</li>
<li>item 4</li>
</ul>
<p>When invoked, the variable <code>listorder</code> would
return as
"<code>items_list[]=3&items_list[]=2&items_list[]=1&items_li
st[]=4</code>".  And this is what you need to update your
database.  Notice, the values returned are associated to
whatever is after the underscore (<code>_</code>) in the
<code>id</code> of the list item.</p>
<p>Now that you have this information, you can easily update
your database with a simple <abbr title="Asynchronous
Javascript And XML">AJAX</abbr> call pointing to your php
file ('save_items_order.php' in this case):</p>
<pre class="brush: javascript">
function saveOrder() {
	listorder = Sortable.serialize(&#039;items_list&#039;);
	var url = &#039;save_items_order.php&#039;;
	var pars = listorder
		+ &#039;&amp;rnd=&#039;
		+ new Date().getTime();
	var myAjax = new Ajax.Request(
		url,
		{
			method: &#039;post&#039;,
			postBody: pars,
			onComplete:  showList
		});
}
</pre>
<p>Finally, the PHP/mySQL piece:</p>
<pre class="brush: php">
if($_POST) {
  $items = $_POST[&#039;items_list&#039;];
  $i = 1; # will represent the actual order (starting at 1).
  foreach($items as $itemid) {
    $query = &quot;
      UPDATE items_table SET order_col = $i WHERE id_col =
$itemid
    &quot;;
    mysql_query($query);
    $i++;  # add 1 to the current value of i (the next order
in the list).
  }
}
</pre>
<p>There you have it.  An example of how to sort a list, and
then store the new order in your database.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.patmullin.com/weblog/2007/01/05/sortable-list-example-with-scriptaculous-and-phpmysql/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
	</channel>
</rss>
