0

I have a problem with my code, it two nested foreach loops:

$message = mysqli_query($mysqli, "SELECT * FROM messages where pseudo ='".$Pseudo."'");

while ($data = mysqli_fetch_assoc($message)) { 
     $items[] = $data;
}

$items = array_reverse($items ,true);

foreach($items as $item) {
         echo"".$item['msg']."";
         $commentaires = mysqli_query($mysqli, "SELECT * FROM commentaires where msg ='".$item['msg']."' and profil ='".$item['pseudo']."'");

         while($row = $commentaires->fetch_array()) { 
              $rows[] = $row;
         }

         foreach($rows as $row) {
              $abc = $row[0];$commen = $row[3];
              echo "$abc : $commen";
         }
}

so i've 2 entries in "messages" and 4 entries for "commentaires" 2 for the first entry of "messages" and 2 for the second. I want that :

message1 -commentaire 1 -commentaire 2

message2 -commentaire 3 -commentaire 4

and this is what i have :

message1 -commentaire 1 -commentaire 2

message2 -commentaire 1 -commentaire 2 -commentaire 3 -commentaire 4

i've search during 2 hours i don't have found a solution in mysqli :( (when i do

mysqli_data_seek($commentaires ,0);

that don't change anything) sorry for my bad English :c

1
  • Reinitialize rows variable before while loop where you fetch comments. $rows = array(); Commented Nov 26, 2015 at 14:19

1 Answer 1

1

Im pretty sure that this:

while($row = $commentaires->fetch_array()){$rows[] = $row;}

appends rows to existing array.. which means you have to clear $rows before appending new data to it, if you want it to work as written.

I'm no PHP shark, but I do believe

unset($rows);

would do the trick

Sign up to request clarification or add additional context in comments.

1 Comment

that's angry me, i've search too long, since 13H, and in 5 min i've the solution, thanks you very much ! (ha, and the array $row is not used in another code, it's weird, but cool :) )

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.