Reputation: 103
Here I got some data from table cart using foreach. Now I want to display as like below (with heading and value as like in the table):
controller
$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');
}
}
view
<?php
foreach($results as $result)
{
echo '<span>'.$result['id'].'</span>';echo'</br>';
echo '<span>'.$result['useremail'].'</span>';echo'</br>';
echo '<span>'.$result['username'].'</span>';echo'</br>';
}
How can I display the foreach result data as like the db table?
Upvotes: 2
Views: 353
Reputation: 708
<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 } ?>
Also you can add some style like:
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 5px;
text-align: left;
}
</style>
Upvotes: 0
Reputation: 1949
You should use the HTML table tags.
<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>
Upvotes: 1
Reputation: 12085
1st : you need to embed the record set into html table
like below
2nd : Read basic html table tag here refer here
<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>
Upvotes: 1
Reputation: 126
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>";
Upvotes: 1
Reputation: 2982
Try using a table, you are doing it wrong
<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>
Upvotes: 2