views:

49

answers:

2

Hello, i have two classes (Database for queries over database) and News for manipulating articles. On index.php (could be any other page) i call News class, which calls Database class. Everything goes ok, until i have to display result on index.php.

Let's say, i save result in $news in News class. How should i retrieve that array in index.php (like return $this->news or something else??). And how to display?

Could someone help.

A: 

On index.php (could be any other page) i call News class, which calls Database class.

You don't "call classes". You call methods of classes.

Let's say, i save result in $news in News class.

I suppose you mean you have a field (also property in PHP) names news, as in:

class News {
    private $news;
    /* ... */
}

How should i retrieve that array in index.php (like return $this->news or something else??)

You could add a method in News that returned this array:

class News {
    private $news;
    function retrieveNews() {
        /* query the DB and store the result in $this->news */
    }
    function getData() { return $this->news; }
}

Or you could make it public and access it directly through $newsObject->news (not recommended).

And how to display?

That depends on the structure of the data and how you want to display it.

Artefacto
applause :D i couldnt do anything with that question :D
Joe Hopfgartner
thanks for reply, first, sorry for bad terminology, im not native English speaker, so words dont come easy:)My problem is actually how to display that content (returned data), just where you stopped in your reply.Assuming that database query was: "select news_id, title, body, date where author_id='aid'";I save result in $this->news and return to index.phpAnd now i tried to use foreach loop, and some other functions but with no success. One of my tries:for($i=0; $i<10; $i++){ foreach($news[$i] as $a=>$b){ echo $b; }}The structure of the display itself is another issue.
phpEnthusiast
@php Again, that depends on the structure of the data. Do a `var_dump($news)` to see it.
Artefacto
All my attempts to display data in index.php get error: "Cannot use object of type News as array in..."
phpEnthusiast
@php Well that's because you're using a News object as in `$newsObject[$index]` and `$newsObject` is neither an array not an object that implements ArrayAccess.
Artefacto
@artefacto thanks, i will try to adjust data in some News method before it is ready for displaying. I can already see its gonna be a long day:)
phpEnthusiast
A: 

class Database{ // database.php function news($user_id){ $q="select all from news where author id='$id'"; $result=$db->query($q); for($i=0; $inum_rows; $i++){
$arr[]=$result->fetch_array;
} return $arr; }}

class News{              //  news.php
function get_news($user_id){
$news=$db->news($user_id);
return $news;
}}

index.php  // problem how to display
$post=new News;
$post->get_news($user_id);
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 ..."
}}
phpEnthusiast