Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Foreach loop question

Posted on 2011-03-22
1
Medium Priority
?
292 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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

885 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