Advertisement
jahrichie

Untitled

Mar 15th, 2011
901
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.74 KB | None | 0 0
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  2.    "http://www.w3.org/TR/html4/loose.dtd">
  3.  
  4. <html lang="en">
  5. <head>
  6.     <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  7.     <title>GALLERY</title>
  8.     <meta name="generator" content="TextMate http://macromates.com/">
  9.     <meta name="author" content="jahrichie">
  10.             <link rel="stylesheet" type="text/css" href="../../admin.css" />
  11.     <!-- Date: 2011-03-15 -->
  12.     <script type="text/javascript" src="mootools-1.2.4.js"></script>
  13.    
  14.     <style type="text/css">
  15.  
  16.  
  17.  
  18.     #sortable-list      { padding:0; }
  19.     #sortable-list li   { padding:4px 8px; color:#000; cursor:move; list-style:none; width:500px; background:#ddd; margin:10px 0; border:1px solid #999; }
  20.     #message-box        { background:#fffea1; border:2px solid #fc0; padding:4px 8px; margin:0 0 14px 0; width:500px; }
  21.  
  22.  
  23.  
  24.  
  25.  
  26.     </style>
  27.    
  28.     <script type="text/javascript">
  29.     /* when the DOM is ready */
  30.     window.addEvent('domready', function() {
  31.         /* grab important elements */
  32.         var sortInput = document.id('sort_order');
  33.         var submit = document.id('autoSubmit');
  34.         var messageBox = document.id('message-box');
  35.         var list = document.id('sortable-list');
  36.  
  37.         /* get the request object ready;  re-use the same Request */
  38.         var request = new Request({
  39.             url: '<?php echo $_SERVER["REQUEST_URI"]; ?>',
  40.             link: 'cancel',
  41.             method: 'post',
  42.             onRequest: function() {
  43.                 messageBox.set('text','Updating the sort order in the database.');
  44.             },
  45.             onSuccess: function() {
  46.                 messageBox.set('text','Database has been updated.');
  47.             }
  48.         });
  49.         /* worker function */
  50.         var fnSubmit = function(save) {
  51.             var sortOrder = [];
  52.             list.getElements('li').each(function(li) {
  53.                 sortOrder.push(li.retrieve('id'));
  54.             });
  55.             sortInput.value = sortOrder.join(',');
  56.             if(save) {
  57.                 request.send('sort_order=' + sortInput.value + '&ajax=' + submit.checked + '&do_submit=1&byajax=1');
  58.             }
  59.         };
  60.  
  61.         /* store values */
  62.         list.getElements('li').each(function(li) {
  63.             li.store('id',li.get('title')).set('title','');
  64.         });
  65.  
  66.         /* sortables that also *may* */
  67.         new Sortables(list,{
  68.             constrain: true,
  69.             clone: true,
  70.             revert: true,
  71.             onComplete: function(el,clone) {
  72.                 fnSubmit(submit.checked);
  73.             }
  74.         });
  75.  
  76.         /* ajax form submission */
  77.         document.id('dd-form').addEvent('submit',function(e) {
  78.             if(e) e.stop();
  79.             fnSubmit(true);
  80.         });
  81.  
  82.  
  83.     });
  84.     </script>
  85.  
  86.  
  87.  
  88.  
  89. <script type="text/javascript">
  90.  
  91.     /* when the DOM is ready */
  92.     jQuery(document).ready(function() {
  93.       /* grab important elements */
  94.       var sortInput = jQuery('#sort_order');
  95.       var submit = jQuery('#autoSubmit');
  96.       var messageBox = jQuery('#message-box');
  97.       var list = jQuery('#sortable-list');
  98.       /* create requesting function to avoid duplicate code */
  99.       var request = function() {
  100.         jQuery.ajax({
  101.           beforeSend: function() {
  102.             messageBox.text('Updating the sort order in the database.');
  103.           },
  104.           complete: function() {
  105.             messageBox.text('Database has been updated.');
  106.           },
  107.           data: 'sort_order=' + sortInput[0].value + '&ajax=' + submit[0].checked + '&do_submit=1&byajax=1', //need [0]?
  108.           type: 'post',
  109.           url: '<?php echo $_SERVER["REQUEST_URI"]; ?>'
  110.         });
  111.       };
  112.       /* worker function */
  113.       var fnSubmit = function(save) {
  114.         var sortOrder = [];
  115.         list.children('li').each(function(){
  116.           sortOrder.push(jQuery(this).data('id'));
  117.         });
  118.         sortInput.val(sortOrder.join(','));
  119.         console.log(sortInput.val());
  120.         if(save) {
  121.           request();
  122.         }
  123.       };
  124.       /* store values */
  125.       list.children('li').each(function() {
  126.         var li = jQuery(this);
  127.         li.data('id',li.attr('title')).attr('title','');
  128.       });
  129.       /* sortables */
  130.       list.sortable({
  131.         opacity: 0.7,
  132.         update: function() {
  133.           fnSubmit(submit[0].checked);
  134.         }
  135.       });
  136.       list.disableSelection();
  137.       /* ajax form submission */
  138.       jQuery('#dd-form').bind('submit',function(e) {
  139.         if(e) e.preventDefault();
  140.         fnSubmit(true);
  141.       });
  142.     });
  143.  
  144. </script>
  145.  
  146.    
  147. </head>
  148. <body>
  149.     <div id="header">
  150.         <ul>
  151.                 <a href="../../">   <li >biography</li></a>
  152.                 <a href="#">    <li>media</li></a>
  153.             <span class="selected"><li>editorial gallery</li></span>
  154.                 <a href="#">    <li>gallery 2</li></a>
  155.                 <a href="#">    <li>gallery 3</li></a>
  156.              
  157.                         </ul>
  158.     </div>  <!-- end header-->
  159.         <div id="whereyouare"> <H1>EDITORIAL GALLERY ORDER</H1></div>   <!-- end whereyouat-->
  160.     <div id="sub-menu">
  161.             <ul>
  162.                         <a href="../">  <li>make</li></a>
  163.                     <a href="#">    <li>delete/edit</li></a>
  164.                     <span class="sub-selected"> <li>change order</li></span>
  165.                             </ul>
  166.         </div>  <!-- end sub-menu-->
  167.  
  168.  
  169.  
  170.  
  171.                     <div id="edit-nav">
  172.                         <?php
  173.                         $connection = mysql_connect("localhost", "root", "root") or die(mysql_error());
  174.                         mysql_select_db("cmurraym_gallery") or die(mysql_error());
  175.                             $query = 'SELECT id, title, copy FROM editorial ORDER BY sort_order ASC';
  176.                             $result = mysql_query($query,$connection) or die(mysql_error().': '.$query);
  177.                             if(mysql_num_rows($result)) {
  178.  
  179.                         ?>
  180.                         <p>Drag and drop the elements below.  The database gets updated on every drop.</p>
  181.  
  182.                         <div id="message-box"><?php echo $message; ?> Waiting for sortation submission...</div>
  183.  
  184.                         <form id="dd-form" action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="post">
  185.                         <p>
  186.                             <input type="checkbox" value="1" name="autoSubmit" id="autoSubmit" <?php if($_POST['autoSubmit']) { echo 'checked="checked"'; } ?> />
  187.                             <label for="autoSubmit">Automatically submit on drop event</label>
  188.                         </p>
  189.  
  190.                         <ul id="sortable-list">
  191.                             <?php
  192.                                 $order = array();
  193.                                 while($item = mysql_fetch_assoc($result)) {
  194.                                     echo '<li title="',$item['id'],'">',$item['title'],'</li>';
  195.                                     $order[] = $item['id'];
  196.                                 }
  197.                             ?>
  198.                         </ul>
  199.                         <br />
  200.                         <input type="hidden" name="sort_order" id="sort_order" value="<?php echo implode(',',$order); ?>" />
  201.                         <input type="submit" name="do_submit" value="Submit Sortation" class="button" />
  202.                         </form>
  203.                         <?php } else { ?>
  204.  
  205.                             <p>Sorry!  There are no items in the system.</p>
  206.  
  207.                         <?php } ?>
  208.  
  209.                         on form submission
  210.                         if(isset($_POST['do_submit']))  {
  211.                             split the value of the sortation
  212.                             $ids = explode(',',$_POST['sort_order']);
  213.                             run the update query for each id
  214.                             foreach($ids as $index=>$id) {
  215.                                 $id = (int) $id;
  216.                                 if($id != '') {
  217.                                     $query = 'UPDATE test_table SET sort_order = '.($index + 1).' WHERE id = '.$id;
  218.                                     $result = mysql_query($query,$connection) or die(mysql_error().': '.$query);
  219.                                 }
  220.                             }
  221.  
  222.                        
  223.                             if($_POST['byajax']) { die(); } else { $message = 'Sortation has been saved.'; }
  224.                         }
  225.                    
  226.  
  227. </div>  <!-- end edit-nav -->
  228.  
  229. </body>
  230. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement