Monday, 21 August 2017

PHP

Global Array:-

<h1> Predefine PHP Array </h1>

<?php
session_start();
# all these are global array
echo "<pre>";
print_r($_SESSION);
echo "<pre>";
 
    echo "<br>";
    echo "********COOKIE INFO********";
echo "<pre>";
print_r($_COOKIE);
echo "<pre>";

    echo "<br>";
    echo "********SERVER INFO********";

echo "<pre>";
print_r($_SERVER); // ask interview questions
echo "<pre>";

?>

************************************************************************
How to Insert the Data:-

<?php
$name = $_POST['fname'];
$mobile = $_POST['mobile'];
$msg = $_POST ['message'];
$pin = $_POST ['pincode'];
    $item = $_POST['item'];
    $myitem = implode(",",$item);

$conn = mysqli_connect("localhost", "root", "", "myphp");

# 1. server name, 2. username, 3. password, 4. database name

$mysql = "insert into mycontact(name,mobile,message,pincode,item)
values('$name','$mobile','$msg','$pin','$myitem')";

$status = mysqli_query($conn, $mysql);

if($status == true){
//echo "success";
header("Location:contactlist.php"); # this function redirect the saved data

}else{
//echo "fail";
header("Location:contact.php");
}

/**$at = $_POST;
echo "<br>";
print_r($at);
echo "</br>";**/
?>

***********************************************************************
How to Fetch Array Object:-

<?php
     
$conn = mysqli_connect("localhost", "root", "", "myphp");
$mysql = "select * from mycontact";
$res = mysqli_query($conn, $mysql); # holding the reference of data, object

while($row = mysqli_fetch_object($res)){

/**echo "<pre>";
print_r($row);
echo"</pre>";**/

echo "<tr>";

        echo "<td> $row->name </td>";
        echo "<td> $row->mobile </td>";
        echo "<td> $row->message </td>";
        echo "<td> $row->pincode </td>";
        echo "<td>";
            $item = explode(",",$row->item);
            for($i=0; $i<count($item); $i++){
                echo $item[$i]. "<br>";
            }
        echo "</td>";
echo "</tr>";

}
# this one is BEST
/**echo "<pre>";
print_r($row);
echo"</pre>";
**/



?>

************************************************************************
How to Delete the Data:-

echo "<td>
        <a href='delete.php?id=$row->id'> Delete </a>

        </td>";

<?php
$userid = $_REQUEST['id'];

include("dbconfig.php");

$sql = "delete from user where id='$userid'";

mysqli_query($conn, $sql);

header("Location:contactlist.php");


?>

**************************************************************************
How to Edit the Data:-

<?php
include("header.php");

include("dbconfig.php");

$userid = $_REQUEST['id'];

$sql = "select * from user where id = '$userid'";
$res = mysqli_query($conn, $sql);
$row = mysqli_fetch_object($res);


?>

<form method="POST" action="updateuser.php">
<table class="table table-bordered">
<caption> Edit User </caption>
<tr>
<th> Full Name </th>
<td>
<input type="text" name="fname" value="<?php echo $row->name; ?>">
</td>

</tr>

<tr>
<th> Mobile No. </th>
<td>
<input type="text" name="mobile" value="<?php echo $row->mobile; ?>">
</td>

</tr>

<tr>
<th> Email </th>
<td>
<input type="text" name="email" value="<?php echo $row->email; ?>">
</td>

</tr>

<tr>
<th> Password </th>
<td>
<input type="text" name="pass" value="<?php echo $row->password; ?>">
</td>

</tr>

<input type="hidden" name="userid" value="<?php echo $row->id; ?>">

<tr>
<th colspan="2" align="center">
<button class="btn btn-primary">Update </button>

</th>


</tr>

</table>

</form>





<?php
include("footer.php");

?>
====================================================================
Update:-
<?php

include ("dbconfig.php");
$userid = $_POST['userid'];

$name = $_POST['fname'];
$mobile = $_POST['mobile'];
$email = $_POST['email'];
$password = $_POST['pass'];


$sql = "update user set name ='$name',
mobile= '$mobile',
email = '$email',
password = '$password'
where id = '$userid' ";

$res = mysqli_query($conn, $sql);

if ($res=true){
header("Location:contactlist.php");
}else{

echo "Update fails";
}


?>
***************************************************************************
SESSION:
<?php
session_start();
if(!isset($_SESSION['id'])){
$_SESSION['msg'] = "Invalid User! ";
header("Location:index.php");
}

