[問題] 自定排序
下面的 code 是假設資料庫的 idno(primary key) 是連續的且從1開始
例如:
idno order
---- -----
1 2
2 3
3 1
4 5
5 4
----
不過,因為我把idno設為auto increment
當某筆資料被刪除以後,idno就會變成不連續的,且不一定從1開始
例如:
idno
----
2
3
5
----
請問我該怎麼修改,好讓order欄位可以從1開始且數字是連續的?
例如:
idno order
---- -----
2 1
3 2
5 3
---- -----
<jQuery>
========================================
$(document).ready(function(){
function slideout(){
setTimeout(function(){
$("#response").slideUp("slow", function () {
});
}, 2000);
}
$("#response").hide();
$(function() {
$("#list ul").sortable({ opacity: 0.8, cursor: 'move', update: function() {
var order = $(this).sortable("serialize") + '&update=update';
$.post("updateList.php", order, function(theResponse){
$("#response").html(theResponse);
$("#response").slideDown('slow');
slideout();
});
}
});});});
========================
<HTML>
========================
<body>
<div id="container">
<div id="list">
<div id="response"> </div>
<ul>
<?php
include("connect.php");
// $query = "SELECT id, text FROM dragdrop ORDER BY listorder ASC";
$query = "SELECT id, name, type FROM project_list ORDER BY 'order' ASC";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$id = stripslashes($row['id']);
$name = stripslashes($row['name']);
$type = stripslashes($row['type']);
?>
<li id="arrayorder_<?php echo $id ?>"> <?php echo $name?> <?php echo $type; ?>
<div class="clear"></div>
</li>
<?php
}
?>
</ul>
</div>
</div>
</body>
==========================
updateList.php
==========================
<?php
include("connect.php");
$array = $_POST['arrayorder'];
if ($_POST['update'] == "update") {
$count = 1;
foreach ($array as $idval) {
$query = "UPDATE project_list SET 'order' = " . $count . " WHERE id = "
. $idval;
mysql_query($query) or die('Error, insert query failed');
$count ++;
}
echo 'Updated!';
}
?>
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 220.142.203.235
※ 編輯: magicfx 來自: 220.142.203.235 (06/07 22:57)
討論串 (同標題文章)
Ajax 近期熱門文章
PTT數位生活區 即時熱門文章