Topics

PHP Tamplate

Benifit Of PHP Tamplate

If your create Libary need lots of time. So if you download this tamplate you make website in short time.

BootStrap all file includes in this tamplate. So you can make responsive website easily.

Jquery Libary includes in this tamplate.

Some method has created in this tamplate.you can use this method insert/edit/delete/select/details Data from Database.

you can easily data validation using Validation Libary.

Also includes Emailvarfication Libary and Session Libary in this tamplate.

this tamplate is SQL Injection and html Injection Protected.

Dymamic your website title text


How to make Create Page

	<?php
	$input = new Validation();
	$db = new DBContext();
	if($_SERVER['REQUEST_METHOD'] == "POST"){
	$input->post("name")->isEmpty();
	$name = $input->values['name'];
	$tablename = "country";
	$data = array('name'=>$name);

	if($input->submit()){
		if($db->addData($tablename,$data)){
				echo 'Data Added';
			}
		}else{
			print_r($input->errors);
			}	
		}

	?>
	<form action="" method="post">
	<input type="text" name="name">
	<input type="submit" name="btn" value="Create">
	</form>


	<a href="<?php appsConfig::URL('/home')?>">Back to list</a>

		

How to make Display Page

<?php
$db = new DBContext();
$tablename = "country";
echo '<table>';
foreach ($db->getData($tablename,array("id","name")) as $key => $value) {
	echo '<tr>';
	echo '<td>'.$value['id'].'</td>';
	echo '<td>'.$value['name'].'</td>';
	echo '<td><a href="'.appsConfig::Link('/edit_country/'.$value['id']).'">Edit</a></td>';
	echo '<td><a href="'.appsConfig::Link('/home/'.$value['id']).'" onclick="return confirm(\'Are you Sure Delete it ???\')">Delete</a></td>';

	echo '<td><a href="'.appsConfig::Link('/home_details/'.$value['id']).'" >Details</a></td>';
	echo '</tr>';
}
echo '</table>';

?>

<a href="<?php appsConfig::URL('/homeCreate')?>">Create
	

How to make Delete Page

<?php
$db = new DBContext();
$tablename = "country";

//start delete method

if(isset($url[1])){
	$id = $url[1];
	$db->deleteData($tablename,array("id"=>$id));
}

//end delete method
?>

		

How to make Edit Page


<?php
if(isset($url[1])){
	$id = $url[1];
}

$input = new Validation();
$db = new DBContext();
$tablename = "country";
$name = "";
foreach ($db->detailsData($tablename,array("id","name"),array("id"=>$id)) as  $value){
	$name = $value['name'];
}

if($_SERVER['REQUEST_METHOD'] == "POST"){
	$input->post("name")->isEmpty();
	$name = $input->values['name'];
	$data = array('name'=>$name);

	if($input->submit()){
		if($db->editData($tablename,$data,$id)){
			echo 'Data Added';
		}

	}else{
		print_r($input->errors);
	}	



}

?>


<form action="" method="post">
<input type="text" name="name" value="<?php echo $name;?>">
<input type="submit" name="btn" value="Create">

</form>


<a href="<?php appsConfig::URL('/home')?>">Back to list</a>
	
		

How to make Details Page

<?php

if(isset($url[1])){
	$id =$url[1];
}
$db = new DBContext();
$tablename = "country";
echo '<table>';
foreach ($db->detailsData($tablename,array("id","name"),array("id"=>$id)) as $key => $value) {
	echo '<tr>';
	echo '<td>'.$value['id'].'</td>';
	echo '<td>'.$value['name'].'</td>';
	echo '<td><a href="">Edit</a></td>';
	echo '<td><a href="">Delete</a></td>';
	echo '</tr>';
}
echo '</table>';


?>


<a href="<?php appsConfig::URL('/home')?>">Back To List</a> | 
<a href="<?php appsConfig::URL('/homeCreate')?>">Create</a>


	
		

Autoload All Class

spl_autoload_register(function($className){
if(file_exists('systems/libs/'.$className.'.php')){
		include_once 'systems/libs/'.$className.'.php';
	}
	else{
		echo "Class  not Exit";
		}

});	
		

Login Libary

<?php
$db = new Database();
if(isset($_POST['btn_login'])){
	$username = $_POST['username'];
	$password = md5($_POST['password']);

	$sql = "select * from user1 where Email = '".$username."' and password1 = '".$password."'";

	if($db->Authontication($sql)){
		foreach ($db->Authontication($sql) as  $value) {
			$_SESSION['name'] = $value['Name'];
			$_SESSION['type'] = $value['Type'];
		}
		if($_SESSION['type'] == "U"){
			if(isset($_SESSION['checkCart'])){
				header("Location:".URL.'/cart');
			}
			else{
				header("Location:".URL.'/welcome');
			}
		}
		else{
			header("Location:".URL.'/admin.php');
		}
	}
	else{
		echo 'username and password does not match';
	}
}

