我需要将m.php中的数组传递回index.php并循环打印每一行,但我找不到怎样的方法呢?任何建议将不胜感激。
在代码下面我在m.php中使用return $ rows但是index.php无法获取它..
的index.php
<?php
require 'm.php';
$select_news = new select_news();
$select_news->select_3();
print_r($rows);
foreach ($rows as $row){
?>
<div><?=$row['id']?></div>
...
<?php
}
?>
m.php
class select_news{
public function select_3(){
global $db;
$sth = $db->prepare('SELECT * FROM news ORDER BY id DESC LIMIT 3');
$sth->execute();
$rows = $sth->fetchAll();
return $rows;
}
}
答案 0 :(得分:1)
只需将该函数调用的值赋给变量...
$select_news = new select_news();
$rows = $select_news->select_3();
print_r($rows);
foreach ($rows as $row){
?>
<div><?=$row['id']?></div>
...
<?php
}
?>
答案 1 :(得分:0)
要将其转换为数组(如果我正确阅读了问题),您可以将其放在foreach
语句的正上方:
$rows = unserialize(serialize(json_decode(json_encode((array) $rows), 1)));