简体   繁体   English

每个按类别joomla k2排序的php

[英]php for each sorting by category joomla k2

Hi I have this code from joomla k2 template 嗨,我有来自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>

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: 我需要它从main和子类别中获取项目并显示如下:

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

and somethere in the bottom is the category name in it. 而底部的一些是其中的类别名称。

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. 像这样的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM