Web Development and Design | Tutorial for Java, PHP, HTML, Javascript: PHP

Web Development and Design | Tutorial for Java, PHP, HTML, Javascript: PHP
Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

PHP session Tutorial


PHP SESSION Tutorial




In this tutorial i exaplain how to use session in php to get value in other page.

session is a way to get value in multiple pages without passing value from one page to another.

Let Start

Filename:- form.php
<html>
<head></head>
<body>
<form action=”show.php” method=”post”>
<label>User</label>
<br>
<input type=”text”name=”user”>
<br>
<input type=”submit” value=”submit”>
<!--here it was just an html form to transfer value to one page to other-->
</form>
</html>


Filename :-show.php
<?php
session_start();

//To use session we must start our session.session_start() is used to start our session.

$user=$_REQUEST[‘user’];
$_SESSION[‘user’]=$user;

//Set value in $_SESSION variable with key user now we get value from session in any page by session.

echo “Hello ”.$user;
echo “<br>”;
echo “<a href=’session.php’>Go To Next Page</a>


?>

Filename:- session.php
<?php
session_start();

//Again started session to get the value from session variable.

echo “Again Hello ”.$_SESSION[‘user’];

//here we accessing the session variable value which we set in show.php

echo “<a href=’destroy.php’>Do You Want To Destroy Session</a>”;

//session destroying link
?>

Filename :-destroy.php

<?php
session_start();
session_destroy();
//here we destroy session by session destroy.
?>


Thank you in next Tutorial i will share how to create a login system.
8

PHP Mysql Tutorial



PHP MySql Tutorial





In this tutorial i will share how to use mysql and php.

Lets get start.

For Cpanel
Open Mysql Database And Create A New Database And Create a New Username and Password then set the username to Database And Give all privilages.
Now Open Your Phpmyadmin from cpanel then select database and create a new table which we use to store data.

In this tutorial i will create a table name login
And Columns are
id - int - 255 - Auto increment - primary key
name - text
password - text
email - text

For xampp:

Open http://localhost/phpmyadmin
And Create a new database and create the table same as which we created in for server.

Now Lets start How to insert,select,update,delete data from database.

Create Database Connection in PHP

Filename: connect.php

<?php
$server=”localhost”;
$username=”root”;
$password=”root”;
$database=”demo”;
$con=mysql_connect($server,$username,$password);

//this function is user to connect our server mysql it takes three parameters first is your server ,second is username and third parameter is password.

$con2=mysql_select_db($database,$con);

//this function is used to select our database. it takes two parameter first parameter is database name and second is the connection which we create to connect our server.
?>



Insert Operation

<?php
include ‘connect.php’;
//inclufe function is used to to include one file into other in php

$name=”sswebtricks”;
$email=”sswebtricks@gmail.com”;
$password=”pass@123”;

$qr=mysql_query(“INSERT INTO login (name,email,password) VALUES (‘$name’,’$email’,’$password’));

//mysql_query is function to write our query it takes one parameter which is our query it returns boolean type value true or false

If($qr)
{
echo “insert sucess”;
}
else
{
echo “failed”;
}
?>


Select Operation

<?php
include ‘connect.php’;
$name=”sswebtricks”;

$qr=mysql_query(“select * from login”);
$row=mysql_fetch_array($qr);

//mysql fetch array function is used to fetch fetch all the data from.database it takes one parameter which is our query and return array value .Here we only fetch single row .

echo $row[‘email’].”<br>”;
//print the email of name

//Now we fetch multiple rows
while($row2=mysql_fetch_array($qr))
{
  echo $row2[‘email’].”<br>”;
}

?>


Update Operation

<?php
include ‘connect.php’;
$name=”sswebtricks”;
$newemail=”sksanjeev362@gmail.com”;

