Reputation: 3
I want to print multiple data from the database in one table, however, it shows one row.
Model code is below. Please look at my codes and give some suggestion on how I can solve this issue.
function show_list()
{
$query=$this->db->get('student');
return $query;
}
Here is my controller code below:
<?php
class Showlist extends CI_Controller {
function index()
{
//$this->load->view('showlist');
$this->load->model('main_model');
$data['no']=$this->main_model->show_list();
//return the data in view
$this->load->view('showlist', $data);
}
}
?>
Here is my view code below:
<style>
table
{
border-collapse: collapse;
}
table, th, td
{
border: 1px solid black;
}
</style>
<body>
<html>
<center>
<table>
<tr>
<th>NAME </th>
<th>Student ID</th>
<th>Address</th>
<th>Fathers Name</th>
<th>Mothers Name</th>
<th>Education</th>
</tr>
<?php
if($no->num_rows()> 0){
foreach ($no->result() as $row) {
?>
<tr>
<td><?php echo $row->Name; ?></td>
<td><?php echo $row->StudentID; ?></td>
<td><?php echo $row->Address; ?></td>
<td><?php echo $row->FatherName; ?></td>
<td><?php echo $row->MotherName; ?></td>
<td><?php echo $row->Eduation; ?></td>
<!-- <td><a href="edit.php?id=<?php echo $row["ID"]; ?>">EDIT</a></td>
<td><a href="delete.php?id1=<?php echo $row["ID"]; ?> "onclick="return confirm('Do you want to delete??');">Delete</a></td>-->
</tr>
<?php
}
?>
<tr>
<td><center><a href='add_new_student.php'>ADD NEW STUDENT</a></center></td>
<td><a href='logout.php'>Logout</a></center></td>
</tr>
<?php
}
else
{
echo 'NO data Found';
}
?>
</table>
</center>
</html>
</body>
It only prints one row:
There are more data in my database. I try result()
but it is not printing the rows that I want.
Upvotes: 0
Views: 329
Reputation: 781
result() display only one row:
Add this line in model code:
return $query->result_array();
And change the foreach loop :
foreach ($no as $row) {..}
Change the line to all the rows:
<td><?php echo $row->Name; ?></td> to
<td><?=$row['Name'];?><td>
Upvotes: 1