Book Image

PHP 7 Programming Blueprints

By : Jose Palala, Martin Helmich
Book Image

PHP 7 Programming Blueprints

By: Jose Palala, Martin Helmich

Overview of this book

When it comes to modern web development, performance is everything. The latest version of PHP has been improvised and updated to make it easier to build for performance, improved engine execution, better memory usage, and a new and extended set of tools. If you’re a web developer, what’s not to love? This guide will show you how to make full use of PHP 7 with a range of practical projects that will not only teach you the principles, but also show you how to put them into practice. It will push and extend your skills, helping you to become a more confident and fluent PHP developer. You’ll find out how to build a social newsletter service, a simple blog with a search capability using Elasticsearch, as well as a chat application. We’ll also show you how to create a RESTful web service, a database class to manage a shopping cart on an e-commerce site and how to build an asynchronous microservice architecture. With further guidance on using reactive extensions in PHP, we’re sure that you’ll find everything you need to take full advantage of PHP 7. So dive in now!
Table of Contents (15 chapters)
PHP 7 Programming Blueprints
Credits
About the Authors
About the Reviewer
www.PacktPub.com
Preface
4
Build a Simple Blog with Search Capability using Elasticsearch

Create a profile input form


Now let's create the HTML form for users to enter their profile data.

Our profiles app would be no use if we didn't have a simple way for a user to enter their user profile details.

We'll create the profile input form like this:

//create_profile.php 
 
<html> 
<body> 
<form action="post_profile.php" method="POST"> 
 
  <label>Name</label><input name="name"> 
  <label>Age</label><input name="age"> 
  <label>Country</label><input name="country"> 
 
</form> 
</body> 
</html> 

In this profile post, we'll need to create a PHP script to take care of anything the user posts. It will create an SQL statement from the input values and output whether or not they were inserted.

We can use the null coalesce operator again to verify that the user has inputted all values and left nothing undefined or null:

$name = $_POST['name'] ?? ""; 
 
$age = $_POST['country'] ?? ""; 
 
$country = $_POST['country'] ?? ""; 

This prevents us from accumulating errors while inserting data into our database.

First, let's create a variable to hold each of the inputs in one array:

$input_values =  [ 
 'name' => $name, 
 'age' => $age, 
 'country' => $country 
]; 

The preceding code is a new PHP 5.4+ way to write arrays. In PHP 5.4+, it is no longer necessary to put an actual array(); the author personally likes the new syntax better.

We should create a new method in our UserProfile class to accept these values:

Class UserProfile { 
 
 public function insert_profile($values)  { 
 
 $link =  mysqli_connect('127.0.0.1', 'username','password', 'databasename'); 
 
 $q = " INSERT INTO " . $this->table . " VALUES ( '".$values['name']."', '".$values['age'] . "' ,'".$values['country']. "')"; 
   return mysqli_query($q); 
 
 } 
} 

Instead of creating a parameter in our function to hold each argument as we did with our profile template render function, we can simply use an array to hold our values.

This way, if a new field needs to be inserted into our database, we can just add another field to the SQL insert statement.

While we are at it, let's create the edit profile section.

For now, we'll assume that whoever is using this edit profile is the administrator of the site.

We'll need to create a page where, provided the $_GET['id'] has been set, that the user that we will be fetching from the database and displaying on the form. Here is how that code will look like:

<?php 
require('class/userprofile.php');//contains the class UserProfile into 
 
$id = $_GET['id'] ?? 'No ID'; 
//if id was a string, i.e. "No ID", this would go into the if block 
if(is_numeric($id)) { 
  $profile =  new UserProfile(); 
  //get data from our database 
  $results =   $user->fetch_id($id); 
  if($results && $results->num_rows > 0  ) { 
     while($obj = $results->fetch_object()) 
   { 
          $name = $obj->name; 
          $age = $obj->age; 
       $country = $obj->country; 
      } 
        //display form with a hidden field containing the value of the ID 
?> 
 
  <form action="post_update_profile.php" method="post"> 
    
  <label>Name</label><input name="name" value="<?=$name?>"> 
  <label>Age</label><input name="age" value="<?=$age?>"> 
  <label>Country</label><input name="country" value="<?=country?>"> 
 
</form> 
 
  <?php 
        
  } else { 
         exit('No such user'); 
  } 
   
} else { 
  echo $id; //this  should be No ID'; 
 exit; 
}   

Notice that we're using what is known as the shortcut echo statement in the form. It makes our code simpler and easier to read. Since we're using PHP 7, this feature should come out of the box.

Once someone submits the form, it goes into our $_POST variable and we'll create a new Update function in our UserProfile class.