Problem with foreach

4 posts by 2 authors in: Forums > CMS Builder
Last Post: March 23, 2010   (RSS)

By Djulia - March 19, 2010 - edited: March 19, 2010

Hi,

I do not manage to solve a problem with foreach.

I would like to obtain the element according to his value of index :

<?php foreach ($record['images'] as $key => $upload):?>
<image_0><?php echo $upload1['urlPath'][0]; ?></image_0>
<image_1><?php echo $upload1['urlPath'][1]; ?></image_1>
<image_2><?php echo $upload1['urlPath'][2]; ?></image_2>
<image_3><?php echo $upload1['urlPath'][3]; ?></image_3>
<image_4></image_4>
<image_5></image 5>
<?php endforeach ?>

An idea to direct my research ?

Thank you for your assistance.

Djulia

Re: [Djulia] Problem with foreach

By Chris - March 19, 2010

Hi Djulia,

You have two options here.

(1) you can drop foreach and reference the elements directly:

<image_0><?php echo @$record['images'][0]['urlPath']; ?></image_0>
<image_1><?php echo @$record['images'][1]['urlPath']; ?></image_1>
<image_2><?php echo @$record['images'][2]['urlPath']; ?></image_2>
<image_3><?php echo @$record['images'][3]['urlPath']; ?></image_3>


Alternately, (2) you can use a counter variable to output the numbers in your tags:

<?php $count = 0 ?>
<?php foreach ($record['images'] as $upload):?>
<image_<?php echo $count ?>><?php echo $upload['urlPath'] ?></image_<?php echo $count ?>>
<?php $count++ ?>
<?php endforeach ?>


Does that help? Please let me know if you have any questions.
All the best,
Chris

Re: [Djulia] Problem with foreach

By Chris - March 23, 2010

Hi Djulia,

preSaveTempId is used internally by CMS Builder to keep track of uploads which belong to a record which does not exist yet.

For example, when a user is creating an article (and has not saved it yet,) they can upload files. Those files are stored with a unique preSaveTempId so that when the record is saved, they can be associated with the new record number. This is only an issue because record numbers are only assigned when records are saved.

Once a record has been saved, the preSaveTempId is ignored.

You shouldn't have to worry about preSaveTempId at all; you can just leave it blank.

I hope this helps. Please let me know if you have any questions.
All the best,
Chris