Wednesday, 17 October 2018

Display Data from Database: MYSQLI CRUD OOP Series

After saving data to database, we will write an algorithm that will display data from database. This time the data will be displayed as is, no pagination applied to it.

Step 6. Display data from database in index.php
Insert the following codes after
the end </form> tag in index.php
<!--display data from database-->
<table align="center">
<?php 
$query_userlevel="SELECT * FROM tbl_userlevel";
$resource_userlevel=mysqli_query($obj->con, $query_userlevel); 
Print "<th>ID</th>";
Print "<th>Userlevel Name</th>";
Print "<th>Description</th>";
Print "<th>Action</th>";
while($result_userlevel=$resource_userlevel->fetch_assoc())

Print "<tr>";
$ulevel_id = $result_userlevel['ulevel_id']; 
Print "<td>". $result_userlevel['ulevel_id']. "</td>";
Print "<td>". $result_userlevel['ulevel_name']. "</td>";
Print "<td>". $result_userlevel['ulevel_desc']. "</td>";
Print "<td>"."<a href ='index.php?edit=$ulevel_id'>"."Edit"."</a>"."</td>";
Print "</tr>";
}
?>

Discussion: 
$resource_userlevel=mysqli_query($obj->con, $query_userlevel); This command will pass the data using the database connection and query to variable $resource_userlevel.
while($result_userlevel=$resource_userlevel->fetch_assoc()) This code will pass the associative array to variable $result_userlevel that will be display in a table format.
The table will allow the user to easily edit the record by clicking on Edit link.

0 comments:

Post a Comment

If possible, leave a positive comment. No hate speech or elicit comments. Thank you.