jQuery Sortable Portlet Save position to database
Hi,
I'm trying to figure out how to make a multi column jquery sortable Portlet, save the column positions to a database onChange, I know that sortable has a serialize function but i really haven't had any luck getting it to work. If someone could please help me out with this. I have spent hours on google trying to find something.
<!doctype html>
- <?php include('connection.php'); ?>
- <html lang="en">
- <head>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <title>jQuery UI Sortable - Portlets</title>
- <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
- <link rel="stylesheet" href="/resources/demos/style.css">
- <style>
- body {
- min-width: 520px;
- }
- .new {
- width: 300px;
- float: left;
- font-size:18px;
- color:#C95F0D;
- border:#636262 solid;
- text-align:center;
- vertical-align:middle;
- line-height:90px;
- }
- .clear {
- width: 300px;
- float: right;
- padding-bottom: 50px;
- font-size:18px;
- color:#7433D0;
- border:#636262 solid;
- margin-right:100px;
- }
- .star {
- width: 300px;
- float: right;
- padding-bottom: 50px;
- font-size:18px;
- color:#7433D0;
- border:#636262 solid;
- margin-right:100px;
- }
- .column {
- width: 200px;
- float: left;
- padding-bottom: 100px;
- }
- .portlet {
- margin: 0 1em 1em 0;
- padding: 0.3em;
- }
- .portlet-header {
- padding: 0.2em 0.3em;
- margin-bottom: 0.5em;
- position: relative;
- }
- .portlet-toggle {
- position: absolute;
- top: 50%;
- right: 0;
- margin-top: -8px;
- }
- .portlet-content {
- display:none; padding: 0.4em;
- }
- .portlet-placeholder {
- border: 1px dotted black;
- margin: 0 1em 1em 0;
- height: 50px;
- }
- </style>
- <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
- <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
- <script>
- $(document).ready(function(){
- $( ".column" ).sortable({
- connectWith: ".column",
- handle: ".portlet-header",
- cancel: ".portlet-toggle",
- placeholder: "portlet-placeholder ui-corner-all",
- stop: function() {
- var dat = [];
- var i = 0;
- $(".column").each(function() {
- dat[i++] = [this.id,$(this).sortable("toArray")]; // this.id is the column id, the 2nd element are the job id's in that column
- });
-
- $.ajax({
- method: "POST",
- url: "save_order.php",
- data: { data: dat }
- });
- }
- });
- $( ".portlet" )
- .addClass( "ui-widget ui-widget-content ui-helper-clearfix ui-corner-all" )
- .find( ".portlet-header" )
- .addClass( "ui-widget-header ui-corner-all" )
- .prepend( "<span class='ui-icon ui-icon-plusthick portlet-toggle'></span>");
- $( ".portlet-toggle" ).on( "click", function() {
- var icon = $( this );
- icon.toggleClass( "ui-icon-minusthick ui-icon-plusthick" );
- icon.closest( ".portlet" ).find( ".portlet-content" ).toggle();
- });
- });
- </script>
- </head>
- <body>
- <div class="toolbar"><a href="newjobs.php"><div class="new">Get New Jobs</div></a><div class="clear">Clear Job</div></div>
-
- <div style="clear:both;"></div>
-
- <?php
- // define status value to label mapping
- $status_map = array();
- $status_map[1] = 'New';
- $status_map[2] = 'Artwork Rec';
- $status_map[3] = 'Approved & Ordered';
- $status_map[4] = 'In Production';
- $status_map[5] = 'Delivered';
- $status_map[6] = 'To Be Invoiced';
- // query for all the data you want, in the order that you want it
- $query = "SELECT * FROM jobs ORDER BY status ASC, job_title DESC";
- $result = mysqli_query($con,$query);
- $data = array();
- while($row = mysqli_fetch_assoc($result))
- {
- $data[$row['status']][] = $row; // index/pivot the data using the status value - 1..6
- }
- // note: i used some made-up data in the $data array at this point. the above query code should work, but is untested.
- // loop over the data and produce the output
- foreach($status_map as $key=>$label)
- {
- echo "<div class='column' id='COL_$key'>\n";
- echo "<h3>$label</h3>\n";
- if(isset($data[$key])) // is there any data from the database for this key/status
- {
- foreach($data[$key] as $row)
- {
- echo "<div class='portlet' id='ID_{$row['id']}'>\n";
- echo "<div class='portlet-header'>";
-
- if ($row['urgent'] == 1) {
- echo "<p style='color:red;'>".$row['job_title']."</p>";
- } else {
- echo "<p style='color:black;'>".$row['job_title']."</p>";
- }
- echo "</div>\n";
- echo "<div class='portlet-content'><a href='pdfs/{$row['pdf_link']}' target='_blank'>View PDF</a></div>\n";
- echo "</div>\n";
- }
- }
- echo "</div>\n";
- }
- ?>
- </body>
- </html>
And this is the save_order.php file. I am unsure how to write the update statement getting the column id etc from the previous page.
<?php
- include('connection.php');
- foreach ($_POST['status'] as $value) {
- // Database stuff
- }
- ?>
All help much appreciated.
Regards,
MsKazza