Jump to content

Move record up down


james182

Recommended Posts

I am trying to get the script to insert the new order into the mysql db but don't know how any help.

 

below is the working up down list but i need it to save / update the new positions.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-US">

<head profile="http://gmpg.org/xfn/11">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />


<title>test list sort</title>

<script type='text/javascript' src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>

<script>
$(document).ready( function () {

   $('table tbody a.control').live('click', function (e) {
	e.preventDefault();

	var tr = $(this); // Start with this link and work upwards from there, this allows the TR element itself to be the control if desired
	var iterations = 0;
	while(tr.attr('tagName') != 'TR') {
		tr = tr.parent();
		iterations += 1;
		if (iterations == 100) {
			return false; // Bail out, surely something is wrong or there is lots and lots of html in there
		}
	}

	if ($(this).attr('rel') == 'up' && tr.prev().length)
		tr.fadeTo('medium', 0.1, function () {
			tr.insertBefore(tr.prev()).fadeTo('fast', 1);
			// If you want to do any more to the table after the move put it here, I am running the reorder function (defined below)
			reorder();

		});
	else if ($(this).attr('rel') == 'down' && tr.next().length)
	   // Same as above only this is for moving elements down instead of up
		tr.fadeTo('fast', 0.1, function () {
			tr.insertAfter(tr.next()).fadeTo('fast', 1);
			// If you want to do any more to the table after the move put it here, I am running the reorder function (defined below)
			reorder();
		});
	else
		//Can't do anything with these
		return false;
	return false;
});

function reorder () {
	var position = 1;
	$('table tbody tr').each(function () {
		// Change the text of the first TD element inside this TR
		$('td:first', $(this)).text(position);
		//Now remove current row class and add the correct one
		$(this).removeClass('row1 row2').addClass( position % 2 ? 'row1' : 'row2');
		position += 1;
	});
}
});

</script>

</head>
<body>


<?php
echo '<table>
	<thead>
		<tr>
   			<th>Position</th><th>Name</th><th>Controls</th>
		</tr>
	</thead>
	<tbody>';

	$con = mysql_connect("localhost", "*******", "*********") or die(mysql_error());	
	mysql_select_db("my_test", $con) or die(mysql_error());

	$sql = "SELECT * FROM tbl_items ORDER BY item_order ASC";
	$result = mysql_query($sql);

	$i = 1;
	while($r = mysql_fetch_assoc($result)):	

	echo '<tr class="'.$i.'">
		<td>'. $i .'</td><td>'. $r['item_name'] .'</td><td> <a href="#up" rel="up" class="control">Up</a> <a href="#down" rel="down" class="control">Down</a></td>
	</tr>';

	$i++;
	endwhile;

echo '</tbody></table>';

?>

</body>
</html>

 

 

db table:

 

item_id | item_name | item_order

---------|--------------|-------------

    1      |  green      |    1

    2      |  blue        |    2

    3      |  red          |    3

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.