Note that there are some explanatory texts on larger screens.

plurals
  1. POPut array of elements under correct header with foreach
    primarykey
    data
    text
    <p>I have an array of vendors called:</p> <pre><code>$listOfVendors </code></pre> <p>I loop through this array with a foreach in my view.</p> <pre><code>&lt;?php foreach($listOfVendors as $vendor):?&gt; &lt;p class="vendor"&gt; &lt;span class="vendor-title"&gt;&lt;?php echo $vendor-&gt;name ?&gt;(&lt;?php echo $vendor-&gt;country ?&gt;)&lt;/span&gt;, &lt;?php echo $vendor-&gt;goods ?&gt;, &lt;a href="&lt;?php echo $vendor-&gt;site_adres ?&gt;" class="vendor-link"&gt;&lt;?php echo $vendor-&gt;site_adres ?&gt;&lt;/a&gt; &lt;/p&gt; &lt;?php endforeach ?&gt; </code></pre> <p>Each vendor object has the following properties:</p> <p><strong>vendors_Id</strong>, <strong>name</strong>, <strong>country</strong>, <strong>goods</strong>, <strong>site_address</strong>, <strong>activities_Id</strong>. 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.</p> <p>Now I want to add a header once for every type of vendor. So want to get something like:</p> <p><strong>Artist</strong></p> <p>Vendor1(DE), Eigen posters e.a. merchandise, <a href="http://www.linktosite.com" rel="nofollow noreferrer">http://www.linktosite.com</a></p> <p><strong>Dealer</strong></p> <p>Vendor2(BE), Toys, DVD's, books, <a href="http://www.anotherlink.be" rel="nofollow noreferrer">http://www.anotherlink.be</a></p> <p>Vendor3(FR), Merchandise, <a href="http://www.anotherlink2link.be" rel="nofollow noreferrer">http://www.anotherlink2link.be</a></p> <p>How do I do this? I just can't seem to get my head around this seemingly simple thing.</p> <h2>Edit for Solution</h2> <h3>Controller actvities -> function vendors</h3> <pre><code>//Get the vendor information and add the needed headers //Here I get all the vendors if($listOfVendors = $this-&gt;atsusacon_model-&gt;get_records()) { foreach($listOfVendors as $vendor) { $listOfVendorsByType[$vendor-&gt;activities_Id][] = $vendor; } $data['listOfVendorsByType'] = $listOfVendorsByType; var_dump($listOfVendorsByType); } </code></pre> <h3>View actvities/vendors</h3> <pre><code>&lt;section class="vendors-overview twelvecol clearBoth"&gt; &lt;?php foreach($listOfVendorsByType as $activity):?&gt; &lt;?php if ($activity[0]-&gt;activities_Id == 1) :?&gt; &lt;h3&gt;Artists&lt;/h3&gt; &lt;?php else :?&gt; &lt;h3&gt;Dealers&lt;/h3&gt; &lt;?php endif ?&gt; &lt;?php foreach($activity as $vendor):?&gt; &lt;p class="vendor"&gt; &lt;span class="vendor-title"&gt;&lt;?php echo $vendor-&gt;name ?&gt;(&lt;?php echo $vendor-&gt;country ?&gt;)&lt;/span&gt;, &lt;?php echo $vendor-&gt;goods ?&gt;, &lt;a href="&lt;?php echo $vendor-&gt;site_address ?&gt;" class="vendor-link"&gt;&lt;?php echo $vendor-&gt;site_address ?&gt;&lt;/a&gt; &lt;/p&gt; &lt;?php endforeach ?&gt; &lt;?php endforeach ?&gt; &lt;/section&gt; </code></pre> <p>I could make it more dynamic but for now, this is exactly how it should be.</p> <p>The var_dump of $listOfVendorsByType looks like this:</p> <p><img src="https://i.stack.imgur.com/tNZIp.png" alt="Var_dump($listOfVendorsByType)"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload