Professional Documents
Culture Documents
IV
DEPARTMENT OF MCA
IV SEMESTER MCA (CBCS SCHEME)
Prepared By Approved By
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”.
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";
}
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)
Output:
2. A file contains lines of employees data, where each line has name; age; department code;
salary.
The names of the entire employee whose names end with “son”.
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">
</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");
}
WEB PROGRAMMING LABORATORY VVIET. IV
Output:
mysql_select_db("college");
$name=$_POST['name'];
$result = mysql_query("SELECT * FROM person WHERE name LIKE '%$name%' ");
if(!$result)
<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>";
?>
Output:
mysql_select_db("college");
$percent=$_POST['percent'];
$result = mysql_query("SELECT * FROM person WHERE age<40 ");
if(!$result)
}
$results = mysql_query("SELECT count(*) as name FROM person WHERE age<40 ");
if(!$results)
}
$results1 = mysql_query("SELECT count(*) as name FROM person ");
WEB PROGRAMMING LABORATORY VVIET. IV
if(!$results1)
}
while($row = mysql_fetch_array($results1))
{
$percentage=$row['name'];
while($row = mysql_fetch_array($results))
{
$percent=$row['name'];
$total=($percent/$percentage)*100;
<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
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)
}
$results = mysql_query("SELECT sum(addr1) as addr1 FROM person WHERE age<40 ");
if(!$results)
}
while($row = mysql_fetch_array($results))
{
$sum=$row['addr1'];
while($row = mysql_fetch_array($result))
{
$percent=$row['name'];
$total=($sum/$percent);
<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
mysql_select_db("college");
<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
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;
else
{
WEB PROGRAMMING LABORATORY VVIET. IV
$_SESSION["count"]++;
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>";
}
?>
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
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" .$row['student_name'] . "</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>
</body></html>
WEB PROGRAMMING LABORATORY VVIET. IV
Lab6_insert.php
<html>
<body>
<?php
$con =
mysql_connect("localhost:3307","root","AiiSh
#4%my"); if (!$con)
mysql_select_db("college");
VALUES ('$_POST[name]','$_POST[addr1]','$_POST[addr2]',
'$_POST[email]')";
if (!mysql_query($sql,$con))
mysql_close($con)
?>
</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");
if(!$result)
{
echo "There is no records";
}
<tr><th>Name</th><th>Addr1</th><th>Addr2</th><th>Email</th></tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
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">
</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)
mysql_select_db("college");
VALUES ('$_POST[name]','$_POST[addr1]','$_POST[addr2]',
'$_POST[email]')";
if (!mysql_query($sql,$con))
mysql_close($con)
?>
</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");
if(!$result)
{
echo "There is no records";
}
<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 "</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>
</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");
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.