Det hela är i första hand ett javascriptproblem. Använder du en select skulle det kunna göras något på det här hållet:
<?php
include "conn.php"; // databasanslutningen
if (isset($_POST['tVal'])) {
$tVal = explode("|", substr($_POST['tVal'], 0, -1));
foreach($tVal as $key => $val) {
$townorder = $key + 1;
$sql = "UPDATE tabell SET townorder=$townorder WHERE id=$val";
mysql_query($sql);
}
}
?>
<script type="text/javascript">
<!--
function moveOption(obj,dir){
if (obj.selectedIndex<0 || (obj.selectedIndex+dir)<0 ||
(obj.selectedIndex+dir)>=obj.options.length)
return;
var tempVal=obj.options[obj.selectedIndex+dir].value;
var tempText=obj.options[obj.selectedIndex+dir].text;
obj.options[obj.selectedIndex+dir].value = obj.options[obj.selectedIndex].value;
obj.options[obj.selectedIndex+dir].text = obj.options[obj.selectedIndex].text;
obj.options[obj.selectedIndex].value = tempVal;
obj.options[obj.selectedIndex].text = tempText;
obj.selectedIndex = obj.selectedIndex+dir;
}
function selectAll(obj){
for(i=0; i<obj.sel1.options.length; i++){
obj.tVal.value += obj.sel1.options[i].value+"|";
}
}
function sortByVal(obj){
var tempArray = new Array();
for(i=0; i<obj.options.length; i++){
tempArray [i]= new Array(obj.options[i].value, obj.options[i].text);
}
tempArray.sort();
for(i=0; i<obj.options.length; i++){
obj.options[i].value = tempArray[i][0];
obj.options[i].text = tempArray[i][1];
}
};
function sortByText(obj){
var tempArray = new Array();
for(i=0; i<obj.options.length; i++){
tempArray [i]= new Array(obj.options[i].text, obj.options[i].value);
}
tempArray.sort();
for(i=0; i<obj.options.length; i++){
obj.options[i].text = tempArray[i][0];
obj.options[i].value = tempArray[i][1];
}
};
// -->
</script>
<form name="form1" action="<?=$_SERVER['PHP_SELF'] ?>"
method="post" onsubmit="selectAll(this)">
<input type="hidden" name="tVal" value="">
<table cellspacing="3">
<tr>
<td rowspan="2">
<select name="sel1" size="10" style="float:left">
<?php
$sql = "SELECT id, town, townorder FROM tabell ORDER BY townorder";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) {
echo "<option value=\"{$row['id']}\">{$row['town']}</option>\n";
}
?>
</select>
</td>
<td>
<a href="javascript:moveOption(document.form1.sel1,-1)">
<img src="../images/pilupp.gif" border="0" alt="Flytta ett steg upp"
title="Flytta ett steg upp">
</a><br>
<a href="javascript:moveOption(document.form1.sel1,+1)">
<img src="../images/pilned.gif" border="0" alt="Flytta ett steg ned"
title="Flytta ett steg ned">
</a>
</td>
</tr>
<tr>
<td valign="bottom">
<input type="submit" value="Spara ändring" style="width:140px;"><br>
</td>
</tr>
</table>
</form>
En liten omarbetning av något jag hade i gömmorna.
