11. Write a PHP program to create a web form that contains a textbox for names, and checkboxes for hobbies. When a form is submitted, check a name is entered, and at least one of the hobbies is selected.

<!doctype html>
<html>
	<head>
        <title>Year : 2016 | Q : 11</title>
    </head>    
    <body>
		<form method = 'post'>
        	Name: <input type = 'text' name = 'nm'><br>
			Hobbies: h1<input type = 'checkbox' name = 'h1' value = 'h1'> h2<input type = 'checkbox' name = 'h2' value = 'h2'><br>
			<input type = 'submit' name = 'sub' value = 'Submit'>
        </form>	 
    	<?php
			if(isset($_POST['sub']))
			{
				$nm = $_POST['nm'];
				$h1 = '';
				$h2 = '';
				if(isset($_POST['h1']))
					$h1 = $_POST['h1'];	
				if(isset($_POST['h2']))
					$h2 = $_POST['h2'];
				if($nm!=''&&($h1!='' || $h2!=''))
					echo "Affirmative.";
				else
					echo "Name field should not be empty, and atleast one hobby should be selected.";	
			}
		?>	
    </body>
</html>

12. Write a PHP program that displays the contents of the database. Make your own assumptions about the database.

<!doctype html>
<html>
	<head>
        <title>Year : 2016 | Q : 12</title>
    </head>    
    <body>
		<?php
			/*
				Database name: 'test'
				Table name: 'info'
				Column names: 'ID', 'Name'
			*/
			$C = mysqli_connect('localhost','root','','test') or die('Error connecting with the database');
			$code = 'SELECT * FROM info';
			$R = mysqli_query($C,$code) or die('Error in code');
			echo "<table border = '1' cellpadding = '15' cellspacing = '0'>";
			echo "<tr> <th>ID</th> <th>Name</th> </tr>";
			while($res = mysqli_fetch_array($R))
			{
				echo "<tr>"; 
				echo " <td>".$res[0]."</td>";
				echo " <td>".$res[1]."</td>";
				echo "</tr>";	
			}
			echo "</table>";
		?>
    </body>
</html>

13. Write a PHP function that takes three arguments: principle, rate, and time and returns the calculated interest. Make all the required validation to calculate the interest.

<!doctype html>
<html>
	<head>
        <title>Year : 2016 | Q : 13</title>
    </head>    
    <body>
        <form method = 'post'>
        	Principle: <input type = 'text' name = 'pri'><br>
            Rate: <input type = 'text' name = 'rate'><br>
            Time: <input type = 'text' name = 'time'><br>
            <input type = 'submit' name = 'cal' value = 'Calculate S.I.'>
        </form>
        <?php
			function si($p,$t,$r)
			{
				return number_format((($p*$t*$r)/100),2);
			}
			if(isset($_POST['cal']))
			{
				$p = $_POST['pri'];
				$r = $_POST['rate'];
				$t = $_POST['time'];
				if(is_numeric($p)&&is_numeric($r)&&is_numeric($t))
					echo "Simple Interest: ".si($p,$t,$r);	
				else
					echo "Input valid data";
			}
		?>
    </body>
</html>

14. Write a program to write value taken from user to a file.

<!doctype html>
<html>
	<head>
        <title>Year : 2016 | Q : 14</title>
    </head>    
    <body>
        <form method = 'post'>
        	<input type = 'text' name = 'inp' placeholder = 'Enter text here' size = '50'>
            <input type = 'submit' name = 'add' value = 'Write to file'>
        </form>
        <?php
			if(isset($_POST['add']))
			{
				$txt = $_POST['inp'];
				$f = fopen('Test.txt','w');
				if(fwrite($f,$txt))
					echo "Text written into the file named 'Test.txt'.";
				else
					echo "Error writing into the file.";
				fclose($f);	
			}
		?>
    </body>
</html>

15. Write a PHP program to create a multidimensional array that holds the cities of districts in Kathmandu valley such as Kathmandu(Newroad, Durbar Marg, Thamel), Lalitpur(Patan, Jawlakhel, Kupndole), Bhaktapur(Durbar Square, Suryabinayak). Then display the contents of the array.

<!doctype html>
<html>
	<head>
        <title>Year : 2016 | Q : 15</title>
    </head>    
    <body>
        <?php
			$arr = array(
					'Kathmandu' => array('Newroad','Durbar Marg','Thamel'),
					'Lalitpur' => array('Patan','Jawlakhel','Kupndole'),
					'Bhaktapur' => array('Durbar Square','Suryabinayak')
				   );
			foreach($arr as $dis => $city)
			{
				echo "District: ".$dis."<br>";	
				foreach($city as $val)
					echo "&nbsp;&nbsp;&nbsp;City: ".$val."<br>";
				echo "<br>";	
			}
		?>
    </body>
</html>

Leave a Reply

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