duanci3845 2016-10-11 14:08
浏览 91

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

I have in my model these two function:

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

and these two methods in my controller:

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

        }

Website core idea is that users can add products and products will be displayed on map with information in infowindow. Originally I created get_coordinates because lat/lng values are stored in user_profiles table. Working with CodeIgniter Google Maps V3 API Library I found it hard to display information in infowindow from different table so I adjusted set_products and lat/lng now are being taken from user_profiles table and stored in products table as well.

I thought that I could use $coords = $this->products_model->get_coordinates(); in my map method but I get error: Trying to get property of non-object on $marker['position'] = $coordinate->latitude.','.$coordinate->longitude; line.

From what I see there is difference in return type for get_coordinates and get_products. I read CodeIgniters user guide on Generating query results but still feel confused.

Of course I could adjust get_coordinates by changing table name(which works) but then I have to functions that are doing besically the same. I would love if I could just use get_puzzles but in order for me to make neccessary changes I need to understand the difference.

So if some one please could describe differences between result_array() and result() in CodeIgniter I would be happy man.

  • 写回答

3条回答 默认 最新

  • dragon188199 2016-10-11 14:17
    关注

    Difference between result_array() and result() :

    result_array() returns output in array of array format but result() returns output in array of object format

    For result_array() you need to use :

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

    for result() you can use :

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

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况