PHP || 2019 Old Paper Solutions

4th semester

Q.No 11

<!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

<!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

<?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

<?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

<!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>

 






Leave a Reply

Your email address will not be published.