SilverRay
SilverRay

Reputation: 331

CodeIgniter dropdown from multiple table

I am new to codeigniter and I am working on some project and I happen to need to display a drop down but filled with data from two or more tables. I don't know if join will solve the problem. Here is my view code:

<div class="form-group">
       <label class="control-label">Store Name</label>
        <select class="form-control" id="store" name="store">
        <?php foreach($dataget as $val)
        {
          ?>
          <option value="<?php echo $val->Store;?>"><?php echo $val->Store;?></option> 
          <?php
        }
          foreach($storename as $value)
          {
            ?>
            <option value="<?php echo $value['StoreName'];?>"><?php echo $value['StoreName'];?></option>
            <?php
          }
          ?>
            </select>
            </div>

and here is my model:

function get_store()
{

    $this->db->select('StoreName');
    $this->db->from('store');
    $query = $this->db->get();
    $result = $query->result_array();

    return $result;
}

and here is my controller:

public function poedit()
{
    $id = $this->uri->segment(3);
    $data['dataget'] = $this->wip_model->getByPOId($id);

    $datadrop['storename'] = $this->wip_model->get_store();

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

}

I am working on the edit page of the project can someone please help. Thanks in advance.

Upvotes: 1

Views: 1506

Answers (2)

Tuan Duong
Tuan Duong

Reputation: 515

Well, looks like you put incorrect view variable

$datadrop['storename'] = $this->wip_model->get_store();

Should be changed to

$data['storename'] = $this->wip_model->get_store();

And I'd recommend to merge 2 arrays ($dataget/$storename) into one in controller, then use one foreach in the view, the code must be better

Upvotes: 0

Praveen Kumar
Praveen Kumar

Reputation: 2408

In your controller

public function poedit()
{
    $id = $this->uri->segment(3);
    $data['dataget'] = $this->wip_model->getByPOId($id);

    $data['storename'] = $this->wip_model->get_store();

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

}

To stop duplicate rows you can use in your view

<div class="form-group">
    <label class="control-label">Store Name</label>
    <select class="form-control" id="store" name="store">
    <?php
    $store_name = array();
    foreach($dataget as $val)
    {
        $store_name[] = $val->Store;
    ?>
    <option value="<?php echo $val->Store;?>"><?php echo $val->Store;?></option> 
    <?php
    }
    foreach($storename as $value)
    {
        if(!in_array($value['StoreName'],$store_name))//it will stop duplication of rows... 
        {
    ?>
    <option value="<?php echo $value['StoreName'];?>"><?php echo $value['StoreName'];?></option>
    <?php
        }
    }
    ?>
    </select>
</div>

Upvotes: 1

Related Questions