简体   繁体   中英

Difference between result_array() and result() in CodeIgniter

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.

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

This is mostly Rijin's answer worded differently.

Difference between result_array() and result() :

result_array() returns an array of arrays. result() returns an array of objects.

I don't see anything wrong with the code as presented in the question. The error message indicates that $coordinate is not an object. But, if the model code shown is accurate, it should be an object.

get_coordinates() could be a lot simpler. The whole foreach loop is completely pointless because it simply recreates the same data structure that $query->result() returns. Try this code

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

If you continue to get the Trying to get property of non-object error try putting

var_dump($coords);
return;

before the foreach loop in your controller. It might be helpful to see exactly what the loop is trying to use.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM