Hello,
In OOP PHP, i got 2 classes: Database and News. In News class i call DB function and get result (multidimensional 2D array). Now i have to display that array in index.php. I call News class (lets say get_news() function) from index.php but cant retrieve data and always get message: "Cannot use object of type News as array in ..."
Here is some of my code:
database.php
class DB{
function news($user_id){
$q="select all from news where author id='$id'";
$result=$db->query($q);
for($i=0; $i<$r->num_rows; $i++){
$arr[]=$result->fetch_array;
}
return $arr; //works fine!
}}
news.php
class News{
function get_news($user_id){
global $db;
$news=$db->news($user_id);
print_r($news); // works fine!
return $news;
}}
index.php // how to display data from News class (news.php)
$post=new News;
$post->get_news($user_id);
.. i tried
for($i=0; $i<5; $i++){
foreach($post[$i] as $k=>$v){
echo $v; //get error: "Cannot use object of type News as array in ..."
}
}