iBrightDev
November 27th, 2007, 19:18
you will need to get a couple things for this to work properly. thanks goes to Joe for helping me get this working 100%.
download all the js files here.
http://www.mct-hosting.com/downloads/js_Sortable_List_Files.zip
in the example below, we will use the sortable list to sort email addresses.
make a file, for argument sake, call it index.php
<?
require('db.php');
$demo = new SortableExample();
$list = $demo->getList();
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Sortables Ajax Example</title>
<link rel="stylesheet" type="text/css" href="style.css">
<script src="js/prototype.js"></script>
<script src="js/scriptaculous.js"></script>
<script>
Event.observe(window,'load',init,false);
function init() {
Sortable.create('listContainer',{tag:'div',onUpdate:updateLi st});
}
function updateList(container)
{
var url = 'ajax.php';
var params = Sortable.serialize(container.id);
var ajax = new Ajax.Request(url,{
method: 'post',
parameters: params,
onLoading: function(){$('workingMsg').show()},
onLoaded: function(){$('workingMsg').hide()}
});
}
function handleResponse(req) {
// this function will fire after the ajax request is complete...but we have nothing to do here
}
</script>
</head>
<body>
<div id="listContainer">
<?
$break = "\r\n";
foreach($list as $item) {
if ($item['display'] <= 7) {
echo '<div id="item_' . $item['id'] . '">' . $item['email'] . '</div>' . $break;
} else {
echo '<div id="item_' . $item['id'] . '">' . $item['email'] . '</div>' . $break;
}
}
?>
</div>
<div id="workingMsg" style="display:none;">Updating database...</div>
</body>
</html>
now make a second file, and call it db.php. Make sure to configure everything properly
<?
class SortableExample {
var $conn;
var $user = 'DB USER NAME GOES HERE';
var $pass = 'DB PASSWORD GOES HERE';
var $dbname = 'DB NAME GOES HERE';
var $host = 'DB HOST HERE'; //ususally localhost
function SortableExample() {
$this->conn = mysql_connect($this->host, $this->user, $this->pass);
mysql_select_db($this->dbname,$this->conn);
}
function getList() {
$sql = "SELECT * FROM TABLE_NAME_HERE ORDER BY display, id";
$recordSet = mysql_query($sql,$this->conn);
$results = array();
while($row = mysql_fetch_assoc($recordSet)) {
$results[] = $row;
}
return $results;
}
function updateList($orderArray) {
$id = 1;
foreach($orderArray as $catid)
{
$catid = (int) $catid;
$sql = "UPDATE `TABLE_NAME_HERE` SET display={$id} WHERE id={$catid}";
$recordSet = mysql_query($sql,$this->conn) OR DIE(mysql_error());
$id++;
}
}
}
?>
make another file, and call it ajax.php
<?
session_start();
require('db.php');
$demo = new SortableExample();
$demo->updateList($_POST['listContainer']);
?>
last, but not least, make a css file called style.css
body {
font-family: Arial, Verdana, sans-serif;
font-size: 12px;
background: #FFFFFF;
}
div#listContainer {
width: 400px;
border: 2px solid #336699;
background: #EFF7FF;
}
div#listContainer div {
border: 1px solid #336699;
margin: 5px;
padding: 3px 5px;
background: #DFEFFF;
font-weight: bold;
cursor: move;
}
.menuItem {
border: 1px solid #1a8918;
margin: 5px;
padding: 3px 5px;
background: #cefbcd;
font-weight: bold;
cursor: move;
}
after you have all four files made, make sure you have downloaded the js files that i provided a link to at the top of this posting, and place them in a file named js. this should automatically happen if you unzip them properly, but, if you dont, make sure you fix whatever error you made.
next, you will need a mysql file to use. so, if you have mysql knowledge, continue, otherwise, you may not want to go on with this. so, create a database, db user/password, and then configure the below sql code for your settings, and upload it through phpmyadmin
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
--
-- Database: `DB_NAME_HERE`
--
-- --------------------------------------------------------
--
-- Table structure for table `TABLE_NAME_HERE`
--
CREATE TABLE `TABLE_NAME_HERE` (
`id` int(25) NOT NULL auto_increment,
`display` int(255) NOT NULL default '0',
`email` varchar(100) default NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=15 ;
--
-- Dumping data for table `TABLE_NAME_HERE`
--
INSERT INTO `TABLE_NAME_HERE` VALUES (1, 6, 'fakeEmail_1@fakeEmail.com');
INSERT INTO `TABLE_NAME_HERE` VALUES (2, 13, 'fakeEmail_2@fakeEmail.com');
INSERT INTO `TABLE_NAME_HERE` VALUES (3, 14, 'fakeEmail_3@fakeEmail.com');
INSERT INTO `TABLE_NAME_HERE` VALUES (4, 4, 'fakeEmail_4@fakeEmail.com');
INSERT INTO `TABLE_NAME_HERE` VALUES (5, 2, 'fakeEmail_5@fakeEmail.com');
INSERT INTO `TABLE_NAME_HERE` VALUES (6, 9, 'fakeEmail_6@fakeEmail.com');
INSERT INTO `TABLE_NAME_HERE` VALUES (7, 1, 'fakeEmail_7@fakeEmail.com');
INSERT INTO `TABLE_NAME_HERE` VALUES (8, 7, 'fakeEmail_8@fakeEmail.com');
INSERT INTO `TABLE_NAME_HERE` VALUES (9, 12, 'fakeEmail_9@fakeEmail.com');
INSERT INTO `TABLE_NAME_HERE` VALUES (10, 8, 'fakeEmail_10@fakeEmail.com');
INSERT INTO `TABLE_NAME_HERE` VALUES (11, 5, 'fakeEmail_11@fakeEmail.com');
INSERT INTO `TABLE_NAME_HERE` VALUES (12, 10, 'fakeEmail_12@fakeEmail.com');
INSERT INTO `TABLE_NAME_HERE` VALUES (13, 3, 'fakeEmail_13@fakeEmail.com');
INSERT INTO `TABLE_NAME_HERE` VALUES (14, 11, 'fakeEmail_14@fakeEmail.com');
finally, upload all files to your server, and you will have a sortable list that will automatically update any time you drag and drop.
download all the js files here.
http://www.mct-hosting.com/downloads/js_Sortable_List_Files.zip
in the example below, we will use the sortable list to sort email addresses.
make a file, for argument sake, call it index.php
<?
require('db.php');
$demo = new SortableExample();
$list = $demo->getList();
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Sortables Ajax Example</title>
<link rel="stylesheet" type="text/css" href="style.css">
<script src="js/prototype.js"></script>
<script src="js/scriptaculous.js"></script>
<script>
Event.observe(window,'load',init,false);
function init() {
Sortable.create('listContainer',{tag:'div',onUpdate:updateLi st});
}
function updateList(container)
{
var url = 'ajax.php';
var params = Sortable.serialize(container.id);
var ajax = new Ajax.Request(url,{
method: 'post',
parameters: params,
onLoading: function(){$('workingMsg').show()},
onLoaded: function(){$('workingMsg').hide()}
});
}
function handleResponse(req) {
// this function will fire after the ajax request is complete...but we have nothing to do here
}
</script>
</head>
<body>
<div id="listContainer">
<?
$break = "\r\n";
foreach($list as $item) {
if ($item['display'] <= 7) {
echo '<div id="item_' . $item['id'] . '">' . $item['email'] . '</div>' . $break;
} else {
echo '<div id="item_' . $item['id'] . '">' . $item['email'] . '</div>' . $break;
}
}
?>
</div>
<div id="workingMsg" style="display:none;">Updating database...</div>
</body>
</html>
now make a second file, and call it db.php. Make sure to configure everything properly
<?
class SortableExample {
var $conn;
var $user = 'DB USER NAME GOES HERE';
var $pass = 'DB PASSWORD GOES HERE';
var $dbname = 'DB NAME GOES HERE';
var $host = 'DB HOST HERE'; //ususally localhost
function SortableExample() {
$this->conn = mysql_connect($this->host, $this->user, $this->pass);
mysql_select_db($this->dbname,$this->conn);
}
function getList() {
$sql = "SELECT * FROM TABLE_NAME_HERE ORDER BY display, id";
$recordSet = mysql_query($sql,$this->conn);
$results = array();
while($row = mysql_fetch_assoc($recordSet)) {
$results[] = $row;
}
return $results;
}
function updateList($orderArray) {
$id = 1;
foreach($orderArray as $catid)
{
$catid = (int) $catid;
$sql = "UPDATE `TABLE_NAME_HERE` SET display={$id} WHERE id={$catid}";
$recordSet = mysql_query($sql,$this->conn) OR DIE(mysql_error());
$id++;
}
}
}
?>
make another file, and call it ajax.php
<?
session_start();
require('db.php');
$demo = new SortableExample();
$demo->updateList($_POST['listContainer']);
?>
last, but not least, make a css file called style.css
body {
font-family: Arial, Verdana, sans-serif;
font-size: 12px;
background: #FFFFFF;
}
div#listContainer {
width: 400px;
border: 2px solid #336699;
background: #EFF7FF;
}
div#listContainer div {
border: 1px solid #336699;
margin: 5px;
padding: 3px 5px;
background: #DFEFFF;
font-weight: bold;
cursor: move;
}
.menuItem {
border: 1px solid #1a8918;
margin: 5px;
padding: 3px 5px;
background: #cefbcd;
font-weight: bold;
cursor: move;
}
after you have all four files made, make sure you have downloaded the js files that i provided a link to at the top of this posting, and place them in a file named js. this should automatically happen if you unzip them properly, but, if you dont, make sure you fix whatever error you made.
next, you will need a mysql file to use. so, if you have mysql knowledge, continue, otherwise, you may not want to go on with this. so, create a database, db user/password, and then configure the below sql code for your settings, and upload it through phpmyadmin
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
--
-- Database: `DB_NAME_HERE`
--
-- --------------------------------------------------------
--
-- Table structure for table `TABLE_NAME_HERE`
--
CREATE TABLE `TABLE_NAME_HERE` (
`id` int(25) NOT NULL auto_increment,
`display` int(255) NOT NULL default '0',
`email` varchar(100) default NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=15 ;
--
-- Dumping data for table `TABLE_NAME_HERE`
--
INSERT INTO `TABLE_NAME_HERE` VALUES (1, 6, 'fakeEmail_1@fakeEmail.com');
INSERT INTO `TABLE_NAME_HERE` VALUES (2, 13, 'fakeEmail_2@fakeEmail.com');
INSERT INTO `TABLE_NAME_HERE` VALUES (3, 14, 'fakeEmail_3@fakeEmail.com');
INSERT INTO `TABLE_NAME_HERE` VALUES (4, 4, 'fakeEmail_4@fakeEmail.com');
INSERT INTO `TABLE_NAME_HERE` VALUES (5, 2, 'fakeEmail_5@fakeEmail.com');
INSERT INTO `TABLE_NAME_HERE` VALUES (6, 9, 'fakeEmail_6@fakeEmail.com');
INSERT INTO `TABLE_NAME_HERE` VALUES (7, 1, 'fakeEmail_7@fakeEmail.com');
INSERT INTO `TABLE_NAME_HERE` VALUES (8, 7, 'fakeEmail_8@fakeEmail.com');
INSERT INTO `TABLE_NAME_HERE` VALUES (9, 12, 'fakeEmail_9@fakeEmail.com');
INSERT INTO `TABLE_NAME_HERE` VALUES (10, 8, 'fakeEmail_10@fakeEmail.com');
INSERT INTO `TABLE_NAME_HERE` VALUES (11, 5, 'fakeEmail_11@fakeEmail.com');
INSERT INTO `TABLE_NAME_HERE` VALUES (12, 10, 'fakeEmail_12@fakeEmail.com');
INSERT INTO `TABLE_NAME_HERE` VALUES (13, 3, 'fakeEmail_13@fakeEmail.com');
INSERT INTO `TABLE_NAME_HERE` VALUES (14, 11, 'fakeEmail_14@fakeEmail.com');
finally, upload all files to your server, and you will have a sortable list that will automatically update any time you drag and drop.