Advertisement
cdsatrian

Order AutoUpdate

May 1st, 2013
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.67 KB | None | 0 0
  1. DATABASE examples
  2. ==============
  3. /*
  4. #table creation  
  5.  
  6. USE `test`;  
  7.  
  8. DROP TABLE IF EXISTS `tbl_order`;  
  9. CREATE TABLE IF NOT EXISTS `tbl_order` (  
  10.  `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key untuk table tbl_order',  
  11.  `id_order` varchar(30) NOT NULL COMMENT 'ID Order',  
  12.  `status` tinyint(2) unsigned NOT NULL DEFAULT 0,  
  13.  PRIMARY KEY(`id`)  
  14. ) ENGINE=MyISAM COMMENT='tabel untuk menyimpan data order';  
  15.  
  16. INSERT INTO `tbl_order`(`id`,`id_order`,`status`) VALUES  
  17. (NULL,'PO00007',1),
  18. (NULL,'PO00006',1),
  19. (NULL,'PO00005',0),
  20. (NULL,'PO00004',1),
  21. (NULL,'PO00003',2),
  22. (NULL,'PO00002',1),
  23. (NULL,'PO00001',3);
  24. */
  25. FILE : db.php
  26. ==============
  27. <?php
  28. //database configuration  
  29. $dbhost='localhost';  
  30. $dbuser='root';  
  31. $dbpass='';  
  32. $dbname='test';  
  33. //database connection  
  34. $db=new mysqli($dbhost,$dbuser,$dbpass,$dbname);  
  35. ?>
  36. FILE : order.php
  37. ==============
  38. <?php
  39. include 'db.php';
  40. $sql="SELECT * FROM tbl_order";
  41. $result=$db->query($sql);  
  42. function selection($id,$value=0){
  43.   $options=array('Pending','Confirmed','Cancelled','Shipped');
  44.   echo "<select id='$id'>\n";
  45.   for($i=0;$i<4;$i++){
  46.     echo "<option".($value==$i?' selected="selected"':'')." value='$i'>".$options[$i]."</option>\n";
  47.   }
  48.   echo "/<select>\n";
  49. }
  50. ?>
  51. <!DOCTYPE html>
  52. <html>
  53.   <head>
  54.     <title>Edit </title>
  55.     <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
  56.     <style type="text/css">
  57.       th {font-family:arial,sans-serif;font-size:12px;background-color:#bbf;}
  58.       td {font-family:arial,sans-serif;font-size:12px;}
  59.     </style>
  60.   </head>
  61.   <body>
  62.     <table>
  63.       <thead>
  64.         <tr>
  65.           <th>No</th>
  66.           <th>ID Order</th>
  67.           <th>Status</th>
  68.         </tr>
  69.       </thead>
  70.       <tbody>
  71. <?php
  72. $i=0;
  73. while($records=$result->fetch_object()){
  74.   echo "
  75.        <tr>
  76.          <td>".++$i."</td>
  77.          <td>".$records->id_order."</td>
  78.          <td>";
  79.   selection('s'.$records->id,$records->status);
  80.   echo "
  81.          </td>
  82.        </tr>\n";
  83. }
  84. ?>
  85.       </tbody>
  86.     </table>
  87.     <script>
  88.       $(document).ready(function(){
  89.         $('select').change(function(){
  90.           $.post("order.process.php", { status: $(this).val(), id: $(this).attr('id').substring(1) } );
  91.         });
  92.       });
  93.     </script>
  94.   </body>
  95. </html>
  96.    
  97. FILE : order.process.php
  98. ==============
  99. <?php
  100. include 'db.php';
  101. if(isset($_POST['status']) && isset($_POST['id'])){
  102.   $db=new mysqli($dbhost,$dbuser,$dbpass,$dbname);  
  103.   $sql="UPDATE tbl_order "
  104.       ."SET status='".$_POST['status']."' "
  105.       ."WHERE id='".$_POST['id']."'";
  106.   $db->query($sql);
  107.   $db->close();
  108. }
  109. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement