Thursday, 19 May 2011

PHP INCLUDE AND PHP REQUIRE




PHP includes allow you to import HTML/PHP code from external files to your current PHP script.

This allows you to reuse the same lines of code with only one declaration and might be very useful, for instance when you want to assign the same template to all the pages of your website without including the whole code of this template within every webpage.

Thefore, if you need to modify the template of your webpages in the future, all you will have to do is modify the original template file and all the other pages of your website will automatically be updated since they all fetch their code from this file. The PHP syntax of theinclude() statement will be introduced below.


PHP INCLUDE AND PHP REQUIRE - PHP INCLUDE



Given a file whose path is called file_path (file_path is a string giving the address of the file, for instance 'file.php' or '/folder/file.php', etc ...), the PHP syntax is given by:
include(file_path)


In the example below, a file called complements.php will be added as an include within another file called main.php (in order to simplify the path, we will put both files in the same folder).

Learn the PHP code:
complements.php
<?php
$s = 1;
?>

main.php
<?php
include('complements.php');
echo $s+1;
?>


Run the PHP script in your web browser:

php include() statement

Remark:

When executing an include() statement, the external file which has been included is processed as HTML by default, even though the include statement was inserted within a php script.
If the include file contains PHP code, you must place that code between the tags <?php and ?> within the include file itself.

PHP INCLUDE AND PHP REQUIRE - PHP REQUIRE


The only difference between the PHP include and PHP require statements is that the PHP require() statement triggers a fatal error when the file included cannot be found, while the include() statement only issues a warning.

The PHP include() and require() statements() allow you to reuse your PHP code and gain time and flexibility. Typically, they allow you to group variables or functions in one PHP file and then make them available to any other page of your website. They also allow you to optimize the development of templates for more flexibility.








///////////////////////////////formvalidation///////////////////////////////////////////


<html>
<head>
<title>JQuery Form Example</title> 
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery.validate/1.7/jquery.validate.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#feedback').load('check.php').show();
$('#namein').keyup(function()
{
$('#feedback').html(" ");
$.post('check.php',{ name:myform.name.value},
function(result)
{


$('#feedback').html(result).show();




});
});


/*$('#pono').onblur(function(){


$.post('check.php',{phno:myform.pheno.value},
function(result){


$('#pono').html(result).show();
});


)};*/


$("#myform").validate({ debug: false,rules: {
name: "required",
email: {required: true,email: true},
pheno: "required",
city: "required"},
messages: {name: "Please let us know who you are.",email: "A valid email will help us get in touch with you.",pheno:"Enter correct no",city:"enter the city name"},

});



});
</script>
<style type="text/css">
label.error { width: 250px; display: inline; color: red;}
div#feedback { display:inline;}
form{position:fixed;
top:30px;
right:5px;}

</style>
</head>
<body>
<form name="myform" id="myform" action="insert.php" method="POST">  
<!-- The Name form field -->
    <label for="name" id="name_label">Name</label>  
    <input type="text" name="name" id="namein" size="30" value=""/><div id='feedback'></div>  
<br>
<label for="password" id="pass">password</label>< 
    <input type="password" name="pass" id="password" size="30" value=""/><br>
<label for="fname" id="fname">firstname</label>  
    <input type="text" name="fname" id="finame" size="30" value=""/><br>
<label for="lname" id="lname">lastname</label>  
    <input type="text" name="lname" id="laname" size="30" value=""/><br>
<!-- The Email form field -->
    <label for="email" id="email_label">Email</label>  
    <input type="text" name="email" id="email" size="30" value=""/> 
<br>
<!-- phno -->
<label>phno</label>
<input type="text" name="pheno" id="phno" size="30" /><br>
<label>city</label>
<input type="text" name="city" id="city" size="30" /><br>
<label for="male" id="male_id">male</label>  
    <input type="radio" name="gender" id="genderid" size="30" value="male"/>
<label for="female" id="female_id">female</label>
<input type="radio" name="gender" id="genderid" size="30" value="Female"/><br>
<!-- The Submit button -->
<input type="submit" name="submit" value="Submit"> 
</form>


</body>
</html>










/////////////////////////////////////style forms/////////////////////////////////////////////////////



<html>
<head>
<title>JQuery Form Example</title> 
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery.validate/1.7/jquery.validate.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#feedback').load('check.php').show();
$('#namein').keyup(function()
{
$('#feedback').html(" ");
$.post('check.php',{ name:myform.name.value},
function(result)
{


$('#feedback').html(result).show();




});
});


/*$('#pono').onblur(function(){


$.post('check.php',{phno:myform.pheno.value},
function(result){


$('#pono').html(result).show();
});


)};*/


$("#myform").validate({ debug: false,rules: {
name: "required",
email: {required: true,email: true},
pheno: "required",
city: "required"},
messages: {name: "Please let us know who you are.",email: "A valid email will help us get in touch with you.",pheno:"Enter correct no",city:"enter the city name"},

});



});
</script>
<style type="text/css">
label.error { width: 250px; display: inline; color: red;}
div#feedback { display:inline;}
form{position:fixed;
top:30px;
right:5px;}

</style>
</head>
<body>
<form name="myform" id="myform" action="insert.php" method="POST">  
<!-- The Name form field -->
    <label for="name" id="name_label">Name</label>  
    <input type="text" name="name" id="namein" size="30" value=""/><div id='feedback'></div>  
