2016-11-12 18 views
0

我想安排一些主標題下的字幕,但它重複標題。如何安排Yii2中的標題下的字幕

<div id="downloads" class="tab-pane" role="tabpanel"> 
    <?php 
    $product_id = $model->id; 
    $downloads_model = Printingmachinedownloads::find()->where(['productid'=>$product_id])->all(); 
    foreach ($downloads_model as $doc) { 
     $doc_type = $doc['type']; 
     $doc_label = $doc['documentlabel']; 
     $doc_title = $doc['documentname']; 
     ?> 
     <div class="amazingcarousel-container-1 tab_style"> 
      <h3><?php echo $doc_type;?></h3> 
      <a target = '_blank' href="<?php echo Yii::$app->homeUrl?>images/printingmachine/downloads/<?php echo $doc_title; ?>"> 
       <?php echo $doc_label ?> 
      </a> 
     </div> 
     <?php 
     } 
     // $doc_title = $downloads_model[0]->documentlabel; 
     ?> 
    </div> 
</div> 

輸出是:

Current O/P

我需要的是

Brochures 
abc 
def 
ghi 


Specificationsheet 
xyz 
lmn 
opq 

誰能告訴我,我有什麼關係?

由於提前

回答

0

如果您還沒有與新概念的MySQL版本組問題,您可以使用類似:

... 
$downloads_model = Printingmachinedownloads::find()->where(['productid'=>$product_id])->groupBy(['type'])->all(); 

foreach($downloads_model as $doc): 
    <div class="amazingcarousel-container-1 tab_style"> 
    <h3><?= type['type'] ?></h3> 
    $subtitles = Printingmachinedownloads::find()->where(['productid'=>$product_id, 'type' => $doc['type'])->all(); 
    foreach($subtitles as $subtitle): 
    <a target = '_blank' href="<?php echo Yii::$app->homeUrl?>images/printingmachine/downloads/<?php echo $subtitle['documentname']; ?>"> 
    <?php echo $subtitle['documentlabel'] ?> 
    </a> 
enforeach; 
</div> 
endforeach; 
... 

如果有問題,集團通過可以使用小非標準的方式,它不是最好的做法和有效的方法:

$downloads_model = Printingmachinedownloads::find()->where(['productid'=>$product_id])->orderBy(['type'=>SORT_DESC])->all(); 

$previous_title = ''; $iteration = 1; 
foreach($downloads_model as $doc): 
$subtitles = Printingmachinedownloads::find()->where(['productid'=>$product_id, 'type' => $doc['type'])->all(); 
$count = count($subtitles); 

$previous_title = $previous_title ?: $doc['type']; 

if($previous_title == $doc['type'] && $count == $iteration): 

     <div class="amazingcarousel-container-1 tab_style"> 
     <h3><?= $previous_title ?></h3> 
     foreach($subtitles as $subtitle): 
     <a target = '_blank' href="<?php echo Yii::$app->homeUrl?>images/printingmachine/downloads/<?php echo $subtitle['documentname']; ?>"> 
     <?php echo $subtitle['documentlabel'] ?> 
     </a> 
    enforeach; 
    </div> 

    $previous_title = ''; $iteration = 1; 
else: 
    $iteration++; 
endif; 
endforeach; 

我沒有測試它,但類似的東西應該工作。 但我的建議和有效的方法將是你使用關係表1標題容器另一個包含子標題的相關表。