在不知道codeIgniter中的字段名的情况下获取数据

在不知道codeIgniter中的字段名的情况下获取数据,codeigniter,get,field,Codeigniter,Get,Field,我对CodeIgniter一无所知 假设我有一个用户表,如下所示: ID | NAME | PASS |…… 1 | n1 | p1xxx |…… 2 | n2 | p2xxx |…… 。|…|…|…|… 要在HTML表中显示它,我应该首先查询获取表User中的所有数据,然后在我的User\u控制器中调用它,将$data['users']传递给我的视图 通常,我们应该知道所有字段名,以便获得特定字段。例如: <?php if (count($users)): foreach ($user

我对CodeIgniter一无所知

假设我有一个用户表,如下所示:

ID | NAME | PASS |……

1 | n1 | p1xxx |……

2 | n2 | p2xxx |……

。|…|…|…|…

要在HTML表中显示它,我应该首先查询获取表User中的所有数据,然后在我的User\u控制器中调用它,将
$data['users']
传递给我的视图

通常,我们应该知道所有字段名,以便获得特定字段。例如:

 <?php if (count($users)): foreach ($users as $user): ?>    
    <tr>
        <td><?php echo $user->id; ?></td>
        <td><?php echo $user->name; ?></td>
        <td><?php echo $user->pass; ?></td>
        <td><?php echo $user->...; ?></td>
        <td><?php echo $user->...; ?></td>
    </tr>
<?php endforeach; endif; ?>

如果我们要处理数百个领域,这是一种令人沮丧的方式

我的问题很简单。我们可以不提->id、->姓名、->通行证而编码吗。。?
我想我们可以使用嵌套的
foreach
。然而,我没有找到如何实现它

您可以使用嵌套循环遍历结果:

$query = $this->db->query('SELECT * FROM user');

foreach($query->result() as $result){
    foreach($result as $key => $value){
        //key will be the field name and value will be 
        //the content of the field
        echo $key . ' - ' . $value .'<br>';
    }
}

基于您的示例表(显然包含您发布的字段——如果有更多字段,它们将被包括在内)。

祝您在codeigniter所称的HTML表类中好运

它将完全按照您的要求执行—从数据库中获取结果并将其显示为表—无需命名字段

  // Always use active record when possible
  // this will return all fields and records 
  if( $data['users'] = $this->db->get('users');  ) {
  // Load your view 
 }

  // In the view 
  $this->load->library('table');
  echo $this->table->generate($users);

它将做更多的工作,查看文档以获得更详细的示例。将它与一点CSS结合起来,只需一点代码就可以创建非常复杂的数据表

我已经按照你的建议进行了编码。但是,我面临一个小问题<代码>查询有问题,或者您没有使用db类
->result()
用于使用
$this->db
生成的查询返回的对象。
  // Always use active record when possible
  // this will return all fields and records 
  if( $data['users'] = $this->db->get('users');  ) {
  // Load your view 
 }

  // In the view 
  $this->load->library('table');
  echo $this->table->generate($users);