<br>
<label for="password" id="pass">password</label>< 
    <input type="password" name="pass" id="password" size="30" value=""/><br>
<label for="fname" id="fname">firstname</label>  
    <input type="text" name="fname" id="finame" size="30" value=""/><br>
<label for="lname" id="lname">lastname</label>  
    <input type="text" name="lname" id="laname" size="30" value=""/><br>
<!-- The Email form field -->
    <label for="email" id="email_label">Email</label>  
    <input type="text" name="email" id="email" size="30" value=""/> 
<br>
<!-- phno -->
<label>phno</label>
<input type="text" name="pheno" id="phno" size="30" /><br>
<label>city</label>
<input type="text" name="city" id="city" size="30" /><br>
<label for="male" id="male_id">male</label>  
    <input type="radio" name="gender" id="genderid" size="30" value="male"/>
<label for="female" id="female_id">female</label>
<input type="radio" name="gender" id="genderid" size="30" value="Female"/><br>
<!-- The Submit button -->
<input type="submit" name="submit" value="Submit"> 
</form>


</body>
</html>






///////////////////////////////////////insert.php//////////////////////////////////////

<?php
mysql_connect("localhost","root","");
mysql_select_db("test") or die(mysql_error());
if(isset($_POST['name'],$_POST['city'],$_POST['phno'],$_POST['email']))
{
$name=$_POST['name'];
$pass=$_POST['city'];
$phno=$_POST['phno'];
$email=$_POST['email'];


/*$error=array();
if(empty($name)||empty($pass)||empty($phno)||empty($email) )
{


$error[]="u forget some filled ";


}
else
{


if(strlen($name)>20)
{  $error[]= "Name too long";  }
if(filter_var($email,FILTER_VALIDATE_EMAIL)===FALSE)
{


$error[]="plz give the valid email";


}
if(!is_numeric($phno))
{  $error[]="plz enter the phone no";}
}
if(!empty($error))
{


foreach($error as $errors)
{


echo "<b>".$errors;


}


}*/




$ins=mysql_query("insert into tab1(name,city,phno,email) values ('$name','$city','$phno','$email')");
if($ins)
{
echo "sucessfully inserted";
}
else
{
echo "it will not insert";
}


}


?>




//////////////////////////////////////////////////pagination////////////////////////////////////////////



<html>
<head>
</head>
<body>
<table border="10">
<tr><th>name</th><th>Email</th><th>phno</th><th>city</th></tr></table>
</body>
</html>
<?php
//mysql start position
$start=0;
//record per page
$pp=2;




//current page
if(!isset($_GET['page']))
{
$page=1;
}
else
{
$page=$_GET['page'];
}




//mysql start position




if($page<=1)
$start=0;
else
$start=$page * $pp - $pp;








//connect  db
$con=mysql_connect("localhost","","");
mysql_select_db("test",$con) or die("no msg");
$r="select * from tab1";
$dis="select * from tab1 LIMIT $start,$pp";
//how much records in db
$no_row=mysql_num_rows(mysql_query($r));
$no_pag=ceil($no_row/$pp);
//append 
//$dis.="LIMIT $start,$pp";
$res=mysql_query($dis);


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


echo $row['name']."&nbsp".$row['phno']."<br>";
}
//prev




$prev=$page-1;








//next
$next=$page+1;
//prev
if($prev > 0)
echo "<a href='?page=$prev'>prev</a>";








$number=1;
for($number;$number<=$no_pag;$number++)
{
echo "<a href='?page=$number'>$number</a> ";
}




if($page<ceil($no_row/$pp))
echo "<a href='?page=$next'>next</a>";




?>





//////////////////////////////////////////loginin.php/////////////////////////////////////////




<?php
include 'oop.php';
if(logi())
{


header("Location:output.php");
}


if(isset($_POST['login']))
{


$name=$_POST['name'];
$pass=$_POST['pass'];
if(isset($_POST['rembr']))
$rembr=$_POST['rembr'];
else
echo "";


if($name&&$pass)
{


$login=mysql_query("select * from log where name='$name'");
while($row=mysql_fetch_assoc($login))
{
$dbp=$row['id'];


if($pass==$dbp)
$loginok=TRUE;
else
$loginok=FALSE;




if($loginok==TRUE)
{


if($rembr=="on")
setcookie("name",$name,time()+7200);


else if($rembr=="")
$_SESSION['uname']=$name;


header("Location:output.php");
exit();
}
else
{die("incorre password ");}


}


}
else
die("incorrect user name/password");


 }




?>
<form action="in.php" method="POST">
name<input type="text" name="name" /><br>
pass<input type="password" name="pass" /><br>
<input type="checkbox" name="rembr" /><br>
<input type="submit" name="login" value="submit"/><br>
</form>




///////////////////////////////oop.php///////////////////////////////////////////////////




<?php
session_start();
mysql_connect("localhost","","") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());




//login check function
function logi()
{


if(isset($_SESSION['uname'])||isset($_COOKIE['name']))
{


$logi=TRUE;
return $logi;


}
}
/*class dis
{
var $name=10;
function get()
{


echo $this->name;
}
function ri()
{
echo $this->get();
}
}


$get=new di";
echo $get->get()."<br>";
echo $get->ri();
echo $get->name;*/

?> 

No comments:

Post a Comment