Advertisement
Guest User

Sortable mySQL with PHP, jQuery, jQuery UI and Bootstrap

a guest
Sep 18th, 2015
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 2.03 KB | None | 0 0
  1. // (c) by krmax44 (http://krmax44.tk)
  2. // more about this script here: http://blog.krmax44.tk/09-08-2015-sortable-mysql
  3. // index.php-file
  4. <html>
  5.     <head>
  6.         <script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
  7.         <script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
  8.         <script src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
  9.         <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" integrity="sha256-MfvZlkHCEqatNoGiOXveE8FIwMzZg4W85qfrfIFBfYc= sha512-dTfge/zgoMYpP7QbHy4gWMEGsbsdZeCXz7irItjcC3sPUFtf0kuFbDz/ixG7ArTxmDjLXDmezHubeNikyKGVyQ==" crossorigin="anonymous">
  10.         <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js" integrity="sha256-Sk3nkD6mLTMOF0EOpNtsIry+s1CsaqQC1rVLTAy+0yc= sha512-K1qjQ+NcF2TYO/eI3M6v8EiNYZfA95pQumfvcVrTHtwQVDG+aHRqLi/ETn2uB+1JqwYqVG3LIvdm9lj6imS/pQ==" crossorigin="anonymous"></script>
  11.         <title>mySQL sortable</title>
  12.         <script type="text/javascript">
  13.         $(document).ready(
  14.             function(){
  15.                 $("#sortable").sortable({
  16.                     update: function(){
  17.                         $.ajax({
  18.                             url: "save.php",
  19.                             type: "POST",
  20.                             data: $("#sortable").sortable("serialize"),
  21.                             error: function(){
  22.                                 alert("There was an error while sending.");
  23.                             }
  24.                         });
  25.                     }
  26.                 });
  27.             }
  28.         );
  29.         </script>
  30.     </head>
  31.     <body>
  32.         <div class="container">
  33.             <ul class="list-group" id="sortable">
  34.                 <?php
  35.                     mysql_connect("localhost", "root", "");
  36.                     mysql_select_db("sortable");
  37.                     $res = mysql_query("SELECT text, id FROM sortable ORDER BY position ASC");
  38.                     while ($row = mysql_fetch_row($res)) {
  39.                         echo '<li class="list-group-item" id="sortable_'.$row[1].'">'.$row[0].'</li>';
  40.                     }
  41.                 ?>
  42.             </ul>
  43.         </div>
  44.     </body>
  45. </html>
  46.  
  47. // save.php-file
  48. <?php
  49.     mysql_connect("localhost", "root", "");
  50.     mysql_select_db("sortable");
  51.     $sortable = $_POST["sortable"];
  52.     for ($i = 0; $i < count($sortable); $i++) {
  53.         mysql_query("UPDATE sortable SET position = '".$i."' WHERE id = '".$sortable[$i]."'");
  54.     }
  55. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement