2012-10-10 24 views
0

我想要的東西有點難以解釋。所以任何困惑都道歉。我正在檢索爲我的產品創建的一組屬性(總共16個)。這16個屬性可根據類型分爲4列(A,B,C,D)。所以我想創建一個以A,B,C,D作爲標題的表格。然後我想顯示相關列的標籤和數據。以下將給你一個想法,我想要什麼。Magento屬性組 - 在使用foreach的表中顯示數據的僞代碼?

<table> 
<tr> 
<td>heading-A</td> 
<td>heading-B</td> 
<td>heading-C</td> 
<td>heading-D</td> 
</tr> 
<tr> 
<td>label-A</td> 
<td>Data-A</td> 
<td>label-B</td> 
<td>Data-B</td> 
<td>label-C</td> 
<td>Data-C</td> 
<td>Label-D</td> 
<td>Data-D</td> 
</tr> 
<tr>...and so on 
</table 

*不要擔心將標籤-A和數據-A置於標題-A下。我可以使用CSS來實現。

我知道我可以通過在每個相關表中直接調用屬性名稱(即$ _product-> getAttribute)來完成此操作。但問題是有幾個屬性集,每個屬性都有不同的屬性(即每個集有16個屬性組)。

因此,不是創建一個巨大的表並單獨調用值,而是嘗試使用僞代碼來實現這一點,這將會更簡單明智。

到目前爲止,我已經嘗試過這樣的:

$attributes = $_product->getAttributes(); 
<table> 
<tr> 
<td>Sight</td> 
<td>Nose</td> 
<td>Palate</td> 
<td>Finish</td> 
</tr> 
<?php foreach($attributes as $attribute):?> 
        <?php if($attribute->getIsVisibleOnFront() && $attribute->getIsUserDefined()):?> 
         <tr> 
          <?php if(stripos($attribute->getAttributeCode(), "_sight")!=0):?><td><th class="data"><?php echo $attribute->getFrontend()->getLabel(); ?></th></td> 
          <td class="data"><?php echo $attribute->getFrontend()->getValue($_product); continue;?></td><?php endif; ?> 

          <?php if(stripos($attribute->getAttributeCode(), "_nose")!=0): ?><td><th class="data"><?php echo $attribute->getFrontend()->getLabel(); ?></th></td> 
          <td class="data"><?php echo $attribute->getFrontend()->getValue($_product); continue;?></td><?php endif; ?> 

          <?php if(stripos($attribute->getAttributeCode(), "_palate")!=0): ?><td><th class="data"><?php echo $attribute->getFrontend()->getLabel(); ?></th></td> 
          <td class="data"><?php echo $attribute->getFrontend()->getValue($_product); continue;?></td><?php endif;?> 

          <?php if(stripos($attribute->getAttributeCode(), "_finish")!=0): ?><td><th class="data"><?php echo $attribute->getFrontend()->getLabel(); ?></th></td> 
          <td class="data"><?php echo $attribute->getFrontend()->getValue($_product); continue;?></td><?php endif;?> 
         </tr> 
        <?php endif; ?> 
       <?php endforeach; ?> 
</table> 

但是,這是給我的輸出都在一列。我不知道如何按照我想要的方式將它們分成4列。有人可以幫助我嗎?

回答

1

繼續將停止循環並繼續下一次迭代。

您有4列標題和8列數據。

<日>是標題單元格http://www.w3schools.com/tags/tag_th.asp

所以我希望下面是更多的對你的解決方案。

$attributes = $_product->getAttributes(); 
<table> 
<tr> 
<td colspan="2">Sight</td> 
<td colspan="2">Nose</td> 
<td colspan="2">Palate</td> 
<td colspan="2">Finish</td> 
</tr> 

<?php foreach($attributes as $attribute): 
    if($attribute->getIsVisibleOnFront() && $attribute->getIsUserDefined()):?> 
     <tr> 
      <?php if(stripos($attribute->getAttributeCode(), "_sight")!=0) { ?> 
       <td class="label"><?php echo $attribute->getFrontend()->getLabel();   ?></td> 
       <td class="data"><?php echo $attribute->getFrontend()->getValue($_product); ?></td> 
      <?php } else { ?> 
       <td class="label"></td> 
       <td class="data"></td> 
      <?php } ?> 

      <?php if(stripos($attribute->getAttributeCode(), "_nose")!=0) { ?> 
       <td class="label"><?php echo $attribute->getFrontend()->getLabel();   ?></td> 
       <td class="data"><?php echo $attribute->getFrontend()->getValue($_product); ?></td> 
      <?php } else { ?> 
       <td class="label"></td> 
       <td class="data"></td> 
      <?php } ?> 

      <?php if(stripos($attribute->getAttributeCode(), "_palate")!=0) { ?> 
       <td class="label"><?php echo $attribute->getFrontend()->getLabel();   ?></td> 
       <td class="data"><?php echo $attribute->getFrontend()->getValue($_product); ?></td> 
      <?php } else { ?> 
       <td class="label"></td> 
       <td class="data"></td> 
      <?php } ?> 

      <?php if(stripos($attribute->getAttributeCode(), "_finish")!=0) { ?> 
       <td class="label"><?php echo $attribute->getFrontend()->getLabel();   ?></td> 
       <td class="data"><?php echo $attribute->getFrontend()->getValue($_product);?></td> 
      <?php } else { ?> 
       <td class="label"></td> 
       <td class="data"></td> 
      <?php } ?> 
     </tr> 
    <?php endif; ?> 
<?php endforeach; ?> 
</table> 
相關問題