0

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...
Snappysites
  • 804
  • 1
  • 10
  • 41

1 Answers1

0

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

Community
  • 1
  • 1
Richard
  • 1,045
  • 7
  • 11