?
Solved

Foreach loop question

Posted on 2011-03-22
1
Medium Priority
?
285 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo‚Ķ
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

801 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