0% found this document useful (0 votes)
73 views15 pages

3739 Assignment 2

The document is an assignment submission for an Internet and Web Applications course. It contains the student's answers to 10 questions related to PHP scripting. The questions cover topics like arrays, string manipulation, sorting arrays, and form validation with data insertion into a database. The student provides the PHP code scripts for each question along with sample inputs and outputs.

Uploaded by

AYUSHI ASTHANA
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
73 views15 pages

3739 Assignment 2

The document is an assignment submission for an Internet and Web Applications course. It contains the student's answers to 10 questions related to PHP scripting. The questions cover topics like arrays, string manipulation, sorting arrays, and form validation with data insertion into a database. The student provides the PHP code scripts for each question along with sample inputs and outputs.

Uploaded by

AYUSHI ASTHANA
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 15

ASSIGNMENT NO-2

[Internet and Web Application]

SUBMITTED BY- SUBMITTED TO-

Ayushi Asthana PROFESSOR ADITI PAUL

ROLL NO-(3739)
QUESTION 1) $color = array('white', 'green', 'red', 'blue', 'black');
Write a script which will display the following string - 
"The memory of that scene for me is like a frame of film forever frozen at that moment: the red
carpet, the green lawn, the white house, the leaden sky. The new president and his first lady. -
Richard M. Nixon"
and the words 'red', 'green' and 'white' will come from $color.

Answer 1)

INPUT:

<?php

$color=array('white','green','red','blue','black');

echo "The memory of that scene for me is like a frame of film forever frozen at that moment: the
$color[2] carpet, the $color[1] lawn, the $color[0] house, the leaden sky. The new president and his
first lady.-Richard M. Nixon”. “\n”

?>

OUTPUT:

The memory of that scene for me is like a frame of film forever frozen at that moment: the red
carpet, the green lawn, the white house, the leaden sky. The new president and his first lady. -
Richard M. Nixon

QUESTION 2) $color = array(4 => 'white', 6 => 'green', 11=> 'red');


Write a PHP script to get the first element of the above array. 

Answer 2)

INPUT:

<?php

$color = array(4 => 'white', 6 => 'green', 11=> 'red');

echo reset($color)."\n";

?>

OUTPUT: white

QUESTION 3) Write a PHP function to compare two multidimensional arrays and returns the
difference.

ANSWER 3)

INPUT:

<?php

function key_compare($a,$b)
{

if($a===$b)

return 0;

return($a>$b)?1:-1;

function multidimenssional_array_diff($arr1, $arr2)

return array_diff_uassoc($arr1['c'], $arr2['c'],"key_compare");

//multidimenssional arrays

$color1=array('a'=>'White','b'=>'Red','c'=>array('a'=>'Green','b'=>'Blue','c'=>'Yellow'));

$color2=array('a'=>'White','b'=>'Red','c'=>array('a'=>'White','b'=>'Red','c'=>'Yellow'));

print_r(multidimenssional_array_diff($color1, $color2));

?>

OUTPUT:

Array ( [a] => Green [b] => Blue )

QUESTION 4)Write a PHP script to remove all white spaces in an array.

ANSWER 4)

INPUT:

<?php

$my_array = array(15, null, " ", -2, NULL, "", " \n", "Red", 54, "\t");

print_r($my_array);

$result = array_filter($my_array, create_function('$x','return preg_match("#\S#", $x);'));

print_r($result);

?>

OUTPUT:

Array ( [0] => 15 [1] => [2] => [3] => -2 [4] => [5] => [6] => [7] => Red [8] => 54 [9] => ) Array ( [0] =>
15 [3] => -2 [7] => Red [8] => 54 )

QUESTION 5)Write a PHP program to filter out some elements with certain key-names.
Test Data :
1st array : ('c1' => 'Red', 'c2' => 'Green', 'c3' => 'White', c4 => 'Black')
2nd array : ('c2', 'c4')
Output :
Array
(
[c1] => Red
[c3] => White
)

INPUT:

<?php

$first_array = array('c1' => 'Red', 'c2' => 'Green', 'c3' => 'White', 'c4' => 'Black');

$second_array = array('c2', 'c4');

$result = array_diff_key( $first_array, array_flip( $second_array));

print_r($result);

?>

OUTPUT:

Array ( [c1] => Red [c3] => White )

QUESTION 6)Write a PHP function to sort an array according to another array acting as a priority list.

ANSWER 6)

INPUT:

<?php

function list_cmp($a, $b)

global $order;

foreach($order as $key => $value)

if($a==$value)

return 0;

break;

if($b==$value)

{
return 1;

break;

$order[0] = 1;

$order[1] = 3;

$order[2] = 4;

$order[3] = 2;

$array[0] = 2;

$array[1] = 1;

$array[2] = 3;

$array[3] = 4;

$array[4] = 2;

$array[5] = 1;

$array[6] = 2;

usort($array, "list_cmp");

print_r($array);

?>

OUTPUT:

Array ( [0] => 1 [1] => 1 [2] => 3 [3] => 4 [4] => 2 [5] => 2 [6] => 2 )

QUESTION 7)Write a PHP script to get the shortest/longest string length from an array. 
Sample arrays : ("abcd","abc","de","hjjj","g","wer")

