title: Foreach iteration only returning last value from last loop
body: <p>I'm trying to grab and count all the replies for each post for example all the replies for a specific post are labeled <code>2</code> under the column <code>level</code> but my foreach loop only grabs the value from the last iteration. For, example lets say I want all the comment replies from <code>post_id</code> <code>91</code> which has the comments <code>92</code> <code>95</code> for their <code>post_id</code> which in turn has the replies <code>93</code> <code>94</code> <code>96</code> for their <code>post_id</code>. My foreach loop only returns the last replies <code>post_id</code> <code>96</code> but I want my foreach loop to save all the posts comments replies post ids for example, it should return all the post ids <code>93</code> <code>94</code> <code>96</code>. I was wondering what do I need to change in my code to achieve this please help. Thanks.</p>
  
  <p>MySQL Table Structure</p>
  
  <pre><code>CREATE TABLE posts(
  post_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
  parent_id INT UNSIGNED NOT NULL DEFAULT 0,
  member_id INT UNSIGNED NOT NULL,
  profile_id INT UNSIGNED NOT NULL,
  level INT UNSIGNED NOT NULL DEFAULT 0,
  post LONGTEXT DEFAULT NULL,
  date_created DATETIME NOT NULL,
  date_updated DATETIME DEFAULT NULL,
  update_count INT UNSIGNED NOT NULL DEFAULT 0,
  PRIMARY KEY (post_id)
  );
  </code></pre>
  
  <p>MySQL Table Data</p>
  
  <pre><code>post_id     parent_id   member_id   profile_id  level   post                    date_created            date_updated    update_count
  96          95          1           1           2       reply 1 for comment 2   2017-08-06 02:26:09     NULL            0
  95          91          1           1           1       comment 2               2017-08-06 02:25:43     NULL            0
  94          92          1           1           2       reply 2                 2017-08-06 02:25:09     NULL            0
  93          92          1           1           2       reply 1                 2017-08-06 02:25:00     NULL            0
  92          91          1           1           1       comment 1               2017-08-06 02:24:47     NULL            0
  91          0           1           1           0       post 1                  2017-08-06 02:24:31     NULL            0
  </code></pre>
  
  <p>PHP</p>
  
  <pre><code>//grab the all the post comments replies
  function grab_all_post_comment_replies_parent_ids($post_id){
      //filter string to contain only numbers just in case
      $post_id = filter_numbers($post_id);
      //escape special characters in the string for use in the MySQL statement
      $post_id = mysqli_real_escape_string(database(), $post_id);
  
      $query = "SELECT `posts`.`post_id`
                FROM `posts` 
                WHERE `parent_id` = '$post_id'
                AND `level` = '1'";
      //short hand if statement ? if-true : if-false to determine the number of results returned by the query                                          
      $count = ($query = mysqli_query(database(), $query)) ? mysqli_num_rows($query) : 0; 
  
      if($count === 0){
          //query returned 0 meaning no results so return false as the value for the function
          return false;
      } else {
          while($row = mysqli_fetch_array($query)){ 
              $results[] = $row['post_id'];
          }
  
          return $results;
      }
  }
  
  
  function all_post_comment_replies($post_id){
      $total = count(grab_all_post_comment_replies_parent_ids($post_id)); 
      $sum = 0;
      $all_ids = array();
      if(is_array(grab_all_post_comment_replies_parent_ids($post_id))){
          foreach(grab_all_post_comment_replies_parent_ids($post_id) as $parent_id){
              //filter string to contain only numbers just in case
              $parent_id = filter_numbers($parent_id);
              //escape special characters in the string for use in the MySQL statement
              $parent_id = mysqli_real_escape_string(database(), $parent_id);
  
              $query = "SELECT *
                        FROM `posts`
                        WHERE `parent_id` = '$parent_id'
                        AND `level` = '2'";
              //short hand if statement ? if-true : if-false to determine the number of results returned by the query                                          
              $count = ($query = mysqli_query(database(), $query)) ? mysqli_num_rows($query) : 0; 
  
              if($count === 0){
                  //query returned 0 meaning no results so return false as the value for the function
                  return false;
              } else {
                  while($row = mysqli_fetch_array($query)){ 
                      $ids[] = $row['post_id'];
                  }
  
                  $all_ids = $ids;
              }
  
              if($sum = $total){
                  return $all_ids;
              } else {
                  return false;
              }
  
              $sum++;
          }
      } else {
          return false;
      }
  }
  </code></pre>