2012-06-21 55 views
3

嗨每個分揀我從K2的Joomla模板PHP按類別的Joomla K2

<div id="itemListPrimary" class="clearfix"> 
<?php $thearray = $this->primary ;?> 
<?php foreach($thearray as $key=>$item): ?> 
    <div class="itemContainer"> 
    <?php 
    $this->item=$item; 
    echo $this->loadTemplate('item'); 
    ?> 
    </div> 
<?php endforeach; ?> 
</div> 

從主類別和子類別和項目顯示現在這個樣子是取放物品的代碼。

item, 
item, 
item, 
item, 

我需要從主和子類別需要的物品,並顯示他們是這樣的:

category1 
item 
item 

category2 
item 
item 

category3 
item 
item 

我怎樣才能做到這一點?

uptade: 陣列被構造類似或它

Array ([0] => stdClass Object ([id] => 41 [title] => test2 [alias] => test2 [catid] => 8 [published] => 1 [introtext] => 
test2 

[fulltext] => [video] => [gallery] => [extra_fields] => [] [extra_fields_search] => [created] => 2012-08-27 16:37:51 [created_by] => 62 [created_by_alias] => [checked_out] => 0 [checked_out_time] => 0000-00-00 00:00:00 [modified] => 0000-00-00 00:00:00 [modified_by] => 0 [publish_up] => 2012-08-27 16:37:51 [publish_down] => 0000-00-00 00:00:00 [trash] => 0 [access] 

至少幾行和somethere在底部是在它的類別名稱。

+0

$ thearray的價值是多少? – Arnaud

+0

$ this-> primary was this chnaged :) to $ thearray :) – andy

+0

@andy:向我們展示這個數組是如何製作的:) –

回答

0

只有一種方法。你必須重新排列你的陣列

foreach($thearray as $key=>$item) { 
    $items[$item->catid][] = $item; 
} 

foreach($items AS $catid => $cat_items) { 
    echo '<h3>'.$catid.'</h3>'; 
    foreach($cat_items AS $item) 
     echo $item->name.'<br>'; 
} 

就像這樣。