• Home
  • About Me
  • Contact
  • Things to do Before I Turn 30

the bofe blog

a twenty something IT professional with a few things to say

Feeds:
Posts
Comments

scriptaculous sortables and PHP

January 17, 2007 by bofe

[geek post, run away if you don’t like things like AJAX, PHP, MySQL, and other acronyms]

I’m developing a wiki/CMS hybrid for my new job. It’s pretty cool so far, but I wanted to offer an easy way for users to change the order of navigation items. For instance, if their navigation was originally displaying like this:

  • About Us
  • Contact
  • Documents

They may decide that “Documents” needs to move to the top, like this:

  • Documents
  • About Us
  • Contact

Enter Scriptaculous Sortables—the basic principle is simple. Make a list that you want to be sortable.

Sortable.create(‘navigation_list’, {onUpdate: updateNavigation});

‘navigation_list’ is the id of the element that contains your list. updateNavigation is the javascript function that is called when the list has changed. For this example, I have my HTML (generated from php, of course):


<ul>
<li id="nav_1" >About Us </li>
<li id="nav_2" >Contact </li>
<li id="nav_3" >Documents </li>
</ul>

and my javascript:


function updateNavigation() {
var url = "http://localhost/ajax.php";
var sorted = escape(Sortable.sequence('navigation_list'));
var updateNavigation = new Ajax.Request(
url,
{
method: 'get',
parameters: "sort_order=" + sorted,
onComplete: showUpdate

});
return true;
}

and my PHP:


$nav_order = 1;
$order_array = explode(",", urldecode($_GET['sort_order']));
foreach($order_array as $k=> $nav) {
$sql = "UPDATE navigation SET item_order = $nav_order WHERE navigation_id = $nav";
mysql_query($sql);

$nav_order++;
}
echo "Navigation order has been updated.";

I couldn’t get this to work using Sortable.serialize()- so I just use Sortable.sequence which gives me a comma seperated list, and then parse it with PHP.

Note: I hate how this wp plugin displays code. I’ll get around to making it pretty one day.

Posted in Personal | Tagged Geek, Personal | 1 Comment

One Response

  1. on March 13, 2007 at 16:30 Morten

    Genious mate!

    I also had serious issues getting Sortable.serialize() to work. Your example worked as a charm and made my day, thanx!



Comments are closed.

  • Pages

    • About Me
    • Contact
    • Things to do Before I Turn 30
  • Recent Comments

    • Some Betty on Why the hate?
    • stinkypie on Why the hate?
    • stinkypie on Why the hate?
    • JohnO on Why the hate?
    • Yee on a pancake on Why the hate?
    • adam on Why the hate?
    • JohnO on Why the hate?
    • deezil on Why the hate?
    • Yee on a pancake on Why the hate?
    • Dave on Why the hate?
  • Recently Popular

    • Guide to Backing up your Virtual Machines with VMware Consolidated Backup (VCB)
    • Sync Google Calendar and Facebook Events
    • How to Use Excel 2007 and Goal Seek to calculate what the minimum grade you need on a final exam
    • VMware Consolidated Backup: Explained
    • Firefox 3 Constantly Crashes -- advice?
    • Problem with Orphaned Users/Logins in SQL Server 2005 Migration
    • Facebook 12 Step Program
    • Sortables with Scriptaculous, PHP, and MySQL in 6 Easy Steps
    • modifying osCommerce - [part 2 - Functions, Cases, Blocks Oh My]
    • point(x,y) versus point(lat, long) or What I Learned Today
  • Archives

  • Tags

    4815162342 advertising ajax apple bofehax byebye css dba delicious links development drm facebook finance firefox Geek gis Google imadork linux mememe Meta metoo money murray Music mysql owensboro Personal personal finance personalfinance philosophy php rss scriptaculous sqlserver sqlserver2005 Tablet PC Uncategorized vmware web Weblogs Web Standards whitewhine work wtf
  • Meta

    • Log in
    • Entries RSS
    • Comments RSS
    • WordPress.com

Blog at WordPress.com.

Theme: Mistylook by Sadish.