Solved

PHP foreach loop question

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
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 modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
The viewer will learn how to dynamically set the form action using jQuery.

776 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