I have this MySQL
table:
+------------+-------------+
| contact_id | _company_id |
+------------+-------------+
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 4 | 2 |
| 5 | 2 |
+------------+-------------+
Im trying to print a new <div>
containing all rows with the same _company_id
So far I have this code:
$previous = '';
while ($result = $stmt->fetch()) {
$current = $_company_id;
//This should be executed every first iteration and every time $_company_id changes
if ($current != $previous) { $html .= '<div id="company-' . $_company_id . '" class="tab-pane fade">'; }
//Iterate all contact_id here with the same _company_id
if ($current != $previous) { $html .= '</div>'; }
$previous = $current;
}
My desired output is:
<div id="company-1" class="tab-pane fade">123</div>
<div id="company-2" class="tab-pane fade">45</div>
Right now the output is:
<div id="company-1" class="tab-pane fade">1</div>
23
<div id="company-2" class="tab-pane fade">4</div>
5
What should I change?