PHP/Mysql array_pop missing first value
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 '<p>'.$comment['Comment'].' - '.$comment['CommentAuthor'].'</p>';
}
}
Problem is, that this code seems to not include the ver开发者_StackOverflowy 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.
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?
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.
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
.
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,...);
You have to put in a counter; for instance: $counter=3 Then your while statement contains that counter, like this:
while ($count < 3) { (Your count starts at 0, so although you have a total of 4 items, 0 counts as one of them. Your program will only count up to 3 with the array you have.)
I cannot help correct anything else in your code because I am too new to programming myself, but at least that will help you keep from having a never-ending loop.
精彩评论