0

当我点击不同的产品类别时,我无法获得对应的产品项目,而只显示活动类别的项目。当我单击另一个导航项时,以前显示的项目保持不变,即无法在选项卡窗格中更改类“显示活动”

代码:

<?php
$this->load->view ( 'templates/header' );
?>
<section class="container">
   <ul class="nav nav-pills mb-3" id="pills-tab" role="tablist">
      <?php $flag = 0; foreach ( $gallery_category as $gallery_category_data ) {?>
      <li class="nav-item">
         <a class="nav-link <?php if($flag==0){echo 'active';$flag ++;}?>" 
            id="pills<?php echo $gallery_category_data['gallery_category_id'];?>" 
            data-toggle="pill" href="#<?php echo $gallery_category_data['gallery_category_id'];?>" 
            role="tab" aria-controls="pill<?php echo $gallery_category_data['gallery_category_id'];?>" 
            aria-selected="<?php if($flag==0){echo 'true';}?>"><?php echo $gallery_category_data['gallery_category_name'];?></a>
      </li>
      <?php     }?>
   </ul>
   <div class="tab-content" id="pills-tabContent">
      <?php $counter=0; foreach ( $gallery_category as $gallery_category_data ) {?>
      <div class="tab-pane fade <?php if($counter==0){echo 'show active';}?>" id="<?php echo $gallery_category_data['gallery_category_id'];?>" role="tabpanel" aria-labelledby="pills<?php echo $gallery_category_data['gallery_category_id'];?>">
         <h6><?php echo $gallery_category_data['gallery_category_name']; ?></h6>
         <div class="row">
            <?php foreach ( $gallery_category_data ['gallery_item'] as $gallery_item_data ) {?>
            <div class="col-md-3 mb-3">
               <figure>
                  <img src="<?php echo base_url();?>assets/img/products/<?php echo $gallery_item_data['gallery_image']; ?>" class="img-fluid">
                  <a href="product_details.php">
                     <figcaption class="text-center">
                        <h6 class="mt-3">Saree</h6>
                        <p class="text-muted"><strong>Rs. 799 /-</strong></p>
                     </figcaption>
                  </a>
               </figure>
            </div>
            <?php }?>
         </div>
      </div>
      <?php $counter++; }?>
   </div>
</section>
<?php
$this->load->view ( 'templates/footer' );
?>

我无法诊断此代码中的问题,请帮助我解决此问题

4

2 回答 2

0

只需匹配foreach循环内的条件:

$cat = $gallery_category_data['gallery_category_name'];    
foreach ( $gallery_category_data ['gallery_item'] as $gallery_item_data ) 
{    
  if($gallery_item_data['category name in table'] == $cat){

  } 
}
于 2019-01-01T19:31:21.083 回答
0

如果您更换:

<?php if($flag==0){echo 'active';$flag ++;}?>

经过

<?php if($flag==0){echo 'nav'.$flag;$flag ++;}?>

并删除:

<?php if($counter==0){echo 'show active';}?>

在你的脚本之后添加这个

<script>
$(document).ready(function(){
  $( ".nav0" ).trigger( "click" );
});
</script>

很好 ?

于 2018-09-20T12:55:25.300 回答