Solved

Foreach loop question

Posted on 2011-03-22
1
282 Views
Last Modified: 2012-05-11
Hi - I have a foreach loop where I want to split the output up so that I can display the content in 2 columns instead of 1.

At the minute I get the following HTML output:

<div id="itemExtraFields">
  <div class="even typeTextarea group6"> <!-- ITEM 1 --></div>
  <div class="odd typeTextarea group6"> <!-- ITEM 2 --></div>
  <div class="odd typeTextarea group6"> <!-- ITEM 3 --></div>
  <div class="even typeTextarea group6"> <!-- ITEM 4 --></div>
  <div class="odd typeTextarea group6"> <!-- ITEM 5 --></div>
  <div class="odd typeTextarea group6"> <!-- ITEM 6 --></div>
</div>


But What I really need to do is wrap a <div> around the 1st 4 items and then a <div> around the last 2. So I can style them as 2 columns.

Below is my php ... hopefully you can suggest the best solution for this.

Thanks


<div class="itemExtraFields">
			<?php foreach ($this->item->extra_fields as $key=>$extraField):?>
			<div class="<?php echo ($key%2) ? "odd" : "even"; ?> type<?php echo ucfirst($extraField->type); ?> group<?php echo $extraField->group; ?>">
				<!--<span class="itemExtraFieldsLabel"><//?php echo $extraField->name; ?>:</span>-->
				<span class="itemExtraFieldsValue"><?php echo $extraField->value; ?></span>
 	            <div class="clear"></div>
            </div>
			<?php endforeach; ?>
	  </div>

Open in new window

0
Comment
Question by:Eternal_Student
1 Comment
 
LVL 17

Accepted Solution

by:
jrm213jrm213 earned 500 total points
ID: 35192725
Hi,

try the following code, it is untested but I think it should work. Basically wrap a div around it for the first pass and every 4 items close the div and open a new one. then close the remaining open div at the end.
<?php $counter=0;?>
<div class="itemExtraFields">
			<?php foreach ($this->item->extra_fields as $key=>$extraField):?>
if($counter == 0){echo("<div class=\"itemExtraFieldsContainer\">");}else{if($counter % 4 == 0){echo("</div><div class=\"itemExtraFieldsContainer\">");}}
			<div class="<?php echo ($key%2) ? "odd" : "even"; ?> type<?php echo ucfirst($extraField->type); ?> group<?php echo $extraField->group; ?>">
				<!--<span class="itemExtraFieldsLabel"><//?php echo $extraField->name; ?>:</span>-->
				<span class="itemExtraFieldsValue"><?php echo $extraField->value; ?></span>
 	            <div class="clear"></div>
            </div>
			<?php  
$counter++;
endforeach; ?>
	  </div></div>

Open in new window

0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question