简体   繁体   中英

Can't show result as select option Codeigniter 3

My Model name mymodel.php

.....    
function all($table,$order)
        {
            $this->db->select('*');
            $this->db->from($table);
            $this->db->order_by($order);
            $query = $this->db->get();
            if ($query->num_rows() == 0) {
                return FALSE;
            } else {
                return $query->result();
            }
        } .....

My controller name donate.php

.....
    function index()
    {
      $data = array(
                        'rekening' => $this->mymodel->all('rekening', 'id_rekening ASC'),
                        );
                    $this->load->view('client/donate/index', $data);
                } .....

And this My View in path view/client/donate/index

<?php foreach ($rekening as $rek): ?>
                <option value="<?php echo $rek->id_rekening; ?>" <?php echo set_select('ke_rekening', $rek->id_rekening); ?>><?php echo $rek->bank .' - '. $rek->atas_nama_rekening; ?></option>
              <?php endforeach ?>

But why cant show in select option? I use codeigniter 3, Show image

I think you have to pass value like this in modal.

function all($table,$order)
        {
            $this->db->select('*');
            $this->db->from($table);
            $this->db->order_by("id_rekening",$order);
            $query = $this->db->get();
            if ($query->num_rows() == 0) {
                return FALSE;
            } else {
                return $query->result();
            }
        } 

if I get it correctly is your problem is to get all list or you want to make that option selected which is previously selected?

Here is the solution for adding value to the <option> in the select box. I have also added how to get the pre-selected value selected.

  In Model:
    $query = $this->db->select('*')
        ->from($table)
        ->order_by($order)
        ->get();

    $your_result_array = $query->result_array();
    return $your_result_array;


    In Controller:

    $data = $this->mymodel->all($data1,$data2);
    $this->load->view('client/donate/index',['data'=>$data]) ;


    Inside Your View:

    <?php

    if(isset($data) && !empty($data)){
        foreach ($data as $single_data){

        ?> <option value="<?= $data['id_rekening] ?>" <?= $your_selected_value_array['your_selected_value'] === $data['id_rekening] ? 'selected':''  ?> > <?= $data['your_other_data_column_name_here] ?> </option> <?php

        }
    }else {

          ?><option> data not found </optiop> <?php
    }
    ?>

controller donate.php

function index()
{
  $data = array('rekening' => $this->mymodel->all('rekening', 'id_rekening'));
  $this->load->view('client/donate/index', $data);
}

Model donate.php

function all($table,$order)
{
   $this->db->select('*');
   $this->db->from($table);
   $this->db->order_by($order, 'asc');
   $query = $this->db->get();
   if ($query->num_rows() == 0) {
      return FALSE;
   } else {
      return $query->result();
   }
}

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