# $img = $page-\>$slide()-\>toFile(); I need to append a value to the end of slide. See code. Thanks

**URL:** <https://forum.getkirby.com/t/img-page-slide-tofile-i-need-to-append-a-value-to-the-end-of-slide-see-code-thanks/7936>\
**Category:** Questions\
**Tags:** v2\
**Created:** [July 10, 2017, 12:47pm UTC](https://forum.getkirby.com/t/img-page-slide-tofile-i-need-to-append-a-value-to-the-end-of-slide-see-code-thanks/7936 "2017-07-10T12:47:42Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![bbarclay](https://avatars.discourse-cdn.com/v4/letter/b/96bed5/32.png) [@bbarclay](https://forum.getkirby.com/u/bbarclay)\
**Post date:** [July 10, 2017, 12:47pm UTC](https://forum.getkirby.com/t/img-page-slide-tofile-i-need-to-append-a-value-to-the-end-of-slide-see-code-thanks/7936/1 "2017-07-10T12:47:42Z")

</div>

I’m trying to loop through the values so I can add a value to the end of the image. If I echo $slide. It looks like it should pass it into $img A ok. But then nothing shows on the page.

```auto
<?
for ($x = 1; $x <= 10; $x++) {
    $slide = 'imageSlide'.$x;
    $img = $page->$slide()->toFile();
     echo $img;

}
?>

```

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [July 10, 2017, 12:53pm UTC](https://forum.getkirby.com/t/img-page-slide-tofile-i-need-to-append-a-value-to-the-end-of-slide-see-code-thanks/7936/2 "2017-07-10T12:53:26Z")

</div>

Do I get this right that `imageSlide` is the name of a field and you have 10 of those fields? And if so, why don’t you use a structure field instead?

---

<div class="post-metadata">

**Author:** ![bbarclay](https://avatars.discourse-cdn.com/v4/letter/b/96bed5/32.png) [@bbarclay](https://forum.getkirby.com/u/bbarclay)\
**Post date:** [July 10, 2017, 12:55pm UTC](https://forum.getkirby.com/t/img-page-slide-tofile-i-need-to-append-a-value-to-the-end-of-slide-see-code-thanks/7936/3 "2017-07-10T12:55:45Z")

</div>

There are actually only 4 fields. They are images, we need them to be assigned an order. We’de prefer to put them in a table in the backend.

---

<div class="post-metadata">

**Author:** ![bbarclay](https://avatars.discourse-cdn.com/v4/letter/b/96bed5/32.png) [@bbarclay](https://forum.getkirby.com/u/bbarclay)\
**Post date:** [July 10, 2017, 12:59pm UTC](https://forum.getkirby.com/t/img-page-slide-tofile-i-need-to-append-a-value-to-the-end-of-slide-see-code-thanks/7936/4 "2017-07-10T12:59:15Z")

</div>

I think I might have answered my own question. I’ve done this before. I’m not sure why it skipped my mind.

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [July 10, 2017, 1:03pm UTC](https://forum.getkirby.com/t/img-page-slide-tofile-i-need-to-append-a-value-to-the-end-of-slide-see-code-thanks/7936/5 "2017-07-10T13:03:29Z")

</div>

There’s nothing wrong with the code, it just won’t output anything if the fields are empty.

---

<div class="post-metadata">

**Author:** ![bbarclay](https://avatars.discourse-cdn.com/v4/letter/b/96bed5/32.png) [@bbarclay](https://forum.getkirby.com/u/bbarclay)\
**Post date:** [July 10, 2017, 1:26pm UTC](https://forum.getkirby.com/t/img-page-slide-tofile-i-need-to-append-a-value-to-the-end-of-slide-see-code-thanks/7936/6 "2017-07-10T13:26:41Z")

</div>

```auto
<?php foreach($page->photoGallery()->yaml() as $name) {

    $title = $name['alt'];
    $image = $name['picture'];
  $imageUrl = $page->file($image)->url();
echo html('<h1>'.$title.'</h1><img src="'.$imageUrl.'" alt="'.$title.'">');

}

?>

```

So I wrote that and works great, the thing is above it I have to show just the first image. How do I get the first value out of the array out of structured data? Thanks

---

<div class="post-metadata">

**Author:** ![bbarclay](https://avatars.discourse-cdn.com/v4/letter/b/96bed5/32.png) [@bbarclay](https://forum.getkirby.com/u/bbarclay)\
**Post date:** [July 10, 2017, 1:29pm UTC](https://forum.getkirby.com/t/img-page-slide-tofile-i-need-to-append-a-value-to-the-end-of-slide-see-code-thanks/7936/7 "2017-07-10T13:29:33Z")

</div>

I think I was overthinking this.

$page-\>photoGallery()-\>picture-\>;

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [July 10, 2017, 1:31pm UTC](https://forum.getkirby.com/t/img-page-slide-tofile-i-need-to-append-a-value-to-the-end-of-slide-see-code-thanks/7936/8 "2017-07-10T13:31:37Z")

</div>

I wouldn’t use `yaml()` (gives you an array) but to `structure()` (gives you a collection); makes your life easier. Then you can get the first element using `first()`.

Make sure the image exists before calling the `url()` method, i.e. use an if statement.

---

<div class="post-metadata">

**Author:** ![bbarclay](https://avatars.discourse-cdn.com/v4/letter/b/96bed5/32.png) [@bbarclay](https://forum.getkirby.com/u/bbarclay)\
**Post date:** [July 10, 2017, 1:50pm UTC](https://forum.getkirby.com/t/img-page-slide-tofile-i-need-to-append-a-value-to-the-end-of-slide-see-code-thanks/7936/9 "2017-07-10T13:50:27Z")

</div>

> [@Getting a URL from new image field](https://forum.getkirby.com/t/getting-a-url-from-new-image-field/4151/2):
>
> -\>url()

I must be doing something wrong I can’t seem to call outside the loop, just the first image inside the structured data.

---

<div class="post-metadata">

**Author:** ![bbarclay](https://avatars.discourse-cdn.com/v4/letter/b/96bed5/32.png) [@bbarclay](https://forum.getkirby.com/u/bbarclay)\
**Post date:** [July 10, 2017, 2:01pm UTC](https://forum.getkirby.com/t/img-page-slide-tofile-i-need-to-append-a-value-to-the-end-of-slide-see-code-thanks/7936/11 "2017-07-10T14:01:16Z")

</div>

Figured it out 🙂 Thanks

```auto
<?php
$item = $page->photoGallery()->picture()->toStructure()->nth(0);
echo $page->contentURL().'/'.$item;
?>

```

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [July 10, 2017, 2:02pm UTC](https://forum.getkirby.com/t/img-page-slide-tofile-i-need-to-append-a-value-to-the-end-of-slide-see-code-thanks/7936/12 "2017-07-10T14:02:11Z")

</div>

This code looks strange…

```auto
<?php
$item = $page->photoGallery()->structure()->first();
$image = $item->picture()->toFile();
if($image):
  echo $image->url();
endif;
?>

```

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [February 6, 2025, 5:35pm UTC](https://forum.getkirby.com/t/img-page-slide-tofile-i-need-to-append-a-value-to-the-end-of-slide-see-code-thanks/7936/13 "2025-02-06T17:35:20Z")

</div>


