Thursday, January 7, 2016

create the index.php file

<?php
require_once 'controller/Contractcontroller.php';
$controller = new Contractcontroller();
$controller->listcontacts();
?>


#######################################################
create controller ContractController.php
<?php

require_once 'model/ContractsService.php';

class ContractController
{
public function __construct()
{
$this->ContractsService=new ContractsService();
}
public function listcontacts()
{
if (!isset($_GET['name']))
{
//get all the details using : getAllContracts()

$names = $this->ContractsService->getAllContacts();
include 'view/contract.php';
}
else
{
//get all the details using : getAllContract()

$name = $this->ContractsService->getname($_GET['name']);
include 'view/ContractSearch.php';
}
}
}
?>

##########################################################################
create model ContractsGateWay.php

<?php

class  ContractsGateWay
{

public function selectAll()
{
// get details from the database using select

$conn = $_SESSION['connection'];

$select_sql = 'select * from ContractDetails order by name';
$sqlresult = $conn->query($select_sql);
$_SESSION['sqlresult']=$sqlresult;


if ($sqlresult->num_rows > 0)
{
return $sqlresult;
}

}
public function Selectname($name)
{

$conn = $_SESSION['connection'];
$select_sql1 = "select * from ContractDetails where name='$name' order by name";
$sqlresult1 = $conn->query($select_sql1);
$_SESSION['sqlresult1']=$sqlresult1;


if ($sqlresult1->num_rows > 0)
{
return $sqlresult1;
}
else
{
return "0 results";
}
}


}
?>

###############################################################################
create model ContractsService.php
<?php
require_once 'model/ContractsGateWay.php';

class ContractsService
{
public function __construct()
{
$this->ContractsGateWay = new ContractsGateWay();
}
private function opendb()
{
include './databse_connection.php';
}
private function clossdb()
{
$conn->close();
}
public function getAllContacts()
{

$this->opendb(); // opendb

$getall_contract = $this->ContractsGateWay->selectAll();

return $getall_contract;
}
public function getname($name)
{

$this->opendb();

$contract_name = $this->ContractsGateWay->Selectname($name);

return $contract_name;
}

}
?>
##############################################################################
create view contract.php

<html>
<head>
<title>Details of contracts</title>
</head>
<body>
<p>
<br> <br>
<a href="GetContract.php"> <input type="submit"  value="add to contract"> </a>
<p>
<br>
<hr>
<table>

<tr>
<th>Name</th>
<th>Phone</th>
<th>Email</th>
<th>Address</th>
</tr>

<tbody>
<?php
$sqlresult=$_SESSION['sqlresult'];
while($row = $sqlresult->fetch_assoc())
{
echo '<tr><td><a href="index.php?name='.$row["name"].'">'.$row["name"].'</a></td><td>'.$row["phone"].'</td><td>'.$row["email"].'</td><td>'.$row["address"].'</td></tr>';
}
?>
</tbody>
</table>

</body>
</html>
########################################################################
create view contactsearch.php
<html>

<head>
<title>user details</title>
</head>

<body>
<p><a href="index.php"> <input type="submit"  value="Home"> </a><p><br/>
<table>
<tr>
<th>Name</th>
<th>Phone</th>
<th>Email</th>
<th>Address</th>
</tr>
<tbody>
<?php
$sqlresult1=$_SESSION['sqlresult1'];
while($row = $sqlresult1->fetch_assoc()) 
{
echo '<tr><td>'.$row["name"].'</td><td>'.$row["phone"].'</td><td>'.$row["email"].'</td><td>'.$row["address"].'</td></tr>';
}
?>
</tbody>
</table>
</body>
</html>

###########################################################################
create sql

drop database if exists  Contract;
create database Contract;
use Contract;

create table if not exists ContractDetails 
(
  
  name varchar(30) not null,
  phone varchar(10) not null,
  email text not null,
  address text not null,
  primary key (name)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;



insert into ContractDetails (name, phone, email, address) 
values
('abcd', '0775412144', 'abcd@gmail.com', 'jaffna'),
('staff', '0754125832', 'staff@gmail.com', 'kandy'),
('admin', '0787541257', 'admin@gmail.com', 'karainagar'),
('customer', '07247854158', 'customer@gmail.com', 'kokuvil');

#########################################################
database connection
<?php 
$db_server = 'localhost';
$db_user = 'root';
$db_pass = '';
$db_name = 'Contract';
session_start();
$conn = new mysqli($db_server, $db_user, $db_pass, $db_name);
$_SESSION['connection']=$conn;
if ($conn->connect_error) 
{
die("Connection failed: " . $conn->connect_error);
?>
###########################################################################
fillfrom getcontract.php

<html>
<head>
<title>Fill Form</title>
</head>
<body>
<center>
<table>
<br><br>
<hr>
<form method="GET">
<tr>
<th>Name:</th>
<td><input type="text" name="name"></td>
</tr>
<tr>
<th>Phone:</th>
<td><input type="text" name="phone"></td>
</tr>
<tr>
<th>Email:</th>
<td><input type="email" name="email"></td>
</tr>
<tr>
<th>Address:</th>
<td><input type="text" name="address"></td>
</tr>
<tr>
<th></th>
<td><input type="submit" name="submit" value="Submit"></td>
</tr>
</form>
</table>
<?php
if(isset($_GET['submit']))
{
if (isset($_GET['name'])) $name=$_GET['name']; else $name="";
if (isset($_GET['phone'])) $phone=$_GET['phone']; else $phone="";
if (isset($_GET['email'])) $email=$_GET['email']; else $email="";
if (isset($_GET['address'])) $address=$_GET['address']; else $address="";
include './databse_connection.php';
$sql_insert = "insert into ContractDetails (name,phone,email,address) values ('$name','$phone','$email','$address')";
if ($conn->query($sql_insert) == TRUE) 
{
echo 'successfully';
else 
{
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
?>
<br/><a href="index.php"> <input type="submit"  value="view contract details"> </a>
</center>
</body>
</html>

No comments:

Post a Comment