tags:

views:

61

answers:

4

Basically what happens is this:

A person goes to a specific gallery, say GalleryID=42. I do a query to grab all of the images in that gallery (with the value of GalleryID=42), and do a separate query to grab all of the comments associated with that gallery (for example GalleryID=42). There may only be 4 comments total on 3 different pictures out of 400 total images.

As I loop through the images with a do/while loop, and display them, I search the array of comments that have been placed for each picture as it loops. If it finds the picture ID that matches a specific picture, it displays the comment values (Comment, CommentAuthor, and CommentDate).

Here is the query for the images: "SELECT * FROM GalleryData WHERE GalleryID = 42"

And the query for the comments: "SELECT Comment, CommentAuthor, CommentDate, ID FROM Comments WHERE CategoryID = 42"

Then I use this code to put the comments in the reusable query: while(($Comments[] = mysql_fetch_assoc($rsComments)) || array_pop($Comments));

Then I use this to this to loop through the array to find the comments associated with the particular picture

foreach($Comments as $comment) { if($comment['ID'] == $row_rsGalleries['ID']) { echo '

'.$comment['Comment'].' - '.$comment['CommentAuthor'].'

'; } }

Problem is, that this code seems to not include the very first comment in the query.

Now, this is one of the first projects I have done something like this, and I am not a php/mysql expert, a novice user.

When I run the query, it comes up with 4 results, but the array only includes 3, the first result is missing.

+4  A: 

This construct looks totally crazy to me. I don't understand why it would cut off the first element, but I don't really feel inclined to even spend the time to find out: You need to fix that statement. Using || in this context is never going to give you the result you want.

Can you describe what this is supposed to do?

Pekka
It takes the values from a recordset:mysql_select_db($database_connUser, $connUser);$query_rsComments = sprintf("SELECT Comment, CommentAuthor, CommentDate, ID FROM Comments WHERE CategoryID = %s", GetSQLValueString($colname_rsComments, "int"));$rsComments = mysql_query($query_rsComments, $connUser) or die(mysql_error());$row_rsComments = mysql_fetch_assoc($rsComments);$totalRows_rsComments = mysql_num_rows($rsComments);
dragboatrandy
@Randy I don't understand what the `|| array_pop($Comments)` part is supposed to do. I think you can just leave that out and it should work
Pekka
+3  A: 

Why do you need the pop code?

while($rsComment = mysql_fetch_assoc($rsComments)) {
    $Comments[] = $rsComment;
}

Doesn't this do everything you need with the advantage of being much more verbose?

Edit The reason your code doesn't work is when the while evaluates to false to stop the loop, it runs the array_pop and this removes 1 element from the array.

D Roddis
Note that when the first element is popped off, a bool is popped back on to the end.
meagar
A: 

mysql_fetch_assoc returns an associative array that corresponds to the fetched row if there are rows present and it returns false if there are no more rows.

So

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

will look like:

while(true || array_pop($Comments)); 

for the first 4 rows. Now the || is short circuited, the pop does not happen. After this when there are no more rows left, mysql_fetch_assoc returns false which gets pushed in the array and then array_pop gets executed which removes the last added element (false).

So effectively the || array_pop($Comments) removes the final boolean value pushed in the array, ensuring that the array has only the rows returned by mysql_fetch_assoc.

codaddict
Incorrect; It's more like `while ($Comments[] = (true || array_pop(...)));` This is an important distinction, as the `array_push` values pushed onto the array are **always bools**.
meagar
Basically what happens is this: A person goes to a specific gallery, say GalleryID=42. I do a query to grab all of the comments associated with value of ID=42. In the gallery, there are 400 some images. Comments have been made on only a few pictures. As I loop through the images and display them, I search the array for comments that have been placed for each picture as it loops. If it finds the picture ID that matches a specific picture, it displays the comment values (Comment, CommentAuthor, and CommentDate).Does this make sense?
dragboatrandy
A: 

I can't figure how this works at all.

At first I assumed it would fill the array with bool values, but after thinking about it, the loop should never terminate.

It will eventually equate to while(($array[] = array_pop($array)));, and for a non-empty array, this will spin forever:

The following never terminates:

<?php

// Simulate MySQL_fetch_assoc; return false when no more results
function fetch($results) {
    if (count($results))
        return array_pop($results);
    return false;
}

$Comments = array();
$rsComments= array(array(3), array(4), array(5), array(6));

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

// $Comments = array(true,true,true,true,true,...);
meagar