php performance of foreach loop when creating table in html
Posted: Mon Nov 21, 2011 4:57 am
I want to construct a table with two td's per row . and each td will contain a list of checkboxes , which will be again two checkboxes per row , and I have to display this inside a popup. For this I am trying to use nested foreach loop , but somehow it seems , that my approach is not optimum since the popup is taking time to load .
let me start with the array structure that I am sending from the backend to the frontend view :
As you can see there are four key fields which will be my table cells and within each table cells , there are several fields with separate display vlaues and backend values , which will form my checkboxes.
Now these key fields and checkbox values are dynamic and they are actually read from a .csv file , as such I have to keep my design layout flexible such that if tomorrow there are only 3 or 5 key fields , my table structure will be adjsted accordingly .
In the html view , I am using the following php loops :
how can I optimize this logic so that my popup doesn't take too much time to load.
will converting array_slice to array_chunk do any difference ?
let me start with the array structure that I am sending from the backend to the frontend view :
Code: Select all
$allParamValues=Array
(
[CATEGORY] => Array
(
[displayValue] => Array
(
//[key] => [value]
)
[backendValue] => Array
(
//[key] => [value]
)
)
[ORIENTATION] => Array
(
//Array similiar to above
)
[CARRIER_DECK] => Array
(
//array similiar to above
)
[POSITION] => Array
(
//array similiar to above
)
)
Now these key fields and checkbox values are dynamic and they are actually read from a .csv file , as such I have to keep my design layout flexible such that if tomorrow there are only 3 or 5 key fields , my table structure will be adjsted accordingly .
In the html view , I am using the following php loops :
Code: Select all
<?php
$countCells = 2;
$totalNoOfRows = ceil(count($allParamValues)/2);
for($j=0;$j<$totalNoOfRows;$j++)
{
echo "<tr>";
$allParamVAluesTmp = array_slice($allParamValues,$countCells*$i,$countCells);
foreach($allParamVAluesTmp as $category=>$optionsArr)
{
?>
<!--create the tds-->
<table>
<?php
//breaks $optionsArray above to keep 2 checkboxes per row using
// the same array_slice
// which results in 1 for loop and 1 interior foreach loop.
?>
<?php echo '</tr>'; ?>will converting array_slice to array_chunk do any difference ?