Saturday, July 19, 2014

Php Lessons

1) To retrive bubble assending

<html>
<head><title>Exam1-bubblesort</title>
<style type="">
    form{
        background-color: lightskyblue;
        margin: 100px auto;
        padding: 20px;
        width: 700px;
    }


    form table tr th{
        font-family: arial;
        font-size: 12px;
        font-weight: normal;
        text-align: right;
    }


    form .textfield{
        width: 200px;
    }
 
    form table tr td .button{
        text-align: right;
    }
</style>
</head>
<body>

<form method="POST" action="exam1.php" name="exam11">
<table>
<tr><td colspan="4"></td>Enter numbers you want to set ascending or descending</tr>

<tr><td><input type="text" name="int1" value=""></td><td><input type="text" name="int2" value=""></td><td><input type="text" name="int3" value=""></td><td></td></tr>



<tr><td><input type="radio" name="val11" value="Asc">Acceding</input>
<td><input type="radio" name="val11" value="Dcsen">Descending</input>
</td><td></td>
<td><input type="submit" name="clickSubmit" value="click1"></td></tr>


</table>
</form>
<?php
function bubble_sort($arr)
{
$length = count($arr);
 //sorting to asscending.................................................................
for ($i = 0; $i < $length; $i++) {
for ($j = $length-1; $j > $i; $j--) {
if ($arr[$j] < $arr[$j-1]) {
$t = $arr[$j];
$arr[$j] = $arr[$j-1];
$arr[$j-1] = $t;
}
}
}

return $arr;
}
// $input=array(2,4,5,6,7,3);


if (isset($_POST['clickSubmit'])) {
//into the array
$arr1[0]=$_POST['int1'];
$arr1[1]=$_POST['int2'];
$arr1[2]=$_POST['int3'];


$selected_radio = $_POST['val11'];

if($selected_radio =='Asc'){
$output = bubble_sort($arr1);
echo "Acceding Order : " ;
foreach($output as $seperatevalue){
echo $seperatevalue.",";
}

}else if($selected_radio =='Dcsen'){
$output = bubble_sort($arr1);
echo "Descending Order : ";
$SortDesc=rsort($output);
foreach($output as $seperatevalue){
echo $seperatevalue.",";
}
}

}

?>


</body>
</html>


2) to retrieve perfect square

<html>
<head><title>Exam 2</title></head>
<script type="text/javascript">

</script>
<body>
<form method="POST" action="" name="Exam2Form">
<table>
<tr><td>Enter Your Number</td><td><input type="text" name="Numberval" value=""></td>
<td><input type="submit" name="EnterBtn"></td></tr>
</table>
</form>

<?php
if(isset($_POST['EnterBtn'])){

$valueTxt=$_POST['Numberval'];

for($x=1;$x<$valueTxt;$x++){

$remnderInt=$x%4;

if($remnderInt==0){
echo $x.",";
}
}
echo "<br/>";

$isSquare=gmp_perfect_square($valueTxt);
if($isSquare){
echo $valueTxt. "is a perfect number";
}
echo "<br/>";
//.................................................................................
$CheckSqrt=sqrt($valueTxt);

for($i=0;$i<$valueTxt;$i++){
$sqrt_int=$i*$i;
if($sqrt_int==$CheckSqrt*$CheckSqrt){
echo $valueTxt. "is a perfect number is sqrt";
}
}
//.................................................................................
}
?>

</body>
</html>

3) to retrieve date difference

<html>
<head><title>Exam 2</title></head>
<script type="text/javascript">

</script>
<body>
<form method="POST" action="" name="Exam2Form">
<table>
<tr><td>year First</td><td><input type="text" name="y1" value=""></td><td>Month</td><td><input type="text" name="m1" value=""></td><td>Date</td><td><input type="text" name="d1" value=""></td></tr>
<tr><td>year Second</td><td><input type="text" name="y2" value=""></td><td>Month</td><td><input type="text" name="m2" value=""></td><td>Date</td><td><input type="text" name="d2" value=""></td></tr>

