terça-feira, 9 de outubro de 2012

jquery com PHP - Ordenar campos e salvar no MySQL

Script Excelente

Retirado de: http://coder1.com/articles/jquery-sortables-php-and-mysql

// make your db connection up here ...
$link = mysql_connect('localhost', 'user', 'pass');
$db = mysql_select_db('dbname', $link);
// handle POST
if ($_POST) {
// use $i to increment the weight
// loop through post array in the order it was submitted
foreach ($_POST['fruit'] as $fruit_id) {
// update the row
$result = mysql_query("UPDATE fruit SET weight=".$i." WHERE id=".mysql_real_escape_string($fruit_id));
// increment weight to make the next fruit heavier
<script type="text/javascript" src="jquery-1.2.6.js"></script>
<script type="text/javascript" src="jquery-ui-1.5.1.min.js"></script>
<script type="text/javascript">
// when the entire document has loaded, run the code inside this function
// Wow! .. One line of code to make the unordered list drag/sortable!
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']?>">
<ul id="fruit_list">
<?php // query fruit table and print out each item
$result = mysql_query("SELECT id, name, weight FROM fruit ORDER BY weight");
// print the list items
while ($row = mysql_fetch_assoc($result)) {
echo '<li>
<input type="hidden" name="fruit[]" value="'.$row['id'].'" />
<input type="submit" name="reorder" value="Re-Order Fruit" />

Nenhum comentário:

Postar um comentário