如何在新行中打印数据库中的每条记录?

时间:2016-02-25 07:04:14

标签: php codeigniter

我有一个名为Parameters的表,其中有一个名为

的字段
  

参数

现在

我从该表中选择*并获取所有记录。

所以我的控制器是这样的:

$this->data['params'] = $this->parameter_m->get();

在我通过所有记录预览的视图中。  像这样:

<tr>

    <td><?php foreach ($params as $param) {echo $param->parameter_name; }  ?></td>
    <td></td>
</tr>

我的问题是参数中的所有数据都显示在第一个<tr>中,我想在单独的<tr>

中显示每个参数

parameters_name包含一些数据,我想将这些数据放在单独的<tr><td>Data1</td></tr> <td>Data2</td></tr>中 我希望你理解我的问题。

数组:

array (size=10)
  0 => 
    object(stdClass)[33]
      public 'id' => string '1' (length=1)
      public 'parameter' => string 'SipUserName' (length=11)
      public 'parameter_name' => string 'SipUserName' (length=11)
      public 'in_type' => string 'readonly' (length=8)
      public 'is_mandatory' => string '0' (length=1)
      public 'arg_1' => string '' (length=0)
      public 'arg_2' => string '' (length=0)
  1 => 
    object(stdClass)[34]
      public 'id' => string '2' (length=1)
      public 'parameter' => string 'SipAuthName' (length=11)
      public 'parameter_name' => string 'SipAuthName' (length=11)
      public 'in_type' => string 'readonly' (length=8)
      public 'is_mandatory' => string '0' (length=1)
      public 'arg_1' => string '' (length=0)
      public 'arg_2' => string '' (length=0)
  2 => 
    object(stdClass)[35]
      public 'id' => string '3' (length=1)
      public 'parameter' => string 'DisplayName' (length=11)
      public 'parameter_name' => string 'DisplayName' (length=11)
      public 'in_type' => string 'readonly' (length=8)
      public 'is_mandatory' => string '0' (length=1)
      public 'arg_1' => string '' (length=0)
      public 'arg_2' => string '' (length=0)
  3 => 
    object(stdClass)[36]
      public 'id' => string '4' (length=1)
      public 'parameter' => string 'Password' (length=8)
      public 'parameter_name' => string 'Password' (length=8)
      public 'in_type' => string 'readonly' (length=8)
      public 'is_mandatory' => string '0' (length=1)
      public 'arg_1' => string '' (length=0)
      public 'arg_2' => string '' (length=0)
  4 => 
    object(stdClass)[37]
      public 'id' => string '5' (length=1)
      public 'parameter' => string 'Domain' (length=6)
      public 'parameter_name' => string 'Domain' (length=6)
      public 'in_type' => string 'readonly' (length=8)
      public 'is_mandatory' => string '0' (length=1)
      public 'arg_1' => string '' (length=0)
      public 'arg_2' => string '' (length=0)
  5 => 
    object(stdClass)[38]
      public 'id' => string '6' (length=1)
      public 'parameter' => string 'Proxy' (length=5)
      public 'parameter_name' => string 'Proxy' (length=5)
      public 'in_type' => string 'readonly' (length=8)
      public 'is_mandatory' => string '0' (length=1)
      public 'arg_1' => string '' (length=0)
      public 'arg_2' => string '' (length=0)
  6 => 
    object(stdClass)[39]
      public 'id' => string '7' (length=1)
      public 'parameter' => string 'Port' (length=4)
      public 'parameter_name' => string 'Port' (length=4)
      public 'in_type' => string 'readonly' (length=8)
      public 'is_mandatory' => string '0' (length=1)
      public 'arg_1' => string '' (length=0)
      public 'arg_2' => string '' (length=0)
  7 => 
    object(stdClass)[40]
      public 'id' => string '8' (length=1)
      public 'parameter' => string 'ServerMode' (length=10)
      public 'parameter_name' => string 'ServerMode' (length=10)
      public 'in_type' => string 'readonly' (length=8)
      public 'is_mandatory' => string '0' (length=1)
      public 'arg_1' => string '' (length=0)
      public 'arg_2' => string '' (length=0)
  8 => 
    object(stdClass)[41]
      public 'id' => string '9' (length=1)
      public 'parameter' => string 'Param_1' (length=7)
      public 'parameter_name' => string 'Param_1' (length=7)
      public 'in_type' => string 'text' (length=4)
      public 'is_mandatory' => string '0' (length=1)
      public 'arg_1' => string '' (length=0)
      public 'arg_2' => string '' (length=0)
  9 => 
    object(stdClass)[42]
      public 'id' => string '10' (length=2)
      public 'parameter' => string 'Param_2' (length=7)
      public 'parameter_name' => string 'Param_2' (length=7)
      public 'in_type' => string 'text' (length=4)
      public 'is_mandatory' => string '0' (length=1)
      public 'arg_1' => string '' (length=0)
      public 'arg_2' => string '' (length=0)

2 个答案:

答案 0 :(得分:2)

你应该循环<tr>而不是<td>,如下所示

<?php foreach($params as $param):?>
<tr>

    <td> <?php echo $param->parameter_name;?></td>

</tr>

<?php endforeach;?>

答案 1 :(得分:1)

<table>
    <tr>
        <td>ID</td>
        <td>parameter</td>
        <td>parameter_name</td>
        <td>in_type</td>
        <td>is_mandatory</td>
        <td>arg_1</td>
        <td>arg_2</td>
    </tr>
<?php
    foreach($params as $row){
        echo "  <tr>";
        echo "      <td>" . $row->id . "</td>";
        echo "      <td>" . $row->parameter . "</td>";
        echo "      <td>" . $row->parameter_name . "</td>";
        echo "      <td>" . $row->in_type . "</td>";
        echo "      <td>" . $row->is_mandatory . "</td>";
        echo "      <td>" . $row->arg_1 . "</td>";
        echo "      <td>" . $row->arg_2 . "</td>";
        echo "  </tr>";
    }
?>
</table>