dongxunhua2054 2016-05-16 10:37
浏览 38
已采纳

在表单OnChange中交换数字的顺序

This is a bit of a weird one, but it would be nice to get some advice on where to begin with this as I've already done a bit of searching and not returned any results.

I have a list of Categories within my database, with an cat_order column to list them correctly. This is a basic numbering system which can be edited. Some examples:

cat_order |  cat_name
   1           Union Rooms 
   2           Jesters
   3           Popworld
   4           Walkabout
   5           Two Trees
   6           Crash Manor

When these categories are displayed to a user, they will be displayed in ASC order using the cat_order column. So it's a pretty simple, basic way to order them. If I swapped 5 and 1, Two Trees would show in Union Rooms place and vise versa.

On the admin side of things, I am trying to create a system to change these ordering numbers. I have chosen a simple form method like so:

<form action="" method="post" id="sort_cat" name="sort_cat">

<?
$sql5 = "SELECT * FROM `new_categories` WHERE `cat_up_lvl` = '$cat' ORDER BY `venue_closed` ASC, `cat_order` ASC";
$result5 = $conn->query($sql5);

          if ($result5->num_rows > 0) {
          // output data of each row
          while($row5 = $result5->fetch_assoc()) {
          $cat_id5 = $row5['cat_id'];
          $cat_name5 = $row5['cat_name'];
          $cat_short_code5 = $row5['short_code'];
          $cat_city5 = $row5['cat_city'];
          $cat_order5 = $row5['cat_order'];

?>        
<div class="form-group">
    <div class="input-group">
      <input type="hidden" id="cat_id[]" value="<? echo $cat_id5; ?>">    
      <input type="number" class="form-control" id="order[]" placeholder="Order..." value="<? echo $cat_order5; ?>">
      <div class="input-group-addon" style="width:80%;"><b><? echo $cat_name5; ?></b> (<? echo $cat_short_code5; ?>)</div>
    </div>
  </div>

<?
          }
     }
?>
</form>

I am looking for a way (If it would even be possible) to swap the numbers around within the form once I change one number. So if on my form I wanted to change Jesters (2) with Walkabout (4), I could change only ONE of the numbers in the form, Jesters (4) and the system would somehow realize - "Okay, Walkabout used to be 4, Jesters used to be 2, Now that Jesters has become 4, Walkabout SHOULD be 2". And somehow do this processing automatically?

Would this use JS or could this be done in PHP and how would be the best way to go about it?

  • Just for an aim, this kind of beats the problem of having multiple numbers by mistake within the database, so you wouldn't get 1, 1, 1, 2, 3, 4 and so on...
  • 写回答

1条回答 默认 最新

  • douzhuiqiu4923 2016-05-16 13:27
    关注

    I would suggest you to use sortable plugin within jqueryUI.

    https://jqueryui.com/sortable/

    Here's an example how to use it together with PHP

    jQuery UI Sortable, then write order into a database

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 像这种代码要怎么跑起来?
  • ¥15 怎么改成循环输入删除(语言-c语言)
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件