我需要在Codeigniter的HTML表库中正确循环客户的名称,但我不知道如何正确使用它。
所以,这是我的模型:
<?php class Dash_model extends CI_Model {
public function __construct()
{
parent::__construct();
$this->load->database();
}
public function customerlist()
{
$query = $this->db->query("SELECT customername FROM customertable ORDER BY customerid");
foreach ($query->result() as $row)
{
echo $row->customers;
}
}
然后这是我的控制器:
class Dash_control extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('dash_model');
$this->load->library('table');
}
public function index()
{
$tmpl = array (
'row_start' => '<tr>',
'row_end' => '</tr>',
'cell_start' => '<td>',
'cell_end' => '</td>',
);
$this->table->set_template($tmpl);
$data['customerloop'] = $this->dash_model->customerlist();
$this->load->view('dashboard',$data);
}
在此之后,代码运行良好,我可以循环客户名称,但我不知道如何将它们传递到我的视图文件中。我正在使用引导表,所以我想我可以让它使用它:
<tbody>
<td><?php echo $customerloop;?></td>
</tbody>
但是当然,它在桌子上不起作用,它只是在没有桌子的情况下循环。
还有一个问题,Codeigniter 的表在 Bootstrap 中工作吗?或者它已经在没有引导的情况下响应?我是Codeigniter的新手,所以谢谢你们给我的每一个建议......
您可以在视图页面中使用 html table class
<table border="1" class="table table-bordered table-hover dataTable">
<tr role="row">
<th class="sorting" width="5%">Customer data</th>
<?php
foreach ($customerloop as $row) {
?><tr>
<td class=" "><?php echo $row->data; ?></td>
</tr>
<?php }
?>
</table>
模型:
public function __construct()
{
parent::__construct();
$this->load->database();
}
public function customerlist()
{
$query = $this->db->query("SELECT customername FROM customertable ORDER BY customerid");
return $query->result();
}
控制器:
class Dash_control extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('dash_model');
$this->load->library('table');
}
public function index()
{
$tmpl = array (
'row_start' => '<tr>',
'row_end' => '</tr>',
'cell_start' => '<td>',
'cell_end' => '</td>',
);
$this->table->set_template($tmpl);
$data['customerloop'] = $this->dash_model->customerlist();
$this->load->view('dashboard',$data);
}
视图:
<tbody>
<?php foreach ($customerloop as $row)
{ ?>
<tr><td><?php echo $row->customers; ?></td></tr>
<?php } ?>
</tbody>