Saturday, 15 September 2012

html - Print new on id iteration change inside php while loop from MySQL result -



html - Print new <div> on id iteration change inside php while loop from MySQL result -

i have mysql table:

+------------+-------------+ | contact_id | _company_id | +------------+-------------+ | 1 | 1 | | 2 | 1 | | 3 | 1 | | 4 | 2 | | 5 | 2 | +------------+-------------+

im trying print new <div> containing rows same _company_id

so far have code:

$previous = ''; while ($result = $stmt->fetch()) { $current = $_company_id; //this should executed every first iteration , every time $_company_id changes if ($current != $previous) { $html .= '<div id="company-' . $_company_id . '" class="tab-pane fade">'; } //iterate contact_id here 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 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 change?

$previous = null; while ($result = $stmt->fetch()) { $current = $_company_id; //this should executed every first iteration , every time $_company_id changes if ($current !== $previous) { if($previous !== null) $html.='</div>'; $html .= '<div id="company-' . $_company_id . '" class="tab-pane fade">'; } //iterate contact_id here same _company_id $previous = $current; } if($previous!==null) $html.='</div>';

php html mysql while-loop

No comments:

Post a Comment