loading...

Building a Dynamic Website (using php as a case study)

walterjnr1 profile image walterjnr1 ・6 min read

INTRODUCTION
PHP is an acronym for "PHP: Hypertext Preprocessor". PHP is an open source scripting language and server side language used for building dynamic websites.

An open-source language refers to a programming language that is not proprietary, and can be modified or built upon in a manner that is open to the public. (Free to download).

A dynamic website is a site that contains dynamic pages such as templates, contents, scripts etc. In a nutshell, the dynamic website displays various content types every time it is browsed.

A PHP script is executed on the server, and the plain HTML result is sent back to the browser.
A PHP file normally contains HTML tags, and some PHP scripting code.
We have versions of php. They latest version is php 7.4.7
Installation
PHP is install by installing WAMP. WAMP is an acronym that stands for Windows, Apache, MySQL, and PHP. It’s a software stack which means installing WAMP installs Apache, MySQL, and PHP on your operating system (Windows in the case of WAMP). WampServer is a Web development platform on Windows that allows you to create dynamic Web applications with Apache2, PHP, MySQL and MariaDB
IDE
An IDE (integrated development environment) is a software development tool that is mainly used by the developers to write and test the programs or software. PHP editors help developers while writing code by Highlighting syntax and Auto-completion.
Top IDE used by PHP include: Notepad++, and Bracket.
File Extension
Default file extension for PHP file is .php
Starting a PHP project
After installation of wamp, a folder called “www” is created in your drive c:
Create a sub-folder and name it accordingly inside the “www” folder
Create a php file inside the sub folder.
Type “localhost” on your address bar and . it displays list of projects in your www folder.
Click on WAMP server icon on your desktop.
Basic PHP syntax
syntax is a set of rules for grammar and spelling. In other words, it means using character structures that a computer can interpret.
A PHP script can be placed anywhere in the code Editor.
A PHP script starts with <?php and ends with ?>:

<?php
Echo “Hello World ”;
?>
PHP requires instructions to be terminated with a semicolon at the end of each statement. The closing tag of a block of PHP code automatically implies a semicolon
Comments in PHP
A comment in PHP code is a line that is not executed as a part of the program. Its only purpose is to be read by someone who is looking at the code.
Comments can be used to let others understand your code and also remind yourself of what you did - Most programmers have experienced coming back to their own work a year or two later and having to re-figure out what they did. Comments can remind you of what you were thinking when you wrote the code
PHP supports several ways of commenting:
// This is a single-line comment

This is also use

//save to db
Form Handling in PHP
Form Handling Entails passing values from a form to a server. When we develop a website or a web application, we often have to create forms to take input from users, like a Login form or a Registration form.
Creating a form on the webpage is accomplished using HTML, while convey those values from the webpage to the server and then in further processing those values.
PHP provides two superglobals $_GET and $_POST for collecting form-data for processing.
Understanding How HTML Form Works
Let's create a simple HTML form and try to understand how it works, what are the different attributes available in the

tag and what are they used for. By default, your code editor like dreamweaver will create your form using $_POST method.

    <form action="form-handler.php" method="POST">
        Name: <input type="text" name="txtname"> <br/>
        Email: <input type="text" name="txtemail"> <br/>
        <input name=”btnsubmit” type="submit">
    </form>

</body>


In the code above, we have used the