$qr=mysql_query(“UPDATE login SET email='$newemail' WHERE name=’$name’");

// Above is updAte query

if(mysql_affected_rows>0)
{
echo “updated”;
}
else
{
echo “failed”;
}


//mysql affected rows check how many rows will affected with our query
?>

Delete Operation

<?php
include ‘connect.php’;
$name=”sswebtricks”;

$qr=mysql_query(“delete from login where name=’$name’”);

if($qr)
{
echo “ deleted”;
}
else
{
echo “failed”;
}
?>


8

PHP , Javascript (JS) Array Basics Create And Print

 
PHP , Javascript (JS) Array Basics Create And Print 


What is Array?

Array is a special type of variable which stores multiple value in a single variable by its different index number.

Graphical Array value Storage View.


How To Declare or Create in PHP and Javascript?

In PHP – $a=array(1,2,3,4,5);
PHP Create Array by Keyword array and value inside its parenthesis seperated by comma (,).
Here We can Access value of a by using its its like if we use $a[0] we get
value equal to 1. Since Zero index value is 1.

In Javascript – var a=[1,2,3,4,5];
Javascript Creates Array By using Square Bracket and value separated by comma (,)
Here We can Access value of a by using its its like if we use a[0] we get
value equal to 1. Since Zero index value is 1.

Example 1. How to Decalare and get value of array .

IN PHP

<?php
$a = array(1,2,3,4); //Creating array

//printing values of array using simple method 1
echo "Index 0 Value is" . $a[0]."</br>";
echo "Index 1 Value is" . $a[1]."</br>";
echo "Index 2 Value is" . $a[2];

//printing values of array using for loop method 2

$count=count($a); //Here Count Function Counts The Array Of Length

for($i=0;$i<$count;$i++)
{
echo "Index ".$i." Value is" . $a[$i]."</br>";
}

//printing values of array using foreach loop method 2

foreach($a as $key => $value)
{
echo "Index ".$key." Value is" . $value."</br>";
}
?>

IN Javascript

<html>
<body>
</body>
<script>
var a = [1,2,3,4]; //Creating array

//printing values of array using simple method 1
console.log( "Index 0 Value is" +a[0]);
console.log( "Index 1 Value is" +a[1]);
console.log( "Index 2 Value is" +a[2]);

//printing values of array using for loop method 2

var count=a.length; //Here .length Function Counts The Array Of Length

for(var i=0;i<count;i++)
{
console.log( "Index "+i+" Value is" +a[i]);
}

//printing values of array using foreach loop method 2

for(var key in a)
{
console.log("Index "+key+" Value is" +a[key]);
}
</script>
</html>

Thank You
9

PHP $_REQUEST, $_GET and $_POST Variables


PHP $_REQUEST, $_GET and $_POST Variables
php get post request variables

This Are the PHP GLOBAL Variable in PHP Which helps to get value from one page to other or in Html Form Submitting or Passing Value with URL Then We Use This Variables to Get the values.

$_GET

$_GET - > This variable is used when we submitting our form by using method is equal to get or opening any page with passing value in url like ( hello.php?id=1 ). Then we get the Value by using $_GET[‘id’] . When We used get method in form Submitting then we see our key and Value pair in url.

What is key and value?

Example : hello.php?id=1

Here id is key and 1 is value.

$_POST

$_POST - > This variable is used when we submitting our form by using method is equal to post . When We used post method in form Submitting then we cannot see our key and Value pair in url.

Example : -

code 1. hello.html

<html>
<head>
<title>Post Example in PHP </title>
</head>
<body>
<form action=”post.phpmethod=”post”>
Name : <input type=”textname=”name”>
<input type=”submitvalue=”submit”>
</form>
</body>
</html>
Code 2. post.php
<?php
echo $_POST[‘name’];
?>

$_REQUEST

$_REQUEST - > This variable is works both with get and post when we submitting our form by using method is equal to get or post Or opening any page with passing value in url like ( hello.php?id=1 ). Then we get the Value by using $_REQUEST[‘id’] . REQUEST is Really Great Variable Works with Both Get and Post We dont care about method When we using REQUEST.

Example : -

code 1. hello2.html

<html>
<head>
<title>Request Example in PHP </title>
</head>
<body>
<form action=”request.phpmethod=”post”>
Name : <input type=”textname=”name”>
<input type=”submitvalue=”submit”>
</form>
</body>
</html>
Code 2. request.php
<?php
echo $_REQUEST[‘name’];
?>

Thank You
0

PHP Functions

 
PHP Functions

php function


Learn How To Create Functions in PHP.
Function are of Four Types :-

a. No Argument With No Return Type
b.No Argument With Return Type
c.Argument With No Return type
d.Argument With return Type

Now Lets Start.

Example of function :-

function hello() // create function hello
{
//body and scope of function
}


1. No Argument With No Return Type

Code : -

<?php

function hello() // Create a function hello with no argument
{
echo “Calling Function hello”;
}

hello(); // Calling Function hello
// Output : Calling Function Hello
?>

2. No Argument With Return Type

Code : -

<?php

function hello() // Create a function hello with no argument
{
$str=“Calling Function hello”;
return $str; // Return Value
}

echo hello(); // Calling Function hello Here We Use Echo To Print
//Because it Only Return Value Not Print it
// Output : Calling Function Hello
?>

3. Argument With No Return Type

Code : -

<?php


function hello($a) // Create a function hello with argument
{
echo $a; // Print Value
}

$ab=”hello test”;

hello($ab); // Calling Function hello With Parameter $ab
// Output : hello test
?>

4. Argument With Return Type

Code : -

<?php


function hello($a) // Create a function hello with argument
{
$a=ucfirst($a);
return $a; // Return Value
}

$ab=”hello test”;

$out=hello($ab); // Calling Function hello With Parameter $ab
echo $out; // Output : Hello test Here first letter of word is capitalize
?>
Thank You
3

PHP and Javascript (JS) if,if else,if elseif...else


PHP and Javascript (JS) if,if else,if elseif...else


What is If Statement in Programing ?
In programing we need to check the condition if condition is true then we perform task 1 else we perform task 2
then we can check this conditions by if statement


Simple Voting Example
If Our Age is Greater then 18 we are eligible for vote else we are not eligible.


if (we are greater than 18)
{
eligible
}
else
{
we are not eligible
}


Condition Statements Are Of Four Types


1. if statement - Execute Condition if true
2. if...else statement - Execute Task 1 if condition true otherwise Perform Task 2
3. if...elseif....else statement - Executes Task More Than One Condition if Condition Satisfy Perform The Task Else Perform Default else Task
4. switch statement - Perform Task If Condition Satify in any Case and Perform The Result.


if syntax


if (condition) {
if condition true
}


Example 1
if Condition in php


<?php
$a=1;
if($a%2==0) //checking the condition
{
echo "even no"; //print even no if condition is true but here we will get nothing because 1 is an odd number
}
?>


if Condition in Javascript (Js)


<html>
<head>
</head>
<body>
<h1>If Statement in Javascript</h1>
</body>
<script>
var a=1;
if(a%2==0) //checking the condition
{
console.log("even no"); //we get the output nothing in our console by clicking on F12
}
</script>
</html>
Flow Chart Of if Statement


if else syntax


if (condition) {
if condition true
}
else
{
else condition true
}


Example 1
if else Condition in php


<?php
$a=1;
if($a%2==0) //checking the condition
{
echo "even no";  //print even no if condition is true but here we will get nothing because 1 is an odd number
}
else{
echo "odd no";  //print odd no. we know that 1 is a odd number
}
?>


if else Condition in Javascript (Js)


<html>
<head>
</head>
<body>
<h1>If else Statement in Javascript</h1>
</body>
<script>
var a=1;
if(a%2==0) //checking the condition
{
console.log("even no"); //we get the output nothing in our console by clicking on F12
}
else
{
console.log('odd no'); //we get output odd no
}
</script>
</html>
Flow Chart Of if else Statement


The if...elseif....else


if (condition) {
if condition true
}
elseif
{
else condition second
}
else
{
else third condition
}


Example 1
if elseif.. if Condition in php


<?php
$a=1;
if($a<0) //checking the condition
{
echo "a is smaller then zero"; 
}
else if($a>1) //second condition
{
echo "a is smaller then 1";
}
else //else last condition
{
echo “a is equal to 1”;
}
?>


if else Condition in Javascript (Js)


<html>
<head>
</head>
<body>
<h1>If elseif... else Statement in Javascript</h1>
</body>
<script>
var a=1;
if(a<0) //checking the condition
{
console.log( "a is smaller then zero"); 
}
else if(a>1) //second condition
{
console.log("a is smaller then 1");
}
else //else last condition
{
console.log( “a is equal to 1”);
}
</script>
</html>
Flow Chart Of if elseif....else Statement

0

For, While, do While Loops in PHP and Javascript (JS)


For, While, do While Loops in PHP and Javascript (JS)

Since Loop is always same in general Programing Languages Like C,C++,Js,PHP
Here We Get The Use Of For Loop For Beginners in Programing Languages.

What is Loop?
Sometime we need to use the task again and again and any calculation we use loops.

Loops are of 3 types for,While,Do While .

1. For Loop

code :

In PHP

<?php

for($i=0;$i<50;$i++)
{
echo $i.”<br>”; //print numbers 1 to 49
}

?>

In Javascript (Js)

<html>
<head>
</head>
<body>
<h1>For Loop In Javascript Please Open Your Console To See The Ouput By Pressing F12 Key</h1>
</body>
<script>
for(var i=0;i<50;i++)
{
console.log(i); //print numbers 1 to 49 in your console
}
</script>
</html>

Here 1st parameter in for loop is intialization (initial value of i for starting of loop)
2nd is for condition checking ( check current value of i is i<50 .if condition satisfy then body of loop run).
3rd is for iteration (Increasing value i=i+1).

Flow Chart Of For Loop



2.While Loop

Code :

In PHP

<?php

$i=0; //initialization

while($i!=50) //Condition Checking
{
echo $i.”<br>”; //print numbers 1 to 49
$i++; //iteration
}

?>

In Javascript (Js)

<html>
<head>
</head>
<body>
<h1>While Loop In Javascript Please Open Your Console To See The Ouput By Pressing F12 Key</h1>
</body>
<script>
var i=0;
while(i!=50)
{ //bodyof loop
console.log(i); //print numbers 1 to 49 in your console
i++; //iteration
}
</script>
</html>

Here First We Check Condition If the condition is satisfied then the body of loop will executed until the condition not getting true.

Flow Chart Of While Loop


2.Do While Loop

Code :

In PHP

<?php

$i=0; //initialization
do //Body of loop execute
{
echo $i.”<br>”; //print numbers 1 to 49
$i++; //iteration
}
while($i!=50); //Condition Checking


?>

In Javascript (Js)

<html>
<head>
</head>
<body>
<h1>Do While Loop In Javascript Please Open Your Console To See The Ouput By Pressing F12 Key</h1>
</body>
<script>
var i=0;
do //body of loop
{
console.log(i); //print numbers 1 to 49 in your console
i++; //iteration
}
while(i!=50);

</script>
</html>

Here First Time loop will be executed then check If the condition is satisfied then the body of loop will execcuted again.

Flow Chat Of Do While Loop



Difference Between For, While and Do while loop

While loop and For Loop Excutes body of loop only if the condition get satisfied .Since Do While loop execute body of loop once then check the condition.

Thank you.
0