如何将Codeigniter查询结果放入表

时间:2016-04-21 08:11:19

标签: php html sql-server codeigniter

我想用我的查询结果创建HTML表。 HTML表应如下所示:

enter image description here

我真的不了解Codeigniter的HTML表类,所以我只是在我的视图中为表的每一列创建一个循环,如下所示:

<tr>
<?php foreach ($nameloop as $row) { ?> <td><?php echo $row->username; ?> </td><?php } ?>
<?php foreach ($addressloop as $row) { ?> <td><?php echo $row->useraddress; ?> </td><?php } ?>
<?php foreach ($ageloop as $row) { ?> <td><?php echo $row->userage; ?></td> <?php } ?>
<?php foreach ($sexloop as $row) { ?> <td><?php echo $row->usersex; ?></td> <?php } ?>
</tr>

原来这是一个非常糟糕的决定。它使我的桌子看起来像水平而不是像以前一样垂直。

这是我的模特:

<?php class Front_model extends CI_Model {

        public function __construct()
        {
                parent::__construct();
                $CI = &get_instance();
                $this->db2 = $CI->load->database('db2', TRUE);
        }


        function namelist()
        {
            $query = $this->db->query("SELECT name as uername FROM table1 ORDER BY name");
            return $query->result();
        }

        function addresslist()
        {
            $query = $this->db->query("SELECT address as useraddress FROM table1 INNER JOIN name
                                       ON address.id = name.id GROUP BY id ORDER BY name");
            return $query->result();
        }

        function agelist()
        {
            $query = $this->db->query("SELECT age as userage FROM table1 INNER JOIN name
                                       ON age.id = name.id GROUP BY id ORDER BY name");
            return $query->result();
        }

        function sexlist()
        {
            $query = $this->db->query("SELECT sex as usersex FROM table1 INNER JOIN name
                                       ON sex.id = name.id GROUP BY id ORDER BY name");
            return $query->result();
        }

}

然后这是我在CONTROLLER上的功能:

public function index()
    {

        $data['nameloop'] = $this->front_model->namelist();
        $data['addressloop'] = $this->front_model->addresslist();
        $data['ageloop'] = $this->front_model->agelist();
        $data['sexloop'] = $this->front_model->sexlist();


        $this->load->view('home',$data);
    }

这让我很困惑。无论是HTML标签还是我编码错误..我不知道...... 谢谢你的帮助,伙计......

1 个答案:

答案 0 :(得分:2)

你可以用更简单的方式做到这一点。

  

您的模型可以简化为使用一种方法获取所有数据

<?php class Front_model extends CI_Model {

        public function __construct()
        {
                parent::__construct();
                $CI = &get_instance();
                $this->db2 = $CI->load->database('db2', TRUE);
        }

        function userInformation()
        {
            $query = $this->db->query("SELECT name as username, address as useraddress, age as userage, sex as usersex FROM table1 INNER JOIN name ON table1.id = name.id GROUP BY id ORDER BY name");
            return $query->result();
        }

}
  

然后在你的控制器中你可以调用这个方法

public function index()
    {
        $data['information'] = $this->front_model->userInformation();
        $this->load->view('home',$data);
    }
  

最后在你看来,你可以只是你的一个预言

<tr>
<?php foreach ($information as $row) { ?> 
    <td><?php echo $row->username; ?></td>
    <td><?php echo $row->useraddress; ?></td>
    <td><?php echo $row->userage; ?></td>
    <td><?php echo $row->usersex; ?></td>
<?php } ?>
</tr>

希望有所帮助。

  

虽然我建议使用CI表library

<强>更新

如果您想使用现有查询,我建议您像这样修改控制器 -

public function index()
    {

        $names = $this->front_model->namelist();
        $addresses = $this->front_model->addresslist();
        $ages = $this->front_model->agelist();
        $sexes = $this->front_model->sexlist();

        $length = sizeOf($names);
        $data['information'] = [];

        for ($i = 0; $i <= $length; $i++) {
           $data['information'][$i] = [$names[$i], $addresses[$i], $ages[$i], ];
        }

        $this->load->view('home',$data);
    }

像这样更新你的观点 -

<tr>
<?php foreach ($information as $info) { ?> 
    <td><?php echo $info[0]->username; ?></td>
    <td><?php echo $info[1]->useraddress; ?></td>
    <td><?php echo $info[2]->userage; ?></td>
    <td><?php echo $info[3]->usersex; ?></td>
<?php } ?>
</tr>