Tsvetomirov Yordan Tsvetomirov Yordan - 1 month ago 10
PHP Question

How to get all results from while loop,from function

So i have a problem getting all of the results from my while loop within a function.When i print the results of the function its just 1 result instead of 5-10 which i need.There is the code:



function display_post_edits($post_id,$post_name){
global $connect;
$result = array();
$rev = sanitize('revision');
$post_name = '$post_id-revision-v1'; //sanitize this soon!!
$query = mysqli_query($connect,"SELECT post_author,post_date FROM posts WHERE post_type ='$rev' AND post_name = '$post_id-revision-v1'");
while ($row = mysqli_fetch_assoc($query)){
$result[]= $row;
}
return $result;
}
//outside of function
$display_edits = display_post_edits(get_post_id($_POST['subject']),$_POST['subject']);
print_r($display_edits);





So i am trying to get all of the results from the while loop and attach it to $display_edits and when i need for example post date only i will use $display_edits['post_date'].

There are some of my tries so far:

foreach($result as $row){
return implode($result[0]);
}


Returns 1 results because $return is printing arrays like http://prntscr.com/gby0xu (array in array) so i need to define a index.Same thing with implode i need to define index.

while ($row = mysqli_fetch_assoc($query)){
$result = array(
'post_author' =>$row'post_author',
'post_date' =>$row'post_date'
);
}


Prints only 1 results again...
If you need anything more,or i missed something to give here please say what and i will add it !
Thanks in advance !

Answer Source

You foreach should be like this:

foreach($result as $row){
    return implode($row[0]);
}

It should use $row and not $result