ANSWER 7)

INPUT:

<?php

$my_array = array("abcd","abc","de","hjjj","g","wer");
$new_array = array_map('strlen', $my_array);

// Show maximum and minimum string length using max() function and min() function

echo "The shortest array length is " . min($new_array) .

". The longest array length is " . max($new_array).'.';

?>

OUTPUT:

The shortest array length is 1. The longest array length is 4.

QUESTION 8) Write a PHP script which displays all the numbers between 200 and 250 that are
divisible by 4.

ANSWER 8)

INPUT:

<?php

echo implode(",",range(200,250,4))."\n";

?>

OUTPUT:

200,204,208,212,216,220,224,228,232,236,240,244,248

QUESTION 9)Write a PHP script to calculate and display average temperature, five lowest and
highest temperatures.Recorded temperatures : 78, 60, 62, 68, 71, 68, 73, 85, 66, 64, 76, 63, 75, 76,
73, 68, 62, 73, 72, 65, 74, 62, 62, 65, 64, 68, 73, 75, 79, 73

ANSWER 9)

INPUT:

<?php

$month_temp = "78, 60, 62, 68, 71, 68, 73, 85, 66, 64, 76, 63, 81, 76, 73,

68, 72, 73, 75, 65, 74, 63, 67, 65, 64, 68, 73, 75, 79, 73";

$temp_array = explode(',', $month_temp);

$tot_temp = 0;

$temp_array_length = count($temp_array);

foreach($temp_array as $temp)

$tot_temp += $temp;

$avg_high_temp = $tot_temp/$temp_array_length;
echo "Average Temperature is : ".$avg_high_temp."

";

sort($temp_array);

echo " List of five lowest temperatures :";

for ($i=0; $i< 5; $i++)

echo $temp_array[$i].", ";

echo "List of five highest temperatures :";

for ($i=($temp_array_length-5); $i< ($temp_array_length); $i++)

echo $temp_array[$i].", ";

?>

OUTPUT:

Average Temperature is : 70.6 List of five lowest temperatures : 60, 62, 63, 63, 64, List of five highest
temperatures : 76, 78, 79, 81, 85,

QUESTION 10) Write a programme in html having a form with Name (firstname+lastname) and
submit button. On clicking button form validation should work (use JavaScript) and data entry should
be saved in database (php code needed for data insertion) form be styled using CSS (by inline,
external or internal mode)

ANSWER 10)

INPUT: In file insert.php-

<?php

include("config.php");

$result=mysqli_query($mysqli,"SELECT* from record ORDER by date DESC");

?>

<!DOCTYPE html>

<html>

<head>

<title></title>
</head>

<body>

<form action="function.php" method="POST">

name<input type="text" name="name"><br>

Email<input type="email" name="email"><br>

Mobile<input type="mobile" name="mobile"><br>

Date<input type="date" name="date"><br>

<input type="submit" name="submit"s>

</form>

<table border="9">

<tr>

<th>Name</th>

<th>Email</th>

<th>Mobile</th>

<th>Date</th>

</tr>

<?php

while($res=mysqli_fetch_array($result)){

echo '<tr>';

echo '<td>'.$res['name'].'</td>';

echo '<td>'.$res['email'].'</td>';

echo '<td>'.$res['mobile'].'</td>';

echo '<td>'.$res['date'].'</td>';

echo '</tr> ';

?>

</table>

</body>
</body>

</html>

In file function.php-

INPUT:

<?php

include("config.php");

if(isset($_POST['submit']))

$name=$_POST['name'];

$email=$_POST['email'];

$mobile=$_POST['mobile'];

$date=$_POST['date'];

$result=mysqli_query($mysqli,"INSERT into record


values('','$name','$email','$mobile','$date')");

if($result)

header("location:insert.php");

else{

echo "failed";

?>
In file config.php-

INPUT:

<?php

$dbhost='localhost';

$dbname='cs tutorial';

$dbusername='root';

$dbpass='';

$mysqli=mysqli_connect($dbhost,$dbusername,$dbpass,$dbname);

?>

SOME OUTPUTS-

1]-Name
Email
Mobile
Date
Submit

2]-

Name Email Mobile Date

neelima neelimaagarwal@gmail.com 76533778090 2022-04-30

ram ramast@gmail.com 786435780887 2022-04-28

cvbb dgfhgf@gmail.com 0899787666 2022-04-27

neerajag neeraj@gmail.com 9567876789 2022-04-20

sdf sdgfygu@gmail.com 9796645768 2022-04-17

madhvi rma@gmail.com 8978535678 2022-04-10

reeta reeta@gmail.com 8987786646 2022-04-09

tdynew wbmba21636_ayushi@banasthali.in 3367889088 2022-04-07

ayus asthanaayushi8@gmail.com 90876544687 2022-04-05

dfgn fxo0u_xlh@mail.mjj.edu.ge 1234567899 2022-04-04


bittu asdhgjhkiju@gmail.com 1234567899 2022-04-03

pooja pooja.ast@gmail.com 87654266797 2022-04-02

pooja pooja.ast@gmail.com 8976543235 2022-04-01

kirti kirtichaudhary@gmail.com 89764327809 2022-04-01

You might also like