Tag: PHP
PHP || College Papers solutions
ASain 2073 1. What are the different ways of writing comments in PHP →// and /* are the different ways of writing comments in PHP 2. Write the function to convert given array to string. Give examples → mysql_fetch_array() is the function to convert given array to string 3. How do you connect to database […]
Continue ReadingPHP || old paper solutions 2015
year 2015 Group B 11. Write a program in PHP to add three numbers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<body> <form action="" method="post"> <label>First Number</label> <input type="number" name="first_num" /> <br /> <label>Second Number</label> <input type="number" name="second_num" /> <br /> <label>Third Number</label> <input type="number" name="third_num" /> <input type="submit" name="" value="Submit" /> </form> </body> <?php if($_POST){ $first_num = $_POST['first_num']; $second_num = $_POST['second_num']; $third_num = $_POST['third_num']; $sum = $first_num + $second_num + $third_num; echo 'Sum: '.$sum; } ?> |
12. Write a PHP program to display first and last character of a string.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<body> <form action="" method="post"> <label>Enter a string</label> <input type="text" name="text" /> <input type="submit" name="" value="submit" /> </form> </body> <?php if($_POST){ $string = trim($_POST['text']); $length = strlen($string); for($i = ($length - 1); $i >= 0; $i--){ if($i == ($length-1)){ echo 'Last character is: '.$string[$i]; } if($i == 0){ echo '<br />First character is: '.$string[$i]; } } } ?> |
13. Create two integer array of size 5 each with data item in them. Then display sum of all the number of both […]
Continue ReadingPHP || old paper solution 2017
Group A 1. Register_global variablesis an internal PHP setting which registers the $_REQUEST array’s elements as variables. If you submit a value in a form, via POST or GET, the value of that input will automatically be accessible via variable in the PHP script, named after the name of the input field. 2. To create […]
Continue ReadingPHP || old paper solution ||2018
2018 Q.No 11
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<!DOCTYPE html> <html> <head> <title> Sum of two numbers </title> </head> <body> <form name="My form" method="POST" action=""> <fieldset> <legend>Sum of Two numbers</legend> <label>First Number: <input type="number" name="n1" placeholder="Enter first number" required=""> </label> <br> <br> <label>Second Number: <input type="number" name="n2" placeholder="Enter second number"> </label> <br> <br> <input type="reset" name="reset" value="Reset"> <input type="submit" name="btn" value="SUM"> </fieldset> </form> <!--php code for sum of two numbers--> <?php if(isset($_POST['btn'])) { $firstnum = $_POST['n1']; $secondnum = $_POST['n2']; $sum = $firstnum + $secondnum; echo "The sum of two given number is ".$sum; } ?> </body> </html> |
1 |
1 |
1 |
Q.No 12
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<!DOCTYPE html> <html> <head> <title>Associative Array</title> </head> <body> <?php $arr = array( 'a' => 'Bachelor In Information Management', 'b' => 'Bachelor In Business Administration', 'c' => 'Bachelor In Business Studies' ); echo"<h3> Different Faculties </h3>"; foreach($arr as $key => $v1 ){ echo "$key. $v1 <br>"; } ?> </body> </html> |
Q.No 13
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
<!DOCTYPE html> <html> <head> <title> PHP program to find the difference between two dates </title> </head> <body> <form name="myform" method="POST" action=""> <fieldset> <legend>Difference Between Two Dates</legend> <label>Fisrt Date: <input type="date" name="d1" required=""> </label> <br> <br> <label>Second Date: <input type="date" name="d2" required=""> </label> <br> <br> <input type="reset" name="reset" value="Reset"> <input type="submit" name="btn" value="Difference"> </fieldset> </form> <!--php code--> <?php if(isset($_POST['btn'])) { $d1 = $_POST['d1']; $d2 = $_POST['d2']; $d3 = strtotime($d1); $d4 = strtotime($d2); $diff = abs($d3 - $d4); $years = floor($diff/(365*60*60*24)); echo "The difference between two dates is :".$years; } ?> </body> </html> |
1 |
1 |
1 |
Q.No 14
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<!DOCTYPE html> <html> <head> <title>Display data from database</title> </head> <body> <?php /*Assume Database Name:'test' Table Name:'info' Column Name:'name','address' */ //connecting database $con = mysqli_connect('localhost','root','','test') or die('Error connecting to database'); $code = 'SELECT * FROM info'; $r = mysqli_query($con,$code) or die('Error in code'); echo "<table border='1' style='border-collapse:collapse';>"; echo "<tr>"; echo "<td align='center'>Name:</td>"; echo "<td align='center'>Address:</td>"; echo "</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> |
Q.No 15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
<!DOCTYPE html> <html> <head> <title> Copying of one text file to another </title> </head> <body> <!--php code--> <?php //copying context to 'text1.txt' to 'text2.txt'; //copy function: copy('text1.txt','text2.txt'); $f1 = fopen('text1.txt','r'); $f2 = fopen('text2.txt','w'); while(!feof($f1)) { if(!fwrite($f2,fgets($f1))) die("Error copying"); } echo "Copied"; fclose($f1); fclose($f2); ?> </body> </html><!DOCTYPE html> <html> <head> <title>Display data from database</title> </head> <body> <?php /*Assume Database Name:'test' Table Name:'info' Column Name:'name','address' */ //connecting database $con = mysqli_connect('localhost','root','','test') or die('Error connecting to database'); $code = 'SELECT * FROM info'; $r = mysqli_query($con,$code) or die('Error in code'); echo "<table border='1' style='border-collapse:collapse';>"; echo "<tr>"; echo "<td align='center'>Name:</td>"; echo "<td align='center'>Address:</td>"; echo "</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> |
PHP || 2019 Old Paper Solutions
Q.No 11
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<!DOCTYPE html> <html> <head> <title></title> </head> <body> <?php function isInList($listOfCountries, $searchValue){ $countries= $listOfCountries; $value= $searchValue; if(in_array($value, $countries)){ echo $value ."is in the list"; } } $countryList= array( "Nepal", "India", "China", "Bangladesh", "Bhutan", "Maldives", "Germany"); isInList($countryList, "Nepal"); ?> </body> </html> |
Q.No 12
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<!DOCTYPE html> <html> <head> <title>Insert CSV file</title> </head> <body> <?php $db = mysqli_connect('localhost','root','','insertcsv'); if(!$db){ echo"failed to connect to MySQL"; exit(); } if(($handle = fopen("item.csv","r"))!==FALSE){ while(($row = fgetcsv($handle))!==FALSE){ $query = "INSERT INTO items(description,quality,rate)VALUES('$row[1]','$row[2],'$row[3]')"; if(mysqli_query($db,$query)){ echo "new records created successfully"; } else{ echo "ERROR:".$query."<br>".mysqli_error($db); } } } |
Q.No 13
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
<?php function displayCalendar($month, $year) { /* draw table */ $calendar = '<table border = 1>'; /* table headings */ $headings = array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'); $calendar .= '<tr><td>' . implode('</td><td>', $headings) . '</td></tr>'; /* days and weeks ... */ $running_day = date('w', mktime(0, 0, 0, $month, 1, $year)); $days_in_month = date('t', mktime(0, 0, 0, $month, 1, $year)); $days_in_this_week = 1; // checks how many days are in this week and how many days has been printed $day_counter = 0; /* row for week one */ $calendar .= '<tr>'; /* print "blank" days until the first of the current week */ for ($x = 0; $x < $running_day; $x++) { $calendar .= '<td> </td>'; $days_in_this_week++; } /* keep going with days.... */ for ($list_day = 1; $list_day <= $days_in_month; $list_day++) { $calendar .= '<td>'; /* add in the day number */ $calendar .= $list_day; $calendar .= '</td>'; if ($running_day == 6) { $calendar .= '</tr>'; //if month is running then tr should be opened if (($day_counter + 1) != $days_in_month) { $calendar .= '<tr>'; } $running_day = -1; //running day starts from 0 and end at 6 $days_in_this_week = 0; //afer last day of the week counter has been reset } $days_in_this_week++; $running_day++; $day_counter++; } /* finish the rest of the days in the week */ if ($days_in_this_week < 8 && $days_in_this_week != 1) { for ($x = 1; $x <= (8 - $days_in_this_week); $x++) { $calendar .= '<td> </td>'; } } /* final row */ $calendar .= '</tr>'; /* end the table */ $calendar .= '</table>'; /* all done, return result */ return $calendar; } /* sample usages */ echo '<h2>July 2021</h2>'; echo displayCalendar(7, 2021); echo '<h2>August 2021</h2>'; echo displayCalendar(8, 2021); echo '<h2>September 2021</h2>'; echo displayCalendar(9, 2021); |
Q.No 14
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php function check($username,$password){ $conn = mysqli_connect("localhost","root","","login") or die(); $sql = "select * from user where uname='$username'"; $result = mysqli_query($conn,$sql); $row = mysqli_num_rows($result); if($row>0){ $rows = mysqli_fetch_assoc($result); if($password==$rows['upass']){ echo "Welcome User ".$rows['uname']; } }else{ header("location:index.php"); } } check("ram sharma","ram123"); ?> |
Q.No 15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
<!DOCTYPE html> <html lang="en"> <head> <title>TU Question No.15, 2019</title> </head> <body> <form action="" method="post"> <input type="text" name="name" placeholder="Enter Name" required=""/><br/> <input type="number" name="age" placeholder="Enter Age" required=""/><br/> <input type="checkbox" name="hobbies[]" value="reading"/>Reading <input type="checkbox" name="hobbies[]" value="playing"/>Playing <input type="checkbox" name="hobbies[]" value="singing"/>Singing<br/> <textarea name = "comment" rows="4" cols="30" placeholder="comment here..." required=""> </textarea><br/> <input type="Submit" name="submit" value="Validate"> </form> <?php if(isset($_POST['submit'])){ $name = $_POST['name']; $age = $_POST['age']; $hobbies = $_POST['hobbies']; $hobbylength = count($hobbies); //validation $nameArray = str_split($name); //convert string into array $nameLength = strlen($name); //name if($nameLength<2 && $nameLength>11){ echo "name length shoud be between 2-10"; } //checking for numbers in strings foreach($nameArray as $key){ if(is_numeric($key)){ echo "character as number not allowed in name"; } } //age if($age<16 && $age>56){ echo "Age should be between 16-55"; } //hobbies should be selected if($hobbylength==0){ echo "one hobby should be selected"; } } ?> </body> </html> |
Continue Reading
PHP Pre Board solution
Asian School of Management and Technology Group A Write some words about php? PHP: Hypertext preprocessor is a widely used open source general purpose scripting language that is especially suited for web development and can be embedded into HTML. How can you differentiate static and dynamic website? Static Website Dynamic Website Content is fixed […]
Continue ReadingChapter 2. Working with Text and Numbers
Data Types Data Types defines the type of data a variable can store. PHP allows eight different types of data types. String :- A string is a sequence of characters, like “Hello world!”. Integer :- An integer data type is a non-decimal number between -2,147,483,648 and 2,147,483,647. Float (floating point numbers – also called […]
Continue ReadingUnit 1: Orientation and First Steps
Definition :- PHP stands for PHP: Hypertext Preprocessor . PHP is a server-side scripting language, like ASP . PHP scripts are executed on the server . PHP supports many databases (MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc.) PHP is an open source software . PHP is free to download and use. 1.1 PHP’s […]
Continue ReadingUnit 9: Handling Dates and Times
The PHP Date() Function The PHP date() function convert a timestamp to a more readable date and time. The computer stores dates and times in a format called UNIX Timestamp, which measures time as a number of seconds since the beginning of the Unix epoch (midnight Greenwich Mean Time on January 1, 1970 i.e. January 1, 1970 00:00:00 GMT […]
Continue Reading