Solved

PHP foreach loop question

Posted on 2011-03-02
3
315 Views
Last Modified: 2012-05-11
Hi,

I want to increment the class name in the attached code by +1 each time it is outputted, so I end up with something like this:

catItemExtraFieldsValue1
catItemExtraFieldsValue2
catItemExtraFieldsValue3

Thanks
<?php foreach ($this->item->extra_fields as $key=>$extraField): ?>
				<span class="catItemExtraFieldsValue"><?php echo $extraField->value; ?></span>
			<?php endforeach; ?>

Open in new window

0
Comment
Question by:Eternal_Student
3 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
Comment Utility
this should do:
<?php $i= 1; foreach ($this->item->extra_fields as $key=>$extraField){ ?>
   <span class="catItemExtraFieldsValue<?php echo $i++; ?>"><?php echo $extraField->value; ?></span>
<?php } //foreach ?>

Open in new window

0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
I think angelIII has answered your question, but I've got the uneasy sense that this might generate something that would cause the HTML to fail validation, or maybe generate unstyled content.  Do you have a pretty tight limit on the number of extra fields?
0
 
LVL 18

Author Comment

by:Eternal_Student
Comment Utility
This worked just fine actually.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
The viewer will learn how to count occurrences of each item in an array.

771 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

11 Experts available now in Live!

Get 1:1 Help Now