<tr><td><input type="submit" name="EnterBtn"></td><td></td><td></td><td></td><td></td></tr>
</table>
</form>

<?php
if(isset($_POST['EnterBtn'])){

$valD1=$_POST['y1'] . '-' . $_POST['m1']. '-' .$_POST['d1'];
$valD2=$_POST['y2'] . '-' . $_POST['m2']. '-' .$_POST['d2'];



$date1 = new DateTime($valD1);
$date2 = new DateTime($valD2);


$diff12 = date_diff($date2, $date1);

$interval = $date1->diff($date2);


echo "difference " . $interval->y . " years, " . $interval->m." months, ".$interval->d." days ";
echo "difference " . $interval->days . " days ";
//.............................................................................................//


//accesing days
$days = $diff12->d;
//accesing months
$months = $diff12->m;
//accesing years
$years = $diff12->y;

echo $days ;


}
?>

</body>
</html>


4)Student data system

<html>
<head><title>Student rating System</title>
<script type="text/javascript">
function validateForm(){
var fname=document.getElementById('fname').value;
var gender=document.getElementById.
}
</script>
</head>
<body>

<div><h1>Welcome to Student mark system</h1></div>

<p>Enter student details</p>

<form method="Post" action="" name="Myform" onsubmit="validateForm();">
<table>

<tr><td>Index Number</td><td><input type="text" name="index"/></td><td></td><td></td></tr>

<tr><td>Destination</td><td>
<select name="Dest">
<option value="0">Select</option>
<option value="Mr">Mr</option>
<option value="Mrs">Mrs</option>
</select>
</td><td></td><td></td></tr>


<tr><td>First Name</td><td><input type="text" id="fname" name="fname"/></td><td></td><td></td></tr>

<tr><td>Gender</td><td><input type="Radio"  value="Male" name="gender"/>Male</td><td><input type="Radio" value="Female" name="gender"/>Female</td><td></td></tr>

<tr><td>Maths</td><td><input type="text" name="maths"/></td> <td>Science</td><td><input type="text" name="science"/></td></tr>

<tr><td><input type="submit" name="subClick" value="Submit"/></td><td></td><td></td><td></td></tr>

</table>
</form>
<form method="post">
<table>

<tr><td>Index Number To search</td><td><input type="text" name="SearchIndex"/></td><td><input type="submit" name="subsearch" value="SubmitSearch"/></td></tr>
<tr><td>Index Number To Delete</td><td><input type="text" name="DeleteIndex"/></td><td><input type="submit" name="subdel" value="SubmitDelete"/></td></tr>
</table>
</form>
<a href="update.php">Click here to update</a>
<br/>
<br/>
<h1>Rating System</h1>

<form method="Post" action="">
<table>
<tr><td><select name="AvMark">
<option value="50">50</option>
<option value="70">70</option>
<option value="90">90</option>
</select></td><td><input type="submit" name="subMax" value="Grade A"/></td><td>Average Marks Over </td></tr>

<tr><td></td><td><input type="submit" name="bestMax" value="Best 5"/></td><td></td></tr>


</table>
</form>

<?php

//connect to database........................................................................
$link = mysqli_connect('localhost', 'root', '', 'test');
if(isset($_POST['subClick'])){

// $con = mysql_connect("localhost","root", "") or die('Cannot connect to the database');
// mysql_select_db("test",$con) or die('cannot select the  database');

$dest=$_POST['Dest'];
$index=$_POST['index'];
$fname=$_POST['fname'];
$gender=$_POST['gender'];
$maths=$_POST['maths'];
$science=$_POST['science'];
$total=$maths+$science;
$average=$total/2;

//insert data..................................................................................

$query = "insert into student (ID,Destination,Firstname,Gender,Maths,Science,Total,Average) values($index,'$dest','$fname','$gender',$maths,$science,$total,$average) ";
$results = mysqli_query($link, $query);
mysqli_query($link,"insert into student (ID,Destination,Firstname,Gender,Maths,Science,Total,Average) values($index,$dest,$fname,$gender,$maths,$science,$total,$average)");
}