tag to create an HTML form, with input fields for Name and Email along with submit button to submit the form-data.
In the tag, we have two attributes, action and method, do you know what they are for?
  1. Action: Using this attribute, we can specify the name of the file which will collect and handle the form-data. In the example above, we have provided name of a Php file.
  2. Method: This attribute specify the means of sending the form-data, whether it will be submitted via POST method or GET method.
    Below we have the same form with method as GET,

    <form action="form-handler.php" method="GET">
        Name: <input type="text" name="name"> <br/>
        Email: <input type="text" name="email"> <br/>
        <input type="submit">
    </form>
    



    Creating a database in MYSQL
    phpMyAdmin is a free software tool written in PHP, intended to handle the administration of MySQL over the Web. phpMyAdmin supports a wide range of operations on MySQL and MariaDB. Frequently used operations (managing databases, tables, columns, relations, indexes, users, permissions, etc) can be performed via the user interface, while you still have the ability to directly execute any SQL statement.
    Database connection ( Using MySQLi or PDO)
    PHP 5 and later can work with a MySQL database using:
    • MySQLi extension (the "i" stands for improved)
    • PDO (PHP Data Objects)
    Earlier versions of PHP used the MySQL extension. However, this extension was deprecated in 2012.
    Both MySQLi and PDO have their advantages:
    PDO will work on 12 different database systems, whereas MySQLi will only work with MySQL databases.
    So, if you have to switch your project to use another database, PDO makes the process easy. You only have to change the connection string and a few queries. With MySQLi, you will need to rewrite the entire code - queries included.
    Both are object-oriented, but MySQLi also offers a procedural API.
    Both support Prepared Statements. Prepared Statements protect from SQL injection, and are very important for web application security.
    MySQL Examples in Both MySQLi and PDO Syntax
    In this, and in the following chapters we demonstrate MySQLi (procedural)
    way of working with PHP and MySQL:

Connect.php
<?php
/* Local Database*/
// Create connection
$conn = mysqli_connect("localhost","root", "", "meetup");
// Check connection
if (!$conn) {
echo("Connection failed: " . mysqli_connect_error());
}
?>
Insert Data
Index.php
<?php
include('connect.php');
if (isset($_POST["btnsubmit"])){

//pass values to variable
$principal = $_POST['txtprincipal'];
$time = $_POST['txttime'];
$rate = $_POST['txtrate'];

$si= (intval($principal) * intval($time) * intval($rate))/100;

echo "Simple Interest is :" . $si;
//save otp
$query = "INSERT into simpleinterest (principal, time, rate,si)
VALUES ( '$principal', '$time','$rate','$si')";
$result = mysqli_query($conn,$query);
if($result){
?>
<br> alert(&#39;Simple Interest Submitted Successfully&#39;);<br>
<?php
}else{
?>
<br> alert(&#39;Problem Submitting Simple Interest&#39;);<br>
<?php
}

}
?>
Registration form
<?php
include('connect.php');
if (isset($_POST['btnsubmit'])){
//pass textfield values to variables
$fullname=$_POST['txtfullname'];
$sex=$_POST['cmdsex'];
$phone=$_POST['txtphone'];
$address=$_POST['txtaddress'];
$state=$_POST['txtstate'];
$country=$_POST['txtphone'];
//insert to Db
$sql= "INSERT into registration (fullname,sex,address,state,country,phone) VALUES ('$fullname','$sex','$address','$state','$country','$phone' ) ";
$result =mysqli_query($conn,$sql);
if($result) {
echo (" window.alert(&#39;Succesfully Registered&#39;) ");
}else {
echo (" window.alert(&#39;Not Registered&#39;) ");
}
}
?>
SIMPLE LOGIN FORM
<?php
include('connect.php');

if (isset($_POST['btnsubmit'])){

$username=$_POST['txtusername'];
$phone=$_POST['txtphone'];

$sql= "select * FROM registration where username='$username' and phone='$phone'";
$result=mysqli_query($conn, $sql);
$row = mysqli_num_rows($result);
if ($row==1){
echo (" alert(&#39;Login Is Successful&#39;) ");
// header("location: index.php");
}else{
echo (" alert(&#39;Login Details Not Correct&#39;) ");
}
}

?>

Display Records On Table

S/N Fullname Username sex Address State Country Phone Action
Delete

The include (or require ) statement takes all the text/code/markup that exists in the specified file and copies it into the file that uses the include statement. Including files is very useful when you want to include the same PHP, HTML, or text on multiple pages of a website.

“mysqli_query(…)” is the function that executes the SQL queries against the database. it can be used to pass in the server connection link.
The mysqli_num_rows() function is an inbuilt function in PHP which is used to return the number of rows present in the result set
The function fetch_assoc() puts all the results into an associative array that we can loop through. The while() loop loops through the result set and outputs the data from the id, firstname and lastname columns.

NDUESO WALTER (+234806736102)

Posted on by:

walterjnr1 profile

walterjnr1

@walterjnr1

I am a software and web developer

Discussion

markdown guide