Skip to content

Commit 4700db1

Browse files
authored
advanced php uploaded
1 parent bfd962f commit 4700db1

File tree

100 files changed

+5501
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

100 files changed

+5501
-0
lines changed

1. Class and Object.php

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
class Mobile{
3+
public $model; // Properties
4+
function showModel(){
5+
echo "Model Number is: $this->model <br>";
6+
}
7+
}
8+
9+
$samsung = new Mobile;
10+
$samsung->model="A8+";
11+
$samsung->showModel();
12+
$lg = new Mobile;
13+
$lg->model="G5";
14+
$lg->showModel();
15+
16+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
// Child Class inherit Parent's Constructor
3+
class Father{
4+
function __construct(){
5+
echo "<br>Parent Constructor Called<br>";
6+
}
7+
}
8+
class Son extends Father{
9+
function __construct(){
10+
// Father::__construct();
11+
parent::__construct(); // calling Parent Class Constructor inside Child Class constructor
12+
echo "Child Constructor Called";
13+
}
14+
}
15+
$objS = new Son();
16+
?>

100. PDOPrepUpdateWithFormTable.php

+174
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
<?php
2+
// create variable for connection
3+
$dsn = "mysql:host=localhost; dbname=test_db";
4+
$db_user = "root";
5+
$db_password = "";
6+
7+
// Create Connection with exception handling
8+
try {
9+
$conn = new PDO($dsn, $db_user, $db_password);
10+
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
11+
echo "Connected <br><hr>";
12+
}
13+
catch(PDOException $e) {
14+
echo "Connection Failed " . $e->getMessage();
15+
}
16+
17+
if(isset($_REQUEST['update'])){
18+
// checking for empty field
19+
if(($_REQUEST['name'] == "") || ($_REQUEST['roll'] == "") || ($_REQUEST['address'] == "")){
20+
echo"<small>Fill all fields..</small><hr>";
21+
}
22+
else {
23+
// Using Named Placeholder
24+
$sql = "UPDATE student SET name = :name, roll = :roll, address = :address WHERE id= :id";
25+
26+
// Prepared Statement
27+
$result = $conn->prepare($sql);
28+
29+
// Bind Parameter to Prepared Statement
30+
$result->bindParam(':name', $name, PDO::PARAM_STR);
31+
$result->bindParam(':roll', $roll, PDO::PARAM_INT);
32+
$result->bindParam(':address', $address, PDO::PARAM_STR);
33+
$result->bindParam(':id', $id, PDO::PARAM_INT);
34+
35+
// Variables and values
36+
$name = $_REQUEST['name'];
37+
$roll = $_REQUEST['roll'];
38+
$address = $_REQUEST['address'];
39+
$id = $_REQUEST['id'];
40+
41+
// Execute Prepared Statement
42+
$result->execute();
43+
44+
echo $result->rowCount() . " Row Updated <br>";
45+
46+
// Close Prepared Statement
47+
unset($result);
48+
}
49+
}
50+
51+
?>
52+
53+
54+
<!DOCTYPE html>
55+
<html lang="en">
56+
57+
<head>
58+
<meta charset="UTF-8">
59+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
60+
<meta http-equiv="X-UA-Compatible" content="ie=edge">
61+
<!-- Bootstrap CSS -->
62+
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm"
63+
crossorigin="anonymous">
64+
<title>SIPL</title>
65+
</head>
66+
67+
<body>
68+
<div class="container">
69+
<div class="row">
70+
<div class="col-sm-4">
71+
<?php
72+
if(isset($_REQUEST['edit'])){
73+
// SELECT with WHERE
74+
$sql = "SELECT * FROM student WHERE id = :id";
75+
76+
// Prepare Statement
77+
$result = $conn->prepare($sql);
78+
79+
// Bind parameter
80+
$result->bindParam(':id', $id);
81+
82+
$id = $_REQUEST['id'];
83+
84+
// Execute statement
85+
$result->execute();
86+
87+
// Fetch Single row data
88+
$row = $result->fetch(PDO::FETCH_ASSOC);
89+
90+
// Close Prepared Statement
91+
unset($result);
92+
93+
}
94+
?>
95+
<form action="" method="POST">
96+
<div class="form-group">
97+
<label for="name">Name</label>
98+
<input type="text" class="form-control" name="name" id="name" value="<?php if(isset($row['name'])){ echo $row['name'];} ?>">
99+
</div>
100+
<div class="form-group">
101+
<label for="roll">Roll</label>
102+
<input type="text" class="form-control" name="roll" id="roll" value="<?php if(isset($row['roll'])){echo $row['roll'];} ?>">
103+
</div>
104+
<div class="form-group">
105+
<label for="address">Address</label>
106+
<input type="text" class="form-control" name="address" id="address" value="<?php if(isset($row['address'])){ echo $row['address'];} ?>">
107+
</div>
108+
<input type="hidden" name="id" value="<?php echo $row['id'] ?>">
109+
<button type="submit" class="btn btn-success" name="update">Update</button>
110+
</form>
111+
112+
</div>
113+
<div class="col-sm-6 offset-sm-2">
114+
<?php
115+
// SELECT All Data
116+
$sql = "SELECT * FROM student";
117+
118+
// Prepared Statement
119+
$result = $conn->prepare($sql);
120+
121+
// Execute Prepared statement
122+
$result->execute();
123+
124+
if($result->rowCount() > 0){
125+
echo '<table class="table">';
126+
echo "<thead>";
127+
echo "<tr>";
128+
echo "<th>ID</th>";
129+
echo "<th>Name</th>";
130+
echo "<th>Roll</th>";
131+
echo "<th>Address</th>";
132+
echo "<th>Action</th>";
133+
echo "</tr>";
134+
echo "</thead>";
135+
echo "<tbody>";
136+
// Fetch all table data
137+
while($row = $result->fetch(PDO::FETCH_ASSOC)){
138+
echo "<tr>";
139+
echo "<td>" . $row['id'] . "</td>";
140+
echo "<td>" . $row['name'] . "</td>";
141+
echo "<td>" . $row['roll'] . "</td>";
142+
echo "<td>" . $row['address'] . "</td>";
143+
echo '<td><form action="" method="POST"><input type="hidden" name="id" value=' . $row["id"] . '><input type="submit" class="btn btn-sm btn-warning" name="edit" value="Edit"></form></td>';
144+
echo "</tr>";
145+
}
146+
echo "</tbody>";
147+
echo "</table>";
148+
} else {
149+
echo "0 Results";
150+
}
151+
?>
152+
</div>
153+
</div>
154+
</div>
155+
156+
<!-- Optional JavaScript -->
157+
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
158+
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN"
159+
crossorigin="anonymous"></script>
160+
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q"
161+
crossorigin="anonymous"></script>
162+
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl"
163+
crossorigin="anonymous"></script>
164+
</body>
165+
<?php
166+
167+
// Close Prepared Statement
168+
unset($result);
169+
170+
// Close Connection
171+
$conn = null
172+
?>
173+
174+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
class Father{
3+
public $a;
4+
function __construct($x){
5+
echo "<br>Parent Constructor Called<br>";
6+
$this->a = $x;
7+
echo $this->a;
8+
}
9+
}
10+
class Son extends Father{
11+
public $b;
12+
function __construct($x, $y){
13+
parent::__construct($x); //calling Parent Class Constructor inside Child Class constructor
14+
echo "<br>Child Constructor Called<br>";
15+
$this->b = $y;
16+
echo $this->b;
17+
}
18+
}
19+
$objS = new Son(10, 20);
20+
?>

