0

I want to create some input fields from an array. I have an array like

$info = array("one" => "one1", "two" => "two2", "three" => "three3" ); foreach ($info as $key => $value){ $field.= "<div class='formcol form-left middle'> \n"; $field.= " <input type='text' id='".$key."' size='12' name='".$key."' style='width:".$tam."px;'/>\n"; $field.= "\t\t\t <div class='text'>".$value."</div>\n"; $field.= "\t\t </div> \n"; } 

And it does its job very nice, however If the size of array is 6 like

$info = array( "one" => "one1", "two" => "two2", "three" => "three3", "four" => "four4", "five" => "five5", "six" => "six6" ); 

I woul like to change the code I have to create first 3 values of array to create a "div" and then create other "div" with last 3 values, I will have always mod 3 = 0 values, but the problem is to create a new div every 3 used values

How to improve the code? I was thinking something like

$size = sizeof($info); foreach ($info as $key => $value){ //if ($size % 3 ==0) $field.= "<div class='formcol form-left middle'> \n"; //else // { $field.= " <input type='text' id='".$key."' size='12' name='".$key."' style='width:".$tam."px;'/>\n"; $field.= "\t\t\t <div class='text'>".$value."</div>\n"; // } //if ($size % 3 ==0) $field.= "\t\t </div> \n"; } 

so for 0,1,2 it is a new div, then for 3,4,5 is new div etc. Is it a best aproach to do this?

4
  • just wondering, why are you bothering to output tabs? and your approach looks good to me. you could calculate $size % 3 == 0 outside the loop (just store it in a variable) to reduce repeated code and, less importantly, save a tiny bit of time. Commented Aug 1, 2013 at 18:57
  • it is because a css has this specific thing, just using php to keep some tedious work filling them one by one Commented Aug 1, 2013 at 18:58
  • I woul like to change the code I have to create first 3 values of array to create a "div" and then create other "div" with last 3 values then finally you create 6 divs, why you need % 3 ? Commented Aug 1, 2013 at 19:01
  • actually I meant to create 2 divs, one div for first 3 values, second for last three values, then make a general code for any (% 3 == 0) Commented Aug 1, 2013 at 19:03

2 Answers 2

1

You need a counter:

$counter = 0; $field .= "<div>"; foreach ($info as $key => $value){ $counter++; if($counter % 3 == 0) { $field .= "</div><div>"; } //your code here } $field.= "</div>"; 
Sign up to request clarification or add additional context in comments.

3 Comments

in the part //your code I would write $field.= " <input type='text' id='".$key."' size='12' name='".$key."' style='width:".$tam."px;'/>\n"; $field.= "\t\t\t <div class='text'>".$value."</div>\n"; ?
Not sure depends on how you want it laid out. But most likely the 4 lines you have inside your foreach()
Or just the middle two, depending on what you are going for.
1

Change foreach to for - looks cleaner

$field .= "<div>"; for ($i=0; $i<count($info); $i++){ if($i % 3 == 0) { $field .= "</div><div>"; } //... } $field.= "</div>"; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.