Im currently sitting with the problem of grouping an array into html tables based on a item in the array called category.
I have the following code:
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; ?>
But how do i tell it, that based on its category it needs to go into a table or create a table for that category?
end result for each separate category should look like:
Adding a description:
Think I fixed it.. I used this code:
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; ?>
and I get the name and description but no items.. Help Greatly Appreciated.
How about this? I didn't test it so pardon any bugs but, you get the idea :) The table in my example is also screwy, umm... Will just take some fiddling to get it right from here ;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; ?>
To include a description for categories, you'll want to rework the array. Now, I don't know where your category descriptions are coming from, so I'll just show you how the array and output should look, and leave it up to you how to build it;
<?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
);
?>
Then the output would look like this;
<?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; ?>
$categories = [];
foreach( $this->items as $item )
{
$categories[$item->category][] = $item;
}
Now you should have an associative $categories
array, with each index being a category, and containing items of that category. Now you would just loop through that with a foreach, output the $category
index, and output the objects contained within.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.