1

I want to know how to list the tables names from a database in a dropdown in CodeIgniter. I tried using $this->db->list_tables() and use it in a model just like when I want to populate dropdown with values from a table.

Controller:

$data['tables'] = $this->model->get_tables();
$this->load->view('view', $data);

View:

<div class="col-md-2">
  <label class="col-md-8 control-label" for="tabel">Data</label>
  <div class="col-md-12">
   <?php $attributes = 'class="form-control" id="tabel"';
   echo form_dropdown('tabel', $tables, set_value('tables'), $attributes);?>
  </div>
</div>

Model:

function get_tables(){
  $result = $this->db->list_tables();

  $tablelist = array('- Table Lists -');

  foreach($result as $row) {
    return $tables = array($tablelist, $row);
  }
}

it returns just the first table [analisis] and the value set to 1 in the dropdown [checked it at the inspect element window].

printscreen.png

If I changed it to:

foreach($result as $row) {
  echo $row;        
}

it returns array of tables [analisistable1table2] on top of the webpage.

2 Answers 2

2

$this->db->list_tables()

Returns an array containing the names of all the tables in the database you are currently connected

So no need of fetch result. Just comment this line of code

 $result= $this->db->list_tables();
 //$result = $query->result();// comment no need of it
Sign up to request clarification or add additional context in comments.

1 Comment

It returns an array of table names on top of the webpage, but the form_dropdown is empty. Even if I don't call / echo it in the view, just put $data['tables'] -> $this->model->get_tables(); , the tables name list is there. Like table1table2table3... Thanks anyway.
0
foreach($result as $row) {
    return $tables = array($tablelist, $row);
}

Because of return, the foreach block above only iterates once, so the contents of $tables are only $tablelist and the first element of $result. Why don't you just return the $this->db->list_tables() like this

function get_tables(){
   return $this->db->list_tables();
}

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.