echo $_SESSION['name'];


?>

**************************************************************************

COOKIE:-

<?php
session_start();
if(!isset($_COOKIE['id'])){
$_SESSION['msg'] = "your session is expired! Please login ";
header("Location:index.php");
}

setcookie("name", $_COOKIE['name'], time()+60); // 60 seconds
setcookie("id", $_COOKIE['id'], time()+40);

echo $_SESSION['name'];



?>

***************************************************************************
How to Upload File:-
<html>
 <body>
<form action="upload.php" method="POST" enctype="multipart/form-data">
<center>
Select Any Image :
<input type="file" name="myfile"/>
<br>
<br>
<button type="submit"> Upload </button>
</center>
</form>
 </body>

 </html>
==================================================================
<?php

$filename = $_FILES['myfile']['name'];

$filesize = $_FILES['myfile']['size'];

$filetype = $_FILES['myfile']['type'];

$filetmpname = $_FILES['myfile']['tmp_name'];

echo $filename . "<br>";
echo $filesize . "<br>";
echo $filetype . "<br>";
echo $filetmpname . "<br>";

move_uploaded_file($filetmpname, "myfile/$filename");


include("dbconfig.php");

$sql = "insert into photo (photoname) values ('$filename')";

mysqli_query($conn,$sql);

header("Location:photolist.php");

?>
========================================================================
<html>
<head>
<link href="assets/css/bootstrap.css" rel="stylesheet">
<style>
.margin10{margin:10px;}
</style>

<head>
<body>
<br>
<div class="container">
<div class="row">
<div class="col-sm-12 text-center">
<a href="mydata.php" class="btn btn-primary "> New Photo </a>
</div>
</div>
<br>

<div class="row">
<?php

include("dbconfig.php");
$sql = "select * from photo order by id desc";
$res = mysqli_query($conn, $sql);

while($row = mysqli_fetch_object($res)){

echo "<div class='col-sm-3'>";
echo "<img src = 'myfile/$row->photoname' class='img-responsive img-rounded margin10'>";

echo "<center> <a href='deletephoto.php?id=$row->id'> Delete </a> </center>";

echo "</div>";
}

?>

</div>

</div>
</body>
</html>
========================================================================

<?php
include("dbconfig.php");
$photoid = $_REQUEST['id']; // id is variable comming through URL

$sql1 = mysqli_query($conn,"select * from photo where id = '$photoid'");
$row = mysqli_fetch_object($sql1);

$photoname = $row->photoname;


unlink("myfile/$photoname"); // to delete a fiel from folder

$sql = "delete from photo where id='$photoid'";

mysqli_query($conn, $sql);

header("Location:photolist.php");



?>


**************************************************************************
Login Body(cookies):

<body> <br>
<div class="container">
<div class="row">

<div class="col-sm-4"> </div>
<div class="col-sm-4">
<?php
session_start();
if(isset($_SESSION['msg'])){
echo $_SESSION['msg'];
unset($_SESSION['msg']);
}
?>
<form method="POST" action="loginchek.php">
<div class="panel panel-primary">
<div class="panel-heading"> Login </div>
<div class="panel-body">
<div class="form-group">
<label> Email </label>
<input type="text" name="email" class="form-control">

</div>

<div class="form-group">
<label> Password </label>
<input type="text" name="password" class="form-control">

</div>

<div class="form-group text-center">
<button class="btn btn-primary "> Login </button>
<hr>
<a href="signup.php"> I am new user </a>

</div>

</div> <!-- panel body end-->
</div> <!-- panel end-->

</form>
</div>
<div class="col-sm-4"> </div>

</div>

</div>


</body>
===================================================================
<?php
session_start();
include("dbconfig.php");
$email = $_POST['email'];
$password = $_POST['password'];

//echo $email;
//echo $password;



$sql = "select * from user where email='$email' and password='$password'";
$res = mysqli_query($conn, $sql);
$totaluser = mysqli_num_rows($res);

//echo $totaluser;

if($totaluser>0){
$row = mysqli_fetch_object($res);

$_SESSION['name']= $row->name; // 1440 seceonds 24 hours
$_SESSION['id']= $row->id;

setcookie("name", $row->name, time()+40); // 40 seconds
setcookie("id", $row->id, time()+40);

header("Location:home.php");
}else{
$_SESSION['msg'] = 'Sorry Invalid user';
header("Location:index.php");
}
?>

