PHP CRUD Application Using Functions - View

From our previous article we went through the way of  entering data to the database using php functions. If your are not familiar with it visit PHP CRUD Application Using Functions-Insert to get to know about it.


Now lets create the User View in the same file. I have made some changes to the insert form to make it look nice. Code for the new form is given below

<div class="row"> 
  <hr>
</div>
<div class="col-md-2"></div>
<div class="col-md-8">
<div class="panel panel-default">
  <div class="panel-body">
  <h2>Add New User</h2>
<form action="" method="POST">

  <div class="form-group">
    <label for="exampleInputEmail1">Name</label>
    <input type="text" class="form-control" id="exampleInputEmail1" placeholder="Name" name="name">
  </div>
  <div class="form-group">
    <label for="exampleInputPassword1">Age</label>
    <input type="number" class="form-control" id="exampleInputPassword1" placeholder="Age" name="age">
  </div>
  
  <button type="submit" class="btn btn-default" name="insert">Submit</button>
</form>
</div>
</div>
</div>
<div class="col-md-2"></div>





Creating the View

<!--View data-->
<div class="row">

  <hr>
</div>
<div class="col-md-2"></div>
<div class="col-md-8">
<div class="panel panel-default">
  <div class="panel-body">
  <h2>View All users</h2>
<?php
                    // Include config file
                    require_once '../connect.php';
                    // calling the viewusers function in functions.php
                    viewusers ();

                    // Close connection
                    mysqli_close($conn);
                    ?>
</div>
</div>
</div>
<div class="col-md-2"></div>


The above code is used to create the view of the database values. viewusers (); function is used to call the sql query to fetch data from the database which is in the functions.php file.

viewusers(); 

The code of the viewusers(); is given below.

function viewusers (){
require 'connect.php';
// Attempt select query execution
                    $sql="SELECT * FROM user ";
                    if($result = mysqli_query($conn, $sql)){
                      //check whether there are more than one result.if yes printing the header.else execute the else
                        if(mysqli_num_rows($result) > 0){
                            echo "<table class='table table-bordered table-striped'>";
                                echo "<thead>";
                                    echo "<tr>";                                        
                                        echo "<th></th>";
                                        echo "<th>User Name</th>";
                                        echo "<th>Age</th>";                             
                                        
                                    echo "</tr>";
                                echo "</thead>";
                                echo "<tbody>";
                                // assigning the data of each row to an array and assigning the db values based on the db field names
                                while($row = mysqli_fetch_array($result)){
                                    echo "<tr>";
                                        echo "<td>" . $row['ID'] . "</td>";
                                        echo "<td>" . $row['UserName'] . "</td>";
                                        echo "<td>" . $row['UserAge'] . "</td>";                                    
                                        
                                    echo "</tr>";
                                }
                                echo "</tbody>";                            
                            echo "</table>";
                            // Free result set
                            mysqli_free_result($result);
                        } else{
                            echo "<p class='lead'><em>No records were found.</em></p>";
                        }
                    } else{
                        echo "ERROR: Could not able to execute $sql. " . mysqli_error($conn);
                    }
}

The code explanation is given in comments of the code.

Now run Home.php and check the results. If you have correctly coded,you would get an output as similar to below image.


Hope to see you soon with delete and update functions. Visit my github to get a better idea.

Comments

Popular posts from this blog

Working with Buttons in Android Studio

Java Part 2 :How to Install Java

Ruby - Dynamic, Open source programming language