我目前正在使用Gmaps3,即時在使用標記填充地圖,但我的foreach循環只返回一個對象的點爲Joomla 2.5谷歌地圖組件。下面foreach循環和json_encode只返回一個對象Joomla 2.5組件
代碼:
我View.json.php:
<?php
defined('_JEXEC') or die;
jimport('joomla.application.component.view');
class LocateViewBranches extends JView
{
public function display($tpl = null)
{
$branch = $this->get('Branches');
foreach ($branch as $row) {
$response = array(
'lat' => $row->branch_latitude,
'lng' => $row->branch_longitude,
'data' => array(),
);
$response['data'][] = array(
'city' => $row->branch_city,
);
}
echo json_encode($response);
}
}
,然後在我的模型;
<?php
defined('_JEXEC') or die;
jimport('joomla.application.component.model');
class LocateModelBranches extends JModel
{
public function getBranches()
{
$db = $this->getDbo();
$query = $db->getQuery(true);
$query->select('*');
$query->from('#__branches');
$query->where("published = 1");
$db->setQuery($query);
$rows = $db->loadObjectList();
return $rows;
}
}
如果你們需要更多代碼,請大聲呼喊,但我認爲這是兩個關鍵文件。
在此先感謝
我認爲你需要'array_push'將'$ response'放到一個新的數組變量中,而'json_encode'則需要這個數組。 – Blazemonger
是的......你在循環的每次迭代中都覆蓋'$ response',而不是增加它。 – prodigitalson
你們會怎麼做呢? –