Solved

Foreach loop question

Posted on 2011-03-22
1
275 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

947 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now