1

I dont know if my logic is incorrect but I cant get my form_dropdown (down down list) list to populate with data from my database.

The error i get is Undefined property: stdClass::$name.

Code bellow.

My Array in $appertisers when print_r($appertisers);

Array ( [0] => stdClass Object ( [product] => Marinated mixed olives ) [1] => stdClass Object ( [product] => Simons ) [2] => stdClass Object ( [product] => Test ) ) 

View

            $array = array();
            foreach($appertisers as $row ){
                    $array = $row->name;
            }
            echo form_dropdown('appetisers',  $array);

    ?>

Model

    class Get_data extends CI_Model{
    function getAppertisers(){
        $query = $this->db->query("SELECT product FROM products WHERE cat = 1");
        return $query->result();
    }
}

Controller

public function index()
{
    $this->load->helper('url');
    $data = array();

    $this->load->model("get_data");
    $data['appertisers']  = $this->get_data->getAppertisers();

    $this->load->view('header');
    $this->load->view('content_contact', $data);


}

2 Answers 2

5

You need to add the row to the array, and reference the product property since there is no name property. You're currently just reassigning it:

foreach($appertisers as $row ){
    $array[] = $row->product;
}
Sign up to request clarification or add additional context in comments.

Comments

0

Your model should be:

class Get_data extends CI_Model
{
    function getAppertisers()
    {
        $this->db->where('cat', 1);
        $query = $this->db->get('products');
        if($query->num_rows() > 0)
        {
            foreach($query->result() as $row)
            {
                $data[] = $row->product;
            }
            return $data;
        }
    }
}

your view:

<?php echo form_dropdown('appertisers', '$appertisers', 'set_value('appetisers')'); ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.