Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

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 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.

share|improve this question
    
It takes all of the values from a Mysql query and puts it into a reusable array that I can use over and over. The values come from: – dragboatrandy Oct 7 '10 at 17:52
    
Well for one thing, you're going to be appending bool values onto $Comments... mysql_fetch_assoc(...) || array_pop(...) is evaluated before assignment, and the result will always be a bool. – meagar Oct 7 '10 at 17:52
1  
Why do you have || array_pop($Comments) ?? – MatTheCat Oct 7 '10 at 17:53
    
Have you tried var_dump($Comments) after the loop? – meagar Oct 7 '10 at 17:54
    
Is there a possibility of just putting the comments into the query that grabs all of the pictures? Keep in mind there may be 10 comments per picture? Like possibly creating a value of comments with each value separate by a ~ for example? – dragboatrandy Oct 7 '10 at 19:38

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?

share|improve this answer
    
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 Oct 7 '10 at 17:54
1  
@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 웃 Oct 7 '10 at 18:00

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.

share|improve this answer
1  
Note that when the first element is popped off, a bool is popped back on to the end. – meagar Oct 7 '10 at 18:01

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.

share|improve this answer
    
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 Oct 7 '10 at 18:16
    
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 Oct 7 '10 at 18:16

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,...);
share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.