I am using Class and function, trying to send data from mysqli while loop to public $post=array(); in Class,
I dunno why while loop only save last data from mysqli
Code
class Post extends Connection{
public $post=array();
function selectPost(){
$query="select * from posts";
$mysqli=$this -> connect();
$select_all_post=$mysqli->query($query);
$x=1;
while($row=$select_all_post->fetch_object()){
$this->post = array( 'Post No ' . $x=> array(
'post_title' => $row->post_title,
'post_author' => $row->post_author,
'post_date' => $row->post_date,
'post_content' => $row->post_content
));
$x++;
}
echo print_r($this->post);
}
}
if im trying to use [] at post array, it works but it make new array Check Output
$this->post[] = array( 'Post No ' . $x=> array(
'post_title' => $row->post_title,
'post_author' => $row->post_author,
'post_date' => $row->post_date,
'post_content' => $row->post_content
));
My question, how to send all data from mysqli using while loop to array in Class?