I have an array of vendors called:
$listOfVendors
I loop through this array with a foreach in my view.
<?php foreach($listOfVendors as $vendor):?>
<p class="vendor">
<span class="vendor-title"><?php echo $vendor->name ?>(<?php echo $vendor->country ?>)</span>,
<?php echo $vendor->goods ?>, <a href="<?php echo $vendor->site_adres ?>" class="vendor-link"><?php echo $vendor->site_adres ?></a>
</p>
<?php endforeach ?>
Each vendor object has the following properties:
vendors_Id, name, country, goods, site_address, activities_Id. The last one, activities_Id, is a FK to the table activities which has, for the time being, two types of activities: Dealers and Artists.
Now I want to add a header once for every type of vendor. So want to get something like:
Artist
Vendor1(DE), Eigen posters e.a. merchandise, http://www.linktosite.com
Dealer
Vendor2(BE), Toys, DVD’s, books, http://www.anotherlink.be
Vendor3(FR), Merchandise, http://www.anotherlink2link.be
How do I do this?
I just can’t seem to get my head around this seemingly simple thing.
Edit for Solution
Controller actvities -> function vendors
//Get the vendor information and add the needed headers
//Here I get all the vendors
if($listOfVendors = $this->atsusacon_model->get_records()) {
foreach($listOfVendors as $vendor) {
$listOfVendorsByType[$vendor->activities_Id][] = $vendor;
}
$data['listOfVendorsByType'] = $listOfVendorsByType;
var_dump($listOfVendorsByType);
}
View actvities/vendors
<section class="vendors-overview twelvecol clearBoth">
<?php foreach($listOfVendorsByType as $activity):?>
<?php if ($activity[0]->activities_Id == 1) :?>
<h3>Artists</h3>
<?php else :?>
<h3>Dealers</h3>
<?php endif ?>
<?php foreach($activity as $vendor):?>
<p class="vendor">
<span class="vendor-title"><?php echo $vendor->name ?>(<?php echo $vendor->country ?>)</span>, <?php echo $vendor->goods ?>, <a href="<?php echo $vendor->site_address ?>" class="vendor-link"><?php echo $vendor->site_address ?></a>
</p>
<?php endforeach ?>
<?php endforeach ?>
</section>
I could make it more dynamic but for now, this is exactly how it should be.
The var_dump of $listOfVendorsByType looks like this:

You have to create a nested array:
Now you have just have to loop over this nested array using two foreach loops.