12. AccessModifierPublic.php

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
// Public Property or Method Can be accessed from anywhere
3+
class Father{
4+
// var $a
5+
// $a
6+
public $a;
7+
// function displayParent()
8+
public function displayParent(){
9+
echo "Parent Function $this->a";
10+
}
11+
}
12+
$objF = new Father;
13+
$objF->a = 10; // accessing public property with object
14+
$objF->displayParent(); // accessing public method with object
15+
16+
// class Son extends Father{
17+
// public function displayChild($x){
18+
// $this->a = $x; // acessing parent class public property
19+
// echo "Child Value is $this->a <br>";
20+
// $this->displayParent(); // accessing parent class public method
21+
// }
22+
// }
23+
// $obj = new Son;
24+
// $obj->displayChild(10);
25+
?>

13. AccessModifierPrivate.php

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
// Private Property or Method can be accessed only within same class
3+
// Private Property cannot be access outside class or with object
4+
// In Inheritance, Child Class cannot access Parent's Private Property or Method
5+
class Father{
6+
private $a; // Private Property
7+
public function displayParent(){ // private function displayParent()
8+
echo "Parent Function $this->a"; // can access private property here within same class
9+
}
10+
}
11+
$objF = new Father;
12+
$objF->a = 10; // Error: accessing Private property with object
13+
$objF->displayParent(); // accessing public method with object
14+
15+
// class Son extends Father{
16+
// public function displayChild($x){
17+
// $this->a = $x; // Cant access parent class private property in child class
18+
// echo "Child Value is $this->a <br>";
19+
// $this->displayParent(); // accessing parent class public method
20+
// }
21+
// }
22+
// $obj = new Son;
23+
// $obj->displayChild(10);
24+
?>