?>

<form method="post">
	<label>User name</label>
	<input type="text" name="username">
	<label>password</label>
	<input type="password" name="password">
	<input type="submit" name="btn_login">
</form>	
		

Registration Libary

	
		

How to remove Browser Cache Data

header("Expires: Tue, 01 Jan 2000 00:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

Clean Website URL using .htaccess

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?page=$1 [QSA,L]	
		

Email Verification Configure

<?php

class EmailVerification 
{	
	public static function sendEmail($toemail,$sendmessage)
	{
		 $to = $toemail;
		 $subject = "Registration Verification Email";
		 $message = $sendmessage;
		 $email ="madeeasyframework@gmail.com";
		 $header ="From:$email";
		 $send =mail($to,$subject,$message,$header);
	}

}

?>	
		

How to make Database Connection in PDO

<?php
/**
* DBConnection Class
*/
class DBConnection 
{
	protected $db;
	const USERNAME = "root";
	const DBNAME = "phptamplate";
	const PASS = "";
	const HOST = "localhost";
	function __construct(){
		$this->Connection();
	}

	private function Connection(){
		try{
			$this->db = new PDO("mysql:dbname=".Self::DBNAME.";host=".self::HOST,self::USERNAME,self::PASS);
		}
		catch(PDOException $ex){
			echo $ex->getMessage();
		}
	}
}

?>	
		

BDContext all method

<?php

/**
* DBContext Class
Useing for Manage data form Database
*/
class DBContext extends DBConnection
{
	protected $Error;
	function __construct(){
		parent::__construct();
	}

	protected function includes($a = array()){
		return $a;
	}

	//$tablename,array('id','name')
	#using for fetchdata from Data base
	public function getData($tablename='',$colName= array()){
		$column = implode(",", $colName);
		$sql = "select $column from $tablename";
		$stmt = $this->db->prepare($sql);
		$stmt->execute();
		if($stmt->rowCount() > 0){
			return $stmt->fetchAll(PDO::FETCH_ASSOC);
		}else{
			$this->Error = $stmt->errorInfo();
			return false;
		}
	}

	#using for insert Data to database
	public function addData($tablename,$data){
		
		$keys = implode(",", array_keys($data));
		$k = ':'.implode(", :",array_keys($data));
		$sql = "insert into $tablename($keys) values($k)";
		$stmt = $this->db->prepare($sql);
		foreach ($data as $key => $value) {
			$stmt->bindValue($key,$value);
		}
		$cond = $stmt->execute();
		$this->lastid = $this->db->lastInsertId();
		if($cond == 1){
			return true;
		}
		else{
			$this->Error = $stmt->errorInfo();
		}
	}
	#using for edit Data from database
	public function editData($tablename,$data,$id){
		
		$keys = null;
		foreach ($data as $key=>$value) {
				$keys .= ", $key = :$key";
			}
		$keyss = ltrim($keys, ",");
		$sql = "update $tablename set $keyss where id =".$id;
		$stmt = $this->db->prepare($sql);
		foreach ($data as $key => $value) {
			$stmt->bindValue($key,$value);
		}
		return $stmt->execute();
	}

	#using for delete Data from database
	#deleteData("country",array("id"=>$id))
	public function deleteData($tablename,$pram){
		
		$key = implode(",",array_keys($pram));
		$sql = "delete from $tablename where $key = :$key";
		$stmt = $this->db->prepare($sql);
		foreach ($pram as $key => $value) {
			$stmt->bindParam($key,$value);
		}
		return $stmt->execute();
	}

	#using for delete Data from database
	public function detailsData($tablename='',$colName= array(),$pram = array()){
		$column = implode(",", $colName);
		$key = implode(",",array_keys($pram));
		$sql = "select $column from $tablename where $key = :$key";
		$stmt = $this->db->prepare($sql);
		foreach ($pram as $key => $value) {
			$stmt->bindParam($key,$value);
		}
		$stmt->execute();
		if($stmt->rowCount() > 0){
			return $stmt->fetchAll(PDO::FETCH_ASSOC);
		}else{
			print_r($stmt->errorInfo());
		}
	}



	public function Authentication($tablename='',$username,$password){
		$sql = "select * from $tablename where email = :email and password = :password";
		$stmt = $this->db->prepare($sql);
		$stmt->bindValue(":email",$username);
		$stmt->bindValue(":password",$password);
		$stmt->execute();
		if($stmt->rowCount() > 0){
			return $stmt->fetchAll(PDO::FETCH_ASSOC);
		}else{
			//print_r($stmt->errorInfo());
		}
	}


}

?>	
		

Validation Libary

<?php
		
	class Validation 
	{
		public $values = array();
		public $errors = array();
		public $currentValue;

		function __construct(){
		}

		

		public function post($key){
			$this->values[$key] = strip_tags(stripslashes(trim($_POST[$key])));
			$this->currentValue = $key;
			return $this;
		}



		public function isEmpty(){
			if(empty($this->values[$this->currentValue])){
				$this->errors[$this->currentValue]['empty'] = "Field must not be Empty";
			}
			return $this;
		}


		public function length($min=0,$max){
			if (strlen($this->values[$this->currentValue]) < $min or strlen($this->values[$this->currentValue]) > $max) {
				$this->errors[$this->currentValue]['length'] = "Should min".$min." and max ".$max." Characters";
			}

			return $this;
		}


		public function submit(){
			if(empty($this->errors[$this->currentValue])){
				return true;
			}
			return false;
		}


		public function character()
		{
			if(!preg_match('%^[A-Za-z ]+$%', $this->values[$this->currentValue])){
				$this->errors[$this->currentValue]['character'] = " Input must be Characters";
			}
			return $this;	
		}

		public function digit()
		{
			if(!preg_match('%^[0-9]+$%', $this->values[$this->currentValue])){
				$this->errors[$this->currentValue]['digit'] = " Input must be Digit";
			}
			return $this;	
		}


		public function password()
		{
			if(!preg_match('%^[A-Za-z\.\-\$]+$%', $this->values[$this->currentValue])){
				$this->errors[$this->currentValue]['password'] = " input is not valid for password";
			}
			return $this;	
		}


		public function compareRepeatPassword($pass1,$pass2)
		{
			echo $pass1;
			echo $pass2;
			if($pass1 != $pass2){
				echo 'testing';
				$this->errors['password']['password'] = " Password does not match";
				print_r($this->errors);
			}
			return $this;
		}


		public function email()
		{
			if(!filter_var($this->values[$this->currentValue], FILTER_VALIDATE_EMAIL))
			{
				$this->errors[$this->currentValue]['email'] = "Email is not Valid";
			}
			return $this;	
		}


	}
		
	?>	
		

Session Libary

<?php
/**
* Session Class
*/
class Session 
{
	
	function __construct(){
		
	}

	public static function init(){
		session_start();
	}

	public static function set($key,$val){
		$_SESSION[$key] = $val;
	}

	public static function get($key){
		if(isset($_SESSION[$key])){
			return $_SESSION[$key];
		}else{
			return false;
		}
	}


	public static function destroy(){
		session_destroy();
	}
}

?>	
		

AppsConfig Libary

		<?php

/**
* Apps Config....
*/ 
class appsConfig 
{
	
	const URL = "http://localhost:8012/phptamplate";

	public static $pagetitle;

	public function URL($url){
		echo self::URL.$url;
	}

	public function Link($url){
		return self::URL.$url;
	}

	public function loadPageTitle(){
		if(isset($_GET['page'])){
			$url = $_GET['page'];
			$url = rtrim($url,"/");
			$url = explode("/", $url);
			//self::$pagetitle = $url[0];
			if($url[0] = str_replace("_", " ", $url[0])){
				self::$pagetitle = $url[0];
			}
		}
		else{
			self::$pagetitle = "home Page";
		}
	}


	public static function renderBody()
	{
		if(isset($_GET['page'])){
			$url = $_GET['page'];
			$url = rtrim($url,"/");
			$url = explode("/", $url);
			if(file_exists('apps/public/'.$url[0].'.php')){
				require_once 'apps/public/'.$url[0].'.php';
			}
			else{
				require_once 'apps/public/404.php';
			}

		}
		else{
			require_once 'apps/public/home.php';
		}

	}

	public function loadLibaryClass()
	{
		 spl_autoload_register(function($className){
			if(file_exists('systems/libs/'.$className.'.php')){
				include_once 'systems/libs/'.$className.'.php';
			}
			else{
				echo "Class  not Exit";
			}
			
		});
	}
	
}


?>	
		

Ajax Libary

<script>
function showhint(str)
{
	if(str.value == "")
	{
		document.getElementById("result").innerHTML = "";
		return;
	}
	
	if(window.XMLHttpRequest)
	{
		xmlhttp = new XMLHttpRequest();
	}
	else
	{
		xmlhttp = new ActiveObject(Microsoft.XMLHTTP);
	}
	
	xmlhttp.onreadystatechange = function()
	{
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
		{
			document.getElementById("result").innerHTML = xmlhttp.responseText;
		}
	
	}
	
	xmlhttp.open("GET","function.php?q="+str,true);
	xmlhttp.send();

}

</script>	
		

Dynamic Menu