2017-01-31 322 views
-3

我想要檢查複選框,檢查一下,我正在推入過濾器ID到數組和過濾器ID來到應該檢查爲了這個我得到$ _GET ['過濾器']所有過濾器,但同時我用$濾波器陣列相比,這意味着in_array始終返回false請幫我有人in_array總是返回false?

下面是HTML和PHP代碼,

<?php echo $header; 
if(isset($_GET['filter'])) 
{ 
    $selected_filter[] = explode(",",$_GET['filter']); 
    print_r($selected_filter); 
    // $flag = 1; 
} 
?> 




    <div class="wrapper"> 
    <?php echo $column_left; ?> 
    <div class="filterDiv"> 
     <h3>Filters by :</h3> 
     <!-- <div class="flList"> 
     <p>price</p> 
     <div class="flDrop price_Module"><div class="flDropDiv">price div</div></div>   
     </div> --> 
     <!-- <div class="list-group-item flList filter_options"> 
     <div class="flDrop price_Module"><div class="flDropDiv">price div</div></div>   
     <span id="amtmin"></span> - <span id="amtmax"></span> 
     <input type="hidden" id="amount" readonly style="border:0; color:#f6931f; font-weight:bold;"> 
     <div id="slider-range"></div> 
     </div> --> 

     <div class="flList"> 
     <p>Categories</p> 
     <div class="flDrop"> 
      <div class="flDropDiv category_fl"> 
       <?php foreach ($categories as $category) { 
       $category_name = explode("(",$category['name']) 
       ?> 
       <div class="flItems"><a href="<?php echo $category['href'] ?>"> <?php echo $category_name[0]; ?> </a> </div> 
       <?php }?> 
      </div> 
     </div>  
     </div> 

      <?php //echo "<pre>"; print_r($filter_groups); die; ?> 
     <?php foreach ($filter_groups as $filter_group) { ?> 
      <div class="flList"> 
      <p><?php echo $filter_group['name']; ?></p> 
       <div class="flDrop"> 
       <div class="flDropDiv"> 
        <?php 
        // $filter_string = ""; 
        foreach ($filter_group['filter'] as $filter) { 
         // $filter_string = implode(',',$filter['']); 
         // print_r($filter_string); 
         if(isset($selected_filter) && in_array($filter['filter_id'],$selected_filter)) 
         { ?> 
         <input name="filter[]" type="checkbox" value="<?php echo $filter['filter_id']; ?>" checked> <?php echo $filter['name'] ?> 
         <?php } else {?> 
          <input name="filter[]" type="checkbox" value="<?php echo $filter['filter_id']; ?>"> <?php echo $filter['name'] ?> 
         <?php }?> 
        <?php }?> 
       </div> 
      </div>  
      </div> 
     <?php }?> 
     <div class="flList"> 
     <?php 
     $amount_min = $min_product_price;$amount_max = $max_product_price; 
     if(isset($_GET['amtmin']) && $_GET['amtmin']!=""){ 
      $amount_min = $_GET['amtmin']; 
     } 
     if(isset($_GET['amtmax']) && $_GET['amtmax']!=""){ 
      $amount_max = $_GET['amtmax']; 
     } 
     ?> 
     <p>Price</p> 
     <div class="flDrop price_Module"> 
      <div class="flDropDiv"> 
       <!-- <a class="list-group-item fltrHdng">Price</a> -->  
       <div class="price_slide"> 
       <!-- <label for="amount">Price range</label> -->     
       <input type="hidden" id="amount" readonly style="border:0; color:#f6931f; font-weight:bold;"> 
       <div id="slider-range"></div> 
       <div class="cf mb10"></div> 
       <span class="pull-left" id="amtmin"></span> <span class="pull-right" id="amtmax"></span>     
       </div> 
      </div> 
     </div> 
     </div> 
     <div class="cf"></div> 
     <?php 
      if(isset($min_p) && ($max_p)){ 
      ?> 
      <div class="filterSelectPrice filterSelect afr"> 
      <div id="fSprice" class="fSbtn">Rs.<?php echo $min_p; ?> - Rs.<?php echo $max_p; ?> <span class="clear fSp"></span></div> 
      </div> 
     <?php } ?> 
     <div class="filterSelect" id="auto_filter_values"></div>  
    </div> 
    </div> 

下面是我的jQuery代碼:

<script type="text/javascript"> 

$('input[name^=\'filter\']').on('change', function() { 
     filter = []; 
     $('input[name^=\'filter\']:checked').each(function(element) { 
     filter.push(this.value); 
     $(filter).prop('checked',true); 
     }); 
     // console.log(filter);return false; 
     window.history.pushState("","",'<?php echo $action; ?>&filter=' + filter.join(',')); 
     //return false; 
     $.ajax({ 
     url: '<?php echo $action; ?>&filter=' + filter, 
     type: 'post', 
     cache: false, 
     contentType: false, 
     processData: false, 
     beforeSend: function() { 
     $('#content').block({message:'<img src="<?php echo HTTP_SERVER; ?>image/ajax-loader.gif">'}); 
     //$("#content").fadeOut("slow"); 
     }, 
     complete: function() {  

     // $(this).remove(); 
     //$("#content").fadeIn("slow"); 
     $('#content').unblock(); 
     },  
     success: function(data) { 
     $("body").empty().append(data); 
     },  
     error: function(xhr, ajaxOptions, thrownError) { 
     alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText); 
     } 
    }); 
    }); 
</script> 
+0

那麼,你不顯示'$ filter ['filter_id']'或'$ selected_filter'我們應該怎麼知道? – AbraCadaver

+0

selected_filter'陣列 ( [0] =>數組 ( [0] => 81 [1] => 86 [2] => 151 ) )' $濾波器[ '過濾器_id'] ''81' – Mohan

+0

我想檢查'if(isset($ selected_filter)&& in_array($ filter ['filter_id'],$ selected_filter)) {?> 「checked><?php echo $ filter ['name']?> <?php} else {?> 「><?php echo $ filter ['name']?> ' – Mohan

回答

0

你是定義一個做[]當多維數組:

$selected_filter[] = explode(",",$_GET['filter']); 

in_array()正在尋找在頂層是一個數組,所以它不會工作。只要做:

$selected_filter = explode(",",$_GET['filter']);