0

totally stumped in this, basically I'm getting a comma seperated list from a user table, using it as an array and then using each value in the array to fetch data from a different table and output then.

$award_array = array($user_class->awards);
foreach($award_array as $award) {
    $getaward = mysql_query("SELECT `name`, `text`, `image` FROM `awards_av` WHERE `id` = '".$award."'");
    $awardstuff = mysql_fetch_array($getaward);
    echo "<img src='".$awardstuff['image']."' alt='".$awardstuff['name']."' title='".$awardstuff['text']."' />";
}

This is only giving out the first number in the array ($user_class->awards in this case is 1,2,3,4,5,6)

Any help is much appreciated!

2
  • 2
    Please append the output of var_dump($award_array); to your question. Commented Feb 4, 2014 at 19:46
  • Not sure I follow, if $user_class->awards has comma separated values $award_array would look like this: array(1) { [0]=> string(11) "1,2,3,4,5,6" } Commented Feb 4, 2014 at 19:50

1 Answer 1

1

I think I see the problem. You cannot simply take a string of a comma separated list, and throw it inside an array() tag and expect it to automatically convert it to an array. You must use the explode() function to do that.

What you're trying to do:

<?php
$string = 'a,b,c,d,e';
$myarray = array($string);
foreach ($myarray as $k => $v) {
    print $v .'<br />';
}
?>

Except that doesn't work. You need to convert the comma-separated list of values (which is a string) into an array, but you don't use array() to do that. You use PHP's built-in function called explode() -> http://php.net/explode like this...

<?php
$string = 'a,b,c,d,e';
$myarray = explode(',' $string);
foreach ($myarray as $k => $v) {
    print $v .'<br />';
}
?>

I think that is the problem you're having

Sign up to request clarification or add additional context in comments.

1 Comment

That has done just the job! You are a gentleman and a scholar

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.