You are on page 1of 26

WEB PROGRAMMING LABORATORY VVIET.

IV

VIDYA VIKAS INSTITUTE OF ENGINEERING

DEPARTMENT OF MCA
IV SEMESTER MCA (CBCS SCHEME)

ADVANCED WEB PROGRAMMING LAB MANUAL M C A IV SEMESTER

Subject Code: 18MCA42

Prepared By Approved By

Mr.Raghavendra GN Dr.Madhu .B.K


Assistant Professor
Department of MCA Prof & HOD
vviet Department of MCA
Mysore Vviet
Mysore
WEB PROGRAMMING LABORATORY VVIET. IV

List of the Programs:

1. Write a PHP program to process a file which contains English words, where each word is
separated from the next word on a line by one space. The file is specified on the
command line. The output of your program is a table in which the first column has
unique words from the input file and second column has the number of times the word
appeared in the file; no word can appear twice in the table. Use two arrays to store the
table, one for the words and one for the frequency values.

2. A file contains lines of employee’s data, where each line has name; age; department
code; salary. Write a PHP program to generate the following output:

a) The names of the entire employee whose names end with “son”.

b) Percentage of employees under 40 years old.

c) Average salary of employees under 40 years old.

d) An alphabetical list of employees who are under 40 years old and


who have salaries more than $40,000.

3 A. Write a PHP program to store current date-time in a COOKIE and display the Last
visited on date-time on the web page upon reopening of the same page
B. Write a PHP program to store page views count in SESSION, to increment the count on
each refresh, and to show the count on web page
4 Write a PHP script that computes the total cost of the ordered light bulbs from Exercise 9.9
after adding 6.2 percent sales tax. The program must inform the buyer of exactly what was
ordered, in a table
5 Write a PHP program to insert name and age information entered by the user into a table
created using MySQL and to display the current contents of this table.
6 Create an XHTML form with Name, Address Line 1, Address Line 2 and E-mail text
fields. On submitting, store the values in MySQL table. Retrieve and display the data
based on Name.
7 Write a PHP program to read student data from an XML file and store into the MYSQL
database. Retrieve and display.
8 Create an XHTML form with Name, Address Line 1, Address Line 2 and E-mail text fields.
On submitting, store the values in MySQL table. Provide buttons to update and delete data
for the same.
9 Build a Rails application to accept book information viz. Accession number, title, authors,
edition and publisher from a web page and store the information in a database and to search
WEB PROGRAMMING LABORATORY VVIET. IV

for a book with the title specified by the user and to display the search results with proper
headings.

1. Write a PHP program to process a file which contains English words, where each word is
separated from the next word on a line by one space. The file is specified on the command line.
The output of your program is a table in which the first column has unique words from the input
file and second column has the number of times the word appeared in the file; no word can
appear twice in the table. Use two arrays to store the table, one for the words and one for the
frequency values.

<?php
function splitter($str)
{
$freq=array();
$words=preg_split("/[\.,;:!\?\s]\s*/",$str); // Split string by a regular expression
foreach($words as $word)
{
$keys=array_keys($freq);
if(in_array($word,$keys))
$freq[$word]++;
else
$freq[$word]=1;
}
return $freq;
}
$str = "data.txt";

if(file_exists($str)) // Check the existence of file


{
$handle = fopen($str, "r") or die("ERROR: Cannot open the file."); // Open the file for reading

$content = fread($handle,"100");// Read fixed number of bytes from the file

fclose($handle); // Closing the file handle

echo $content; // Display the file content

}
else
{
echo "ERROR: File does not exist.";
}
WEB PROGRAMMING LABORATORY VVIET. IV

$str=$content;
$tbl=splitter($str);
print "<br> word frequency<br>";
$sorted_keys =array_keys($tbl);
sort($sorted_keys);
echo "<table border=1>
<tr><td>Word</td><td>Frequency</td></tr>";
foreach($sorted_keys as $word)

print "<tr><td>$word</td><td> $tbl[$word]</td></tr>";


echo "</table>";
?>

Output:

2. A file contains lines of employees data, where each line has name; age; department code;
salary.

Write a PHP program to generate the following output:

 The names of the entire employee whose names end with “son”.

 Percentage of employees under 40 years old.

 Average salary of employees under 40 years old.


WEB PROGRAMMING LABORATORY VVIET. IV

 An alphabetical list of employees who are under 40 years old and who have salaries
more than $40,000.

Program 1:
<html>

<body bgcolor="blue">

<h2>Enter Employees Data</h2>

<form action="index1.php" method="post">

