CodeIgniter中result_array()和result()之间的区别

时间:2016-10-11 14:08:54

标签: php codeigniter

我的模型中有这两个功能:

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谷歌地图V3 API库我发现很难在不同的表中显示infowindow中的信息所以我调整了set_products和lat / lng现在从user_profiles表中获取并存储在products表中。

我认为我可以在我的地图方法中使用$coords = $this->products_model->get_coordinates();,但我在Trying to get property of non-object行上收到错误:$marker['position'] = $coordinate->latitude.','.$coordinate->longitude;

据我所知,get_coordinatesget_products的回复类型存在差异。我在Generating query results上阅读了CodeIgniters用户指南,但仍感到困惑。

当然我可以通过更改表名来调整get_coordinates(它可以工作)但是我必须使用相同的函数。如果我能使用get_puzzles,我会很高兴,但为了让我做出必要的改变,我需要了解其中的差异。

因此,如果有人可以在CodeIgniter中描述result_array()和result()之间的差异,我会很开心。

3 个答案:

答案 0 :(得分:1)

result_array()和result()之间的区别:

result_array()以数组格式的数组返回输出但是 result()以对象格式数组

返回输出

对于result_array(),您需要使用:

foreach($datas as $row) {
   $name = $row['name'];
}

for result()你可以使用:

foreach($datas as $row) {
   $name = $row->name;
}

答案 1 :(得分:0)

这主要是瑞金的答案措辞不同。

result_array()result()之间的差异:

result_array()返回一个数组数组。 result()返回一个对象数组。

我没有看到问题中提供的代码有任何问题。该错误消息表明$coordinate不是对象。但是,如果显示的模型代码是准确的,它应该是一个对象。

get_coordinates()可能会简单得多。整个foreach循环完全没有意义,因为它只是重新创建$query->result()返回的相同数据结构。试试这段代码

public function get_coordinates()
{
    $this->db->select("latitude, longitude, user_id");
    $query = $this->db->get("user_profiles");
    return $query->num_rows() > 0 ?  $query->result() : array();
}

如果您继续遇到Trying to get property of non-object错误,请尝试输入

var_dump($coords);
return;

在控制器中的foreach循环之前。查看循环尝试使用的内容可能会有所帮助。

答案 2 :(得分:0)

Result_array:

$query = $this->db->query("YOUR QUERY");

foreach ($query->result_array() as $row)
{
   echo $row['title'];
   echo $row['name'];
   echo $row['body'];
}

Result:

$query = $this->db->query("YOUR QUERY");

foreach ($query->result() as $row)
{
   echo $row->title;
   echo $row->name;
   echo $row->body;
}





So the main difference is "result is object" and "result_array is array"




and FYI
Row_array:

$query = $this->db->query("YOUR QUERY");

if ($query->num_rows() > 0)
{
   $row = $query->row_array(); 

   echo $row['title'];
   echo $row['name'];
   echo $row['body'];
}