Php 如何在CodeIgniter视图中将CSS添加到foreach结果?
这里,我使用foreach从表购物车中获取了一些数据。现在我想显示如下所示(标题和值与表中相同): 控制器Php 如何在CodeIgniter视图中将CSS添加到foreach结果?,php,html,css,codeigniter,foreach,Php,Html,Css,Codeigniter,Foreach,这里,我使用foreach从表购物车中获取了一些数据。现在我想显示如下所示(标题和值与表中相同): 控制器 $query = $this->db->query('SELECT id,username,useremail FROM tbl_cart'); $resultdata['results'] = $query->result_array(); $this->load->view('one/home_comman_page/head'); $
$query = $this->db->query('SELECT id,username,useremail FROM tbl_cart');
$resultdata['results'] = $query->result_array();
$this->load->view('one/home_comman_page/head');
$this->load->view('one/usercart', $resultdata);
$this->load->view('one/home_comman_page/footer');
$this->load->view('one/home_comman_page/script');
}else{
redirect('users/login');
}
}
看法
您应该使用HTML表格标记
<table>
<?php foreach($results as $result)
{
echo '<tr style="border:1px solid grey;">';
echo '<td>.$result['id'].</td>';
echo '<td>.$result['useremail'].</td>';
echo '<td>.$result['username'].</td></tr>';
} ?>
</table>
尝试使用表格,你做错了
<table>
<thead>
<tr>
<td>ID</td>
<td>User Email</td>
<td>Username</td>
</tr>
</thead>
<tbody>
<?php
foreach ($results as $result) { ?>
<tr>
<td><?php echo $result['id']; ?></td>
<td><?php echo $result['useremail']; ?></td>
<td><?php echo $result['username']; ?></td>
</tr>
<?php
}
?>
</tbody>
身份证件
用户电子邮件
用户名
echo”“;
foreach($results作为$result)
{
回声“;
回显“$result['id']”;
回显“$result['useremail']”;
回显“$result['username']”;
回声“;
}
回声“;
1st:您需要将记录集嵌入html表中,如下所示
2nd:在此处阅读基本html表格标记
序号
身份证件
用户ID
用户名
.......
......
身份证件
用户名
用户电子邮件
您还可以添加一些样式,如:
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 5px;
text-align: left;
}
</style>
表,th,td{
边框:1px纯黑;
边界塌陷:塌陷;
}
th,td{
填充物:5px;
文本对齐:左对齐;
}
您正在使用
标记,这些标记是内联元素。这意味着他们会在页面上挨着坐。您可能希望使用
标记切换到表结构:)tr
应该在for
循环中,否则所有数据将显示在一行中,即在一个tr
中。这个奖肯定是给阿米拉的。非常感谢。
echo "<table class='table_Class' >";
foreach($results as $result)
{
echo "<tr>";
echo "<td>".$result['id']."</td>";
echo "<td>".$result['useremail']."</td>";
echo "<td>".$result['username']."</td>";
echo "</tr>";
}
echo "</table>";
<table border="1px">
<thead>
<tr>
<th>S.no</th>
<th>ID</th>
<th>User ID</th>
<th>UserName</th>
.......
</tr>
</thead>
<tbody>
<?php
foreach ($results as $result) { ?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $result['user_id']; ?></td>
<td><?php echo $result['username']; ?></td>
......
</tr>
<?php
}
?>
</tbody>
</table>
<table style="width:100%">
<tr>
<th>ID</th>
<th>User Name</th>
<th>User Email</th>
</tr>
<?php foreach($results as $result) {?>
<tr>
<td><?php echo $result['id']; ?></td>
<td><?php echo $result['username']; ?></td>
<td><?php echo $result['useremail']; ?></td>
</tr>
<?php } ?>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 5px;
text-align: left;
}
</style>