Showing posts with label Tutorials. Show all posts
Showing posts with label Tutorials. Show all posts

Complete PHP+MySQL Training with Projects

PHP stands for HyperText Pre-Processor, which is a popular server side scripting language and I’m one of the fans of PHP myself. I shared a tutorial already for PHP lovers about uploading files/images in this server side scripting language and I wanted to take some time to make a complete video tutorials series of this awesome web development tool. So I’m here with my complete video course of PHP which is having 23 video tutorials right now, but I hopefully can say that I’ll update this course time to time, and my aim is to make 100 video tutorials by 2014. I’ll be adding all PHP related tutorials to this playlist, which I’m gonna share on this page. However, you can also subscribe to my YouTube channel for all of my tutorials. Now let’s talk about this complete video course.

Why PHP video training is important?

As you know there are hundreds of websites on the internet who provide PHP training through text articles or by online examples, but I thought the video is 100 times better than a text-based tutorial, therefore, I’ve taken the responsibility and as far as I can, I’d like to present something that is worth watching. And PHP is very important to learn now a days, because this is the most used popular web development language and is free of cost open source .

What will we be learning during this course?

I’ve started the course from an introduction which doesn’t have enough theory, because I like practical work in a programming language, therefore, You’ll just come to know about the tools which we’ll be using to run PHP properly on our local server. However, after few basic classes ofEcho command, Local variables, Global Variables, IF & Else statements, Header command, include command, require command and other simple commands, I’ve straight taken you to creating different applications in PHP.

How many applications we’ll be creating in this course?

We’ll be creating applications as many as possible, the first application I’ve taught you in this course is; an online calculator which we’ve simply created by using IF statement in PHP, and after calculator I’ve created an online currency calculator in this course.
And the most important projects of this course are; creating a complete registration form in PHP with admin panel for viewing the users’ data, for deleting the users and for editing the users.
In the future I’ll add more projects to this playlist such as creating a CMS (Content Management System) in PHP along with a website which will be having a separate admin panel, A social network site using PHP with all of great features and also I’ll be creating an eCommerce online shopping center using PHP.
There will be more small projects added to this PHP course in the future.

How to Upload Files to Web Server in PHP?

PHP is a server side scripting language and mostly used for creating dynamic web pages. The live examples of PHP are: Facebook.com, WordPress.com, Joomla.com and many other websites. PHP is an open source free of cost web language which is used by millions of web developers now a days. It simplify your online web projects and applications and adds beauty to them. I’m myself a lover of PHP and studying it for couple of months. I am also interested to teach PHP on this blog by making video tutorials and text-based tutorials for all those who want to learn practical usage of PHP. So today, I have a tutorial for you in which we’ll be discussing how to upload files to web server via PHP.
If you are interested in learning PHP then you might know about a complete offline package for Using PHP called “XAMPP” and also “WAMP”. So before starting learning PHP you must have installed either “XAMPP” or “WAMP” which provide you a complete environment for making web applications in PHP.
Whether you install xampp or wamp, after installing the software you’ll find its folder directly in your C: drive. There will be a folder called “htdocs” (in Xampp) & “www” (in wamp) in your installed folder, so this is your root folder and you’ll have to save all your files there in that folder, and after creating a file or new folder there. You’ll just open it on your browser with this address:http://localhost/test/file.php, so in this example localhost is my local server and the test is a folder which I created inside “htdocs” folder and the file.php is my file inside test folder. Also after installing the xampp or wamp package check the installation by entering this address in your browser: http://localhost, so if it is installed correctly then it should work just fine.
These two software have all the necessary tools which are required in order to run PHP on your computer, because PHP is not a normal web language like HTML, CSS & JavaScript. It is an advance server side language which needs to be execute by a server. Now lets start the tutorial about adding files via PHP.

How to add images in PHP?

