not sure, but might be an issue: in updateList function you have *$orderid = 0;* $add="catID ".strval($catid)." orderid".strval($orderid)."\n" ;
but yet in save_items_order.php you have: *$i = 1;* # will represent the actual order (starting at 1). $query = "UPDATE hln_stations SET order_col = $i WHERE id = $itemid"; in the function you start at 0 in the update file you start at 1. as a follow up -- on the link you posted as your reference they have order starting at 1: public function updateList($orderArray) { *$orderid = 1;* foreach($orderArray as $catid) { $catid = (int) $catid; $sql = "UPDATE categories SET orderid={$orderid} WHERE catid={$catid}"; $recordSet = mysql_query($sql,$this->conn); $orderid++; } } On Sat, Nov 5, 2011 at 5:00 PM, joelbrave <electronicacous...@gmail.com>wrote: > I can't figure out where else to post this question, and though I > don't like the idea of posting a ton of code here, I don't know what > else to do. My code is based on this: > > > http://www.gregphoto.net/index.php/2007/01/16/scriptaculous-sortables-with-ajax-callback/ > > I have a database with a few columns, most notable the id column and > the order_col and a form called "organize.php" that allows you to re- > order the items that it lists with drag-and-drop via script.alicio.us. > > The problem is it doesn't seem to be getting the correct ID for each > row it iterates through to do the updates. I"m posting several files > here, "organize.php" which is what the user interacts with, > connect.php which makes the connections both for the script.alicio.us > and pre-existing parts of the app, and also stores the bulk of the > update code, a file called "ajax.php" which is supposed to do the real- > time updates, and a file called save_items_order.php, followed by the > database schema. If any mods don't like that I posted this here, feel > free to delete the post. I just don't know what else to do, I've been > hacking on this problem since early yesterday morning... > > Here is organize.php: > > <? > require('connect.php'); > $demo = new SortableExample(); > $list = $demo->getList(); > $addtxt=" \n"; > $f = fopen("file.txt", "w"); > fwrite($f, $addtxt); > fclose($f); > > ?> > > <html> > <head> > <meta http-equiv="content-type" content="text/html; > charset=iso-8859-1"> > <title></title> > <style type="text/css"> > <!-- > .style1 {font-family: Verdana, Arial, Helvetica, sans-serif} > .style3 {font-family: Verdana, Arial, Helvetica, sans-serif; font- > size: 14px; } > .style4 {font-size: 16px} > body { > background-color: #333333; > } > body,td,th { > color: #ebebeb; > font-family: Verdana, Geneva, sans-serif; > font-size: 14px; > } > a:link { > color: #78CAEC; > text-decoration: none; > } > a:visited { > text-decoration: none; > color: #78CAEC; > } > a:hover { > text-decoration: underline; > } > a:active { > text-decoration: none; > } > .style6 {font-size: 14px} > .style4 p { > font-family: Verdana, Geneva, sans-serif; > } > .style4 a { > font-family: Verdana, Geneva, sans-serif; > font-size: 14px; > } > .rightside { > color: #FFC300; > } > --> > </style> > > <script type="text/javascript" src="javascripts/lib/prototype.js"></ > script> > <script type="text/javascript" src="javascripts/src/ > scriptaculous.js"></script> > <script> > Event.observe(window,'load',init,false); > function init() { > > Sortable.create('listContainer',{tag:'div',onUpdate:updateList}); > } > 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) { > //do nothing > } > </script> > > > </head> > > <body> > > <a href="add.php">Add entry</a><br> > > > > <b>Drag Station Name to re-organize order of stations!</b> > <br> > <br> > > <div id="listContainer"> > <? > foreach($list as $item) { > ?> > <div id="item_<?=$item['order_col'];?>"><?= > $item['station_display_name'];?></div> > <? > } > ?> > </div> > > <div id="workingMsg" style="display:none;">Updating database...</div> > > > > </body> > </html> > > > Here is Connect.php > > <?php > /// For the following details, > /// please contact your server vendor > > $hostname='mysql'; //// specify host, i.e. 'localhost' > $user='username'; //// specify username > $pass='password'; //// specify password > $dbase='hlnRadioManager'; //// specify database name > $connection = mysql_connect("$hostname" , "$user" , "$pass") > or die ("Can't connect to MySQL"); > $db = mysql_select_db($dbase , $connection) or die ("Can't select > database."); > > > > class SortableExample { > protected $conn; > protected $user = 'username'; > protected $pass = 'password'; > protected $dbname = 'hlnRadioManager'; > protected $host = 'mysql'; > > public function __construct() { > $this->conn = mysql_connect($this->host, $this->user, > $this->pass); > mysql_select_db($this->dbname,$this->conn); > } > > public function getList() { > $sql = "SELECT * FROM hln_stations ORDER BY order_col ASC"; > $recordSet = mysql_query($sql,$this->conn); > $results = array(); > while($row = mysql_fetch_assoc($recordSet)) { > $results[] = $row; > } > return $results; > } > > public function updateList($orderArray) { > $f = fopen("file.txt", "a"); > fwrite($f,"this array has ".strval(count($orderarray))." > members > \n"); > fwrite($f,"****************************************\n"); > fwrite($f,"Order Array".implode("\n",$orderarray)); > $orderid = 0; > foreach($orderArray as $catid) { > $catid = (int) $catid; > > $sql = "UPDATE hln_stations SET > order_col={$orderid} WHERE > id={$catid}"; > > $cr="\n"; > fwrite($f,$catid.$cr); > $add="catID ".strval($catid)." > orderid".strval($orderid)."\n"; > fwrite($f, $add); > $sst=$sql."\n"; > fwrite($f, $sst); > $recordSet = mysql_query($sql,$this->conn); > $orderid++; > } > fclose($f); > } > } > > > > ?> > > > here is ajax.php: > > <? > session_start(); > require('connect.php'); > $demo = new SortableExample(); > $demo->updateList($_POST['listContainer']); > ?> > > > and finally save_items_order.php > > <? > > > if($_POST) { > $items = $_POST['items_list']; > $i = 1; # will represent the actual order (starting at 1). > foreach($items as $itemid) { > $query = "UPDATE hln_stations SET order_col = $i WHERE id = > $itemid"; > mysql_query($query); > echo "<b>$itemid</b><br>"; > > $add=$query; > $f = fopen("file.txt", "a"); > fwrite($f, $add); > fclose($f); > > > $i++; # add 1 to the current value of i (the next order > in the list). > } > } > > ?> > > Here is the db schema: > > -- phpMyAdmin SQL Dump > -- version 2.11.9.6 > -- http://www.phpmyadmin.net > -- > -- Host: mysql > -- Generation Time: Nov 05, 2011 at 08:17 PM > -- Server version: 4.1.14 > -- PHP Version: 5.2.12 > > SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO"; > > -- > -- Database: `hlnRadioManager` > -- > > -- -------------------------------------------------------- > > -- > -- Table structure for table `hln_stations` > -- > > CREATE TABLE IF NOT EXISTS `hln_stations` ( > `id` int(6) NOT NULL auto_increment, > `country_state` varchar(50) NOT NULL default '', > `genre` varchar(25) NOT NULL default '', > `station_title` varchar(60) NOT NULL default '', > `station_display_name` varchar(60) NOT NULL default '', > `station_subtitle` varchar(60) NOT NULL default '', > `station_detailed_description` text NOT NULL, > `stream_url_or_playlist_url` text NOT NULL, > `link_type` varchar(25) NOT NULL default '', > `sdtv_thumbnail_graphic_url` text NOT NULL, > `hdtv_thumbnail_graphic_url` text NOT NULL, > `order_col` int(11) NOT NULL default '0', > UNIQUE KEY `id` (`id`), > UNIQUE KEY `order_col` (`order_col`) > ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=24 ; > > -- > You received this message because you are subscribed to the Google Groups > "Prototype & script.aculo.us" group. > To post to this group, send email to > prototype-scriptaculous@googlegroups.com. > To unsubscribe from this group, send email to > prototype-scriptaculous+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/prototype-scriptaculous?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group. To post to this group, send email to prototype-scriptaculous@googlegroups.com. To unsubscribe from this group, send email to prototype-scriptaculous+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en.