【问题标题】:php for each sorting by category joomla k2php 为每个按类别排序的 joomla k2
【发布时间】:2012-06-24 06:40:39
【问题描述】:

您好,我有来自 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

等等

我该怎么做?

更新: 数组是这样构造的,或者至少有几行

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]

底部的某处是其中的类别名称。

【问题讨论】:

  • $thearray 的值是多少?
  • $this->primary 只是将 :) 更改为 $thearray :)
  • @andy:向我们展示这个数组是如何制作的 :)
  • 如果我理解正确,则创建数组:Array ( [0] => stdClass Object ( [id] => 40 [title] => test1 [alias] => test1 [catid] => 8 [已发布] => 1 [介绍] => test1 [全文] => [视频] => [图库] => [extra_fields] => [] [extra_fields_search] => [创建] => 2012-08-27 16:37:39 [created_by] => 62 [created_by_alias] => [checked_out] => 0 [checked_out_time] => 0000-00-00 00:00:00 [修改] => 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] = ) 等等。

标签: php joomla foreach joomla-extensions joomla-k2


【解决方案1】:

只有一种方法。你必须重新排列你的数组

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

类似的东西。

【讨论】:

    猜你喜欢
    • 2012-04-13
    • 2012-09-05
    • 2014-11-01
    • 2012-11-04
    • 2014-05-02
    • 2014-11-11
    • 2015-06-08
    • 1970-01-01
    • 2013-05-05
    相关资源
    最近更新 更多