views:

27

answers:

1

I have a Mysql Recordset that I have put into an associative array so that I can reuse it over and over.

I used this function to put the values into the array:

while(($Comments[] = mysql_fetch_assoc($rsComments)) || array_pop($Comments));

Here is what the print_r($Comments) displays

Array ( [0] => Array ( [CommentID] => 10 [Comment] => Ouch [CommentAuthor] => Randy Krohn [CommentDate] => 2010-10-06 17:19:49 [ID] => 1231 [CategoryID] => 42 ) [1] => Array ( [CommentID] => 12 [Comment] => This is the Dirty Duck [CommentAuthor] => John Lemoine [CommentDate] => 2010-10-06 17:22:43 [ID] => 1411 [CategoryID] => 42 ) [2] => Array ( [CommentID] => 13 [Comment] => Talk about deja vu! [CommentAuthor] => dber [CommentDate] => 2010-10-06 17:24:48 [ID] => 1473 [CategoryID] => 42 ) )

I am looping through a list of images, and I want to display only the comments associated with an images specified ImageID (for example 1473).

I need to display only the ones where the ID is equal to a specified value?

This must be easy, but for some reason, it is just flying over my head.

Thanks for you help!

+1  A: 

Simplest way is to loop through 'Comments' associative array (aka dictionary) with foreach and check the value of 'ID' key, if it matches the desired value, print the value of 'Comment' key:

$imageId = 1473;
foreach($Comments as $comment) {
    if($comment['ID'] == $imageId) {
        echo $comment['Comment'];
    }
}
vls
+1 perfect answer
steven_desu
Thank you so much. I knew it was simple, I just couldn't grasp it this morning. Exactly what I wanted.
dragboatrandy
Why would this function not include the first value in the recordset?while(($Comments[] = mysql_fetch_assoc($rsComments)) || array_pop($Comments));
dragboatrandy
First record should be included unless you called mysql_fetch_assoc or any other fetch method that would have moved the result set pointer forward somewhere prior to this but after calling query.
vls