display operation


Now, we will go to another operation that selects defined records from the data base and displays in a view file. For that you can create a separate file but here we are using insert.php file itself.

Display file will basically have an HTML table with table headings similar to the database column names and the data will be display in rows in an organized manner. So, let's create a table first.

CREATE TABLE TO DISPLAY DATA FROM DATABASE

<div class="container">	
	<h4><i class="fa fa-users"></i> Registered Users</h4><hr>
	<table class="table table-bordered table-striped table-hover">
		<thead>
			<tr>
				<th>S.N.</th>
				<th>Username</th>
				<th>Email</th>
				<th>Gender</th>
				<th>Language</th>
				<th>Country</th>
				<th>Action</th>
			</tr>
		</thead>
		<tbody>	
			<tr>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
			</tr>
		</tbody>
	</table>
</div>			

PHP MYSQLI QUERY TO DISPLAY DATA

In a static HTML page, we'd add rows below the header manually but here we'll use a php loop to retrieve data from the database and print here. That's why the section is empty as of now. I opted for not displaying the password here and will add the links to edit and delete individual recordset in the action column.

If you are creating a separate display file, you can include the connect.php file to get connected to the database. Here, i'll do the same as well the would need to remove that session_start() function from the top from this page as it has already been started in that connection file and php assumes it as an error if session is started multiple times.

Now, you can add the SQL SELECT query in the page and print desired values from the recordset.

<?php
// select query
$query = "SELECT * FROM user_info";

// execution of query
$result = mysqli_query($connect,$query);

//check result
var_dump($result);

Since, you've got multiple results for your SQL statement you can print them using loop but what if there's no data. That'll display error message right? So' lets check it first.

// count the number of record-set rows and hold it in a variable	
$number =  mysqli_num_rows($result);	

USING FOREACH LOOP

Now, we can use if else statement to print data in case there's data or display predefined message if there's no data. Further, we can use foreach loop to iterate the table row with each record-set.

<tbody>
<?php
$query = "SELECT * FROM user_info";
$result = mysqli_query($connect,$query);
$number =  mysqli_num_rows($result);
	
if($number > 0){
foreach($result as $key => $data){
?>					
	<tr>
		<td><?= ++$key ?></td>
		<td><?= $data['username'] ?></td>
		<td><?= $data['email'] ?></td>
		<td><?= $data['gender'] ?></td>
		<td><?= $data['language'] ?></td>
		<td><?= $data['country'] ?></td>
		<td>
			<i class="fa fa-edit"></i>
			<i class="fa fa-trash"></i>
		</td>
	</tr>
<?php } }else{ ?>
	<tr>
		<td colspan="7">No Data Found</td>
	</tr>';
<?php } ?>								
</tbody>

Here, if the record-set count count is greater than 0, we'll display them in the table under respective column else we'll display the message no data found.