【问题标题】:Difference between result_array() and result() in CodeIgniterCodeIgniter 中 result_array() 和 result() 的区别
【发布时间】: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_coordinatesget_products 的返回类型有所不同。我在Generating query results 上阅读了 CodeIgniters 用户指南,但仍然感到困惑。

当然,我可以通过更改表名(有效)来调整 get_coordinates,但是我必须使用基本相同的函数。如果我可以只使用 get_puzzles,我会很高兴,但为了让我做出必要的改变,我需要了解其中的区别。

所以如果有人可以描述 CodeIgniter 中 result_array() 和 result() 之间的区别,我会很高兴的。

【问题讨论】:

  • 提供的代码没有错误。 sn-p 不是精炼的或最优的,但也没有被破坏。 题外话:无法重现

标签: php codeigniter


【解决方案1】:

result_array() 和 result() 的区别:

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

对于 result_array() 你需要使用:

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

对于结果(),您可以使用:

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

【讨论】:

  • 我做了更改$marker['position'] = $coordinate['latitude.']','.$coordinate['longitude'];,现在我得到错误:Message: syntax error, unexpected '','' (T_CONSTANT_ENCAPSED_STRING) 我真的没有得到相同结果的两种不同语法。能否请您帮忙解决错误并建议关键字查找以便更好地理解?
  • 你需要用 $coordinate['latitude'] 来匹配 ','。你错过了一个“。”那里..
  • 使用这个 $marker['position'] = $coordinate['latitude'].','.$coordinate['longitude'];
【解决方案2】:

这主要是日进的答案,措辞不同。

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 循环之前。准确了解循环尝试使用的内容可能会有所帮助。

【讨论】:

  • 再简单一点,result() 无条件地生成一个数组——它要么生成一个空数组,要么生成一个对象数组。 num_rows() 条件是不必要的。我会做public function get_coordinates(): array { return $this->db->select("latitude, longitude, user_id")->get("user_profiles")->result(); }。我发现这个问题是题外话:无法重现
【解决方案3】:

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;
}

所以主要区别是“result is object”和“result_array is array”。

仅供参考,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'];
}

【讨论】:

猜你喜欢
  • 2013-05-06
  • 2014-06-07
  • 1970-01-01
  • 1970-01-01
  • 2018-05-16
  • 2013-11-09
  • 2018-12-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多