php - How to split an array into two table rows "<tr>" within one foreach on template side -
due working restricted working environment, need put half of array 1 table row , half .
this array (already sorted in order of need):
array ( [product] => kfe-1221 [description] => classic blue [current_stock] => 630 [purchase_order] => [total_sales] => 0 [avail_to_sell] => 630 [size_run1] => 15 [size_run2] => 62 [size_run3] => 122 [size_run4] => 113 [size_run5] => 102 [size_run6] => 92 [size_run7] => 63 [size_run8] => 61 [size_run9] => 0 [size_run10] => 0 )
i need split array @ "size_run1" - "size_run10" separate table row.
previously, had display array table (standard foreach) when didn't need size_run. due request adding in. (and yes using deprecated php)
<?php while($data = mssql_fetch_assoc($result_ats)) { if ($data['avail_to_sell'] <= 0){ echo '<tr class="error">'; } else { echo '<tr>'; } foreach($data $k => $v){ echo '<td>'.$v.'</td>'; echo '</tr>'; } } ?>
the reason want keep 1 array because using 1 query information, , huge database i'm working with, makes more sense me split on template side rather have separate arrays , add process of matching product size_run.
what best way of doing without having split array different arrays?
my desired output, split array '$v' 2 separate tables below:
<tr> <th>product</th> <th>description</th> <th>current_stock</th> <th>purchase_order</th> <th>total_sales</th> <th>avail_to_sell</th> </tr> <tr> <th>size_run1</th> <th>size_run2</th> <th>size_run3</th> <th>size_run4</th> <th>size_run5</th> <th>size_run6</th> <th>size_run7</th> <th>size_run8</th> <th>size_run9</th> <th>size_run10</th> </tr>
something this:
$firstrow = array('product', 'description', 'current_stock', 'purchase_order', ... ); $secondrow = array('size_run1', 'size_run2', 'size_run3', ... ); while($data = mssql_fetch_assoc($result_ats)) { echo '<tr>'; foreach($firstrow $key){ echo '<td>' . $data[$key] . '</td>'; } echo '</tr>'; echo '<tr>'; foreach($secondrow $key){ echo '<td>' . $data[$key] . '</td>'; } echo '</tr>'; }
still maintainable if want change keys.
Comments
Post a Comment