Page 1 of 1

php performance of foreach loop when creating table in html

Posted: Mon Nov 21, 2011 4:57 am
by debashishnit
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 :

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
        )

    )
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 :

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>'; ?>
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 ?

Re: php performance of foreach loop when creating table in h

Posted: Mon Nov 21, 2011 5:35 am
by maxx99
What is $i ? Some other counter or did you mean $j ?

Code: Select all

            $allParamVAluesTmp = array_slice($allParamValues,$countCells*$i,$countCells);
How much time to generate this view for how many rows are we talking about?

Re: php performance of foreach loop when creating table in h

Posted: Mon Nov 21, 2011 6:17 am
by debashishnit
@maxx99 , sorry it will be $j , $i was by mistake .

actually , my html comes embedded inside a popup message , and it should not take more than 1 sec to generate it , otherewise the popup will take time to load.So far as the number of checkboxes are concerned , all total they are 50 in number. on an average it's (50/4) = 14 checkboxes per td.

Re: php performance of foreach loop when creating table in h

Posted: Mon Nov 21, 2011 7:39 am
by maxx99
http://www.phpbench.com/ check out this performance tips :) its for bigger scale but anyway its good to know this stuff

And for your problem...
I dont really see why you use the first for loop and then slice $allParamValues every time.

2x foreach should be enough

Code: Select all

$cellsPerRow = 2;
$counter = 0;
foreach($allParamValues as $categoryKey => $singleParamValues){
 //if you need categoryKey name you can store/use it here
 foreach($singleParamValues as $key=>$value){
if($counter % $cellsPerRow == 0) echo '<tr>';

//do stuff to your [key] => [value]

if($counter % $cellsPerRow == 0) echo '</tr>';
$counter++;
 }
}