11. Write a PHP code to perform server-side validation. (Check for empty, alphabets, numbers, email and password length validation)

<!doctype html>
<html>
    <head>
        <title>Year : 2017 | Q : 11</title>
    </head>
    <body>
 		<?php
			function validate($n,$a,$e,$p)
			{
				//Checking for empty spaces
				if($n==''||$a==''||$e==''||$p==''){
					echo 'Empty spaces not permitted';
					return false;	
				}
				//Checking for numbers
				$regN = "/^[A-Z]+[A-z\s]+[a-z]$/m";
				if(!preg_match($regN,$n))
				{
					echo "Invalid name";
					return false;	
				}
				//Age
				$regA = '/^[\d][\d]{0,2}$/m';
				if(!preg_match($regA,$a))
				{
					echo "Invalid age";
					return false;
				}
				//email
				if(!filter_var($e,FILTER_VALIDATE_EMAIL))
				{
					echo "Invalid email";
					return false;	
				}
				//password
				$regpw = '/.{7,15}/';//7 to 15 characters
				if(!preg_match($regpw,$p))
				{
					echo "Password must be 7 to 15 characters long.";
					return false;	
				}
				return true;
			}
			if(isset($_POST['sub']))
			{
				if(validate($_POST['nm'],$_POST['age'],$_POST['email'],$_POST['pw']))		
					die("Data submitted Succesfully");
			}	
		?>
    	<form method = 'post'>
        	Name : <input type = 'text' name = 'nm'><br>
			Age : <input type = 'text' name = 'age'><br>
			E-mail: <input type = 'text' name = 'email'><br>
			Password: <input type = 'text' name = 'pw'><br>
        	<input type = 'submit' name = 'sub' value = 'Submit data'> 
        </form>
        
    </body>
</html>

12. Write a function to calculate the sum of all even numbers that occur between given two integers passed as parameters.

<!doctype html>
<html>
    <head>
        <title>Year : 2017 | Q : 12</title>
    </head>
    <body>
    	<form method = "post">
 			Number 1: <input type = 'number' name = 'n1' required><br>
			Number 2: <input type = 'number' name = 'n2' required><br>       	
        	<input type = 'submit' name = 'cal' value = 'Calculate the sum of all the even numbers that occurs between them'>
        </form>
		<?php
			function sum($n1,$n2)
			{
				$s = 0;
				$a = ($n1>$n2)?$n1:$n2;
				$b = ($n1<$n2)?$n1:$n2; 
				for($i=$b;$i<=$a;$i++)
				{
					if($i%2==0)
						$s+=$i;	
				}
				return $s;	
			}
			if(isset($_POST['cal']))
			{
				echo "Sum of all the even numbers between the given two integers is: ".sum($_POST['n1'],$_POST['n2']);	
			}
		?>
    </body>
</html>

13. Create an array that contains your name, address, age, gender. Then write the array to the file name “profile.csv”.

<!doctype html>
<html>
    <head>
        <title>Year : 2017 | Q : 13</title>
    </head>
    <body>
		<?php
			$arr = array('ABC','HTD',20,"Male"); //name, address, age, gender
			$f = fopen("profile.csv","w");
			if(fputcsv($f,$arr))
				echo("Data written in file successfully.");
			else
				echo("Error in writing the data.");
			fclose($f);
		?>
    </body>
</html>

14. Create a login system, make your own assumptions about the database, authorize the login credentials.

<!doctype html>
<html>
    <head>
        <title>Year : 2017 | Q : 14</title>
    </head>
    <body>
    	<?php
			/*
				Database name : 'test'
				Table name : 'account'
				Column names : 'username', 'password'
			*/
			if(isset($_POST['log']))
			{
				$C = mysqli_connect('localhost','root','','test') or die("Error while connecting to the database");
				$un = $_POST['un'];
				$pw = $_POST['pw'];	
				$code = "SELECT * FROM account WHERE username = '$un' AND password = '$pw'";
				$R = mysqli_query($C,$code);
				$chk = false;
				while(mysqli_fetch_array($R))
					$chk = true;		
				if($chk)
					die("Welcome ".$un);
				else
					echo "Invalid username or password.";
			}
		?>
		<form method = 'post'>
        	Username: <input type = 'text' name = 'un'><br>
			Password: <input type = 'password' name = 'pw'><br>
			<input type = 'submit' value = 'Login' name = 'log'>
        </form>
    </body>
