Judul : PHP OBJECT ORIENTED CODING
link : PHP OBJECT ORIENTED CODING
PHP OBJECT ORIENTED CODING
First create table record:
step 1: create table record
(id int primary key auto_increment,
name varchar(200),
city varchar(200)
);
step2 : write code to display all record & save this displayall.php :
<?php
class db
{
var $servername = "localhost";
var $username = "root";
var $password = "";
var $dbname = "prakash";
var $conn;
function __construct()
{
// Create connection
$this->conn = new mysqli($this->servername, $this->username, $this->password, $this->dbname);
// Check connection
if ($this->conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
}
function displayall()
{
$sql = "SELECT * FROM record";
$result = $this->conn->query($sql);
if ($result->num_rows > 0)
{
// output data of each row
while($row = $result->fetch_assoc())
{
echo "id: " . $row["id"]. " - Name: " . $row["name"]. " " . $row["city"]. "<br>";
}
}
else
{
echo "0 results";
}
$this->conn->close();
}
}
$obj=new db();
$obj->displayall();
?>
=================insert =======example==========(1)write code for insert.html file:
<form action="insert.php" method="post">
Id <input type=text name="id">
name <input type=text name="name">
city <input type=text name="city">
<input type=submit name=submit value=insert>
</form>
(2)write code for db.php file:
<?php
class db
{
var $servername = "localhost";
var $username = "root";
var $password = "";
var $dbname = "prakash";
var $conn;
function __construct()
{
// Create connection
$this->conn = new mysqli($this->servername, $this->username, $this->password, $this->dbname);
// Check connection
if ($this->conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
}
function insert($a,$b,$c)
{
$sql="insert into record(id,name,city)values('$a','$b','$c')";
$result = $this->conn->query($sql);
if ($result)
{
// output data of each row
echo "inserted";
}
else
{
echo "not inserted";
}
$this->conn->close();
}
}
$obj=new db();
?>
(3)write code for insert.php :
<?php
include('db.php');
$id=$_POST['id'];
$name=$_POST['name'];
$city=$_POST['city'];
$obj->insert($id,$name,$city);
?>
=============update======example=============
(1)write code for update.html file:
<form action="updatet.php" method="post">
Id <input type=text name="id">
name <input type=text name="name">
city <input type=text name="city">
<input type=submit name=submit value=insert>
</form>
(2)write code for db.php file:
<?php
class db
{
var $servername = "localhost";
var $username = "root";
var $password = "";
var $dbname = "prakash";
var $conn;
function __construct()
{
// Create connection
$this->conn = new mysqli($this->servername, $this->username, $this->password, $this->dbname);
// Check connection
if ($this->conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
}
function update($id,$name,$city)
{
$sql="update record set name='$name',city='$city' where id='$id'";
$result = $this->conn->query($sql);
if ($result)
{
// output data of each row
echo "inserted";
}
else
{
echo "not inserted";
}
$this->conn->close();
}
}
$obj=new db();
?>
(3)write code for update.php file :
<?php
include('db.php');
$id=$_POST['id'];
$name=$_POST['name'];
$city=$_POST['city'];
$obj->update($id,$name,$city);
?>
===============delete example =======================
(1)write delete.html file code :
<form action="delete.php" method="post">
id<input type="text" name="id">
<input type="submit" name="submit" value="submit">
</form>
(2)write code for delete.php file :
<?php
include('dbdelete.php');
$id=$_POST['id'];
$obj->delete($id);
?>
(3)write code for dbdelete.php file:
<?php
class db
{
var $servername="localhost";
var $username="root";
var $password="";
var $dbname="oppsankit";
var $con;
function __construct()
{
$this->con=new mysqli($this->servername,$this->username,$this->password,$this->dbname);
if($this->con->connect_error)
{
die("connection failed:".$con->connect_error);
}
}
function delete($a)
{
$sql="delete from ankitopps where id='$a'";
$res=$this->con->query($sql);
if($res)
{
echo "deleted";
}
else
{
echo "not deleted";
}
$this->con->close();
}
}
$obj=new db();
?>
==============search example=============================
(1)write code search.html :
<form action="search.php" method="post">
id<input type="text" name="id">
<input type="submit" name="submit" value="submit">
</form>
(2)write code for search.php file :
<?php
include('dbsearch.php');
$id=$_POST['id'];
$obj->search($id);
?>
(3)write code dbsearch.php file :
<?php
class db
{
var $servername="localhost";
var $username="root";
var $password="";
var $dbname="oppsankit";
var $con;
function __construct()
{
$this->con=new mysqli($this->servername,$this->username,$this->password,$this->dbname);
if($this->con->connect_error)
{
die("connection failed:".$con->connect_error);
}
}
function search($a)
{
$sql = "SELECT * FROM ankitopps where id='$a'";
$result =$this->con->query($sql);
if ($result->num_rows > 0)
{
// output data of each row
while($row = $result->fetch_assoc())
{
echo "id: " . $row["id"]. " - Name: " . $row["name"]. " " . $row["city"]. "<br>";
}
}
else
{
echo "0 results";
}
$this->con->close();
}
}
$obj=new db();
?>
===================================================================Object oriented Programming Practice for including all database connectivity insert,update ,delete ,display code all in one in a single page :
First create table record:
step 1: create table record
(id int primary key auto_increment,
name varchar(200),
city varchar(200)
);
(1)write code for All_model.php file:
<?php
class All_model
{
var $servername="localhost";
var $username="root";
var $pwd="";
var $dbname="prakash";
var $con;
function __construct()
{
$this->con=new mysqli($this->servername,$this->username,$this->pwd,$this->dbname);
if($this->con->connect_error)
{
die ("connection failed:".$con->connect_error);
}
}
function insert($a,$b,$c)
{
$sql="insert into record(id,name,city)values('$a','$b','$c')";
$res=$this->con->query($sql);
if($res)
{
echo "inserted";
}
else
{
echo "not inserted";
}
$this->con->close();
}
function update($a,$b,$c)
{
$sql="update record set name='$b', city='$c' where id='$a'";
$res=$this->con->query($sql);
if($res)
{
echo "updated";
}
else
{
echo "not updated";
}
$this->con->close();
}
function delete($a)
{
$sql="delete from record where id='$a'";
$res=$this->con->query($sql);
if($res)
{
echo "deleted";
}
else
{
echo "not deleted";
}
$this->con->close();
}
function search($name)
{
$sql = "SELECT * FROM record where name='$name'";
$result = $this->con->query($sql);
if ($result->num_rows > 0)
{
// output data of each row
while($row = $result->fetch_assoc())
{
echo "id: " . $row["id"]. " Name: " . $row["name"]. " " . $row["city"]. "<br>";
}
}
else
{
echo "0 results";
}
$this->con->close(); //connection close
}
function displayall()
{
$sql = "SELECT * FROM record";
$result = $this->con->query($sql);
if ($result->num_rows > 0)
{
// output data of each row
while($row = $result->fetch_assoc())
{
echo "id: " . $row["id"]. " - Name: " . $row["name"]. " " . $row["city"]. "<br>";
}
}
else
{
echo "0 results";
}
$this->con->close(); //connection close
}
}
$obj=new All_model();
?>
=================insert data================
(1)insert.html file code:
<form action="insert.php" method="post">
id<input type="text" name="id">
name<input type="text" name="name">
city<input type="text" name="city">
<input type="submit" name="submit" value="insert">
</form>
(2)insert.php file code:
<?php
include('All_model.php');
$id=$_POST['id'];
$name=$_POST['name'];
$city=$_POST['city'];
$obj->insert($id,$name,$city);
?>
=================update data===========
(1)update.html file code:
<form action="update.php" method="post">
id<input type="text" name="id">
name<input type="text" name="name">
city<input type="text" name="city">
<input type="submit" name="submit" value="update">
</form>
(2)update.php file code :
<?php
include('All_model.php');
$id=$_POST['id'];
$name=$_POST['name'];
$city=$_POST['city'];
$obj->update($id,$name,$city);
?>
==================delete==data =======================
(1)write delete.html file:
<form action="delete.php" method="post">
id<input type="text" name="id">
<input type="submit" name="submit" value="delete">
</form>
(2)write delete.php file:
<?php
include('All_model.php');
$id=$_POST['id'];
$obj->delete($id);
?>
======================search example=====================
(1)write search.html file:
<form action="search.php" method="post">
name<input type="text" name="name">
<input type="submit" name="submit" value="search">
</form>
(2)write search.php file code:
<?php
include('All_model.php');
$name=$_POST['name'];
$obj->search($name);
?>
PURE & PERFECT OBJECT ORIENTED TECHNIQUES
========================================================================
step 1: create table record
(id int primary key auto_increment,
name varchar(200),
city varchar(200)
);
step2 : write code to display all record & save this displayall.php :
<?php
class db
{
var $servername = "localhost";
var $username = "root";
var $password = "";
var $dbname = "prakash";
var $conn;
function __construct()
{
// Create connection
$this->conn = new mysqli($this->servername, $this->username, $this->password, $this->dbname);
// Check connection
if ($this->conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
}
function displayall()
{
$sql = "SELECT * FROM record";
$result = $this->conn->query($sql);
if ($result->num_rows > 0)
{
// output data of each row
while($row = $result->fetch_assoc())
{
echo "id: " . $row["id"]. " - Name: " . $row["name"]. " " . $row["city"]. "<br>";
}
}
else
{
echo "0 results";
}
$this->conn->close();
}
}
$obj=new db();
$obj->displayall();
?>
=================insert =======example==========(1)write code for insert.html file:
<form action="insert.php" method="post">
Id <input type=text name="id">
name <input type=text name="name">
city <input type=text name="city">
<input type=submit name=submit value=insert>
</form>
(2)write code for db.php file:
<?php
class db
{
var $servername = "localhost";
var $username = "root";
var $password = "";
var $dbname = "prakash";
var $conn;
function __construct()
{
// Create connection
$this->conn = new mysqli($this->servername, $this->username, $this->password, $this->dbname);
// Check connection
if ($this->conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
}
function insert($a,$b,$c)
{
$sql="insert into record(id,name,city)values('$a','$b','$c')";
$result = $this->conn->query($sql);
if ($result)
{
// output data of each row
echo "inserted";
}
else
{
echo "not inserted";
}
$this->conn->close();
}
}
$obj=new db();
?>
(3)write code for insert.php :
<?php
include('db.php');
$id=$_POST['id'];
$name=$_POST['name'];
$city=$_POST['city'];
$obj->insert($id,$name,$city);
?>
=============update======example=============
(1)write code for update.html file:
<form action="updatet.php" method="post">
Id <input type=text name="id">
name <input type=text name="name">
city <input type=text name="city">
<input type=submit name=submit value=insert>
</form>
(2)write code for db.php file:
<?php
class db
{
var $servername = "localhost";
var $username = "root";
var $password = "";
var $dbname = "prakash";
var $conn;
function __construct()
{
// Create connection
$this->conn = new mysqli($this->servername, $this->username, $this->password, $this->dbname);
// Check connection
if ($this->conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
}
function update($id,$name,$city)
{
$sql="update record set name='$name',city='$city' where id='$id'";
$result = $this->conn->query($sql);
if ($result)
{
// output data of each row
echo "inserted";
}
else
{
echo "not inserted";
}
$this->conn->close();
}
}
$obj=new db();
?>
(3)write code for update.php file :
<?php
include('db.php');
$id=$_POST['id'];
$name=$_POST['name'];
$city=$_POST['city'];
$obj->update($id,$name,$city);
?>
===============delete example =======================
(1)write delete.html file code :
<form action="delete.php" method="post">
id<input type="text" name="id">
<input type="submit" name="submit" value="submit">
</form>
(2)write code for delete.php file :
<?php
include('dbdelete.php');
$id=$_POST['id'];
$obj->delete($id);
?>
(3)write code for dbdelete.php file:
<?php
class db
{
var $servername="localhost";
var $username="root";
var $password="";
var $dbname="oppsankit";
var $con;
function __construct()
{
$this->con=new mysqli($this->servername,$this->username,$this->password,$this->dbname);
if($this->con->connect_error)
{
die("connection failed:".$con->connect_error);
}
}
function delete($a)
{
$sql="delete from ankitopps where id='$a'";
$res=$this->con->query($sql);
if($res)
{
echo "deleted";
}
else
{
echo "not deleted";
}
$this->con->close();
}
}
$obj=new db();
?>
==============search example=============================
(1)write code search.html :
<form action="search.php" method="post">
id<input type="text" name="id">
<input type="submit" name="submit" value="submit">
</form>
(2)write code for search.php file :
<?php
include('dbsearch.php');
$id=$_POST['id'];
$obj->search($id);
?>
(3)write code dbsearch.php file :
<?php
class db
{
var $servername="localhost";
var $username="root";
var $password="";
var $dbname="oppsankit";
var $con;
function __construct()
{
$this->con=new mysqli($this->servername,$this->username,$this->password,$this->dbname);
if($this->con->connect_error)
{
die("connection failed:".$con->connect_error);
}
}
function search($a)
{
$sql = "SELECT * FROM ankitopps where id='$a'";
$result =$this->con->query($sql);
if ($result->num_rows > 0)
{
// output data of each row
while($row = $result->fetch_assoc())
{
echo "id: " . $row["id"]. " - Name: " . $row["name"]. " " . $row["city"]. "<br>";
}
}
else
{
echo "0 results";
}
$this->con->close();
}
}
$obj=new db();
?>
===================================================================Object oriented Programming Practice for including all database connectivity insert,update ,delete ,display code all in one in a single page :
First create table record:
step 1: create table record
(id int primary key auto_increment,
name varchar(200),
city varchar(200)
);
(1)write code for All_model.php file:
<?php
class All_model
{
var $servername="localhost";
var $username="root";
var $pwd="";
var $dbname="prakash";
var $con;
function __construct()
{
$this->con=new mysqli($this->servername,$this->username,$this->pwd,$this->dbname);
if($this->con->connect_error)
{
die ("connection failed:".$con->connect_error);
}
}
function insert($a,$b,$c)
{
$sql="insert into record(id,name,city)values('$a','$b','$c')";
$res=$this->con->query($sql);
if($res)
{
echo "inserted";
}
else
{
echo "not inserted";
}
$this->con->close();
}
function update($a,$b,$c)
{
$sql="update record set name='$b', city='$c' where id='$a'";
$res=$this->con->query($sql);
if($res)
{
echo "updated";
}
else
{
echo "not updated";
}
$this->con->close();
}
function delete($a)
{
$sql="delete from record where id='$a'";
$res=$this->con->query($sql);
if($res)
{
echo "deleted";
}
else
{
echo "not deleted";
}
$this->con->close();
}
function search($name)
{
$sql = "SELECT * FROM record where name='$name'";
$result = $this->con->query($sql);
if ($result->num_rows > 0)
{
// output data of each row
while($row = $result->fetch_assoc())
{
echo "id: " . $row["id"]. " Name: " . $row["name"]. " " . $row["city"]. "<br>";
}
}
else
{
echo "0 results";
}
$this->con->close(); //connection close
}
function displayall()
{
$sql = "SELECT * FROM record";
$result = $this->con->query($sql);
if ($result->num_rows > 0)
{
// output data of each row
while($row = $result->fetch_assoc())
{
echo "id: " . $row["id"]. " - Name: " . $row["name"]. " " . $row["city"]. "<br>";
}
}
else
{
echo "0 results";
}
$this->con->close(); //connection close
}
}
$obj=new All_model();
?>
=================insert data================
(1)insert.html file code:
<form action="insert.php" method="post">
id<input type="text" name="id">
name<input type="text" name="name">
city<input type="text" name="city">
<input type="submit" name="submit" value="insert">
</form>
(2)insert.php file code:
<?php
include('All_model.php');
$id=$_POST['id'];
$name=$_POST['name'];
$city=$_POST['city'];
$obj->insert($id,$name,$city);
?>
=================update data===========
(1)update.html file code:
<form action="update.php" method="post">
id<input type="text" name="id">
name<input type="text" name="name">
city<input type="text" name="city">
<input type="submit" name="submit" value="update">
</form>
(2)update.php file code :
<?php
include('All_model.php');
$id=$_POST['id'];
$name=$_POST['name'];
$city=$_POST['city'];
$obj->update($id,$name,$city);
?>
==================delete==data =======================
(1)write delete.html file:
<form action="delete.php" method="post">
id<input type="text" name="id">
<input type="submit" name="submit" value="delete">
</form>
(2)write delete.php file:
<?php
include('All_model.php');
$id=$_POST['id'];
$obj->delete($id);
?>
======================search example=====================
(1)write search.html file:
<form action="search.php" method="post">
name<input type="text" name="name">
<input type="submit" name="submit" value="search">
</form>
(2)write search.php file code:
<?php
include('All_model.php');
$name=$_POST['name'];
$obj->search($name);
?>
PURE & PERFECT OBJECT ORIENTED TECHNIQUES
========================================================================
(1)FIRST OF create table about:
SQL>
create table about
(id int primary key auto_increment,
pagetitle varchar(200),
description varchar(200),
keyword varchar(200),
editor1 varchar(200)
);
(2)write code for database.php file :
<?php
class DBController
{
private $host = "localhost";
private $user = "root";
private $password = "";
private $database = "test";
function __construct()
{
$conn = $this->connectDB();
if(!empty($conn))
{
$this->selectDB($conn);
}
}
function connectDB()
{
$conn = mysql_connect($this->host,$this->user,$this->password);
return $conn;
}
function selectDB($conn)
{
mysql_select_db($this->database,$conn);
}
function displayQuery($query)//this function is for display record and for search query
{
$result = mysql_query($query);
while($row=mysql_fetch_assoc($result)) {
$resultset[] = $row;
}
if(!empty($resultset))
return $resultset;
}
function runQuery($query) // this function is for insert,update,delete query
{
$result = mysql_query($query);
return $result;
}
function numRows($query)
{
$result = mysql_query($query);
$rowcount = mysql_num_rows($result);
return $rowcount;
}
}
?>
========================================================================
INSERT QUERY USING OBJECT ORIENTED
========================================================================
(1)write code for insert.html file:
<form action="insert.php" method="post">
Page title<input type=text name=pagetitle>
description <input type=text name=description>
keyword <input type=text name=keyword>
editor1 <input type=text name=editor1>
<input type=submit name=insert value=insert>
</form>
(2)write code for insert.php file:
<?php
include('database.php');
$pagetitle=$_POST['pagetitle'];
$description=$_POST['description'];
$keyword=$_POST['keyword'];
$editor1=$_POST['editor1'];
$query="insert into about(pagetitle,description,keyword,editor1)values('$pagetitle','$description','$keyword','$editor1')";
$obj=new DBController();
$result=$obj->runQuery($query);
if($result)
{
echo "record inserted";
}
else
{
echo "not inserted";
}
?>
SQL>
create table about
(id int primary key auto_increment,
pagetitle varchar(200),
description varchar(200),
keyword varchar(200),
editor1 varchar(200)
);
(2)write code for database.php file :
<?php
class DBController
{
private $host = "localhost";
private $user = "root";
private $password = "";
private $database = "test";
function __construct()
{
$conn = $this->connectDB();
if(!empty($conn))
{
$this->selectDB($conn);
}
}
function connectDB()
{
$conn = mysql_connect($this->host,$this->user,$this->password);
return $conn;
}
function selectDB($conn)
{
mysql_select_db($this->database,$conn);
}
function displayQuery($query)//this function is for display record and for search query
{
$result = mysql_query($query);
while($row=mysql_fetch_assoc($result)) {
$resultset[] = $row;
}
if(!empty($resultset))
return $resultset;
}
function runQuery($query) // this function is for insert,update,delete query
{
$result = mysql_query($query);
return $result;
}
function numRows($query)
{
$result = mysql_query($query);
$rowcount = mysql_num_rows($result);
return $rowcount;
}
}
?>
========================================================================
INSERT QUERY USING OBJECT ORIENTED
========================================================================
(1)write code for insert.html file:
<form action="insert.php" method="post">
Page title<input type=text name=pagetitle>
description <input type=text name=description>
keyword <input type=text name=keyword>
editor1 <input type=text name=editor1>
<input type=submit name=insert value=insert>
</form>
(2)write code for insert.php file:
<?php
include('database.php');
$pagetitle=$_POST['pagetitle'];
$description=$_POST['description'];
$keyword=$_POST['keyword'];
$editor1=$_POST['editor1'];
$query="insert into about(pagetitle,description,keyword,editor1)values('$pagetitle','$description','$keyword','$editor1')";
$obj=new DBController();
$result=$obj->runQuery($query);
if($result)
{
echo "record inserted";
}
else
{
echo "not inserted";
}
?>
========================================================================
UPDATE QUERY USING OBJECT ORIENTED
========================================================================
<form action="update.php" method="post">
Page title<input type=text name=pagetitle>
description <input type=text name=description>
keyword <input type=text name=keyword>
editor1 <input type=text name=editor1>
<input type=submit name=update value=update>
</form>
(2)write code for update.php file:
<?php
include('database.php');
$pagetitle=$_POST['pagetitle'];
$description=$_POST['description'];
$keyword=$_POST['keyword'];
$editor1=$_POST['editor1'];
$query="update about set pagetitle='$pagetitle',description='$description',keyword='$keyword',editor1='$editor1' where pagetitle='$pagetitle'";
$obj=new DBController();
$result=$obj->runQuery($query);
if($result)
{
echo "record updated";
}
else
{
echo "not updated";
}
?>
========================================================================
DELETE QUERY USING OBJECT ORIENTED
========================================================================
(1)Write code for delete.html file:
<form action="delete.php" method="post">
Page title<input type=text name=pagetitle>
<input type=submit name=delete value=delete>
</form>
(2)write code for delete.php file:
<?php
include('database.php');
$pagetitle=$_POST['pagetitle'];
$query="delete from about where pagetitle='$pagetitle'";
$obj=new DBController();
$result=$obj->runQuery($query);
if($result)
{
echo "record deleted";
}
else
{
echo "not deleted";
}
?>
========================================================================
DISPLAY ALL RECORD QUERY USING OBJECT ORIENTED
========================================================================
<?php
include('database.php');
$query="select * from about";
$obj=new DBController();
$data=$obj->displayQuery($query);
foreach($data as $v)
{
echo $v['pagetitle'];
echo $v['keyword'];
echo $v['description'];
echo $v['editor1'];
echo '<br>';
}
?>
========================================================================
SEARCH RECORD QUERY USING OBJECT ORIENTED
========================================================================
(1)Write code for search.html file:
<form action="search.php" method="post">
Page title<input type=text name=pagetitle>
<input type=submit name=search value=search>
</form>
(2)Write code for search.php file:
<?php
include('database.php');
$pagetitle=$_POST['pagetitle'];
$query="select * from about where pagetitle='$pagetitle'";
$obj=new DBController();
$data=$obj->displayQuery($query);
$num=$obj->numRows($query);
if($num>0)
{
foreach($data as $v)
{
echo $v['pagetitle'];
echo $v['keyword'];
echo $v['description'];
echo $v['editor1'];
echo '<br>';
}
}
else
{
echo "no record found";
}
?>
SEARCH RECORD QUERY USING OBJECT ORIENTED
========================================================================
(1)Write code for search.html file:
<form action="search.php" method="post">
Page title<input type=text name=pagetitle>
<input type=submit name=search value=search>
</form>
(2)Write code for search.php file:
<?php
include('database.php');
$pagetitle=$_POST['pagetitle'];
$query="select * from about where pagetitle='$pagetitle'";
$obj=new DBController();
$data=$obj->displayQuery($query);
$num=$obj->numRows($query);
if($num>0)
{
foreach($data as $v)
{
echo $v['pagetitle'];
echo $v['keyword'];
echo $v['description'];
echo $v['editor1'];
echo '<br>';
}
}
else
{
echo "no record found";
}
?>
Demikianlah Artikel PHP OBJECT ORIENTED CODING
Sekianlah artikel PHP OBJECT ORIENTED CODING kali ini, mudah-mudahan bisa memberi manfaat untuk anda semua. baiklah, sampai jumpa di postingan artikel lainnya.
Anda sekarang membaca artikel PHP OBJECT ORIENTED CODING dengan alamat link https://othereffect.blogspot.com/2017/01/php-object-oriented-coding.html
0 Response to "PHP OBJECT ORIENTED CODING "
Post a Comment