//search data...................................................................................

if(isset($_POST['subsearch'])){
$searchtxt=$_POST['SearchIndex'];

$result = mysqli_query($link,"select * from student where ID=$searchtxt ");

echo "<table>";
echo "<tr><td>".'Index'."</td><td>".'First Name'."</td><td>".'Total'."</td><td>".'Average'."</td></</tr>";
while ($row = mysqli_fetch_array($result)) {
   
echo "<tr>";
    echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['Firstname'] . "</td>";
echo "<td>" . $row['Total'] . "</td>";
echo "<td>" . $row['Average'] . "</td>";
    echo "</tr>";
}
echo "</table>";


//delete data.....................................................................................
if(isset($_POST['subdel'])){
$result = mysqli_query($link,"delete  from student where ID=$deltxt ");
echo $result;
}
}

//search for ratng.....................................................................

if(isset($_POST['subMax'])){
$searchtxt=$_POST['AvMark'];

$result = mysqli_query($link,"select * from student where Average>$searchtxt ");

echo "<table>";
echo "<tr><td>".'Index'."</td><td>".'First Name'."</td><td>".'Total'."</td><td>".'Average'."</td></</tr>";
while ($row = mysqli_fetch_array($result)) {
   
echo "<tr>";
    echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['Firstname'] . "</td>";
echo "<td>" . $row['Total'] . "</td>";
echo "<td>" . $row['Average'] . "</td>";
    echo "</tr>";
}
echo "</table>";
}

//................best Max.............................


if(isset($_POST['bestMax'])){
$query="select * from student order by Total Asc Limit 3 ";
$results = mysqli_query($link, $query1);


echo "Best Three Positions";
while ($row = mysqli_fetch_array($results1)) {

echo $row['Firstname'];
echo "<br/>";
}




}
?>



</body>
</html>
.......................................To update...............................

<html>
<head></head>
<body>
<?php
//database connection info
$host = "localhost"; //server
$db = "test"; //database name
$user = "root" ;//dabases user name
$pwd = ""; //password

//connecting to server and creating link to database
$link = mysqli_connect($host, $user, $pwd, $db);

$indexU="";
$fnameU="";
$mathsU="";
$scienceU="";

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

//search text and insert it to form.............................................................................

$searchtxt=$_POST['index'];
$result = mysqli_query($link,"select * from student where ID=$searchtxt ");

while ($row = mysqli_fetch_array($result)) {
$indexU=$row['ID'];
$fnameU=$row['Firstname'];
$mathsU=$row['Maths'];
$scienceU=$row['Science'];
}
//update values......................................................................................................

if(isset($_POST['subupd'])){
$updFname=$_POST['fname'];
$updmaths=$_POST['maths'];
$updsci=$_POST['science'];
$searchtxt=$_POST['index'];
$result = mysqli_query($link,"UPDATE student SET  Firstname='$updFname' , Maths=$updmaths , Science=$updsci where ID=$searchtxt ");
echo var_dump($result);
}

?>
<form method="Post" action="" name="Myform1">
<table>


<tr><td>Index Number</td><td><input type="text" value="<?php echo $indexU; ?>" name="index"/></td><td><input type="submit" name="subClick" value="Submit"/></td><td></td></tr>

<tr><td>First Name</td><td><input type="text" value="<?php echo $fnameU; ?>" name="fname"/></td><td></td><td></td></tr>

<tr><td>Maths</td><td><input type="text" value="<?php echo $mathsU; ?>" name="maths"/></td><td></td><td></td></tr>

<tr><td>Science</td><td><input type="text" value="<?php echo $scienceU; ?>" name="science"/></td><td></td><td></td></tr>

<tr><td><input type="submit" name="subupd" value="Update"/></td><td></td><td></td></tr>

</table>
</form>

</body>