-1

I am familiar with php but still learning Js. Some people helped me on completing a ajax form to send data to mysql. But now I am stuck on below:

I used while loop as I want to print every thing which matches query

while($row = mysqli_fetch_assoc($result))
{
     comment_id = $row['comment_id'];
     echo "<script>";
     echo "var count = ".json_encode($comment_id);
      echo "</script>";
    }

But what I get a last value on while loop is assigned to js variable count as php is server sided and js is client sided. Then I tried adding array (as it can hold many values)

    while($row = mysqli_fetch_assoc($result)) 
{
    $comment_id = $row['comment_id'];
     echo "<script>";
                                    echo "var count = [];";
                                    echo.     "count.push($com);";
                                    echo "</script>"; 
}

But again i am getting last value in while loop. Is there any way I can assign whole data inside a php while loop to js variable.

Thanks :)

Aman Singh
  • 21
  • 1
  • 6
  • What value do you want/expect `count` to ultimately have? Basically, what information are you trying to get PHP to put into your JavaScript and for what use? – David Jul 22 '19 at 23:46
  • 1
    Even when you add the value into an array, you reset the array each iteration. Leave the `var count = [];` outside of the loop; – catcon Jul 22 '19 at 23:50
  • Thanks for helping me, @David actually it there is a reply button under every comment(that form will be hidden).. When user presses that button it will be shown and its same ajax form. It was working fine(hidden and shown) but when i added 2nd comment, it worked only for 1st comment, for 2nd comment form was shown. I think you might get confuse i will send a video link. At.catcon oh right i have added it outside loop. – Aman Singh Jul 23 '19 at 00:21

1 Answers1

0

I just got an idea and I think it is now resolved. I did is taken a php array inside while loop and assigned it to js array.

    $count = [];
    while($row = mysqli_fetch_assoc)
    $comment_id = $row['comment_id'];
     array_push($count,$comment_id);
    echo "<script>";
    echo "var count =".json_encode($count);
    echo "</script>";
}
Aman Singh
  • 21
  • 1
  • 6