1

我目前正面临着根据数组中名为 category 的项目将数组分组到 html 表中的问题。

我有以下代码:

foreach ($this->items as $item) {
    $categories[] = $item->category;
}

<?php foreach ($categories as $category): ?>
        <table class="table table-bordered">
        <?php foreach ($this->items as $item) :?>
            <tr>
                <td></td>
                <td><?php echo $item->description; ?></td>
                <td><?php echo $item->price; ?></td>
            </tr>
        <?php endforeach; ?>
        </table> 
    <?php endforeach; ?>

但是我怎么告诉它,根据它的类别,它需要进入一个表或为该类别创建一个表?

每个单独类别的最终结果应如下所示:

在此处输入图像描述

添加描述:

想我修好了..我使用了这段代码:

foreach( $this->items as $item ){
if( !isset( $zones[ $item->zone ] ) )
$zone_items[ $item->zone] = array();
$zone_items[ $item->zone ][] = $item;

    $zones[$item->zone] = array(
        'zone' => $item->zone,
        'zone_description' => $item->zone_description,
        'items' => $zone_items[$item->zone]
    );

}


<?php foreach( $zones as $zone): ?>
    <?php echo $zone['zone']; ?><br />
    <?php echo $zone['zone_description']; ?>

    <?php foreach( $zone['items'] as $items ) :?>
        <?php echo $items->name; ?>
        <?php echo $items->description; ?>
        <?php echo $items->category; ?>
    <?php endforeach; ?>

<?php endforeach; ?>

我得到了名称和描述,但没有项目.. 非常感谢。

4

2 回答 2

0

这个怎么样?我没有测试它,所以请原谅任何错误,但是,你明白了:)我的例子中的表格也很糟糕,嗯......只需摆弄一下就可以从这里得到它;P

$categories = array();
foreach( $this->items as $item )
{
    if( !isset( $categories[ $item->category ] ) ) $categories[ $item->category] = array();
    $categories[ $item->category ][] = $item;
}

<?php foreach( $categories as $category => $items ): ?>
    <table class="table table-bordered">
        <tr>
            <td rowspan="<?php echo count( $items ); ?>"><?php echo $category; ?></td>
            <?php foreach( $items as $item ) :?>
                <td><?php echo $item->description; ?></td>
                <td><?php echo $item->price; ?></td>
            <?php endforeach; ?>
        </tr>
    </table>
<?php endforeach; ?>

要包含类别的描述,您需要重新处理数组。现在,我不知道您的类别描述来自哪里,所以我将向您展示数组和输出的外观,并留给您如何构建它;

<?php
$categories = array(
    array(
        'name' => 'Category Name',
        'description' => 'This is the category description',
        'items' => $itemsForThisCategory //you can still sort the items by category like I did above and then do "$itemsSortedByCategory[ 'Category Name' ]" here
    ),
    array(
        'name' => 'Another Category Name',
        'description' => 'This is the description for another category',
        'items' => $itemsForAnotherCategory
    ),
    //etc... as many categories as you have
);
?>

然后输出将如下所示;

<?php foreach( $categories as $category ): ?>
    <table class="table table-bordered">
        <tr>
            <td rowspan="<?php echo count( $category['items'] ); ?>">
                <?php echo $category['name']; ?><br />
                <?php echo $category['description']; ?>
            </td>
            <?php foreach( $category['items'] as $item ) :?>
                <td><?php echo $item->description; ?></td>
                <td><?php echo $item->price; ?></td>
            <?php endforeach; ?>
        </tr>
    </table>
<?php endforeach; ?>
于 2013-08-15T09:47:07.767 回答
0
$categories = [];

foreach( $this->items as $item )
{
    $categories[$item->category][] = $item;
}

现在您应该有一个关联$categories数组,每个索引都是一个类别,并包含该类别的项目。现在您只需使用 foreach 循环遍历它,输出$category索引并输出其中包含的对象。

于 2013-08-15T09:46:00.247 回答