Web Technologies 13 25
Web Technologies 13 25
14. Define Complex class in Ruby and do write methods to carry operations on Complex objects.
Cmplx.rb
class Cmplx
attr_accessor :real, :imag
def read
@real=gets.to_i
@imag=gets.to_i
end
def add(other)
ob = Cmplx.new
ob.real = @real+other.real;
ob.imag = @imag+other.imag;
return ob
end
def subtract(other)
ob = Cmplx.new;
ob.real = @real-other.real;
ob.imag = @imag-other.imag;
return ob;
end
def multiply(other)
ob = Cmplx.new
ob.real = (@real * other.real)-(imag*other.imag);
ob.imag = (@real * other.imag)+(imag*other.real);
return ob;
end
def divide(other)
t = Cmplx.new;
ob = Cmplx.new;
t.imag = -other.imag;
r =(other.real).abs;
i =(other.imag).abs;
d =(r*r)+(i*i);
ob.real = ((@real * other.real)-(imag*t.imag))/d;
ob.imag = ((@real * t.imag)+(imag*other.real))/d;
return ob;
end
def disp
if @imag<0
puts "#{@real}#{@imag}i"
else
puts "#{@real}+#{@imag}i"
end
end
end
t1 = Cmplx.new
t2 = Cmplx.new
t3 = Cmplx.new
t4 = Cmplx.new
t5 = Cmplx.new
t6 = Cmplx.new
Ch. Vijayananda
LENDI INSTITUTERatnam@Dept. Of Computer
OF ENGINEERING & TECH Science 23
Web Technologies Lab Manual as per JNTUK-R13
print "ADDITION:"
t3 = t1.add(t2)
t3.disp
print "SUBTRACTION:"
t4 = t1.subtract(t2)
t4.disp
print "PRODUCT:"
t5 = t1.multiply(t2)
t5.disp
print "DIVSION:"
t6 = t1.divide(t2)
t6.disp
print "=================";
OUTPUT:
Ch. Vijayananda
LENDI INSTITUTERatnam@Dept. Of Computer
OF ENGINEERING & TECH Science 24
Web Technologies Lab Manual as per JNTUK-R13
15. Write a program which illustrates the use of associative arrays in Perl.
pAssoc.pl
%ages = ('kiran'=>19,'vijay'=>21,'raju'=>20);
print"Original Array:\n";
print"===========================\n";
while (($key) = each %ages)
{
print "$key is $ages{$key} years old\n ";
}
$ages{'mayur'} = 24;
print"\nAfter adding element:\n";
print"===========================\n";
while(($key) = each %ages)
{
print "$key is $ages{$key} years old\n ";
}
delete( $ages{'vijay'});
print"\nAfter deleting element:\n";
print"===========================\n";
@all_keys = keys(%ages);
print "Keys are: @all_keys\n";
@all_values = values(%ages);
print "Values are: @all_values";
OUTPUT:
Ch. Vijayananda
LENDI INSTITUTERatnam@Dept. Of Computer
OF ENGINEERING & TECH Science 25
Web Technologies Lab Manual as per JNTUK-R13
16. Write Perl program takes a set names along the command line and prints whether they are regular
files or special files
fileTest.pl
$len = @ARGV;
for ($i=0;$i<$len;$i++)
{
if(-e $ARGV[$i])
{
if(-T $ARGV[$i])
{
print "$ARGV[$i] is a text file\n";
}
else
{
print "$ARGV[$i] is a special file\n";
}
}
else
{
print "$ARGV[$i] does not exists";
}
}
OUTPUT:
Ch. Vijayananda
LENDI INSTITUTERatnam@Dept. Of Computer
OF ENGINEERING & TECH Science 26
Web Technologies Lab Manual as per JNTUK-R13
unixPw.pl
my $salt='';
my $encrypted='';
my $password='';
my $use = 'Usage: Please provide password for encrypt';
my @saltchars=('.', '/', 0..9, 'A'..'Z', 'a'..'z');
my $args=@ARGV;
if ( $args < 1 || $args > 2 )
{
print "$use\n";
exit;
}
$password=$ARGV[0];
if( $args == 1 )
{
$salt = join('',@saltchars[rand(64),rand(64)]);
}
else
{
$salt=$ARGV[1];
}
$encrypted=crypt($password,$salt);
print "$password --> $encrypted\n";
OUTPUT:
Ch. Vijayananda
LENDI INSTITUTERatnam@Dept. Of Computer
OF ENGINEERING & TECH Science 27
Web Technologies Lab Manual as per JNTUK-R13
18. An example Perl program to connect to a MySQL Database table and executing simple commands.
Note: Create the following table in MySQL and insert some data in to the table.
students table
create table students
(
roll varchar(3) not null,
name varchar(3) not null,
branch varchar(3) not null
);
dbConn.pl
use DBI;
#definition of variables
$db="test";
$host="localhost";
$user="root";
$password="";
OUTPUT:
Ch. Vijayananda
LENDI INSTITUTERatnam@Dept. Of Computer
OF ENGINEERING & TECH Science 28
Web Technologies Lab Manual as per JNTUK-R13
contacts.php
<?php
if(isset($_REQUEST['vname']))
{
$vname=$_REQUEST['vname'];
$email=$_REQUEST['email'];
$message=$_REQUEST['message'];
$from="From: $vname<$email>\r\nReturn-path: $email";
$subject="Message sent using your contact form";
$m = mail("[email protected]", $subject, $message, $from);
if($m)
echo "<h1>Email sent!";
else
echo "Send Failed:".mysql_error();
echo " <a href='contacts.php'>[Back to Contacts]</a>";
}
else { ?>
<html>
<head>
<title>Contacts Page</title>
</head>
<body>
<font face="Trebuchet ms" size="6">
<table border="1">
<tr><th>Contact Us</th></tr>
<tr><td>
<form action="contacts.php" method="POST" enctype="multipart/form-data">
Your name<br/>
<input type="text" name="vname" value="" size="30"/><br>
Your email<br>
<input type="text" name="email" value="" size="30"/><br>
Your message<br>
<textarea name="message" rows="7" cols="30"></textarea><br>
<input type="submit" value="Send email"/>
</form>
</td></tr>
</table>
</body>
</html>
<?php } ?>
OUTPUT:
Ch. Vijayananda
LENDI INSTITUTERatnam@Dept. Of Computer
OF ENGINEERING & TECH Science 29
Web Technologies Lab Manual as per JNTUK-R13
Ch. Vijayananda
LENDI INSTITUTERatnam@Dept. Of Computer
OF ENGINEERING & TECH Science 30
Web Technologies Lab Manual as per JNTUK-R13
login.html
<html>
<head>
<title>Login Page</title>
</head>
<body>
<center>
<h3> Login Page</h3>
<form name="f1" action="setcookie.php" method="post">
<table frame="border">
<tr>
<td>User</td>
<td><input type="text" name="user" size="20" value=""/></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="pwd" size="20" value=""/></td>
</tr>
<tr>
<td align="center" colspan="2"><input type="submit" value="Sign in"/>
<input type="reset" /></td>
</tr>
</table>
</form>
</body></center>
</html>
setcookie.php
<html>
<head>
<title>Set Cookie</title>
</head>
<body>
<?php
$user = $_POST['user'];
$pwd = $_POST['pwd'];
setcookie("user",$user,time()+3600*2);
setcookie("pwd",$pwd,time()+3600*2);
echo "<h3>The Cookie Added...</h3>";
?>
</body>
</html>
LENDI INSTITUTERatnam@Dept.
Ch. Vijayananda OF ENGINEERING & TECH Science
Of Computer 31
Web Technologies Lab Manual as per JNTUK-R13
getcookie.php
<html>
<head>
<title>Get Cookie</title>
</head>
<body>
<?php
$user = $_COOKIE['user'];
$pwd = $_COOKIE['pwd'];
if($user=="user1")
{
if($pwd == "pwd1")
echo "<h2>Welcome User1 </h2>";
else
echo "<h2>You are not an authenticated user.</h2>";
}
elseif($user=="user2")
{
if($pwd == "pwd2")
echo "<h2>Welcome User2</h2>";
else
echo "<h2>You are not an authenticated user.</h2>";
}
elseif($user=="user3")
{
if($pwd == "pwd3")
echo "<h2>Welcome User3</h2>";
else
echo "<h2>You are not an authenticated user.</h2>";
}
elseif($user=="user4")
{
if($pwd == "pwd4")
echo "<h2>Welcome User4</h2>";
else
echo "<h2>You are not an authenticated user.</h2>";
}
else
echo "<h2>Invalid Username/Password</h2>";
?>
</body>
</html>
Ch. Vijayananda
LENDI INSTITUTERatnam@Dept. Of Computer
OF ENGINEERING & TECH Science 32
Web Technologies Lab Manual as per JNTUK-R13
Ch. Vijayananda
LENDI INSTITUTERatnam@Dept. Of Computer
OF ENGINEERING & TECH Science 33
Web Technologies Lab Manual as per JNTUK-R13
Ch. Vijayananda
LENDI INSTITUTERatnam@Dept. Of Computer
OF ENGINEERING & TECH Science 34
Web Technologies Lab Manual as per JNTUK-R13
Ch. Vijayananda
LENDI INSTITUTERatnam@Dept. Of Computer
OF ENGINEERING & TECH Science 35
Web Technologies Lab Manual as per JNTUK-R13
Ch. Vijayananda
LENDI INSTITUTERatnam@Dept. Of Computer
OF ENGINEERING & TECH Science 36
Web Technologies Lab Manual as per JNTUK-R13
Ch. Vijayananda
LENDI INSTITUTERatnam@Dept. Of Computer
OF ENGINEERING & TECH Science 37
Web Technologies Lab Manual as per JNTUK-R13
b) AIM: Use init-parameters to do the above task. Store the user-names and passwords in the
“users.ini” file and access them in php page by using the “parse_ini_file()” method.
mylogin.html
<html>
<head>
<title>Login Page</title>
</head>
<body>
<center>
<h3> Login Page</h3>
<form name="f1" action="initparam.php" method="post">
<table frame="border">
<tr>
<td>User</td>
<td><input type="text" name="user" size="20" value=""/></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="pwd" size="20" value=""/></td>
</tr>
<tr>
<td align="center" colspan="2"><input type="submit" value="Sign in"/>
<input type="reset" /></td>
</tr>
</table>
</form>
</body></center>
</html>
users.ini
[first_section]
u1=user1
p1=pwd1
initparam.php
<html>
<head>
<title>Init Parameters</title>
</head>
<body>
<?php
$user = $_POST['user'];
$pwd = $_POST['pwd'];
$temp = parse_ini_file("users.ini");
foreach($temp as $value)
{
$users[] = $value;
}
if($user==$users[$i])
{
if($pwd==$users[$i+1])
echo "<h3>Welcome $users[$i]</h3>";
else
echo "<h3>You are not an authenticated user</h3>";
}
else
echo "<h3>Invalid Username/Password</h3>";
}
?>
</body>
</html>
Ch. Vijayananda
LENDI INSTITUTERatnam@Dept. Of Computer
OF ENGINEERING & TECH Science 38
Web Technologies Lab Manual as per JNTUK-R13
Ch. Vijayananda
LENDI INSTITUTERatnam@Dept. Of Computer
OF ENGINEERING & TECH Science 39
Web Technologies Lab Manual as per JNTUK-R13
Ch. Vijayananda
LENDI INSTITUTERatnam@Dept. Of Computer
OF ENGINEERING & TECH Science 40
Web Technologies Lab Manual as per JNTUK-R13
21. Example PHP program for registering users of a website and login.
registration1 table:
<script language="javascript">
function validate()
{
var nam = document.f1.uname.value;
if(nam=="")
{
alert("Please enter name");
document.f1.uname.focus();
return false;
}
Ch. Vijayananda
LENDI INSTITUTERatnam@Dept. Of Computer
OF ENGINEERING & TECH Science 41
Web Technologies Lab Manual as per JNTUK-R13
document.f1.lang.focus();
return false;
}
document.f1.addr.focus();
return false;
}
}
</script>
insert.php
<?php
$conn = mysql_connect("localhost","root","") or die("Failed to Connect:".mysql_error());
mysql_select_db("test",$conn) or die("No Database existing:".mysql_error());
if(isset($_POST['uname']))
{
$uname=$_POST['uname'];
$pass=$_POST['pass'];
$email=$_POST['email'];
$phno=(float)$_POST['phone'];
$gender=$_POST['gen'];
$day=(int)$_POST['day'];
$month=(int)$_POST['month'];
$year=(int)$_POST['year'];
$dob = "$day-$month-$year";
$str=$_POST['lang'];
$lang="";
foreach ($str as $val)
{ $lang = $lang." ".$val;}
$text=$_POST['addr'];
$addr = str_replace("\n","<br>",$text);
$query = "INSERT INTO registration1 VALUES ('$uname', '$pass', '$email', '$phno', '$gender',
'$dob', '$lang', '$addr')";
$res = mysql_query($query);
if($res)
{
$script = "<script>alert('User Registration Successful');";
Ch. Vijayananda
LENDI INSTITUTERatnam@Dept. Of Computer
OF ENGINEERING & TECH Science 42
Web Technologies Lab Manual as per JNTUK-R13
OUTPUT
Ch. Vijayananda
LENDI INSTITUTERatnam@Dept. Of Computer
OF ENGINEERING & TECH Science 43
Web Technologies Lab Manual as per JNTUK-R13
pavan
pavan
Ch. Vijayananda
LENDI INSTITUTERatnam@Dept. Of Computer
OF ENGINEERING & TECH Science 44
Web Technologies Lab Manual as per JNTUK-R13
Ch. Vijayananda
LENDI INSTITUTERatnam@Dept. Of Computer
OF ENGINEERING & TECH Science 45
Web Technologies Lab Manual as per JNTUK-R13
B) Login Verification
login.html
<html>
<head>
<title> Sign-in Page </title>
<script language="javascript">
function validate()
{
var nam = document.f1.uname.value;
if(nam=="")
{
alert("Please enter name");
document.f1.uname.focus;
return false;
}
var pwd = document.f1.pass.value;
if(pwd=="")
{
alert("Please enter Password");
document.f1.pass.focus;
return false;
}
}
</script>
</head>
<body>
<br/><br/><br/>
<center>
<form name="f1" action="validation.php" method="post" onsubmit="javascript:return
validate()">
<table border="3" cellpadding="0" cellspacing="0">
<tr>
<td>
<table cellspacing="10">
<tr>
<td colspan="2" align="center"><h2><u>Login Page<u></h2></td>
</tr>
<tr>
<td> User Name</td>
<td><input type="text" name="uname" size="15"></td>
</tr>
<tr>
<td> Password</td>
<td><input type="password" name="pass" size="15"></td>
</tr>
<tr>
<td align="center"><input type="submit" value="Sign in"></td>
<td align="center"><input type="reset" value="Clear"></td>
</tr>
</table>
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
validation.php
<?php
$conn = mysql_connect("localhost","root","") or die("Failed to Connect:".mysql_error());
mysql_select_db("test",$conn) or die("No Database existing:".mysql_error());
if(isset($_POST['uname']))
{
$uname=$_POST['uname'];
$pass=$_POST['pass'];
$res = mysql_query("select * from registration1 where uname='$uname' and pass ='$pass'");
$count=mysql_num_rows($res);
Ch. Vijayananda
LENDI INSTITUTERatnam@Dept. Of Computer
OF ENGINEERING & TECH Science 46
Web Technologies Lab Manual as per JNTUK-R13
if($count)
echo "<script>alert('Welcome $uname');window.location='catalogue.html';</script>";
else
echo "<br/><br/><br/><h2 align='center'>Invalid Username/Password/Try
again!!!!!</h2>";
}
?>
OUTPUT:
Ch. Vijayananda
LENDI INSTITUTERatnam@Dept. Of Computer
OF ENGINEERING & TECH Science 47
Web Technologies Lab Manual as per JNTUK-R13
Ch. Vijayananda
LENDI INSTITUTERatnam@Dept. Of Computer
OF ENGINEERING & TECH Science 48
Web Technologies Lab Manual as per JNTUK-R13
registration table:
registration.html
<html>
<head>
<title> User Registration Page</title>
<script language="javascript">
function validate()
{
var nam = document.f1.uname.value;
if(nam=="")
{
alert("Please enter name");
document.f1.uname.focus;
return false;
}
var pwd = document.f1.pass.value;
if(pwd=="")
{
alert("Please enter Password");
document.f1.pass.focus;
return false;
}
var email = document.f1.email.value;
if(email=="")
{
alert("Please enter youe email");
document.f1.email.focus;
return false;
}
var phno = document.f1.phone.value;
len=phno.length
if(phno=="" || len != 10)
{
alert("Please enter phno or should be strictly 10 digits");
document.f1.phone.focus;
return false;
}
}
</script>
</head>
<body>
<br/><br/><br/>
<center>
<form name="f1" action="insertData.php" method="post" onsubmit="javascript:return
validate()">
<table border="3" cellpadding="0" cellspacing="0">
<tr>
<td>
<table cellspacing="10">
<tr>
<td colspan="2" align="center"><h2><u>User Registration Form</u></h2></td>
</tr>
Ch. Vijayananda
LENDI INSTITUTERatnam@Dept. Of Computer
OF ENGINEERING & TECH Science 49
Web Technologies Lab Manual as per JNTUK-R13
<tr>
<td> User Name</td>
<td><input type="text" name="uname" size="50"></td>
</tr>
<tr>
<td> Password</td>
<td><input type="password" name="pass" size="50"></td>
</tr>
<tr>
<td> E-mail</td>
<td><input type="text" name="email" size="50"></td>
</tr>
<tr>
<td> Phone</td>
<td><input type="text" name="phone" size="15"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="submit"></td>
</tr>
</table>
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
insertData.php
<?php
$conn = mysql_connect("localhost","root","");
if($conn)
echo "Connected to database!!!";
else
echo "Failed to Connect:".mysql_error();
mysql_select_db("test",$conn) or die("No Database existing:".mysql_error());
if(isset($_REQUEST['uname']))
{
$uname=$_REQUEST['uname'];
$pass=$_REQUEST['pass'];
$email=$_REQUEST['email'];
$phno=(float)$_REQUEST['phone'];
$query = "INSERT INTO registration VALUES('$uname','$pass','$email','$phno')";
mysql_query($query);
$result = mysql_query("select * from registration");
?>
<html>
<body>
<br/><br/><br/>
<p align="right"><a href="registration.html">[Registration Page]</a></p>
<center>
<font face="verdana" size="4">
<table border="1" cellpadding="0" cellspacing="0">
<tr>
<th colspan="4" align="center">User List</td>
</tr>
<tr>
<th> S.No.</th>
<th> User Name</th>
<th> Email</th>
<th>Phone</th>
</tr>
<?php $num=1; while($row = mysql_fetch_array($result))
{ ?>
<tr>
<td><?php echo $num++; ?> </td>
<td><?php echo $row['uname']; ?> </td>
<td><?php echo $row['email']; ?> </td>
Ch. Vijayananda
LENDI INSTITUTERatnam@Dept. Of Computer
OF ENGINEERING & TECH Science 50
Web Technologies Lab Manual as per JNTUK-R13
OUTPUT:
Ch. Vijayananda
LENDI INSTITUTERatnam@Dept. Of Computer
OF ENGINEERING & TECH Science 51
Web Technologies Lab Manual as per JNTUK-R13
Ch. Vijayananda
LENDI INSTITUTERatnam@Dept. Of Computer
OF ENGINEERING & TECH Science 52
Web Technologies Lab Manual as per JNTUK-R13
23. Write a PHP which does the following job: Insert the details of the 3 or 4 users who register with the
web site by using registration form. Authenticate the user when he/she submits the login form using the
user name and password from the database..
login.html
<html>
<head>
<title> Sign-in Page </title>
<script language="javascript">
function validate()
{
var nam = document.f1.uname.value;
if(nam=="")
{
alert("Please enter name");
document.f1.uname.focus;
return false;
}
var pwd = document.f1.pass.value;
if(pwd=="")
{
alert("Please enter Password");
document.f1.pass.focus;
return false;
}
}
</script>
</head>
<body>
<br/><br/><br/>
<center>
<form name="f1" action="validation.php" method="post" onsubmit="javascript:return
validate()">
<table border="3" cellpadding="0" cellspacing="0">
<tr>
<td>
<table cellspacing="10">
<tr>
<td colspan="2" align="center"><h2><u>Login Page<u></h2></td>
</tr>
<tr>
<td> User Name</td>
<td><input type="text" name="uname" size="15"></td>
</tr>
<tr>
<td> Password</td>
<td><input type="password" name="pass" size="15"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="submit"></td>
</tr>
</table>
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
Ch. Vijayananda
LENDI INSTITUTERatnam@Dept. Of Computer
OF ENGINEERING & TECH Science 53
Web Technologies Lab Manual as per JNTUK-R13
validation.php
<?php
$conn = mysql_connect("localhost","root","");
if($conn)
echo "<h4>Connected to Database.....!!";
else
echo "Failed to Connect:".mysql_error()."</h4>";
mysql_select_db("test",$conn) or die("No Database existing:".mysql_error());
if(isset($_POST['uname']))
{
$uname=$_POST['uname'];
$pass=$_POST['pass'];
echo"<br/><br/><br/><p align='right'><a href='login.html'>[Login Page]</a></p>";
$res = mysql_query("select * from registration where uname='$uname' and pass ='$pass'");
$count=mysql_num_rows($res);
if($count)
echo "<br/><br/><br/><h2 align='center'>Welcome $uname!!!!</h2>";
else
echo "<br/><br/><br/><h2 align='center'>Invalid Username/Password/Try
again!!!!!</h2>";
}
?>
OUTPUT:
Ch. Vijayananda
LENDI INSTITUTERatnam@Dept. Of Computer
OF ENGINEERING & TECH Science 54
Web Technologies Lab Manual as per JNTUK-R13
Ch. Vijayananda
LENDI INSTITUTERatnam@Dept. Of Computer
OF ENGINEERING & TECH Science 55
Web Technologies Lab Manual as per JNTUK-R13
Ch. Vijayananda
LENDI INSTITUTERatnam@Dept. Of Computer
OF ENGINEERING & TECH Science 56
Web Technologies Lab Manual as per JNTUK-R13
24. Create tables in the database which contain the details of items (books in our case like Book name,
Price, Quantity, Amount) of each category. Modify your catalogue page (week 2)in such a way that you
should connect to the database and extract data from the tables and display them in the catalogue page
using PHP
Catalogue table
bkEntryForm.html
<html>
<head>
<title> Catalogue Page</title>
<script language="javascript">
function validate()
{
var nam = document.f1.bname.value;
if(nam=="")
{
alert("Please enter Book name");
document.f1.bname.focus();
return false;
}
var auth = document.f1.auth.value;
if(auth=="")
{
alert("Please enter Author Name");
document.f1.auth.focus();
return false;
}
var publ = document.f1.publ.value;
if(publ=="")
{
alert("Please enter Publisher name");
document.f1.publ.focus();
return false;
}
var isbn = document.f1.isbn.value;
if(isbn=="")
{
alert("Please enter ISBN Number");
document.f1.isbn.focus();
return false;
}
var ed = document.f1.edi.value;
if(ed=="")
{
alert("Please enter book edition");
document.f1.edi.focus();
return false;
}
var cost = document.f1.cost.value;
if(cost=="")
{
alert("Please enter cost of the book");
document.f1.cost.focus();
return false;
}
Ch. Vijayananda
LENDI INSTITUTERatnam@Dept. Of Computer
OF ENGINEERING & TECH Science 57
Web Technologies Lab Manual as per JNTUK-R13
Ch. Vijayananda
LENDI INSTITUTERatnam@Dept. Of Computer
OF ENGINEERING & TECH Science 58
Web Technologies Lab Manual as per JNTUK-R13
Catalogue.php
<?php
$conn = mysql_connect("localhost","root","");
if($conn)
echo "Connected to database!!!";
else
echo "Failed to Connect:".mysql_error();
mysql_select_db("test",$conn) or die("No Database existing:".mysql_error());
if(isset($_POST['bname']))
{
$bname=$_POST['bname'];
$auth=$_POST['auth'];
$publ=$_POST['publ'];
$isbn=$_POST['isbn'];
$edi=$_POST['edi'];
$cost=(float)$_POST['cost'];
$qty=(int)$_POST['qty'];
echo"<br/><br/><br/><p align='right'><a href='bkEntryForm.html'>[Book Entry Page]</a></p>";
$query = "INSERT INTO catalogue VALUES('$bname','$auth','$publ','$isbn','$edi','$cost','$qty')";
mysql_query($query);
$result = mysql_query("select * from catalogue");
?>
<html>
<body><center>
<font face="verdana" size="4">
<table border="1" cellpadding="0" cellspacing="0">
<tr>
<th colspan="8" align="center">Books List</td>
</tr>
<tr>
<th>S.No.</th>
<th>Title</th>
<th>Author</th>
<th> Publication</th>
<th>ISBN</th>
<th>Edition</th>
<th>Cost</th>
<th>Quantity</th>
</tr>
<?php $num=1; while($row = mysql_fetch_array($result))
{ ?>
<tr>
<td align="center"><?php echo $num++; ?> </td>
<td align="left"><?php echo $row['bname']; ?> </td>
<td align="left"><?php echo $row['auth']; ?> </td>
<td align="left"><?php echo $row['publ']; ?> </td>
<td align="center"><?php echo $row['isbn']; ?> </td>
<td align="center"><?php echo $row['edi']; ?> </td>
<td align="center"><?php echo $row['cost']; ?> </td>
<td align="center"><?php echo $row['qty']; ?> </td>
</tr>
<?php }?>
</table>
</center>
</body>
</html>
<?php } ?>
Ch. Vijayananda
LENDI INSTITUTERatnam@Dept. Of Computer
OF ENGINEERING & TECH Science 59
Web Technologies Lab Manual as per JNTUK-R13
OUTPUT:
Ch. Vijayananda
LENDI INSTITUTERatnam@Dept. Of Computer
OF ENGINEERING & TECH Science 60
Web Technologies Lab Manual as per JNTUK-R13
Ch. Vijayananda
LENDI INSTITUTERatnam@Dept. Of Computer
OF ENGINEERING & TECH Science 61
Web Technologies Lab Manual as per JNTUK-R13
The user may add some items to cart from the catalog page. He can check the cart page for the selected
items. He may visit the catalogue again and select some more items. Here our interest is the selected
items should be added to the old cart rather than a new cart. Multiple users can do the same thing at a
time (i.e., from different systems in the LAN using the ip-address instead of local host). This can be
achieved through the use of sessions. Every user will have his own session which will be created after
his successful login to the website. When the user logs out his session should get invalidated (by using
the method session. invalidate () ).
Modify your catalogue and cart PHP pages to achieve the above mentioned functionality using sessions.
style.css
body
{
width:800px;
}
.txt-heading
{
padding: 5px 10px;
font-size:1.1em;
font-weight:bold;
color:#999;
}
.btnRemoveAction
{
color:#D60202;
border:0;
padding:2px 10px;
font-size:0.9em;
}
#btnEmpty
{
background-color:#D60202;
border:0;
padding:1px 10px;
color:#FFF;
font-size:0.8em;
font-weight:normal;
float:right;
text-decoration:none;
}
.btnAddAction
{
background-color:#79b946;
border:0;
padding:3px 10px;
color:#FFF;
margin-left:1px;
}
#shopping-cart
{
border-top: #79b946 2px solid;
margin-bottom:30px;
}
#shopping-cart .txt-heading
{
background-color: #D3F5B8;
}
#shopping-cart table
{
width:100%;
background-color:#F0F0F0;
}
#shopping-cart table td
Ch. Vijayananda
LENDI INSTITUTERatnam@Dept. Of Computer
OF ENGINEERING & TECH Science 62
Web Technologies Lab Manual as per JNTUK-R13
{
background-color:#FFFFFF;
}
.cart-item
{
border-bottom: #79b946 1px dotted;
padding: 10px;
}
#product-grid
{
border-top: #F08426 2px solid;
margin-bottom:30px;
}
#product-grid .txt-heading
{
background-color: #FFD0A6;
}
.product-item {
float:left;
background:#F0F0F0;
margin:15px;
padding:5px;
}
.product-item div
{
text-align:center;
margin:10px;
}
.product-price
{
color:#F08426;
}
.product-image {
height:100px;
background-color:#FFF;
}
dbController.php
<?php
class DBController {
private $host = "localhost";
private $user = "root";
private $password = "";
private $database = "test";
function __construct() {
$conn = $this->connectDB();
if(!empty($conn)) {
$this->selectDB($conn);
}
}
function connectDB() {
$conn = mysql_connect($this->host,$this->user,$this->password);
return $conn;
}
function selectDB($conn) {
mysql_select_db($this->database,$conn);
}
function runQuery($query) {
$result = mysql_query($query);
while($row=mysql_fetch_assoc($result)) {
$resultset[] = $row;
}
if(!empty($resultset))
return $resultset;
}
Ch. Vijayananda
LENDI INSTITUTERatnam@Dept. Of Computer
OF ENGINEERING & TECH Science 63
Web Technologies Lab Manual as per JNTUK-R13
index.php
<?php
session_start();
require_once("dbcontroller.php");
$db_handle = new DBController();
if(!empty($_GET["action"])) {
switch($_GET["action"]) {
case "add":
if(!empty($_POST["quantity"])) {
$productByCode = $db_handle->runQuery("SELECT * FROM tblproduct WHERE
code='" . $_GET["code"] . "'");
$itemArray =
array($productByCode[0]["code"]=>array('name'=>$productByCode[0]["name"],
'code'=>$productByCode[0]["code"], 'quantity'=>$_POST["quantity"], 'price'=>$productByCode[0]["price"]));
if(!empty($_SESSION["cart_item"])) {
if(in_array($productByCode[0]["code"],$_SESSION["cart_item"])) {
foreach($_SESSION["cart_item"] as $k => $v) {
if($productByCode[0]["code"] == $k)
$_SESSION["cart_item"][$k]["quantity"]
= $_POST["quantity"];
}
} else {
$_SESSION["cart_item"] =
array_merge($_SESSION["cart_item"],$itemArray);
}
} else {
$_SESSION["cart_item"] = $itemArray;
}
}
break;
case "remove":
if(!empty($_SESSION["cart_item"])) {
foreach($_SESSION["cart_item"] as $k => $v) {
if($_GET["code"] == $k)
unset($_SESSION["cart_item"][$k]);
if(empty($_SESSION["cart_item"]))
unset($_SESSION["cart_item"]);
}
}
break;
case "empty":
unset($_SESSION["cart_item"]);
break;
}
}
?>
<HTML>
<HEAD>
<TITLE>Simple PHP Shopping Cart</TITLE>
<link href="style.css" type="text/css" rel="stylesheet" />
</HEAD>
<BODY>
<div id="shopping-cart">
<div class="txt-heading">Shopping Cart <a id="btnEmpty" href="index.php?action=empty">Empty Cart</a></div>
<?php
if(isset($_SESSION["cart_item"])){
$item_total = 0;
?>
<table cellpadding="10" cellspacing="1">
<tbody>
<tr>
<th><strong>Name</strong></th>
<th><strong>Code</strong></th>
<th><strong>Quantity</strong></th>
<th><strong>Price</strong></th>
<th><strong>Action</strong></th>
</tr>
Ch. Vijayananda
LENDI INSTITUTERatnam@Dept. Of Computer
OF ENGINEERING & TECH Science 64
Web Technologies Lab Manual as per JNTUK-R13
<?php
foreach ($_SESSION["cart_item"] as $item){
?>
<tr>
<td><strong><?php echo $item["name"]; ?></strong></td>
<td><?php echo $item["code"]; ?></td>
<td align=center><?php echo $item["quantity"]; ?></td>
<td align=right><?php echo "$".$item["price"]; ?></td>
<td><a href="index.php?action=remove&code=<?php echo $item["code"];
?>" class="btnRemoveAction">Remove Item</a></td>
</tr>
<?php
$item_total += ($item["price"]*$item["quantity"]);
}
?>
<tr>
<td colspan="5" align=right><strong>Total:</strong> <?php echo "$".$item_total; ?></td>
</tr>
</tbody>
</table>
<?php
}
?>
</div>
<div id="product-grid">
<div class="txt-heading">Products</div>
<?php
$product_array = $db_handle->runQuery("SELECT * FROM tblproduct ORDER BY id ASC");
if (!empty($product_array)) {
foreach($product_array as $key=>$value){
?>
<div class="product-item">
<form method="post" action="index.php?action=add&code=<?php echo
$product_array[$key]["code"]; ?>">
<div class="product-image"><img src="<?php echo $product_array[$key]["image"];
?>" height="100" width="75"></div>
<div><strong><?php echo $product_array[$key]["name"]; ?></strong></div>
<div class="product-price"><?php echo "$".$product_array[$key]["price"]; ?></div>
<div><input type="text" name="quantity" value="1" size="2" /><input type="submit"
value="Add to cart" class="btnAddAction" /></div>
</form>
</div>
<?php
}
}
?>
</div>
</BODY>
</HTML>
Ch. Vijayananda
LENDI INSTITUTERatnam@Dept. Of Computer
OF ENGINEERING & TECH Science 65
Web Technologies Lab Manual as per JNTUK-R13
OUTPUT
Empty Cart
Ch. Vijayananda
LENDI INSTITUTERatnam@Dept. Of Computer
OF ENGINEERING & TECH Science 66
Web Technologies Lab Manual as per JNTUK-R13
After modification
Ch. Vijayananda
LENDI INSTITUTERatnam@Dept. Of Computer
OF ENGINEERING & TECH Science 67