Solved

PHP foreach loop question

Posted on 2011-03-02
3
329 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 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 35020954
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 109

Expert Comment

by:Ray Paseur
ID: 35022895
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
ID: 35048073
This worked just fine actually.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
This article discusses how to create an extensible mechanism for linked drop downs.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

830 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