14. AccessModifierProtected1.php

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
// Cannot access Protected Property or Method Outside Class or Object
3+
// Protected Property or Class can be accessed within same class and
4+
// Child Class can access Parent's or GrandParent's Protected Property or Method
5+
// For accessing parent's protected property syntax are:
6+
// parent::propertyName or ClassName::propertyName
7+
class Father{
8+
protected $a; // Protected Property
9+
public function displayParent(){ // protected function displayParent()
10+
echo "Parent Function $this->a";
11+
}
12+
}
13+
$objF = new Father;
14+
$objF->a = 10; // Error: accessing Protected property with object
15+
$objF->displayParent(); // accessing public method with object
16+
17+
// class Son extends Father{
18+
// public function displayChild($x){
19+
// $this->a = $x; // Can access parent class protected property in child class
20+
// echo "Child Value is $this->a <br>";
21+
// $this->displayParent(); // accessing parent class public method
22+
// }
23+
// }
24+
// $obj = new Son;
25+
// $obj->displayChild(10);
26+
?>

15. AccessModifierProtected2.php

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
// Cannot access Protected Property or Method Outside Class or Object
3+
// Protected Property or Class can be accessed within same class and
4+
// Child Class can access Parent's or GrandParent's Protected Property or Method
5+
// For accessing parent's protected property syntax are:
6+
// parent::propertyName or ClassName::propertyName
7+
class Father{
8+
protected $a; // Protected Property
9+
protected function displayParent(){ // protected function displayParent()
10+
echo "Parent Function $this->a";
11+
}
12+
}
13+
14+
class Son extends Father{
15+
16+
}
17+
18+
class GrandSon extends Son {
19+
public function displayGrandChild($x){
20+
$this->a = $x; // Can access Grandparent class protected property in Grandchild class
21+
echo "Child Value is $this->a <br>";
22+
$this->displayParent(); // accessing Grandparent class Protected method
23+
}
24+
}
25+
$obj = new GrandSon;
26+
$obj->displayGrandChild(10);
27+
?>

16. Constructor with Protecte.php

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
class Father{
3+
public $a;
4+
protected function __construct($x){
5+
echo "<br>Parent Constructor Called<br>";
6+
$this->a = $x;
7+
echo $this->a;
8+
}
9+
}
10+
class Son extends Father{
11+
public $b;
12+
function __construct($x, $y){
13+
parent::__construct($x); // calling Parent Class Constructor inside Child Class constructor
14+
echo "<br>Child Constructor Called<br>";
15+
$this->b = $y;
16+
echo $this->b;
17+
}
18+
}
19+
$objS = new Son(10, 20);
20+
?>

17. static Property.php

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
class Father{
3+
public static $a = 10;
4+
public function disp(){
5+
echo self::$a;
6+
}
7+
}
8+
// accessing static property outside class
9+
Father::$a=20;
10+
$obj = new Father;
11+
$obj->disp();
12+
?>

18. static method.php

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
class Father{
3+
public static function disp(){
4+
echo "Hellow saminfratech";
5+
}
6+
}
7+
Father::disp(); // accessing static method without object
8+
?>

19. PassingvaluetoStaticMethod.php

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
class Father{
3+
public static function disp($nm){
4+
echo "Hellow " . $nm;
5+
}
6+
}
7+
Father::disp("Saminfratech");
8+
?>

0 commit comments

Comments
 (0)