The most viewed items
This step is a little bit more complex. In this step, we also check the User Id already exists or not. But, not too difficult. I hope you will be ok.
1. Create html form.
2. Connect to server.
3. Check form is filled or not.
4. Check the User Id already exists or not.
5. Insert data into table.
<?php
@$server = 'localhost';
@$username = 'root';
@$password = ' ';
@$db = 'TNW';
@$conn = mysqli_connect($server, $username, $password, $db);
if($conn){
if(isset($_POST['UserId']) && isset($_POST['FirstName']) && isset($_POST['LastName']) && isset($_POST['UserName'])&& isset($_POST['Password'])){
$userid = $_POST['UserId'];
$firstname = $_POST['FirstName'];
$lastname = $_POST['LastName'];
$username = $_POST['UserName'];
$password = $_POST['Password'];
//check form is filled or not
if(!empty($userid) && !empty($firstname) && !empty($lastname) && !empty($username) && !empty($password)){
//check userid already exists or not
$query = "SELECT `UserId` FROM `username` WHERE `UserId` = '".$userid."'";
$query_run = mysqli_query($conn, $query);
$row = mysqli_num_rows($query_run);
if($row == 1){
echo 'User Id already exists. Please choose another User Id.';
}else{
//insert data into table
$query = "INSERT INTO `username` VALUES('".$userid."', '".$firstname."', '".$lastname."',
'".$username."', '".$password."')";
$query_run = mysqli_query($conn, $query);
if($query_run){
echo 'Data is successfully inserted into table.';
}else{
echo 'Inserting Error!';
}
}
}else{
echo 'Please fill all fields.';
}
}
}else{
echo 'Error in Server connection!';
}
?>
<html>
<head>
<title>Inserting Data into table</title>
<style>
.tnwform{
padding: 20px;
}
</style>
</head>
<body>
<form action="inserting_data_into_tabel_3.php" method="POST" class="tnwform">
<label for="UserId">User Id</label>
<input type="text" name="UserId"><br /><br />
<label for="FirstName">First Name</label>
<input type="text" name="FirstName"><br /><br />
<label for="LastName">Last Name</label>
<input type="text" name="LastName"><br /><br />
<label for="UserName">User Name</label>
<input type="text" name="UserName"><br /><br />
<label for="Password">Password</label>
<input type="password" name="Password"><br /><br />
<input type="submit" value="Submit">
</form>
</body>
This step is a litter bit complex. But, not too difficult. I hope you will be ok.
1. Create html form.
2. Connect to server.
3. Check form is filled or not.
4. Insert data into table.
<?php
@$server = 'localhost';
@$username = 'root';
@$password = ' ';
@$db = 'TNW';
@$conn = mysqli_connect($server, $username, $password, $db);
if($conn){
if(isset($_POST['UserId']) && isset($_POST['FirstName']) && isset($_POST['LastName']) && isset($_POST['UserName']) && isset($_POST['Password'])){
$userid = $_POST['UserId'];
$firstname = $_POST['FirstName'];
$lastname = $_POST['LastName'];
$username = $_POST['UserName'];
$password = $_POST['Password'];
if(!empty($userid) && !empty($firstname) && !empty($lastname) && !empty($username) && !empty($password)){
$query = "INSERT INTO `username` VALUES('".$userid."', '".$firstname."', '".$lastname."',
'".$username."', '".$password."')";
$query_run = mysqli_query($conn, $query);
if($query_run){
echo 'Data is successfully inserted into table.';
}else{
echo 'Inserting Error!';
}
}else{
echo 'Please fill all fields.';
}
}
}else{
echo 'Error in Server connection!';
}
?>
<html>
<head>
<title>Inserting Data into table</title>
<style>
.tnwform{
padding: 20px;
}
</style>
</head>
<body>
<form action="inserting_data_into_tabel_2.php" method="POST" class="tnwform">
<label for="UserId">User Id</label>
<input type="text" name="UserId"><br /><br />
<label for="FirstName">First Name</label>
<input type="text" name="FirstName"><br /><br />
<label for="LastName">Last Name</label>
<input type="text" name="LastName"><br /><br />
<label for="UserName">User Name</label>
<input type="text" name="UserName"><br /><br />
<label for="Password">Password</label>
<input type="password" name="Password"><br /><br />
<input type="submit" value="Submit">
</form>
</body>
</html>
<?php
$server = 'localhost';
$username = 'root';
$password = ' ';
$db = 'TNW';
$conn = mysqli_connect($server, $username, $password, $db);
if($conn){
$query = "INSERT INTO `username` VALUES(1, 'Thet', 'Naing', 'thet', 'password1'),
(2, 'Michael', 'Win', 'win', 'password2'),
(3, 'Mya', 'Mya', 'myamya', 'password3')";
$query_run = mysqli_query($conn, $query);
if($query_run){
echo 'Successfully inserted data into table.';
}else{
echo 'Inserting error or Data already exits.';
}
}else{
echo 'Connecting to a database Error.';
}
?>
<?php
$server = 'localhost';
$username = 'root';
$password = '';
$db = 'TNW';
$conn = mysqli_connect($server, $username, $password, $db);
if($conn){
$query = "DROP TABLE `username`";
$query_run = mysqli_query($conn, $query);
if($query_run){
echo 'Successfully deleted a table.';
}else{
echo 'Deleting a table Error.';
}
}else{
echo 'Connecting to a database Error.';
}
?>
<?php
$server = 'localhost';
$username = 'root';
$password = '';
$db = 'TNW';
$conn = mysqli_connect($server, $username, $password, $db);
if($conn){
$query = "CREATE TABLE `username`(
UserId int(30) NOT NULL AUTO_INCREMENT PRIMARY KEY,
FirstName varchar(30) NOT NULL,
LastName varchar(30) NOT NULL,
UserName varchar(30) NOT NULL,
Password varchar(50) NOT NULL
)";
$query_run = mysqli_query($conn, $query);
if($query_run){
echo 'Successfully created a table.';
}else{
echo 'Creating a table Error.';
}
}else{
echo 'Connecting to a database Error.';
}
?>
<?php
$server = 'localhost';
$username = 'root';
$password = '';
$db = 'TNW';
$conn = mysqli_connect($server, $username, $password, $db);
if($conn){
echo 'Successfully connected to a database -' .$db. '.';
}else{
echo 'Connecting to a database Error.';
}
?>
<?php
$server = 'localhost';
$user = 'root';
$password = '';
$conn = mysqli_connect($server, $user, $password);
if($conn){
$query = "DROP DATABASE `TNW`";
$query_run = mysqli_query($conn, $query);
if($query_run){
echo 'Successfully deleted a Database.';
}else{
echo 'Deleting a database Error.';
}
}else{
echo 'Connection Error';
}
?>
<?php
$server = 'localhost';
$user = 'root';
$password = '';
$conn = mysqli_connect($server, $user, $password);
if($conn){
$query = "CREATE DATABASE `TNW`";
$query_run = mysqli_query($conn, $query);
if($query_run){
echo 'Successfully created a Database.';
}else{
echo 'Creating a database Error.';
}
}else{
echo 'Connection Error';
}
?>
The following is the example of connecting to database server in php.
<?php
$server = 'localhost';
$user = 'root';
$password = '';
$conn = mysqli_connect($server, $user, $password);
if($conn){
echo 'Successfull connected to the server.';
}else{
echo 'Connection Error.';
}
?>
We can set the limitations in uploading files. For example, we allow to upload only jpg or jpeg images and the maximum file size is 800KB.
<?php
@$name = $_FILES['file']['name'];
@$type = $_FILES['file']['type'];
@$size = $_FILES['file']['size'];
@$extension = substr($name, strpos($name, '.')+1);
@$max_size = 800000;
@$tmp_name = $_FILES['file']['tmp_name'];
@$location = 'tnw87/';
if(isset($name) && !empty($name)){
if($extension == 'jpg' || $extension == 'jpeg'){
if($size < $max_size){
if(move_uploaded_file($tmp_name, $location.$name)){
echo 'Successfully uploaded!';
}else{
echo 'Upload Errors!';
}
}else{
echo 'The maximum file size is 800KB';
}
}else{
echo 'Only jpg or jpeg image is allowed!';
}
}else{
echo 'Please choose a file to upload';
}
?>
<form action = "upload.php" method = "POST" enctype = "multipart/form-data">
<input type = "file" name="file"><br /><br />
<input type = "submit" value = "Submit">
</form>
In this example, we will upload file to the designated location. So, we will create a folder to store the files we've uploaded and give name to that folder. I give it as tnw87. Ok, lets start write the following codes and discover how it works.
<?php
$name = $_FILES['file']['name'];
$type = $_FILES['file']['type'];
$size = $_FILES['file']['size'];
$tmp_name = $_FILES['file']['tmp_name'];
if(@isset($name) && @!empty($name)){
$location = 'tnw87/';
if(move_uploaded_file($tmp_name, $location.$name)){
echo $name. ' is successfully uploaded.';
}else{
echo 'Upload Errors';
}
}else{
echo 'Please select file to upload';
}
?>
<style type = "text/css">
form{margin-top : 100px; margin-left : 50px;}
</style>
<form action = "upload.php" method = "POST" enctype = "multipart/form-data">
<input type = "file" name = "file"></br></br>
<input type = "submit" value = "Upload">
</form>
When we upload a file into one location, the file is firstly saved in temporary location as a temp file. Ok, lets discover how it works. We will write the following codes and test it.
<?php
$name = $_FILES['file']['name'];
$type = $_FILES['file']['type'];
$size = $_FILES['file']['size'];
$tmp_name = $_FILES['file']['tmp_name'];
if(@isset($name) && @!empty($name)){
$location = 'tnw87/';
if(move_uploaded_file($tmp_name, $location.$name)){
echo $name. ' is successfully uploaded.';
}else{
echo 'Upload Errors';
}
}else{
echo 'Please select file to upload';
}
?>
<style type = "text/css">
form{margin-top : 100px; margin-left : 50px;}
</style>
<form action = "upload.php" method = "POST" enctype = "multipart/form-data">
<input type = "file" name = "file"></br></br>
<input type = "submit" value = "Upload">
</form>
<?php
@$name = $_FILES['file']['name'];
@$type = $_FILES['file']['type'];
@$size = $_FILES['file']['size'];
if(@isset($name) && @!empty($name)){
echo 'File name is <strong> '.$name.'</strong>, File type is <strong>'.$type.'</strong> nad File size is <strong> '.$size.'</strong>';
}else{
echo 'Please select file to upload';
}
?>
<style type = "text/css">
form{margin-top : 100px; margin-left : 50px;}
</style>
<form action = "upload.php" method = "POST" enctype = "multipart/form-data">
<input type = "file" name = "file"></br></br>
<input type = "submit" value = "Upload">
</form>
<?php
$time = time();
$time_now = date('D M Y & H:i:s', $time);
$time_modified = date ('D M Y @ H:i:s', strtotime('+ 1 year') );
echo 'The current time is '.$time_now.'.</br>The time modified is '.$time_modified.'.' ;
?>
<?php
$redirect_page = 'https://www.tnw87.com';
$redirect = true;
if($redirect == true){
header('location:' .$redirect_page);
}
?>
<?php
$offset = 0;
if(isset($_POST['text']) && isset($_POST['search']) && isset($_POST['replace'])){
$text = $_POST['text'];
$search = $_POST['search'];
$replace = $_POST['replace'];
$search_length = strlen($search);
if(!empty($text) && !empty($search) && !empty($replace)){
while($strpos = strpos($text, $search, $offset)){
$offset = $strpos + $search_length;
$text = substr_replace($text, $replace, $strpos, $search_length);
echo $text;
}
}else{
echo 'Please fill all fields!';
}
}
?>
<hr />
<form action="searchandreplace.php" method="POST">
<textarea name="text" rows="15" cols="60"></textarea><br /><br />
Search : </br>
<input type="text" name="search"><br /><br />
Replace : </br>
<input type="text" name="replace"><br /><br />
<input type="submit" value="Submit">
</form>
<?php
$string = 'Hi, this is the php testing!';
$string1 = 'Hi, this is string testing!';
similar_text($string, $string1, $result);
echo $result;
?>
From the following example, you can learn the usage of form, textarea, isset, strtolower and str_replace. Every word 'love' you type in the text box, the codes will change with the word 'hate'.
<?php
$find = 'love';
$replace = 'hate';
if(isset($_POST['user_input']) && !empty($_POST['user_input'])){
$user_input = strtolower($_POST['user_input']);
$user_input_new = str_replace($find, $replace, $user_input);
echo $user_input_new.'</br>';
}else{
echo 'Please fill in the text box!';
}
?>
<form action = "wordcensoring.php" method = "POST">
<textarea name = "user_input" cols = "40" widths = "500"></textarea></br></br>
<input type = "submit" value = "Submit">
</form>
 Normally There are three types of array: Indexed Array, Associative Array and Multi-Dimentional Array.
