在 Zend Framework 1 中输出 json 中的多行
Output multiple rows in json in Zend Framework 1
我进行了搜索,但找不到问题的答案。
我有以下代码,它循环遍历一个数组,然后取回不同 $id 的结果。
使用 echo json_encode($row);
returns 时的输出显示所有结果,但显示 zend 布局。
但是,当使用 $this->_helper->json($row,true);
时,布局不会显示,而只会显示一个结果 returns。
如何 return 多个结果?
如有任何帮助,我们将不胜感激。
public function testAction()
{
//Get latest revision from database and loop through $id's
$id = array('308', '307', '306');
//Connect to database
foreach($id as $lId) {
$db = Zend_Db_Table::getDefaultAdapter();
$select = $db->select('')
->from('LinktagRevisions')
->where('linktagId = ?', $lId)
->order('updated DESC')
->limit(1);
$stmt = $select->query();
while ($row = $stmt->fetch()) {
$this->_helper->json($row,true);
//Encode as json and echo result
// echo json_encode($row);
}
}
}
我想你可以试试这个:
$result = array();
foreach($id as $lId) {
....
$stmt = $select->query();
$result[$lId] = $stmt->fetchAll();
}
$this->_helper->json($result,true);
我进行了搜索,但找不到问题的答案。
我有以下代码,它循环遍历一个数组,然后取回不同 $id 的结果。
使用 echo json_encode($row);
returns 时的输出显示所有结果,但显示 zend 布局。
但是,当使用 $this->_helper->json($row,true);
时,布局不会显示,而只会显示一个结果 returns。
如何 return 多个结果?
如有任何帮助,我们将不胜感激。
public function testAction()
{
//Get latest revision from database and loop through $id's
$id = array('308', '307', '306');
//Connect to database
foreach($id as $lId) {
$db = Zend_Db_Table::getDefaultAdapter();
$select = $db->select('')
->from('LinktagRevisions')
->where('linktagId = ?', $lId)
->order('updated DESC')
->limit(1);
$stmt = $select->query();
while ($row = $stmt->fetch()) {
$this->_helper->json($row,true);
//Encode as json and echo result
// echo json_encode($row);
}
}
}
我想你可以试试这个:
$result = array();
foreach($id as $lId) {
....
$stmt = $select->query();
$result[$lId] = $stmt->fetchAll();
}
$this->_helper->json($result,true);