Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // (c) by krmax44 (http://krmax44.tk)
- // more about this script here: http://blog.krmax44.tk/09-08-2015-sortable-mysql
- // index.php-file
- <html>
- <head>
- <script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
- <script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
- <script src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
- <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">
- <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>
- <title>mySQL sortable</title>
- <script type="text/javascript">
- $(document).ready(
- function(){
- $("#sortable").sortable({
- update: function(){
- $.ajax({
- url: "save.php",
- type: "POST",
- data: $("#sortable").sortable("serialize"),
- error: function(){
- alert("There was an error while sending.");
- }
- });
- }
- });
- }
- );
- </script>
- </head>
- <body>
- <div class="container">
- <ul class="list-group" id="sortable">
- <?php
- mysql_connect("localhost", "root", "");
- mysql_select_db("sortable");
- $res = mysql_query("SELECT text, id FROM sortable ORDER BY position ASC");
- while ($row = mysql_fetch_row($res)) {
- echo '<li class="list-group-item" id="sortable_'.$row[1].'">'.$row[0].'</li>';
- }
- ?>
- </ul>
- </div>
- </body>
- </html>
- // save.php-file
- <?php
- mysql_connect("localhost", "root", "");
- mysql_select_db("sortable");
- $sortable = $_POST["sortable"];
- for ($i = 0; $i < count($sortable); $i++) {
- mysql_query("UPDATE sortable SET position = '".$i."' WHERE id = '".$sortable[$i]."'");
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement