3

Hi I have this code from joomla k2 template

<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>

Now it is taking items from main category and sub categories and displaying items like this.

item,
item,
item,
item,

I need it to take items from main and subcategories and display them like this:

category1
item
item

category2
item
item

category3
item
item

and etc.

How can I do this?

uptade: The array is constructed like that or at least few lines of it

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]

and somethere in the bottom is the category name in it.

andy
  • 177
  • 2
  • 12
  • what is the value of $thearray? – Arnaud Jun 22 '12 at 06:40
  • $this->primary was this just chnaged :) to $thearray :) – andy Jun 22 '12 at 11:22
  • @andy: show us how that array is made :) – Gianpaolo Di Nino Jun 23 '12 at 23:59
  • if i understand correctly the array is made: Array ( [0] => stdClass Object ( [id] => 40 [title] => test1 [alias] => test1 [catid] => 8 [published] => 1 [introtext] => test1 [fulltext] => [video] => [gallery] => [extra_fields] => [] [extra_fields_search] => [created] => 2012-08-27 16:37:39 [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:39 [publish_down] => 0000-00-00 00:00:00 [trash] = ) and etc. – andy Aug 27 '12 at 16:45

1 Answers1

0

There is only one method. You have to reorder your array

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>';
}

Something like this.

Sergey Romanov
  • 2,949
  • 4
  • 23
  • 38