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 stm32开发clion时遇到的编译问题
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法