Advertisement
Guest User

PHPAPU

a guest
Nov 5th, 2014
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.17 KB | None | 0 0
  1. SEARCH_IMAGES.PHP
  2.  
  3.  
  4. <?php
  5. // Get the file name
  6. $filename = $_GET['filename'];
  7. // Find all the images
  8. $images = glob("images/file/path/*_{$filename}");
  9. // Return the images as json
  10. echo json_encode($images);
  11. ?>
  12.  
  13.  
  14.  
  15. //////////////////////////////////////////////
  16.  
  17. UPDATE.PHP
  18.  
  19.  
  20. <?php
  21. if (!empty($_GET['id']) && !empty($_GET['value'])) {
  22.    
  23.     $id = $_GET['id'];
  24.     $value = $_GET['value'];
  25.    
  26.     try {
  27.        
  28.         $objDb = new PDO('mysql:host=localhost;dbname=cars', 'root', 'password');
  29.         $objDb->exec('SET CHARACTER SET utf8');
  30.        
  31.         $sql = "SELECT *
  32.                 FROM `categories`
  33.                 WHERE `master` = ".$value;
  34.         $statement = $objDb->prepare($sql);
  35.         $statement->execute(array($value));
  36.         $list = $statement->fetchAll(PDO::FETCH_ASSOC);
  37.        
  38.         if (!empty($list)) {
  39.            
  40.             $out = array('<option value="">Select one</option>');
  41.            
  42.             foreach($list as $row) {
  43.                 $out[] = '<option value="'.$row['id'].'">'.$row['name'].'</option>';
  44.             }
  45.            
  46.             echo json_encode(array('error' => false, 'list' => implode('', $out)));
  47.            
  48.         } else {
  49.             echo json_encode(array('error' => true));
  50.         }
  51.        
  52.     } catch(PDOException $e) {
  53.         echo json_encode(array('error' => true));
  54.     }
  55.    
  56. } else {
  57.     echo json_encode(array('error' => true));
  58. }
  59.  
  60. ?>
  61.  
  62.  
  63.  
  64. /////////////////////////////////////////////////////
  65.  
  66. CORE.JS
  67.  
  68.  
  69. var formObject = {
  70.     run : function(obj) {
  71.         if (obj.val() === '') {
  72.             obj.nextAll('.update').html('<option value="">----</option>').attr('disabled', true);
  73.         } else {
  74.             var id = obj.attr('id');
  75.             var v = obj.val();
  76.             jQuery.getJSON('mod/update.php', { id : id, value : v }, function(data) {
  77.                 if (!data.error) {
  78.                     obj.next('.update').html(data.list).removeAttr('disabled');
  79.                 } else {
  80.                     obj.nextAll('.update').html('<option value="">----</option>').attr('disabled', true);
  81.                 }
  82.             });
  83.         }
  84.     }
  85. };
  86. $(function() {
  87.    
  88.     $('.update').live('change', function() {
  89.         formObject.run($(this));
  90.     });
  91.    
  92. });
  93.  
  94. ////////////////////////////////////////////////////////////
  95.  
  96. INDEX.PHP
  97.  
  98. <?php
  99.  
  100. try {
  101.    
  102.     $objDb = new PDO('mysql:host=localhost;dbname=cars', 'root', 'password');
  103.     $objDb->exec('SET CHARACTER SET utf8');
  104.    
  105.     $sql = "SELECT *
  106.             FROM `categories`
  107.             WHERE `master` = 0";
  108.     $statement = $objDb->query($sql);
  109.     $list = $statement->fetchAll(PDO::FETCH_ASSOC);
  110.    
  111. } catch(PDOException $e) {
  112.     echo 'There was a problem';
  113. }
  114.  
  115.  
  116. ?>
  117.  
  118.  
  119. <form action="" method="post">
  120.        
  121.         <select name="gender" id="gender" class="update">
  122.             <option value="">Select one</option>
  123.             <?php if (!empty($list)) { ?>
  124.                 <?php foreach($list as $row) { ?>
  125.                     <option value="<?php echo $row['id']; ?>">
  126.                         <?php echo $row['name']; ?>
  127.                     </option>
  128.                 <?php } ?>
  129.             <?php } ?>
  130.         </select>
  131.        
  132.         <select name="category" id="category" class="update"
  133.             disabled="disabled">
  134.             <option value="">Please choose brand first!</option>
  135.         </select>
  136.        
  137.        
  138.        
  139.     </form>
  140.  
  141.  
  142.  
  143. <script type="text/javascript">
  144.  
  145. // Every time a category is selected we request the files
  146. $('#category').on('change', function() {
  147.     // If we have an element with images loaded, lets delete it
  148.     var searchResult = $("#search-result").empty();
  149.     // Now we serialize the form data, add an id to the form
  150.     var searchBrand = $('#gender').find('option:selected').text();
  151.     var searchModel = $('#category').find('option:selected').text();
  152.     var fileName = searchBrand + '.' + searchModel + '.jpg';
  153.     var searchData = { filename: fileName };
  154.     // Now we create the ajax request with the form data
  155.     var request = $.getJSON('mod/search_images.php', searchData);
  156.     // If the request success we show the images
  157.     request.done(function(data) {
  158.         // For each image found we add a new image to the DOM
  159.         $.each(data, function(index, image_uri) {
  160.             // First let's create the image element
  161.             var img = $("<img>", {
  162.                 "class": "result-image",
  163.                 "src": image_uri
  164.             });
  165.             // Then we append it to the DOM
  166.             searchResult.append( img );
  167.         });
  168.     });
  169.     // If the search fails, we notify that no results were found
  170.     request.fails(function() {
  171.         alert('No results found');
  172.     });
  173. });
  174.  
  175. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement