{"id":2126,"date":"2009-09-30T17:05:23","date_gmt":"2009-09-30T21:05:23","guid":{"rendered":"http:\/\/teamtutorials.com\/?p=2126"},"modified":"2014-12-05T12:38:20","modified_gmt":"2014-12-05T17:38:20","slug":"using-jquery-to-reorder-a-list-and-update-a-database","status":"publish","type":"post","link":"https:\/\/teamtutorials.com\/web-development-tutorials\/using-jquery-to-reorder-a-list-and-update-a-database","title":{"rendered":"Using jQuery to Reorder a List and Update a Database"},"content":{"rendered":"
Warning:<\/strong> This tutorial uses old techniques. It is insecure and will leave your server vulnerable to SQL Injection attacks<\/strong>.This tutorials also uses mysql_ functions that are no longer support. For updated tutorials look for a PDO<\/a> or MySQLi tutorial.This post will be delete or revised in the future.<\/p>\n<\/div>\n This tutorial will show you how to use jQuery to allow as list (UL) to be reordered and will also make an AJAX call to a PHP file that will update the database when an item is dropped. I found this useful for a project I was working on recently. The user wanted to be able to change the order that categories displayed in a report. The items are stored in a database with a field called display_order. When the report is generated we simply order by that field.<\/p>\n This tutorial requires:<\/strong><\/p>\n First you are going to need to download jQuery Ui.<\/a> There are plenty of feature within jQuery User Interface that I will not be touching in this tutorial, but for sake of the tutorial download the entire bundle. Also I will be using the default (UI Lightness) theme. Since jQuery comes with an example called index.html I am going to name my file test.php. You can call it whatever you want. Create the file. We will use it later. We’ll set the database up first.<\/p>\n I like to use HeidiSQL<\/a> when working with MySQL. Now on the database server I am going to create a database call test_tutorials, with a table called report_items. Here are the field properties for the report_items table: Now populate your table with some test data. I’ll insert 6 rows. Also make sure you insert the display order starting from 0. You can save the changes to your data base and get back to working on test.php. Open the file. We’ll start with the basic layout. <\/p>\n Next we’ll include the stylesheet from our jQuery theme. I’ll also add some CSS from the example on the jQuery UI website. Then we’ll include the jQuery javascript files.<\/p>\n We are going to pull the intial data from the database. In order to do this we’ll need to make a database connection. Create a new file called connect.php and create a database connection (obvious added your credentials where needed.<\/p>\n Save connect.php and include it into your test.php file:<\/p>\n Now we need to retrieve the database results and dsiplay them. The items will be display in an un-ordered list. We’ll add this code to the body.<\/p>\n Take not above. When we echo the li we are adding the ui-state-default class which is just a jQuery theme class and the span that we included is an arrow icon also provide with jQuery Ui. Where you see item_$catid, that should be your primary key from your database (this will be used later to reorder items.<\/p>\n Now save the file and load it in your browser. If you followed everything hopefully you will get something like this: Now we’ll enable the sortable function on our list. Remeber I gave the UL tag an id of sortable. Add this code in the head below your jquery javascript files:<\/p>\n The code above enables sorting of the UL with the id #sortable. The placeholder: ‘ui-state-highlight’ tells jquery we want it to show a place holder of where we can drop the item. In the them I am using it is a yellow box. Make the changes above and save your page. You should now be able to drap and drop your list items. Now that you have that working we can move to the AJAX call. When an item is dropped, we want to call a php file to update the database. We do that by using the stop: action. So when dragging stops, the script will call the url (updatedb.php) we provided below. To make the AJAZ called, we let jQuery do the work.<\/p>\n You see the ajax function above. We tell it what type of request, in this case wer’ll use get variables. We also tell it what URL, this is a php file we’ll create to update the database. Then we add the data that will be sent. <\/p>\n The code $(“#sortable”).sortable(“serialize”)}); will take the items within our ordered list and serialize it. It will take the items of the list, in whatever order they are in, and turn them into a get variable. So for each item it will add it to an array and format is properly to make the call to the PHP file. The data will look like this: If you want to debug AJAX request I recommend using Firefox with the Firebug plugin. If you want to see that the request is being attempted and that what the data looks like, you can add an alert box just below the placeholder like this: alert($(“#sortable”).sortable(“serialize”)); Now we need to actually update the database. Create a file called updatedb.php. This file is pretty simple. We take each item in the array and then update the database based on the array position($key) and the primary key ($value)<\/p>\n Save that file and test the reorder. If you have problems and you think it is with the AJAX call. Check fire bug. Here is an example of a bad request in this case I tried to include a file that was not there. I hope this tutorial helped someone out<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":" This tutorial will show you how to use jQuery to allow as list (UL) to be reordered and will also make an AJAX call to a PHP file that will update the database when an item is dropped. I found this useful for a project I was working on recently. The user wanted to be able to change the order that categories displayed in a report. The items are stored in a database with a field called display_order. When the report is generated we simply order by that field.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[92,24,17],"tags":[50,164,49,53,47,98],"yoast_head":"\n\n
\nCreate a folder for this project on your server. You will need to unzip the jQuery files and put them all in this directory.
\n<\/a><\/p>\n
\n<\/a><\/p>\n
\n<\/a><\/p>\n\r\n<!DOCTYPE html PUBLIC "-\/\/W3C\/\/DTD XHTML 1.0 Transitional\/\/EN" "http:\/\/www.w3.org\/TR\/xhtml1\/DTD\/xhtml1-transitional.dtd">\r\n<html xmlns="http:\/\/www.w3.org\/1999\/xhtml">\r\n<head>\r\n<meta http-equiv="Content-Type" content="text\/html; charset=utf-8" \/>\r\n<title>Reorder Test<\/title>\r\n<\/head>\r\n\r\n<body>\r\n\r\n<\/body>\r\n<\/html>\r\n<\/pre>\n
\r\n<!DOCTYPE html PUBLIC "-\/\/W3C\/\/DTD XHTML 1.0 Transitional\/\/EN" "http:\/\/www.w3.org\/TR\/xhtml1\/DTD\/xhtml1-transitional.dtd">\r\n<html xmlns="http:\/\/www.w3.org\/1999\/xhtml">\r\n<head>\r\n<meta http-equiv="Content-Type" content="text\/html; charset=utf-8" \/>\r\n<title>Reorder Test<\/title>\r\n<\/head>\r\n\r\n<link rel="stylesheet" type="text\/css" href="development-bundle\/themes\/base\/ui.all.css"\/>\r\n\r\n<style type="text\/css">\r\n\t#sortable { \r\n\t\tlist-style-type: none; \r\n\t\tmargin: 0; \r\n\t\tpadding: 0; \r\n\t\twidth: 60%; \r\n\t}\r\n\t#sortable li { \r\n\t\tmargin: 0 3px 3px 3px; \r\n\t\tpadding: 0.4em; \r\n\t\tpadding-left: 1.5em; \r\n\t\tfont-size: 1.4em; \r\n\t\theight: 18px; \r\n\t}\r\n\t#sortable li span { \r\n\t\tposition: absolute; \r\n\t\tmargin-left: -1.3em; \r\n\t}\r\n<\/style>\r\n\r\n<script type="text\/javascript" src="js\/jquery-1.3.2.min.js"> <\/script>\r\n<script type="text\/javascript" src="js\/jquery-ui-1.7.2.custom.min.js"><\/script>\r\n\r\n<body>\r\n\r\n<\/body>\r\n<\/html>\r\n<\/pre>\n
\r\n<?php\r\n\r\n$servername='localhost'; \/\/no need to change if the file is located on the same server\r\n$dbusername='username'; \/\/your db username\r\n$dbpassword='password'; \/\/your db password\r\n$dbname='database'; \/\/for this tutorial I called my db test_tutorials\r\n\r\nconnecttodb($servername,$dbname,$dbusername,$dbpassword);\r\nfunction connecttodb($servername,$dbname,$dbuser,$dbpassword){\r\n\tglobal $link;\r\n\t$link=mysql_connect ($servername,$dbuser,$dbpassword);\r\n\tif(!$link){die("Could not connect to MySQL");}\r\n\tmysql_select_db($dbname,$link) or die ("could not open db".mysql_error());\r\n}\r\n\r\n\r\n?>\r\n<\/pre>\n
\r\n<?php include('connect.php');?>\r\n\r\n<!DOCTYPE html PUBLIC "-\/\/W3C\/\/DTD XHTML 1.0 Transitional\/\/EN" "http:\/\/www.w3.org\/TR\/xhtml1\/DTD\/xhtml1-transitional.dtd">\r\n<html xmlns="http:\/\/www.w3.org\/1999\/xhtml">\r\n<head>\r\n<meta http-equiv="Content-Type" content="text\/html; charset=utf-8" \/>\r\n<title>Reorder Test<\/title>\r\n<\/head>\r\n\r\n<link rel="stylesheet" type="text\/css" href="development-bundle\/themes\/base\/ui.all.css"\/>\r\n\r\n<style type="text\/css">\r\n\t#sortable { \r\n\t\tlist-style-type: none; \r\n\t\tmargin: 0; \r\n\t\tpadding: 0; \r\n\t\twidth: 60%; \r\n\t}\r\n\t#sortable li { \r\n\t\tmargin: 0 3px 3px 3px; \r\n\t\tpadding: 0.4em; \r\n\t\tpadding-left: 1.5em; \r\n\t\tfont-size: 1.4em; \r\n\t\theight: 18px; \r\n\t}\r\n\t#sortable li span { \r\n\t\tposition: absolute; \r\n\t\tmargin-left: -1.3em; \r\n\t}\r\n<\/style>\r\n\r\n<script type="text\/javascript" src="js\/jquery-1.3.2.min.js"> <\/script>\r\n<script type="text\/javascript" src="js\/jquery-ui-1.7.2.custom.min.js"><\/script>\r\n\r\n<body>\r\n\r\n<\/body>\r\n<\/html>\r\n<\/pre>\n
\r\n<body>\r\n\t<ul id="sortable">\r\n <?php\r\n\t$sql = "select * from report_items order by Category_Display_Order ASC";\r\n\t$query = mysql_query($sql);\r\n\t\r\n\twhile($row = mysql_fetch_array($query))\r\n\t{\r\n\t\t$catid = $row['Category_ID'];\r\n\t\t$catname = $row['Category_Name'];\t\r\n\t\techo "<li id='item_$catid' class='ui-state-default'><span class='ui-icon ui-icon-arrowthick-2-n-s'><\/span>$catname<\/li>";\t\t\r\n\t}\r\n\r\n\t?> \r\n <\/ul>\r\n<\/body> <\/pre>\n
\n<\/a><\/p>\n\r\n<script type="text\/javascript">\r\n\t$(function() {\r\n\t\t$("#sortable").sortable({\r\n\t\t\tplaceholder: 'ui-state-highlight',\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t});\r\n\t\t$("#sortable").disableSelection();\r\n\t});\r\n<\/script>\r\n<\/pre>\n
\n<\/a><\/p>\n\r\n<script type="text\/javascript">\r\n\t$(function() {\r\n\t\t$("#sortable").sortable({\r\n\t\t\tplaceholder: 'ui-state-highlight',\r\n\t\t\tstop: function(i) {\r\n\t\t\t\tplaceholder: 'ui-state-highlight'\r\n\t\t\t\t$.ajax({\r\n\t\t\t\t\ttype: "GET",\r\n\t\t\t\t\turl: "updatedb.php",\r\n\t\t\t\t\tdata: $("#sortable").sortable("serialize")});\r\n\t\t\t}\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t});\r\n\t\t$("#sortable").disableSelection();\r\n\t});\r\n<\/script>\r\n\r\n<\/pre>\n
\nitem[]=1&item[]=2&item[]=3&item[]=4&item[]=5&item[]=6<\/p>\n
\nSave the changes to you file we are done with test.php. Here is my full source of test.php up to this point.<\/p>\n\r\n<?php include('connect.php');?>\r\n\r\n<!DOCTYPE html PUBLIC "-\/\/W3C\/\/DTD XHTML 1.0 Transitional\/\/EN" "http:\/\/www.w3.org\/TR\/xhtml1\/DTD\/xhtml1-transitional.dtd">\r\n<html xmlns="http:\/\/www.w3.org\/1999\/xhtml">\r\n<head>\r\n<meta http-equiv="Content-Type" content="text\/html; charset=utf-8" \/>\r\n<title>Reorder Test<\/title>\r\n<\/head>\r\n\r\n<link rel="stylesheet" type="text\/css" href="development-bundle\/themes\/base\/ui.all.css"\/>\r\n\r\n<style type="text\/css">\r\n\t#sortable { \r\n\t\tlist-style-type: none; \r\n\t\tmargin: 0; \r\n\t\tpadding: 0; \r\n\t\twidth: 60%; \r\n\t}\r\n\t#sortable li { \r\n\t\tmargin: 0 3px 3px 3px; \r\n\t\tpadding: 0.4em; \r\n\t\tpadding-left: 1.5em; \r\n\t\tfont-size: 1.4em; \r\n\t\theight: 18px; \r\n\t}\r\n\t#sortable li span { \r\n\t\tposition: absolute; \r\n\t\tmargin-left: -1.3em; \r\n\t}\r\n<\/style>\r\n\r\n<script type="text\/javascript" src="js\/jquery-1.3.2.min.js"><\/script>\r\n<script type="text\/javascript" src="js\/jquery-ui-1.7.2.custom.min.js"><\/script>\r\n<script type="text\/javascript">\r\n\t$(function() {\r\n\t\t$("#sortable").sortable({\r\n\t\t\tplaceholder: 'ui-state-highlight',\r\n\t\t\tstop: function(i) {\r\n\t\t\t\tplaceholder: 'ui-state-highlight'\r\n\t\t\t\t$.ajax({\r\n\t\t\t\t\ttype: "GET",\r\n\t\t\t\t\turl: "updatedb.php",\r\n\t\t\t\t\tdata: $("#sortable").sortable("serialize")});\r\n\t\t\t}\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t});\r\n\t\t$("#sortable").disableSelection();\r\n\t});\r\n<\/script>\r\n<body>\r\n\t<ul id="sortable">\r\n <?php\r\n\t$sql = "select * from report_items order by Category_Display_Order ASC";\r\n\t$query = mysql_query($sql);\r\n\t\r\n\twhile($row = mysql_fetch_array($query))\r\n\t{\r\n\t\t$catid = $row['Category_ID'];\r\n\t\t$catname = $row['Category_Name'];\t\r\n\t\techo "<li id='item_$catid' class='ui-state-default'><span class='ui-icon ui-icon-arrowthick-2-n-s'><\/span>$catname<\/li>";\t\t\r\n\t}\r\n\r\n\t?> \r\n <\/ul>\r\n<\/body>\r\n<\/html>\r\n\r\n<\/pre>\n
\r\n<?php\r\ninclude('connect.php');\r\n\r\nforeach($_GET['item'] as $key=>$value) {\r\n\tmysql_query("UPDATE report_items SET Category_Display_Order = '$key' WHERE Categoryt_ID ='$value';");\r\n}\r\n?> <\/pre>\n
\n<\/a><\/p>\n