1

Hey guys I'm having a bit of trouble trying to make my SQL column a variable in PHP.

So my SQL row contains image URL's and are formatted like this:

http://www.website.com/image.jpg,http://www.website.com/image2.jpg,http://www.website.com/image3.jpg

So now I need to display the first URL in the row.

I have this short line of code:

file = '.(explode(',', $cardata["PictureRefs"])[0]).';

I am basically trying to give the variable a value of:

$file = "http://www.website.com/image.jpg"

My current code is obviously wrong however I feel like I must be quite close. How can I achieve this?

3 Answers 3

3

Your short hand is correct, but the string encapsulation is simply not required.

$file = explode(',', $cardata['pictureRefs'])[0]; // first image SRC

Then you can utilize it like:

echo '<img src="'.$file.'"/>';
Sign up to request clarification or add additional context in comments.

2 Comments

I've just tried this and I'm getting this syntax error: Parse error: syntax error, unexpected '$cardata' (T_VARIABLE) in... Any idea why?
@PeterHardy Missed a comma after ','. Check again, sorry.
0

No problem.

$file=explode(',',$cardata)[0];

Comments

-1

Try this:

$file = explode(',',$cardata);
$img = $file[0];

1 Comment

No need to introduce that extra variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.