Name : <input type="text" name="name" /><br><br>


Age : <input type="text" name="age" /><br><br>
Department Code : <input type="text" name="department" /><br><br>
Salary: <input type="text" name="salary" /><br><br>
<input type="submit" value="submit" />

<input type="reset" value="reset" />

</form>
<form action="search.php" method="post"><input type="submit" value="Search" /></form>
</body>

</html>

Program 2:
<?php
$con = mysql_connect("localhost:3307","root","AiiSh#4%my"); if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("college");

$sql="INSERT INTO person (name, addr1, addr2, email)

VALUES ('$_POST[name]','$_POST[age]','$_POST[department]', '$_POST[salary]')";


if (!mysql_query($sql,$con))

die('Error: ' . mysql_error());

}
WEB PROGRAMMING LABORATORY VVIET. IV

echo "1 record added"; mysql_close($con)


?>

Output:

i. Search by Employee names, whose name ends with 'son':


<?php

$con = mysql_connect("localhost:3307","root","AiiSh#4%my"); if (!$con)


{

die('Could not connect: ' . mysql_error());

mysql_select_db("college");
$name=$_POST['name'];
$result = mysql_query("SELECT * FROM person WHERE name LIKE '%$name%' ");
if(!$result)

echo "There is no records";

echo "<table border='1'>


WEB PROGRAMMING LABORATORY VVIET. IV

<tr><th>Name</th><th>Age</th><th>Department Code</th><th>Salary</th></tr>";

while($row = mysql_fetch_array($result))

echo "<tr>";
echo "<td>" .$row['name'] . "</td>";
echo "<td>" .$row['addr1'] . "</td>";
echo "<td>" .$row['addr2'] . "</td>";
echo "<td>" .$row['age'] . "</td>";
echo "</tr>";

echo "</table>"; mysql_close($con);

?>

Output:

II. Percentage of employees under 40 years old:


<?php

$con = mysql_connect("localhost:3307","root","AiiSh#4%my"); if (!$con)


{

die('Could not connect: ' . mysql_error());

mysql_select_db("college");
$percent=$_POST['percent'];
$result = mysql_query("SELECT * FROM person WHERE age<40 ");
if(!$result)

echo "There is no records";

}
$results = mysql_query("SELECT count(*) as name FROM person WHERE age<40 ");
if(!$results)

echo "There is no records";

}
$results1 = mysql_query("SELECT count(*) as name FROM person ");
WEB PROGRAMMING LABORATORY VVIET. IV

if(!$results1)

echo "There is no records";

}
while($row = mysql_fetch_array($results1))

{
$percentage=$row['name'];
while($row = mysql_fetch_array($results))

{
$percent=$row['name'];
$total=($percent/$percentage)*100;

echo "<table border='1'>

<tr><th>Name</th><th>Salary</th><th>Department Code</th><th>Age</th></tr>";

while($row = mysql_fetch_array($result))

echo "<tr>";
echo "<td>" .$row['name'] . "</td>";
echo "<td>" .$row['addr1'] . "</td>";
echo "<td>" .$row['addr2'] . "</td>";
echo "<td>" .$row['age'] . "</td>";
echo "</tr>";

}
}
}

echo "</table><br>";
echo "Total Percentage of employees under 40 years old is:$total";
mysql_close($con);

?>

Output

III. Average salary of employees under 40 years old:


<?php
WEB PROGRAMMING LABORATORY VVIET. IV

$con = mysql_connect("localhost:3307","root","AiiSh#4%my"); if (!$con)


{

die('Could not connect: ' . mysql_error());

mysql_select_db("college");
$percent=$_POST['salary'];
$display = mysql_query("SELECT * FROM person WHERE age<40 ");
if(!$display)
{
echo "There is no records";
}
$result = mysql_query("SELECT count(*) as name FROM person WHERE age<40 ");
if(!$result)

echo "There is no records";

}
$results = mysql_query("SELECT sum(addr1) as addr1 FROM person WHERE age<40 ");
if(!$results)

echo "There is no records";

}
while($row = mysql_fetch_array($results))

{
$sum=$row['addr1'];
while($row = mysql_fetch_array($result))

{
$percent=$row['name'];
$total=($sum/$percent);

echo "<table border='1'>

<tr><th>Name</th><th>Salary</th><th>Department Code</th><th>Age</th></tr>";

while($row = mysql_fetch_array($display))

echo "<tr>";
echo "<td>" .$row['name'] . "</td>";
echo "<td>" .$row['addr1'] . "</td>";
echo "<td>" .$row['addr2'] . "</td>";
echo "<td>" .$row['age'] . "</td>";
echo "</tr>";

}
}
}