There are many type of files you can upload to your server via PHP such as images, videos, text files and so on. But we’ll upload some images in today’s tutorial and you can also add other files by using the same method.
In order to upload files to your web server or local server, you’ll need to create a new file by using Notepad or Notepad++ or Dreamweaver.
For uploading files we’ll need to create an HTML page to show the uploading options to the users, and we can create a script in PHP for uploading the files into our server. However, you can use a single HTML page in which you’ll insert all the HTML and PHP codes together. Here is the simple HTML code which we’ll need to receive the data from the user, and for this purpose we use HTML forms:
<center>
<form action=”file.php” method=”post” enctype=”multipart/form-data”> 
<table border=”2″>
<tr height=”100″><td>Upload A file:</td>
<td><input type=”file” name=”file”></td></tr>
<tr height=”100″><td>Click Upload File:</td><td><input type=”submit” name=”submit” value=”Upload File”></td></tr>
</table>
</form></center>
In above coding you can see we added a form in HTML and its attributes are: actionmethod andenctype, these three attributes are very important in order to receive data like images & videos etc. The file.php will be the file to which the user will be sent after clicking the upload filebutton. Method is post for receiving the data and it is indeed the best method in PHP. Also the enctype is very very important in this case, because we must need it for receiving data like images and videos etc. Images and videos have multiple parts such as their names, types, sizes and so on. So therefore, we add enctype in this. The rest coding is just for making the form readable.
Now here is the PHP simple script for above form: 
if (isset($_POST[‘submit’])) {
$name=$_FILES[‘file’][‘name’];
$type=$_FILES[‘file’][‘type’];
$size=$_FILES[‘file’][‘size’];
$tmp=$_FILES[‘file’][‘tmp_name’];
echo “$name <br> $type <br> $size”;
}
In above PHP script we used the $_POST array with an if statement, because we earlier mentioned in form that we’ll get the data via Post method, and we created 4 different variables for all the parts of the file such as name, type, size and tmp; and we collected the data via another default PHP array $_FILES which is used for getting files’ data. And in last we printed the data and when the user will click the upload file button he will see the name of the image, type of the image, size of the image on the same page. We did this because we wanted to check our script whether it’s working or not.

How to validate the files before uploading?

Now the interesting part is here. We can’t allow people to upload the files as they want. We’ll validate the variables for allowing people to upload only the specific kind of files. We’ll validate the type of the files, the size of the files and after that we’ll save the files somewhere on our local computer or in web server. So here is the complete PHP script for validating the files uploading them:
<?php
if (isset($_POST[‘submit’])) {
$name=$_FILES[‘file’][‘name’];
$type=$_FILES[‘file’][‘type’];
$size=$_FILES[‘file’][‘size’];
$tmp=$_FILES[‘file’][‘tmp_name’];
if ($name==”){
echo “<script>alert(‘Please Select a file from computer!’)</script>”;
exit();
}
if (($type == “image/jpeg”) || ($type == “image/gif”) || ($type == “image/png”)) {
if (file_exists(“images/” . $_FILES[“file”][“name”]))
{
echo “This file $name is already exist!<br> please try another one..”;
exit();
}
if ($size <= 50000){
move_uploaded_file($tmp,”images/$name”);
echo “<center><font color=red>File was uploaded successfully!</font><br>Uploaded Image is here<br><img src=’images/$name’> </center>”;
}
else {
echo “Size of the image $size is larger than 50kb and it’s not allowed… <br> image size must be less than 50kb..”;
}
}
else {
echo “$type this is not a valid type of file<br>only upload Jpeg, PNG and Gif images”;
}
}
?>
In above example I’ve given the complete PHP script altogether. In this script we added all the necessary validations and also we uploaded the file to our server via the move_uploaded_filedefault PHP command.
In above example, we said in an if statement that only Jpeg, PNG & gif type of images will be allowed to upload, also we said that size must be less than 50kb, and we also used a query which checks for the existing file names, this query will stop the script if it finds a file which is already existing. And finally we added the file to our computer by using move_uploaded_file function.
We also printed the image on the same page. So when you’ll upload an image and click the upload file button the image will be shown on the same page. And we moved the uploaded file to a folder called “images” which you should create inside the folder where you’ve kept the file.phpfile. However, if you don’t want to save the images inside a folder then change: $tmp,”images/$name” to only $tmp, $name and also change <img src=’images/$name’> to <img src=’$name’> so it will be uploaded directly to the same folder where the file.php is existing.
Now for your clarification the final code including both HTML and PHP which you can directly copy and paste inside a blank notepad document, you’ll have to just save it as file.php and will have to save it inside “htdocs” folder in the xampp or wamp installation folder, here is the complete code:
<html>
<head>
<title>Uploading Files via PHP</title>
</head>
<body>
<center><form action=”file.php” method=”post” enctype=”multipart/form-data”>
<table border=”2″>
<tr height=”100″><td>Upload A file:</td>
<td><input type=”file” name=”file”></td></tr>
<tr height=”100″><td>Click Upload File:</td><td><input type=”submit” name=”submit” value=”Upload File”></td></tr>
</table>
</form></center>
<?php
if (isset($_POST[‘submit’])) {
$name=$_FILES[‘file’][‘name’];
$type=$_FILES[‘file’][‘type’];
$size=$_FILES[‘file’][‘size’];
$tmp=$_FILES[‘file’][‘tmp_name’];
if ($name==”){
echo “<script>alert(‘Please Select a file from computer!’)</script>”;
exit();
}
if (($type == “image/jpeg”) || ($type == “image/gif”) || ($type == “image/png”)) {
if (file_exists(“images/” . $_FILES[“file”][“name”]))
{
echo “This file $name is already exist!<br> please try another one..”;
exit();
}
if ($size <= 50000){
move_uploaded_file($tmp,”images/$name”);
echo “<center><font color=red>File was uploaded successfully!</font><br>Uploaded Image is here<br><img src=’images/$name’> </center>”;
}
else {
echo “Size of the image $size is larger than 50kb and it’s not allowed… <br> image size must be less than 50kb..”;
}
}
else {
echo “$type this is not a valid type of file<br>only upload Jpeg, PNG and Gif images”;
}
}
?>
</body>
</html>
Now after saving the file check it on the browser with this address:http://localhost/test/file.php and you’ll find the HTML form which will be showing the file upload field and uploading button, just upload a picture and click the button; you’ll see the result, but making it sure you’ve created  a folder ‘test’ inside ‘htdocs‘ and have saved thefile.php inside test folder.
This tutorial was for uploading files to your local server by using XAMPP or wamp packages, but you can also use this tutorial for uploading files to your online web server.

