Ver Mensaje Individual
  #5 (permalink)  
Antiguo 03/12/2010, 08:32
Ojete
 
Fecha de Ingreso: agosto-2010
Ubicación: Oakland california
Mensajes: 393
Antigüedad: 13 años, 7 meses
Puntos: 3
Respuesta: Ayuda para editar el orden de los registros de una tabla

Cita:
Iniciado por masterpuppet Ver Mensaje
Porque campo los estas ordenando?, estas seguro que el ajax actualiza bien el orden de los registros?
beuno lo deje como esta en el tutorial solo cambie la conexion y la tabla, probe con la tabla que muestran en el tutorial pero tampoco, sigue haciendo lo mismo... Alguna idea?


Código PHP:
Ver original
  1. <script type="text/javascript">
  2. $(document).ready(function(){
  3.                            
  4.     $(function() {
  5.         $("#contentLeft ul").sortable({ opacity: 0.2, cursor: 'move', update: function() {
  6.             var order = $(this).sortable("serialize") + '&action=updateRecordsListings';
  7.             $.post("updateDB.php", order, function(theResponse){
  8.                 $("#contentRight").html(theResponse);
  9.             });                                                              
  10.         }                                
  11.         });
  12.     });
  13.  
  14. });
  15. </script>
  16.  
  17. </head>
  18. <body>
  19.  
  20.     <div id="contentWrap">
  21.  
  22.         <div id="contentTop">
  23.           <p>To read more &amp; download the demo files:            <a href="http://www.webresourcesdepot.com/dynamic-dragn-drop-with-jquery-and-php">Dynamic Drag’n Drop With jQuery And PHP</a><br />
  24.           </p>
  25.           <p>Drag'n drop the items below. Their new positions are updated in the database with an Ajax request in the backend.<img src="arrow-down.png" alt="Arrow Down" width="32" height="32" /></p>
  26.       </div>
  27.    
  28.         <div id="contentLeft">
  29.             <ul>
  30.                 <?php
  31.                 $query  = "SELECT * FROM records ORDER BY recordListingID desc";
  32.                 $result = mysql_query($query);
  33.                
  34.                 while($row = mysql_fetch_array($result, MYSQL_ASSOC))
  35.                 {
  36.                 ?>
  37.                     <li id="recordsArray_<?php echo $row['recordID']; ?>"><?php echo $row['recordID'] . ". " . $row['recordText']; ?></li>
  38.                 <?php } ?>
  39.             </ul>
  40.         </div>
  41.        
  42.         <div id="contentRight">
  43.           <p>Array will be displayed here.</p>
  44.           <p>&nbsp; </p>
  45.         </div>
  46.    
  47.     </div>
  48.  
  49. </body>
  50. </html>

Código PHP:
Ver original
  1. <?php
  2. require("db.php");
  3.  
  4. $action                 = mysql_real_escape_string($_POST['action']);
  5. $updateRecordsArray     = $_POST['recordsArray'];
  6.  
  7. if ($action == "updateRecordsListings"){
  8.    
  9.     $listingCounter = 1;
  10.     foreach ($updateRecordsArray as $recordIDValue) {
  11.        
  12.         $query = "UPDATE records SET recordListingID = " . $listingCounter . " WHERE recordID = " . $recordIDValue;
  13.         mysql_query($query) or die('Error, insert query failed');
  14.         $listingCounter = $listingCounter + 1; 
  15.     }
  16.    
  17.     echo '<pre>';
  18.     print_r($updateRecordsArray);
  19.     echo '</pre>';
  20.     echo 'If you refresh the page, you will see that records will stay just as you modified.';
  21. }
  22. ?>


Código SQL:
Ver original
  1. SET FOREIGN_KEY_CHECKS=0;
  2. -- ----------------------------
  3. -- Table structure for records
  4. -- ----------------------------
  5. CREATE TABLE `records` (
  6.   `recordID` INT(11) NOT NULL AUTO_INCREMENT,
  7.   `recordText` VARCHAR(255) DEFAULT NULL,
  8.   `recordListingID` INT(11) DEFAULT NULL,
  9.   PRIMARY KEY  (`recordID`)
  10. );
  11.  
  12. -- ----------------------------
  13. -- Records
  14. -- ----------------------------
  15. INSERT INTO `records` VALUES ('1', 'Once dropped, an Ajax query is activated', '3');
  16. INSERT INTO `records` VALUES ('2', 'Dragging changes the opacity of the item', '2');
  17. INSERT INTO `records` VALUES ('3', 'Returned array can be found at the right', '1');
  18. INSERT INTO `records` VALUES ('4', 'It is very very easy', '4');