下拉菜单中的数据库中的Codeigniter list_tables()

时间:2021-11-21 14:12:37

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.

我想知道如何在CodeIgniter的下拉菜单中列出数据库中的表名。我尝试使用$this->db->list_tables(),并在模型中使用它,就像我想用表中的值填充下拉菜单一样。

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].

它只返回第一个表[analisis],下拉列表中的值设置为1[检查元素窗口]。

下拉菜单中的数据库中的Codeigniter list_tables()

If I changed it to:

如果我把它改成:

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

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

它返回页面顶部的表格数组[analisistable1table2]。

2 个解决方案

#1


2  

$this->db->list_tables()

$ 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

#2


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

由于返回,上面的foreach块只迭代一次,所以$table的内容只有$tablelist,是$result的第一个元素。为什么不返回$this->db->list_tables()

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

#1


2  

$this->db->list_tables()

$ 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

#2


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

由于返回,上面的foreach块只迭代一次,所以$table的内容只有$tablelist,是$result的第一个元素。为什么不返回$this->db->list_tables()

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