</html>

15. Write a PHP function to find the difference between two dates in PHP.

www.spastakhabar.com
<!doctype html>
<html>
	<head>
        <title>Year : 2017 | Q : 15</title>
    </head>    
    <body>
		<form method = 'post'>
        	Date 1 : <input type = 'text' name = 'd1' required><br>
			Date 2 : <input type = 'text' name = 'd2' required><br>
			<input type = 'submit' value = 'Calculate difference' name = 'diff'>
        </form>
        <?php
			function diff($d1,$d2)
			{
				$d1 = strtotime($d1);
				$d2 = strtotime($d2);
				$diff = abs($d1-$d2);		
				return floor($diff/(60*60*24));
			}
			if(isset($_POST['diff']))
			{	
				echo "The difference between the given dates is of about ".diff($_POST['d1'],$_POST['d2'])." days.";
			}
		?> 
    </body>
</html>

16. Write down the PHP script to display data from a database table to a web page in the following format. Make your own assumption about the database and table name.

IDProduct nameCategoryBrandSize
     
<!doctype html>
<html>
	<head>
        <title>Year : 2017 | Q : 16</title>
    </head>    
    <body>
		<?php
			/*
				Database name: 'test'
				Table name: 'temp'
				Column names: 'ID', 'Product_name', 'Category', 'Brand', 'Size'
			*/
			$C = mysqli_connect('localhost','root','','test') or die("Error connecting with the database.");			
			$code = "SELECT * FROM temp";
			$R = mysqli_query($C,$code) or die("Error in code");
			echo "<table border = '1' cellpadding = '15' cellspacing = '0'>";
			echo "<tr> <th>ID</th> <th>Product name</th> <th>Category</th> <th>Brand</th> <th>Size</th> </tr>";
			while($res = mysqli_fetch_array($R))
			{
				echo "<tr align = 'center'>";
				echo " <td>".$res[0]."</td> ";
				echo " <td>".$res[1]."</td> ";
				echo " <td>".$res[2]."</td> ";
				echo " <td>".$res[3]."</td> ";
				echo " <td>".$res[4]."</td> ";
				echo "</tr>";
			}
			echo "</table>";
		?> 
    </body>
</html>

17. Create an associative array of size seven, and display the contents in ascending order of the key.

<!doctype html>
<html>
	<head>
        <title>Year : 2017 | Q : 17</title>
    </head>    
    <body>
		<?php
			$arr = array(3=>'3Apple',1=>'1Ball',34=>'5Cat',9=>'4Dog',78=>'7Elephant',44=>'6Fish',2=>'2Ghost');
			echo "<h3>Array:</h3>";
			foreach($arr as $key => $val)
				echo "Key: $key & value: $val<br>";	
			
			ksort($arr);
			/*
			$index;
			$inc = 0;
			foreach($arr as $i => $vi)
				$index[$inc++] = $i; 		
			echo "<br>";
			$i=0;$j=0;
			$n = sizeof($index);
			
			while($i<$n)
			{
				$j = $i+1;
				while($j<$n)
				{
					if($index[$i]>$index[$j])
					{
						$temp = $index[$i];
						$index[$i] = $index[$j];
						$index[$j] = $temp;	
					}
					$j++;	
				}$i++;
			}$i=0;
			
			$temparr = $arr;
			
			echo"<br>";
			foreach($arr as $key => $value)
			{
				$arr[$key] = $temparr[$index[$i]];
				//$arr[$index[$i]] = $arr[$key];
				//unset($arr[$key]); 
				$i++;
			}
			print_r($arr);
			Values are sorted according to the key, but difficulty in sorting the keys.
			*/
			echo "<h3>Sorted array according to key:</h3>";
			foreach($arr as $key => $val)
				echo "Key: $key & value: $val<br>";		
		?>		 
    </body>
</html>

Leave a Reply

Your email address will not be published. Required fields are marked *