【发布时间】:2016-10-11 14:08:54
【问题描述】:
我的模型中有这两个功能:
function get_products($product_id = FALSE)
{
if ($product_id === FALSE)
{
$query = $this->db->get('products');
return $query->result_array();
}
$query = $this->db->get_where('products', array('product_id' => $product_id));
return $query->row_array();
}
function get_coordinates()
{
$return = array();
$this->db->select("latitude,longitude, user_id");
$this->db->from("user_profiles");
$query = $this->db->get();
if ($query->num_rows()>0) {
foreach ($query->result() as $row) {
array_push($return, $row);
}
}
return $return;
}
以及我的控制器中的这两种方法:
function index()
{
$data['products'] = $this->products_model->get_products();
$this->load->view('templates/header', $data);
$this->load->view('products/index', $data);
$this->load->view('templates/footer');
}
public function map()
{
...
$coords = $this->products_model->get_coordinates();
// Loop through the coordinates we obtained above and add them to the map
foreach ($coords as $coordinate) {
$marker = array();
$marker['position'] = $coordinate->latitude.','.$coordinate->longitude;
$marker['infowindow_content'] = $coordinate->user_id;
$this->googlemaps->add_marker($marker);
}
// Create the map
$data = array();
$data['map'] = $this->googlemaps->create_map();
// Load our view, passing through the map
$this->load->view('templates/header', $data);
$this->load->view('maps_view', $data);
}
网站的核心思想是用户可以添加产品,产品将显示在地图上,并在信息窗口中显示信息。 最初我创建了 get_coordinates 因为 lat/lng 值存储在 user_profiles 表中。使用 CodeIgniter Google Maps V3 API 库时,我发现很难在 infowindow 中显示来自不同表的信息,因此我调整了 set_products 和 lat/lng 现在从 user_profiles 表中获取并存储在 products 表中。
我认为我可以在我的 map 方法中使用 $coords = $this->products_model->get_coordinates();,但我得到错误:Trying to get property of non-object on $marker['position'] = $coordinate->latitude.','.$coordinate->longitude; 行。
据我所知,get_coordinates 和 get_products 的返回类型有所不同。我在Generating query results 上阅读了 CodeIgniters 用户指南,但仍然感到困惑。
当然,我可以通过更改表名(有效)来调整 get_coordinates,但是我必须使用基本相同的函数。如果我可以只使用 get_puzzles,我会很高兴,但为了让我做出必要的改变,我需要了解其中的区别。
所以如果有人可以描述 CodeIgniter 中 result_array() 和 result() 之间的区别,我会很高兴的。
【问题讨论】:
-
提供的代码没有错误。 sn-p 不是精炼的或最优的,但也没有被破坏。 题外话:无法重现
标签: php codeigniter