Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

nicholas maddren
nicholas maddren
12,793 Points

Displaying one part of a PDO string

Hey guys I'm having an issue trying to get my PHP to display the first url of my exploded string and non of the others.

The SQL table column contains strings like this:

http://www.imgurl.com/image.jpg,http://www.imgurl.com/image2.jpg,http://www.imgurl.com/image3.jpg,http://www.imgurl.com/image4.jpg

I am currently using this code to display the table column onto my front end:

<img src="'.implode('"/><img src="',explode(',', $row["PictureRefs"])[0]).'"/>

However I am getting a parse error due to the fact I have added [0] to the code to try and display only the first exploded part of the string.

Any idea how I can only display the first exploded part of the string?

Thanks

2 Answers

Hang on, I think I understand what you're trying to do. If you just want the first URL in that string, this should work:

<?php

$first_item = explode( ',', $sql_string )[0];

Will this work? I'm having some difficulty understanding what you're trying to do.

<?php

$srcs = explode( ',', $sql_string );

foreach ( $srcs as $src ): ?>

   <img src="<?php echo $src; ?>" />

<?php endforeach;