1. Indexed Array (Works with number)
<?php
$snack = array('Banana', 'Ice-cream', 'Coconet', 'Mango', 'Orange');
$snack[1]='Alcohol';
echo $snack[1];
//print_r($snack);
?>
2. Associative Array (Works with name)
<?php
$snack = array('Banana' => 100, 'Apple' => 200, 'Cake' => 300);
echo 'The cake is '.$snack['Cake']. ' Kyat.';
?>
3. Multi-Dimentional Array
<?php
$food = array('Healthy'=>array('Salad'=>200, 'Milk'=>100, 'Coconet'=>200),
'Unhealthy'=>array('Alcohol'=>200, 'Beer'=>300));
foreach($food as $element => $inner_array){
echo '<br /><strong>'.$element.'</strong><br />';
foreach($inner_array as $inner_element => $item){
echo $inner_element.' = '.$item.'<br />';
}
}
?>
Â
<?php
$string = 'I am Thet Naing Win and who are you?';
$find = '/000/';
function Match($find, $string){
if(preg_match($find, $string)){
return true;
}else{
return false;
}
}
if(Match($find, $string)){
echo 'Match Found!';
}else{
echo 'Match Not Found!';
}
?>
<?php
$string = 'Hi How are you?. I am Thet Naing Win. Nice to meet you!';
$find = '/000/';
if (preg_match($find, $string)){
echo 'Match Found!';
}else{
echo 'Match Not Found!';
}
?>
<?php
if(isset($_POST['roll'])){
$rand = rand(000, 999);
echo 'You rolled a '.$rand.'.';
}
?>
<form action = "rand.php" method = "POST">
<input type = "submit" name = "roll" value = "Roll a Dice">
</form>
<?php
if(isset($_POST["username"]) && isset($_POST["password"])){
$username = $_POST["username"];
$password = $_POST["password"];
if(!empty($username) && !empty($password)){
echo 'Welcome to TNW, '.$username.'!';
}else{
echo 'Fill in all fields!';
}
}
?>
<form action = "test.php" method = "POST">
Username:<br />
<input type = "text" name = "username"><br /><br />
Password:<br />
<input type = "password" name = "password"><br /><br />
<input type = "submit" value = "Log In">
</form>
<?php
function Add($num1, $num2){
$result = $num1 + $num2;
return $result;
}
function Divide($num1, $num2){
$result = $num1/$num2;
return $result;
}
$info = Add(Add(5,5), Divide(4,2));
echo $info;
?>
<?php
function MyName(){
echo 'Thet Naing Win.';
}
echo 'My Name is ';
MyName();
?>
<?php
$count = 1;
$message = 'Hello!';
do {
echo $count.'. '.$message.'</br>';
$count ++;
}
while($count<=10)
?>
<?php
$text = 'I am Thet Naing Win.';
$count = 1;
while($count <= 10){
echo $count. ' '.$text.'!</br>';
$count ++;
}
?>
<?php
$text = 'I am Thet Naing Win.';
for($count = 1; $count <= 10; $count++){
echo $count. ' '.$text. '!</br>';
}
?>
<?php
$food = array('Healthy'=>array('Salad', 'Milk', 'Coconet', 'Ground_nut'),
'Unhealthy'=>array('Alcohol', 'Beer', 'Ice_cream'));
foreach($food as $element => $inner_element){
echo '</br><strong>'.$element.'</strong></br>';
foreach($inner_element as $item){
echo $item.'</br>';
}
}
?>
$date = 'Sunday';
switch($date){
case 'Saturday';
case 'Sunday';
echo 'It is weekend!';
break;
default;
echo 'It is not weekend day!';
break;
}
?>
<?php
$num1 = 30;
$num2 = 40;
if($num1 > $num2){
echo 'Num1 is greater than Num2!';
} else if($num1 == $num2){
echo 'Num1 and Num2 are equal!';
}else{
echo 'Num2 is greater than Num1!';
}
?>
<?php
$string = 'I am thet naing win';
$find = array('thet', 'naing', 'win');
$replace = array('THET', 'NAING', 'WIN');
$string_new = str_replace($find, $replace, $string);
echo $string_new;
?>
<?php
$string = 'I am Thet Naing Win. This is PHP Testing and Word position Test. If you are insterested in PHP, please join us!';
$find = 'T';
$find_length = strlen($find);
$offset = 1;
while ($strpos = strpos($string, $find, $offset)){
echo 'The Character <strong>'.$find. '</strong> is found at '.$strpos.'.</br>';
$offset = $strpos + $find_length;
}
?>
We can change all characters to lower case by using strtolower. The following code is an example.
<?php
$string = 'I am ThEt NainG WiN!';
$string_low = strtolower($string);
echo $string_low;
?>
We can also change all characters to upper case by using strtoupper. The following is an example.
<?php
$string = 'I am ThEt NAInG win!';
$string_up = strtoupper($string);
echo $string_up;
?>
<?php
$string = 'I am Thet Naing Win.';
$string_trimmed = rtrim($string);
echo $string_trimmed;
?>
<?php
$string = 'I am Thet Naing Win';
$string_reversed = strrev($string);
echo $string_reversed;
?>
<?php
$string = 'I am Thet Naing Win and who are you?';
$string_word_count = str_word_count($string, 1);
print_r($string_word_count);
?>
<?php
$string = 'I am Thet Naing Win';
$strlen = strlen($string);
echo $strlen;
?>
Write the following code and save it as info.php in C > xampp > htdoc.
<?php
phpinfo();
?>
Open Web Browser and type localhost/info.php in it and press Enter. Then, you will see the php version as the below picture.
We need Apache Service to run php codes. Now I hope you've already installed the Apache Service in your Windows PC. If you have not installed, please click here.
We need Text Editor to write php codes. If you don't have any text Editor, please click here.
If you've completed the above steps. We will start to write php codes.
1. Open your Text Editor (here I use ConText) and create New File.
2. Choose PHP instead of Text files as the below picture.
3. Save that file as test.php in the C > xampp > htdoc.
4. In test.php, write the following codes.
We need Apache service to run php codes. Xampp is the open source software we can use as Apache Server in Windows. Please click here to download.
Before installing Xampp in Windows PC, you better decrease UAC Level and disable Firewall or Antivirus.You can set back default setting after installation.
How to decrease UAC Level
1. Type uac in the search box and press Enter.User Account Control Setting Box will be appeared.
2. Choose Lowest Level and Click Ok.
How to disable Firewall
1. Press Windows and R Keys together at the same time.Run Box will be appeared.
2. Type firewall.cpl in the Run Box and click OK.
2. Click Windows Firewall on or off.
3. Choose Turn off Windows Firewall and click OK.
Then, start to run the exe file you've downloaded and follow the procedures.It will take some time to finish installation.
Now, I hope you've successful installed. Open the Xampp Control Panel. Start Apache and MySQL. You will see as the following picture.
To test Apache Service in Windows,
Open Web Browser and type localhost in it and press Enter. You will as the following picture.
1. To check php version, click PHPInfo.
2. To MySQL Service, click phpMyAdmin.
We need Apache Service to run php codes. Now, I hope you've already installed Apache Service in your Ubuntu PC. If you have not installed it, please click here. Then, follow the below steps.
1. Click Open Folder Location.
2. Double click htdoc.
3. Right Click in htdoc and Click Open In Terminal.
4. Type nano in the Terminal and press Enter. And then, type the following php codes.
Installing php 7.0
sudo apt-get install php7.0
To search php extensions
apt-cache search php7.*
Then, you can install php extensions upon the services you need.