How to Create a Simple Calculator in PHP

 
previously shared my complete PHP video series with you and now I’m going to share a simple online calculator with you which is created using PHP without MySQL. Because you can create any simple application in PHP by simply using IF statements. So I’ve done the same in this tutorial. However, My aim is to provide you a large number of tutorials in which we’ll be making small applications and projects such as online currency converter, visitors counter, love calculator, Luck Calculator and many more. I’ll be providing you video tutorials and the source code which we’d have used during the video tutorials. For now let’s create a simple calculator application in PHP. There will be 2 video tutorials which will practically teach you doing this and source code is also available for you to practice on.

Creating a simple calculator in PHP

Let’s assume, we want to create a simple calculator and without using MySQL. So yes, it is possible only by using IF statement in PHP. First you need to create a new file for writing the codes, and you can use Notepad or notepad++ for this purpose, though, I’ve used Notepad++ in these video tutorials. Just save the file and name it as calculator.php; save the file in the htdocsfolder of your XAMPP Installation, because that is our root folder and we run files from that folder on our local server (Localhost). Now for creating a calculator you need to first create a form in HTML and add some input tags.
Here is the HTML code for the calculator, and we’ll later on add PHP code to this:
<html>
<head>
<title>Simple PHP Calculator</title>
</head>
<body>
<form method=’post’ action=’calculator.php’>
<input type=’text’ name=’value1′>
<input type=’text’ name=’value2′>
<select name=’action’>
<option>+</option>
<option>-</option>
<option>*</option>
<option>/</option>
</select>
<input type=’submit’ name=’submit’ value=’Calcaulate Now’>
</form>
</body>
</html>
And after creating the HTML form we need to start work in PHP so that this project will be completed, watch below video for practically learn about creating a PHP script for this calculator.

I hope you’ve understood creating it the PHP script easily for this simple calculator, however, the source code is being shared here, just copy it and paste it inside that file in which we already created an HTML form:
<?php
if(isset($_POST[‘submit’])){
$value1 = $_POST[‘value1’];
$value2 = $_POST[‘value2’];
$action = $_POST[‘action’];
if($action==”+”){
echo “<b>Your Answer is:</b><br>”;
echo $value1+$value2;
}
if($action==”-“){
echo “<b>Your Answer is:</b><br>”;
echo $value1-$value2;
}
if($action==”*”){
echo “<b>Your Answer is:</b><br>”;
echo $value1*$value2;
}
if($action==”/”){
echo “<b>Your Answer is:</b><br>”;
echo $value1/$value2;
}
}
?>
Now check your online calculator in your browser, and remember, always open your PHP files using this address: localhost. Sometimes mistakenly you directly click on the file which is inside a folder.

Kategori

Kategori