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: [chris] Problem with foreach

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

Hi Chris,

Thanks ! That really helped me! :)

I write a script of importation. That functions correctly, but I did not really understand the interest of preSaveTempId.
Is this column used at the time of the
[font "Times New Roman"]downloading of the files?

Thank you for the feedback ! :)

Djulia