***************************************************************************
FILE MANAGEMENT

How create and delete a Folder/Directory:
<h1> Directry management </h1>
<?php

/*
if( file_exists("test"))
{
echo "Already one folder is available with same name";
}else{
mkdir("test");
echo "Folder created Successfully";
}
*/

 //how to delete a folder ?

  if( file_exists("test"))
  {
 rmdir("test");
 echo"Folder deleted successfully";
  }else{

 echo "Sorry, the are no any folder to delete";
  }
?>
=======================================================================
How to delete a variable:

<h1>Directory Management </h1>

<?php 
 $a = 100;
 echo "<hr>";

 unset($a);

 echo $a;


?>
========================================================================
How to create a New File:
<h1> Create a  New File using PHP </h1>

<?php 

$obj = fopen("abc.txt", "w");
fclose($obj);

echo "file created successfully!";


?>
========================================================================
How to write in a File:

<h1> Create a  New File using PHP </h1>

<?php

$obj = fopen("abc.txt", "w");

$data = "Hello My Test";

fwrite($obj, $data);

fclose($obj);

echo "file created successfully!";
echo "<br>";

?>
========================================================================
How to read a File:

<h1> Using 2 methods we can read data from a file </h1>

<?php

$obj = fopen("abc.txt", "r");

$data1 = fread($obj, 5000);

fclose($obj);

echo "<hr>";

echo $data1;

$data2 = file_get_contents("abc.txt");
// read all contents of a file at one time and store as a string

echo "<hr>";
echo "this is 2nd method";
echo "<br>";
echo $data2;
?>
========================================================================
How to delete a File:

h1> PHP unlink() used to delete a file </h1>

<?php
if(file_exists("abc.txt")){

unlink("abc.txt");
echo "File deleted successfully";
}else{

echo "There is no any file exists under current folder";
}
?>
========================================================================
DataBase to XML:

<?php
$conn = mysqli_connect("localhost", "root", "", "myproject");
$sql = "select * from user ";

$res = mysqli_query($conn, $sql);

$userdata = "";

while($row=mysqli_fetch_object($res)){

$userdata = $userdata . "<user>";

$userdata = $userdata . "<name> $row->name </name>";
$userdata = $userdata . "<mobile> $row->mobile </mobile>";
$userdata = $userdata . "<email> $row->email </email>";
$userdata = $userdata . "<password> $row->password </password>";

$userdata = $userdata . "</user>";
}
 $xml = "<?xml version='1.0' ?>";
 $xml = $xml . "<userlist>" . $userdata. "</userlist>";

 $filename = time() . ".xml";
 $obj = fopen("$filename", "w");

 fwrite($obj ,$xml);

 fclose($obj);

 echo "New XML file created!";
 echo "<a href='$filename'> Click here to open </a>";


?>
========================================================================
XML to DataBase:

<?php
$conn = mysqli_connect("localhost", "root", "", "myproject");
$xml = simplexml_load_file("1503568755.xml");

for($i=0; $i<count($xml); $i++){

$name = $xml->user[$i]->name;
$mobile = $xml->user[$i]->mobile;
$email = $xml->user[$i]->email;
$password = $xml->user[$i]->password;

$sql = "insert into user(name,mobile,email,password) 
        values('$name','$mobile','$email','$password')";

mysqli_query($conn, $sql);

}

echo "Data Uploaded From XML file to DataBase";


?>
========================================================================
XML to JSON:

<?php
$conn = mysqli_connect("localhost", "root", "", "myproject");
$xml = simplexml_load_file("1503568755.xml");

$json = json_encode($xml);

echo $json;


?>
=======================================================================
DataBase to JSON:

<?php 
$conn = mysqli_connect("localhost", "root", "", "myproject");
$sql = "select * from user ";
$res = mysqli_query($conn, $sql);

$user = array();

while($row=mysqli_fetch_object($res)){

array_push($user, $row);
}
  echo "<pre>";
  print_r($user);
  echo "</pre>";
  
 echo "<hr color='red'>";

 $json = json_encode($user); 
 echo $json;

?>
*********************************************************************************

No comments:

Post a Comment

Download

https://drive.google.com/open?id=0BwJUPBOmHUUoLXA3TWdFQnRIeXc https://drive.google.com/file/d/1DMlCY1tycw-USBbs9kuxUtqo64fXRwU3/view?usp=...