Working with Array['keys']
2 posts by 2 authors in: Forums > CMS Builder
Last Post: June 21, 2012 (RSS)
By Perchpole - June 21, 2012 - edited: June 21, 2012
Lets say I have an editor set up called $record with 3 textfields:
text1
text2
text3
On my page I'm using a foreach loop to pull in some dynamic content to create 3 info boxes:
NB: The loop will break after the third iteration.
What I want to do is insert one $record textfield into each box according to the iteration count. So $record['text1'] would appear in the first box, $record['text2'] into the second and $record['text3'] into the last box.
We'd end up with something like this:
I imagine there's a couple of ways to do this. I'd prefer to find a solution that uses some kind of counter so the $record is pulled in if the ['textn'] matches the count++.
In effect I need to contrive some bit of code which would allow me to produce the same result as this: $record['textcounter++'] - which I assume is not allowed!
Make sense?
:0s
Perch
text1
text2
text3
On my page I'm using a foreach loop to pull in some dynamic content to create 3 info boxes:
<?php foreach ($otherInfo as $info): ?>
<?php echo $info['title'] ?>
<?php echo $info['body'] ?>
<?php end foreach ?>
NB: The loop will break after the third iteration.
What I want to do is insert one $record textfield into each box according to the iteration count. So $record['text1'] would appear in the first box, $record['text2'] into the second and $record['text3'] into the last box.
We'd end up with something like this:
//Box 1
<?php echo $info['title'] ?>
<?php echo $info['body'] ?>
<?php echo $record['text1'] ?>
//Box 2
<?php echo $info['title'] ?>
<?php echo $info['body'] ?>
<?php echo $record['text2'] ?>
//Box 3
<?php echo $info['title'] ?>
<?php echo $info['body'] ?>
<?php echo $record['text3'] ?>
I imagine there's a couple of ways to do this. I'd prefer to find a solution that uses some kind of counter so the $record is pulled in if the ['textn'] matches the count++.
In effect I need to contrive some bit of code which would allow me to produce the same result as this: $record['textcounter++'] - which I assume is not allowed!
Make sense?
:0s
Perch
Re: [Perchpole] Working with Array['keys']
By Jason - June 21, 2012
Hi,
You pretty much have it.
try this:
Hope this helps
You pretty much have it.
try this:
<?php $recordCount = 0; ?>
<?php foreach ($otherInfo as $info): ?>
<?php echo $info['title'] ?>
<?php echo $info['body'] ?>
<?php echo $record["text".++$recordCount];
<?php end foreach ?>
Hope this helps
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com
Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/
Jason Sauchuk - Project Manager
interactivetools.com
Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/