Wednesday, February 3, 2016

PHP for beginners 2

PHP Sessions example:
Tutorial5_session.php
<?php
session_start();
$_SESSION['Name']='mark';
$_SESSION['Age']=15;
$_SESSION['Weight']=50;
echo "Done";
?>
Tutorial5.php
<?php
session_start();
$name=$_SESSION['Name'];
$age=$_SESSION['Age'];
$weight=$_SESSION['Weight'];
echo $name.'<br>';
echo $age.'<br>';
echo $weight.'<br>';
?>
Unset session:
<?php
session_start();
// session_destroy();
unset($_SESSION['Name']);
?>
PHP Cookie example:
Tutorial6_cookie.php
<?php
// COOKIE EXAMPLE
//          setcookie(name, value, expire, path, domain);
$time = time();
setcookie('student', 'Mark', $time+10);
echo 'Cookie is created '.$time;
?>
Tutorial6.php
<?php
$student =$_COOKIE['student'];
// 10 second after cookie is destroyed
echo $student;
?>
Getting Data from MySQL Database using PHP
Tutorial7.php
<?php
$mysql_host='localhost';
$mysql_user='root';
$mysql_password='root';

if(!@mysql_connect($mysql_host, $mysql_user, $mysql_password)){
             die('Cannot Connect to database');
 } else {
                         if(@mysql_select_db('student_phpexample')){
                        //                      echo 'Connection success';
             } else
                         die('Cannot Connect to database');
 }

?>
Tutorial7_getdata.php
<?php
include 'tutorial7.php';
$query="SELECT * FROM `user_info`";
if($is_query_run=mysql_query($query)){
            // echo "query executed";
            while($query_execute=mysql_fetch_assoc($is_query_run)){
                        echo '<table border ="1" style="width:300px"><tr>';
                        echo '<td>'.$query_execute['Name'].'</td>';
                        echo '<td>'.$query_execute['Surname'].'</td>';
                        echo '<td>'.$query_execute['Age'].'</td>';
                        echo '</tr>';
            }
} else {
            echo "query not executed";
}
?>
PHP Md5 Encryption example:
<?php
$password="pass";
echo md5($password);
if(isset($_POST['password']) && !empty($_POST['password'])){
            $check_password = $_POST['password'];
            if(md5($check_password)==md5($password))
            {
                        echo "Password correct";
            } else {
                        echo "Password is not correct";
            }
}
?>
<form action="tutorial8.php" method="post">
<input type ="text" name="password">
<br>
<input type="submit">

</form>

PHP for beginners 1

example:
<?php
// Echo

$value = "ProgrammingKnowledge";
echo "Hello=$value <br>";
echo 'Hello='.$value.'<br>';
$googlelink = "Google Link";
echo '<a href ="http://www.google.com">'.$googlelink.'</a><br>';
echo 'it\'s a nice day';

// if statement

$num1 = '10';
$num2 = 10;
if ($num1 == $num2)
  echo "Equal";
else echo "Not equal";

// Array

$names = array('Mark'=>65, 'John'=>25, 'Bob'=>29);
echo 'Weight of Mark is '.$names['Mark'];
print_r($names);

// Multidimensional Arrays in PHP

$students = array(array('Name'=>'Mark', 'Age'=>15, 'Weight'=>56),
array('Name'=>'Jonh', 'Age'=>15, 'Weight'=>56),
array('Name'=>'Bob', 'Age'=>15, 'Weight'=>56));
// echo $students[0][0].' is here : '. $students[0][1] .' and '. $students[0][2];
echo $students[1]['Name'].' is here : '. $students[1]['Age'].' and '.$students[1]['Age'];

// While Do...While

$counter = 1;
while($counter<=10){
echo "ProgrammingKnowledge ".$counter++."<br>";
}
for($counter=10; $counter>0; $counter--)
echo "ProgrammingKnowledge ".$counter."<br>";

// foreach

$names = array('Mark', 'John', 'Tom', 'Partick', 'July');
foreach( $names as $name)
{
echo $name.'<br>';
}
$students = array(array('Name'=>'Mark', 'Age'=>15, 'Weight'=>56),
array('Name'=>'Jonh', 'Age'=>15, 'Weight'=>56),
array('Name'=>'Bob', 'Age'=>15, 'Weight'=>56));
foreach($students as $stud => $inner)
{
echo '<b>'.$stud.'</b> ';
foreach($inner as $item)
{
echo $item.' ';
}
echo '<br>';
}

// switch

$grade = 'C';
switch($grade)
{
case 'A': echo "Excellent"; break;
case 'B': echo "Very good"; break;
case 'C': echo "Good"; break;
case 'D': case 'F': echo "Not Bad"; break;
default: echo "No grades found"; break;
}
?>

GET example:
Tutorial2.php
<?php
// Get
if(isset($_GET["stu_name"]) && isset($_GET["stu_age"]) && isset($_GET["stu_weight"]) ){
            $name = $_GET["stu_name"];
            $age = $_GET["stu_age"];
            $weight = $_GET["stu_weight"];  
            if(!empty($name) && !empty($age) && !empty($weight)){
                                    echo 'Name:'.$_GET["stu_name"].'<br>';
                                    echo 'Age:'.$_GET["stu_age"].'<br>';
                                    echo 'Weight:'.$_GET["stu_weight"].'<br>';        
            }
}  else {
            echo "Please enter all field";
}
?>
Tutorial2_student.php
<html>
<body>
<h1>Student info</h1>
<form action = "tutorial2.php" method ="get">
<b>Name:<b><input type="text" name="stu_name">
<b>Age:<b><input type="text" name="stu_age">
<b>Weight:<b><input type="text" name="stu_weight">
<br>
<input type="submit">
</form>
</body>
</html>
POST example:
Tutorial3.php
<?php
// POST
$_POST["username"];
$_POST["password"];
if($_POST["username"]==="mark" &&  $_POST["password"]==="pass"){
            echo "username and password is correct";
} else echo "username and password is not correct";

?>
Tutorial3_login.php
<html>
<body>
<h1>Login Form</h1>
<form action="tutorial3.php" method="post">
<b>Username:<b><input type="text" name="username">
<b>Password:<b><input type="password" name="password">
<br>
<input type="submit">
</form>
</body>
</html>
Functions with Arguments example:
<?php
function hello_world($num1, $num2){
            echo $num1 + $num2.'<br>';
}
function add($num1, $num2){
            $result = $num1 + $num2;
            return $result;
}
//          hello_world(15, 35);
//                      hello_world(55, 156);
$add1 = add(25, 28);
$add2 = add(55, 12);
echo $add1 * $add2;
?>
Date and Time example:
<?php
// Day/Month/Year
$date = date('d-m-y');
echo $date.'<br>';
// hour:minutes:second
$time = date('h-m-s');
echo $time;
?>
Global Variable example:
<?php
global $name = "Mark";
function showMe(){
            global $name;
            echo '<b>The name of the student is: </b>'.$name;
}
showMe();
?>
include() and require() functions example:
tutorial4.php
<?php
include 'tutorial4_header.php';
include 'tutorial4_header.php';
include_once ('tutorial4_header.php');

echo $page1.'<br>';
echo $page2;
?>
Tutorial4_header.php
<html>
<body>
<h1>Programming Knowledge</h1>
</body>
</html>
<?php
            $page1 = "We are page 1";
            $page2 = "We are page 2";
?>

Programming Flappy Bird in Java

Monday, February 1, 2016