0

I'm trying to determine whether or not the user has voted on a certain item and changing the image for the vote icon accordingly.

$user_likes = mysql_query("SELECT * from ratings WHERE userid = '$uid' AND aggregate = 1");
while ($row_likes = mysql_fetch_array($user_likes)) {
$row_likes['imageid'] = $likes_array;
}

Now in my content, I have a PHP foreach that returns a menu item for each submission (that is what users can vote on), the $image['idnum'] is the ID value for that individual image. The 'ratings' table seen in the query above is where I store votes like this: user id, image id, aggregate (in this case 1, to represent a like). Here is the in_array function that is giving me trouble:

if (in_array($image['idnum'], $likes_array)) {
    echo 'vote_triangle.png';
}

I'm getting the following error message:

Warning: in_array() [function.in-array]: Wrong datatype for second argument in (filename) on line 33

1
  • You are not passing an array as the second parameter. Commented Dec 28, 2011 at 4:39

1 Answer 1

2

I think you have this part backwards:

while ($row_likes = mysql_fetch_array($user_likes)) {
    $row_likes['imageid'] = $likes_array; // ????
}

Should be:

// Initialize the array
$likes_array = array();

while ($row_likes = mysql_fetch_array($user_likes)) {
     $likes_array[] = $row_likes['imageid'];
}
Sign up to request clarification or add additional context in comments.

5 Comments

Awesome! I had a feeling it would be something messed up with my array. I'll select your answer as soon as it lets me.
Do you fully understand it? The first version makes no sense, looks like it was basically a typo or "dumb" mistake.
I'm a bit embarrassed, but MySQL arrays confuse the hell out of me. I do better understand why I needed the [] to determine that it's an array and then use it as such in the function. I just thought that the variable would have stored $row_likes['imageid'] as an array.
$row_likes is reset in every iteration in the while loop, and $likes_array is undefined. I used $likes_array = array(); in case we don't get any results from the database, you'd still have a valid (empty) array.
I didn't even notice the array() but that definitely makes more sense. Thanks again!

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.