0

I'm stacked on this matter. I have a model that retrieves data from a mysql database

class load_model extends CI_Model{
function __construct(){
parent::__construct();
}
Function loadsuppliers()
{       
    $this->db->select('SupplierID, Name');
    $records=$this->db->get('supplier');
    $data=array();

    foreach ($records->result() as $row)
    {
        $data[$row->SupplierID] = $row->Name;
    }
    return ($data);
}

} ?>

This model submits value to a function in my controller

public function getSupplier()
{
    $this->load->model('load_model');
    $data['unit'] = $this->load_model->loadsuppliers();
    $this->load->view('SupplierMGT', $data);

}

and I want to display the retrieved data to my view as a combo box. I tried to check if I am able to retrieve database values using echo json_encode($data) and it returns {"unit":{"2":"test","3":"Delta"}} ,

Could you help me with this? I tried using

<?php foreach($unit as $result):
print_r($result); 
endforeach;?>

to check if i am able to pass the value but i failed.

1
  • what are you getting in $data['unit'] in controller? Commented Jul 29, 2013 at 13:42

1 Answer 1

1

Small changes in the model:

function loadsuppliers()
{       
    $this->db->select('SupplierID, Name');
    $records=$this->db->get('supplier');
    $data=array();
    if($records->num_rows() > 0){
        $data   = $records->result_array();
    }
    return ($data);
}

In your view SupplierMGT.php write this:

<select name="" id="" multiple="">
    <?php
        if(isset($unit) && is_array($unit) && count($unit) > 0){
            foreach($unit as $key=>$each){

    ?>
    <option value="<?=$each['SupplierID']?>"><?=$each['Name']?></option>
    <?php
            }
        }
    ?>
</select>
Sign up to request clarification or add additional context in comments.

2 Comments

Oh Thanks for that, i did not notice that my code is somehow working.... This idea is really helpful @Niloy Saha
Happie to help. Accept the answer if it helped you.

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.