Solved

Foreach loop question

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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 …
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
The viewer will learn how to dynamically set the form action using jQuery.
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 …

744 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

14 Experts available now in Live!

Get 1:1 Help Now