返回循环中创建的所有对象

时间:2014-07-27 12:18:21

标签: php mysqli

我是php OOP的新手,我正在尝试选择使用OOP,我有一个

class page {
    var $title;

    function settitle($title){ $this->title = $title;}
}

现在

class model {

    connection $done ;


    function getAllPages(){
        $query3 = "select * from pages";
        $result3 = mysqli_query($this->con,$query3);
        while ($rows=mysqli_fetch_array($result3)){
            $page = new Page;
            $page->setP_title($rows['p_title']);
        }
        return $page;
    }
}

现在

class controller{
    function indexAllPage(){
        $pages = $this->conn->getAllPages();
        include('allpages.php');
    }
}

现在

查看 allpages.php

1 个答案:

答案 0 :(得分:1)

如果要返回多个页面,则需要一个数组:

$pages = array();
while ($rows=mysqli_fetch_array($result3)){
    $page = new Page;
    $page->settitle($rows['p_title']);
    $pages[] = $page;
}
return $pages;

更新:

$pages = $this->conn->getAllPages();

// $pages is now an array, so you have to loop through it or get a specific index

// get first page from array
$page = $pages[0];

// loop through it
foreach ($pages as $page) {
    $title = $page->getTitle();
}