echo "</table><br>";
echo "Average salary of employees under 40 years old is:$total";
mysql_close($con);
WEB PROGRAMMING LABORATORY VVIET. IV

?>

output

IV. An alphabetical list of employees who are under 40 years of old & who have salaries
more than 40,000.
<?php

$con = mysql_connect("localhost:3307","root","AiiSh#4%my"); if (!$con)


{

die('Could not connect: ' . mysql_error());

mysql_select_db("college");

$display = mysql_query("SELECT * FROM person WHERE age<40 and addr1>40000 ORDER


BY `person`.`name` ASC ");
if(!$display)

echo "There is no records";

echo "<table border='1'>

<tr><th>Name</th><th>Salary</th><th>Department Code</th><th>Age</th></tr>";

while($row = mysql_fetch_array($display))

{
echo "<tr>";
echo "<td>" .$row['name'] . "</td>";
echo "<td>" .$row['addr1'] . "</td>";
echo "<td>" .$row['addr2'] . "</td>";
echo "<td>" .$row['age'] . "</td>";
echo "</tr>";

}
echo "</table><br>";
mysql_close($con);
WEB PROGRAMMING LABORATORY VVIET. IV

?>

Output

3.a Write a PHP program to store current date-time in a COOKIE and display the Last
visited on date-time on the web page upon reopening of the same page

II. COOKIES.php
<html>
<body bgcolor="aqua" text="red">
<?php
$duration=time()+60*60*24*60;
$found=0;
$visit=0;
if(isset($_COOKIE[$visit]))
{
$found=1;
$lastvisit=$_COOKIE[$visit];
}
setcookie($visit,date("m/d/y-G:i:s"),$duration);
print "<center>";
if($found==1)
{
print "<h2>Welcome back, You have visited on
$lastvisit</h2>";
}
else
{
WEB PROGRAMMING LABORATORY VVIET. IV

print "<h3>Welcome to this website</h3>";


}
print "</center>";
?>
</body>
</html>

OUTPUT: COOKIES.php

3.b Write a PHP program to store page views count in SESSION, to increment the count on
each refresh, and to show the count on web page

III. SESSION.php

<?php

session_start();

print "<center>";

if(!isset($_SESSION))

$_SESSION["count"]=0;

echo "counter initialized \n";

else

{
WEB PROGRAMMING LABORATORY VVIET. IV

$_SESSION["count"]++;

echo "The counter is now <b> $_SESSION[count]</b>" . " <p>


Reload this page to increment </p>";

print "</center>";

?>

OUTPUT:session.php

4. Write a PHP script that computes the total cost of the ordered light bulbs from Exercise 9.9
after adding 6.2 percent sales tax. The program must inform the buyer of exactly what was
ordered, in a table.

Index.html
<html>
<body>
<form name="form_option" action="index1.php" method="POST">
Username: <input type="text" name="user">
<p>Option Check Boxes</p>
<input type="checkbox" name="option1[]" value="2.39">Four 25-watt light bulbs for
$2.39<br>
<input type="checkbox" name="option1[]" value="4.29">Eight 25-watt light bulbs
for $4.29<br>
<input type="checkbox" name="option1[]" value="3.95">Four 25-watt light bulbs for
$3.95<br>
<input type="checkbox" name="option1[]" value="7.49">Eight 25-watt light bulbs
for $7.49
<p>Option Radio Button</p>
<input type="radio" name="option2" value="Visa">Visa<br>
<input type="radio" name="option2" value="Master Card">Master Card<br>
<input type="radio" name="option2" value="Discover">Discover<br>
<input type="submit" value="Submit" name="submit">
</form>
</body>
</html>

Index.php
<?php
// define variables and set to empty values
$name = $email = $gender = $comment = $website = ""; $sum=0;

if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST["user"];
WEB PROGRAMMING LABORATORY VVIET. IV

$radio = $_POST["option2"];

foreach($_POST['option1'] as $option) {

$sum += $option;

}
$percent=$sum+($sum*6.62)/100;
if(isset($_POST['submit'])){

if(!empty($_POST['option1'])) {
echo "Selected bulbs are <br><br>";
foreach($_POST['option1'] as $value){
if($value=='2.39')
{
echo "Four 25-watt light bulbs for $2.39<br><br>";
}
else if($value=='4.29'){
echo " Eight 25-watt light bulbs for $4.29<br><br>";
}
else if($value=='3.95'){
echo " Four 25-watt light bulbs for $3.95<br><br>";
}
else if($value=='7.49'){
echo " Eight 25-watt light bulbs for $7.49<br><br>";
}

echo "Name:".$name."<br><br>Cost:".$sum."<br><br>Mode of Payment card:".$radio;


echo "<br><br>Sales tax after adding 6.62 percent is Cost: $percent";
}

?>
WEB PROGRAMMING LABORATORY VVIET. IV

5. Write a PHP program to insert name and age information entered by the user into a table
created using MySQL and to display the current contents of this table.
CREATE DATABASE IF NOT EXISTS colleges;
CREATE TABLE students(
student_name varchar(255)not null,
student_age varchar(95) not null );

<?php

$connection = mysql_connect("localhost:3307", "root", "AiiSh#4%my");


if (!$connection)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("colleges", $connection); // Selecting Database from Server

if(isset($_POST['submit'])) //Fetching variables of the form which travels in URL


{
$name = $_POST['name'];
$age = $_POST['age'];
$sql="INSERT INTO students VALUES ('$name','$age')";
if (!mysql_query($sql,$connection))
{
die('Error: ' . mysql_error());
WEB PROGRAMMING LABORATORY VVIET. IV

echo "<br/><br/><span>Data Inserted successfully...!!</span>";

$result =mysql_query("SELECT * FROM students where student_name='$name' ");


if(!$result)
{
echo "There is no records";
}
else
{
echo "<table border='1' width='250px'>
<br>
<tr><th>Name</th><th>age</th</tr>";

while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" .$row['student_name'] . "</td>";

echo "<td>" .$row['student_age'] . "</td>";

echo "</tr>";
}
echo "</table>";
}
}
mysql_close($connection); // Closing Connection with Server
?>

6. Create an XHTML form with Name, Address Line 1, Address Line 2 and E-mail text
fields. On submitting, store the values in MySQL table. Retrieve and display the data
based on Name.
html:index.html
WEB PROGRAMMING LABORATORY VVIET. IV

<html>

<body bgcolor="blue">
3
<h2> Form to insert values</h2>

<form action="Lab11.php" method="post">

Name : <input type="text" name="name" /><br><br>

Add1 : <input type="text" name="addr1" /><br><br>

Add2 : <input type="text" name="addr2" /><br><br>

Email: <input type="text" name="email" /><br><br>

<input type="submit" value="submit" />

<input type="reset" value="reset" />

</form>

</body></html>
WEB PROGRAMMING LABORATORY VVIET. IV

Lab6_insert.php

<html>

<body>

<?php

$con =

mysql_connect("localhost:3307","root","AiiSh

#4%my"); if (!$con)

die('Could not connect: ' . mysql_error());

mysql_select_db("college");

$sql="INSERT INTO person (name, addr1, addr2, email)

VALUES ('$_POST[name]','$_POST[addr1]','$_POST[addr2]',
'$_POST[email]')";

if (!mysql_query($sql,$con))

die('Error: ' . mysql_error());

echo "1 record added";

mysql_close($con)

?>

<h2> Form to retrieve values based on name</h2>

<form action="Lab11a.php" method="post">

Name: <input type="text" name="name" />

<input type="submit" />

</form>

</body>

</html>
WEB PROGRAMMING LABORATORY VVIET. IV

Lab6.php
<html>
<body>
<?php

$con = mysql_connect("localhost","root","");

if (!$con)

{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("college");

$result = mysql_query("SELECT * FROM person where


name='$_POST[name]'");

if(!$result)
{
echo "There is no records";
}

echo "<table border='1'>

<tr><th>Name</th><th>Addr1</th><th>Addr2</th><th>Email</th></tr>";

while($row = mysql_fetch_array($result))
{
echo "<tr>";

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

echo "<td>" . $row['addr1'] . "</td>";


echo "<td>" . $row['addr2'] . "</td>";
echo "<td>" . $row['email'] . "</td>";

echo "</tr>";
}
WEB PROGRAMMING LABORATORY VVIET. IV

echo "</table>";

mysql_close($con);

?> </body></html>

Output:1

Output 2:

Output 3:
WEB PROGRAMMING LABORATORY VVIET. IV

7. Write a PHP program to read student data from an XML file and store into the MYSQL
database. Retrieve and display.
<?php
$link=mysqli_connect("localhost:3307","root","AiiSh#4%my","college"
);
$name=$_GET['name'];
$studs=simplexml_load_file("student.xml");
foreach($studs as $stud)
{
$name = $stud->name;
$usn = $stud->usn;
mysqli_query($link,"insert into person
values('$name','$usn','','')");
}
$name=$_GET['name'];
$res=mysqli_query($link,"select * from person ");
if(mysqli_num_rows($res)==0)
{
echo "$name doesn't exist in the table";
}
else
{
echo "<table border='1' align=center width=60%>";
echo "<tr><td width=15% align=center>NAME</td><td width=15%
align=center>USN</td></tr>";
while($arr=mysqli_fetch_row($res))
echo "<tr><td>$arr[0]</td><td>$arr[1]</td></tr>";
echo "</table>";
}
?>
WEB PROGRAMMING LABORATORY VVIET. IV

8. Create an XHTML form with Name, Address Line 1, Address Line 2 and E-mail text
fields. On submitting, store the values in MySQL table. Provide buttons to update and
delete data for the same.
IV. Html:index.html

<html>

<body bgcolor="blue">

<h2> Form to insert values</h2>

<form action="Lab11.php" method="post">

Name : <input type="text" name="name" /><br><br>

Add1 : <input type="text" name="addr1" /><br><br>

Add2 : <input type="text" name="addr2" /><br><br>

Email: <input type="text" name="email" /><br><br>

<input type="submit" value="submit" />

<input type="reset" value="reset" />

</form>

</body></html>
WEB PROGRAMMING LABORATORY VVIET. IV

V. Lab_insert.php

<html>

<body>

<?php

$con =

mysql_connect("localhost:3307","root","AiiSh

#4%my"); if (!$con)

die('Could not connect: ' . mysql_error());

mysql_select_db("college");

$sql="INSERT INTO person (name, addr1, addr2, email)

VALUES ('$_POST[name]','$_POST[addr1]','$_POST[addr2]',
'$_POST[email]')";

if (!mysql_query($sql,$con))

die('Error: ' . mysql_error());

echo "1 record added";

mysql_close($con)

?>

<h2> Form to retrieve values based on name</h2>

<form action="Lab11a.php" method="post">

Name: <input type="text" name="name" />

<input type="submit" />

</form>

</body>

</html>
WEB PROGRAMMING LABORATORY VVIET. IV

VI. Lab.php
<html>
<body>
<?php

$con = mysql_connect("localhost","root","");

if (!$con)

{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("college");

$result = mysql_query("SELECT * FROM person where


name='$_POST[name]'");

if(!$result)
{
echo "There is no records";
}

echo "<table border='1'>

<tr><th>Name</th><th>Addr1</th><th>Addr2</th><th>Email</th></tr>";

while($row = mysql_fetch_array($result))
{
echo "</tr>";
echo "<tr>"; $row['name'] . "</td>";

echo "<td>" . $row['addr1'] . "</td>";

echo "<td>" . $row['addr2'] . "</td>";

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


echo "<td>" .
}

echo "</table>";

mysql_close($con);

?> </body></html>

24
WEB PROGRAMMING LABORATORY VVIET. IV

Retrieve.php
<html>
<body>
<h2> Form to retrieve values based on name</h2>

<form action="search.php" method="post">


Name: <input type="text" name="name" />
<input type="submit" />

</form>
</body>
</html>
Delete/update.php
<html>
<body>
<?php

$con = mysql_connect("localhost:3307","root","AiiSh#4%my"); if
(!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("college");

$result = mysql_query("SELECT * FROM person where


name='$_POST[name]'");
if(!$result)
{
echo "There is no records";
}
echo "<h2>Records to be Deleted or Updated</h2><br><br><table
border='1' style='border-collapse: collapse';>
<form action='updel.php' method='post'>
<tr><th>Name</th><th>Addr1</th><th>Addr2</th><th>Email</th><th>Dele
te or Update</th></tr>";

while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td><input type='text' name='name' value='$row[name]' style='height:
40px';></td>";
echo "<td><input type='text' name='addr1' value='$row[addr1]' style='height:
40px'; ></td>";
echo "<td><input type='text' name='addr2' value='$row[addr2]' style='height:
40px';></td>";
echo "<td><input type='text' name='email' value='$row[email]' style='height:
40px';></td>";
echo "<td> <input type='submit' value='Delete' name='delete' style='height:
30px';><input type='submit' name='update' value='Update' style='height:
30px';></td>";
echo "<tr>";
}

25
WEB PROGRAMMING LABORATORY VVIET. IV

echo "</form>";
echo "</table>";
mysql_close($con);
?> </body></html>

9.Build a Rails application to accept book information viz. Accession number, title, authors,
edition and publisher from a web page and store the information in a database and to search
for a book with the title specified by the user and to display the search results with proper
headings.

You might also like