显示foreach循环第一 -

问题描述:

我有只有两个ID以下列方式bestsellersnot-bestsellers在foreach循环许多<li>元素Magento的产品列表:显示foreach循环第一 -

<li id="not-bestsellers">These are for "not-besellers" products</li> 
<li id="not-bestsellers">These are for "not-besellers" products</li> 
<li id="bestsellers">These are for "besellers" products</li> 
<li id="not-bestsellers">These are for "not-besellers" products</li> 
<li id="bestsellers">These are for "besellers" products</li> 
<li id="bestsellers">These are for "besellers" products</li> 
<li id="not-bestsellers">These are for "not-besellers" products</li> 
<li id="not-bestsellers">These are for "not-besellers" products</li> 
<li id="bestsellers">These are for "besellers" products</li> 
<li id="bestsellers">These are for "besellers" products</li> 

如何显示ID为元素=“畅销书”首先,然后不是畅销书呢?

<li id="bestsellers">These are for "besellers" products</li> 
<li id="bestsellers">These are for "besellers" products</li> 
<li id="bestsellers">These are for "besellers" products</li> 
<li id="not-bestsellers">These are for "not-besellers" products</li> 
<li id="not-bestsellers">These are for "not-besellers" products</li> 
<li id="not-bestsellers">These are for "not-besellers" products</li> 

编辑

<?php foreach ($_productCollection as $_product): ?> 
    <?php $attributeValue = Mage::getModel('catalog/product')->load($_product->getId())->getAttributeText('bestsellers'); ?> 
     <li id="<?php if ($attributeValue == 'Yes') : ?> 
      <?php echo 'bestsellers';?> 
      <?php else:?> 
      <?php echo 'not-bestsellers' ;?> 
      <?php endif; ?> 
     </li> 
     . 
     . 
     . 
     . 
     . 
<?php endforeach; ?> 
+1

你不能有多个具有相同ID的元素。 – Pupil

+0

这个'id'从哪里来?如果它来自数据库,请在其上添加排序。 – Pupil

+0

我正在用以下 –

$data = array(
     '<li id="not-bestsellers">These are for "not-besellers" products</li>', 
     '<li id="not-bestsellers">These are for "not-besellers" products</li>', 
     '<li id="bestsellers">These are for "besellers" products</li>', 
     '<li id="not-bestsellers">These are for "not-besellers" products</li>', 
     '<li id="bestsellers">These are for "besellers" products</li>', 
     '<li id="bestsellers">These are for "besellers" products</li>', 
     '<li id="not-bestsellers">These are for "not-besellers" products</li>', 
     '<li id="not-bestsellers">These are for "not-besellers" products</li>', 
     '<li id="bestsellers">These are for "besellers" products</li>', 
     '<li id="bestsellers">These are for "besellers" products</li>' 
    ); 
    sort($data); 
    foreach($data as $value){ 
     echo $value."<br/>"; 
    } 
+0

使用sort()方法的短阵列或者它的ID ....比把它放到foreach中并获得欲望输出... –

你不应该有重复的ID,使用类来代替。

在这旁边,你可以在“畅销书”和“不,畅销书”存储在分开排列,然后告诉他们,这样的事:

<?php foreach ($_productCollection as $_product): ?> 
    <?php $attributeValue = Mage::getModel('catalog/product')->load($_product->getId())->getAttributeText('bestsellers'); ?> 
    <?php if ($attributeValue == 'Yes') : ?> 
     <?php $bestsellers[]= $_product;?> 
    <?php else:?> 
     <?php $no_bestsellers[]= $_product;?> 
    <?php endif; ?> 
<?php endforeach; ?> 
<?php foreach ($bestsellers as $bestseller): ?> 
    <li class="bestsellers"> 
     <?php echo $bestseller //or whatever you show here ?> 
    </li> 
<?php endforeach; ?> 
<?php foreach ($no_bestsellers as $no_bestseller): ?> 
    <li class="not-bestsellers"> 
     <?php echo $no_bestseller //or whatever you show here ?> 
    </li> 
<?php endforeach; ?> 

您可以检查工作的例子here

+0

我希望它能找到工作,但这会影响显示产品的限制页。我会很感激,如果你能帮助我的整个事情,http://pastebin.com/xaT6jDfR –

+0

我看了看代码,我看不出它会影响每页的限制... – jolmos