ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Object of class CI_DB_mysql_result could not be converted to int

April 22, 2013 4:38pm

Subscribe [2]
  • #1 / Apr 22, 2013 4:38pm

    I have a controller which looks like

    class Products extends CI_Controller    {
    
        public function __construct(){
            //
            parent::__construct();
        }   
    
        public function index() {
            //
            $this->load->model('products_model');
            $data = $this->products_model->getProducts();
    
            $this->load->view('products',$data);    
        }
    
    }

    And model using Active Record

    class Products_model extends CI_Model {
    
        private function getMake()
        {
            $this->db->order_by('make');
            return $this->db->get('manufacturer');
        }
    
        public function getProducts() {
    
            $meta_title = "Meta Title";
            $meta_keywords = "keywords";
            $meta_description = "Page description"; 
    
            $make = array('' => 'Select a make')+$this->getMake();      
    
            return array("make" => $make, "meta_title" => $meta_title,"meta_keywords" => $meta_keywords,"meta_description" => $meta_description);   
    
        }
    }

    And a view which is a simple drop down for make:

    <? echo form_dropdown('make',$make,$this->input->post('make')); ?>

    Whole combination above throws me an error ‘Object of class CI_DB_mysql_result could not be converted to int’ on a line

    $make = array('' => 'Select a make')+$this->getMake();

    Any clue why?

    Thanks

  • #2 / Apr 22, 2013 4:45pm

    SitesByJoe

    163 posts

    My first guess is your “+” sign. $this->getMake() is returning a db result object, not an array. and the “+” is trying to operate on it.

  • #3 / Apr 22, 2013 4:56pm

    I have got replaced that line with:

    $make = array_merge(array(''=>'Select a make'), $this->getMake()->result());

    But now I’m getting empty drop down with that error:

    Object of class stdClass could not be converted to string

  • #4 / Apr 22, 2013 5:08pm

    SitesByJoe

    163 posts

    Exactly, result() gives you an object not an array. Use result_array() instead.

  • #5 / Apr 22, 2013 5:13pm

    With result_array I was able to generate a drop down, but each result it goes as separate select option instead of ID as value and MAKE as a choice name

  • #6 / Apr 22, 2013 5:16pm

    SitesByJoe

    163 posts

    You probably need to go through the result_array() to then make a new array with the id as your key and name as the value, THEN tack it to your other array.

    Does that make sense?  Without a code example that’s the clearest way to explain it.

  • #7 / Apr 23, 2013 10:31am

    OK, I have ended with

    $makes_temp = array();
    foreach ($this->getMake()->result_array() as $explode_temp) {
       $makes_temp[$explode_temp['id']]=$explode_temp['make']; 
    
    } 
    
    $make = array(''=>'Select a make') + $makes_temp;
.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases