Advertisement
Guest User

JIMMY 2

a guest
Jan 30th, 2015
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.88 KB | None | 0 0
  1. <?php
  2.   /*
  3.   $colors = array(
  4.     array('url' => 'http://img.com/img1.jpg?color=red', 'color_code' => '1', 'color_picture' => '1'),
  5.     array('url' => 'http://img.com/img2.jpg?color=red', 'color_code' => '1'),
  6.     array('url' => 'http://img.com/img3.jpg?color=red', 'color_code' => '1'),
  7.   );
  8.   */
  9.   $colors = array(
  10.     array('name' => 'Red', 'id' => 1, 'images' => array('img1-red.jpg','img2-red.jpg','img3-red.jpg')),
  11.     array('name' => 'Green', 'id' => 2, 'images' => array('img1-green.jpg','img2-green.jpg')),
  12.     array('name' => 'Blue', 'id' => 3, 'images' => array('img1-blue.jpg'))
  13.   );
  14.  
  15.   //echo json_encode($result);
  16. ?>
  17. <html>
  18. <head>
  19.   <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet">
  20. </head>
  21. <body>
  22.   <div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
  23.  
  24.     <!-- Wrapper for slides -->
  25.     <div class="carousel-inner" role="listbox">
  26.       <div class="item active">
  27.         <img src="http://img2.game-oldies.com/sites/default/files/packshots/nintendo-game-boy-advance/pokemon.jpg" alt="...">
  28.         <div class="carousel-caption">
  29.         </div>
  30.       </div>
  31.       <div class="item">
  32.         <img src="http://img2.game-oldies.com/sites/default/files/packshots/nintendo-game-boy-advance/pokemon.jpg" alt="...">
  33.         <div class="carousel-caption">
  34.         </div>
  35.       </div>
  36.     </div>
  37.  
  38.     <!-- Controls -->
  39.     <a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
  40.       <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
  41.       <span class="sr-only">Previous</span>
  42.     </a>
  43.     <a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
  44.       <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
  45.       <span class="sr-only">Next</span>
  46.     </a>
  47.   </div>
  48.  
  49.   <?php if (!empty($colors)): ?>
  50.     <select class="color-option">
  51.       <?php foreach ($colors as $color): ?>
  52.         <option value="<?php echo htmlspecialchars($color['id']); ?>"
  53.                 data-links="<?php echo htmlspecialchars(json_encode($color['images'])); ?>"><?php echo htmlspecialchars($color['name']); ?></option>
  54.       <?php endforeach; ?>
  55.     </select>
  56.   <?php endif; ?>
  57.  
  58.   <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
  59.   <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.2/js/bootstrap.min.js"></script>
  60.   <script>
  61.     $(function() {
  62.       var color_option = $('.color-option');
  63.      
  64.       if (color_option.length) {
  65.         color_option.on('change', function() {
  66.           var selectedId = $(this).val();
  67.           var name = $(this).find(':selected').text();
  68.           var links = $(this).find(':selected').data('links');
  69.          
  70.           console.log(selectedId, name, links);
  71.          
  72.           if (!links.length) {
  73.             return;
  74.           }
  75.          
  76.           var carousel = $('#carousel-example-generic');
  77.          
  78.           if (carousel.length) {
  79.             carousel.carousel('pause');
  80.            
  81.             var carousel_inner = carousel.find('.carousel-inner');
  82.             if (carousel_inner.length) {
  83.               carousel_inner.empty();
  84.              
  85.               for (var i = 0, ilen = links.length; i < ilen; i++) {
  86.                 var active = '';
  87.                 if (i == 0) {
  88.                   active = ' active';
  89.                 }
  90.                 carousel_inner.append(
  91.                   $('<div class="item' + active + '">').append(
  92.                     $('<img alt="">').prop('src', links[i]).attr('data-color_code', selectedId)
  93.                   )
  94.                 );
  95.               }
  96.             }
  97.            
  98.             carousel.carousel('cycle');
  99.           }
  100.          
  101.         });
  102.       }
  103.      
  104.     });
  105.   </script>
  106. </body>
  107. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement