0% found this document useful (0 votes)
10 views39 pages

Web Prac

Uploaded by

Priyanka Yadav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views39 pages

Web Prac

Uploaded by

Priyanka Yadav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 39

INDEX

1. Use of Basic Tags


a. Design a web page using different text formatting tags
b. Design a web page with links to different pages and allow navigation
between web pages
c. Design a web page demonstrating all Style sheet types:

2. Image maps, Tables, Forms and Media


a.Design a web page with Imagemaps.
b.Design a web page demonstrating different semantics
c.Design a web page with different tables. Design a webpages using table so
that the content appears well placed.
d. Design a web page with a form that uses all types of controls
e. Design a web page embedding with multimedia features.

3. Java Script
a. Using JavaScript design, a web page that prints factorial/Fibonacci series/any
given series.
b. Design a form and validate all the controls placed on the form using Java
Script.
c. Write a JavaScript program to display all the prime numbers between 1 and
100.
d. Write a JavaScript program to accept a number from the user and display the
sum of its digits.
e. Write a program in JavaScript to accept a sentence from the user and display
the number of words in it. (Do not use split () function).
f. Write a java script program to design simple calculator.

4. Control and looping statements and Java Script references


a. Design a web page demonstrating different conditional statements.
b. Design a web page demonstrating different looping statements.
c. Design a web page demonstrating different Core JavaScript references
(Array, Boolean, Date, Function, Math, Number, Object, String, reg Exp).

5. Basic PHP I
a. Write a PHP Program to accept a number from the user and print it factorial.
b. Write a PHP program to accept a number from the user and print whether it
is prime or not.

6. Basic PHP II
a. Write a PHP code to find the greater of 2 numbers. Accept the no. from the
user.
b. Write a PHP program to display the following Binary Pyramid:

7. String Functions and arrays


a. Write a PHP program to demonstrate different string functions.
b. Write a PHP program to create one dimensional array.

8. PHP and Database


a. Write a PHP code to create: • Create a database College • Create a table
Department (Dname, Dno, Number_Of_faculty)
b. Write a PHP program to create a database named “College”.Create a table
named “Student” with following fields (sno, sname, percentage). Insert 3
records of your choice. Display the names of the students whose percentage
is between 35 to 75 in a tabular format. 1
c. Design a PHP page for authenticating a user.

9. Email
1. Use of Basic Tags
a. Design a web page using different text formatting tag.
CODE:
<!DOCTYPE html>
<html>
<head>
<title>different text formatting tags</title>
</head>
<body>
<hgroup>
<h1>heading1</h1>
<h2>heading2</h2>
</hgroup>
<p>the gardener poem says…. </p>

2
<blockquote>
silly gardener!summer goes<br>
and winter comes with
pinching<br>
when in the garden bare and brown<br>
you must lay your barrow down<br>
</blockquote><br>
<b><i>hello friends</i></b>
h<sub>2</sub>so<sub>4</sub><br>
(a+b)<sup>2</sup>=a<sup>2</sup>+2ab+b<sup>2</sup>
<br> h<sub>2</sub>o<br>
<kbd>hello</kbd><br>
<code>hello</code><br>
<samp>hello</samp><br><br>
<pre>hello friends how are you? I am fine and I hope you will also fine.
</pre><br><br>
</body>
<html>

OUTPUT:

b. Design a web page with links to different pages and allow navigation between web pages
a. Create page one.html having a link to two.html, create two.html having a link to
three.html. Print “Welcome to Hyperlinks” in three.html.
b. Create an html page and demonstrate “Hyperlinking to anchor” i.e. linking within
the same page using the concept of bookmarks/anchors.

CODE:
One.html
<!DOCTYPE html>
<html>
<head> <title>One</title>

3
</head>
<body>
<a href="two.html">Go to Two.html</a>
</body>
</html>
Two.html
<!DOCTYPE html>
<hrtml>
<head> <title>Two</title>
</head>
<body>
Welcome to two.html.
<br>
<a href="three.html">Go to three.html</a>
</body>
</html>

Three.html
<!DOCTYPE html>
<html>
<head> <title>Three</title>
</head>
<body>
Welcome to Hyperlinks.
</body>
</html>

OUTPUT:

b. Create an html page and demonstrate “Hyperlinking to anchor” i.e. linking within
the same page using the concept of bookmarks/anchors.

CODE:
<!DOCTYPE html>
<html>
<head>
<title>linking within the same file</title>
</head>
<body>
<a name="top">top region</a>
<br>
<a href="#bottom">go to bottom</a>
<! —add large texual content -->
4
<a name="bottom">bottom region</a>
<br>
<a href="#top">go to top</a>
</body>
</html>
OUTPUT:

c. Design a web page demonstrating all Style sheet types:

CODE:
Main.html:
<!DOCTYPE html>
<html>
<head>
<title>nested lists</title>
<style type="text/css">
.fruits{color:blue}

5
.vegetables{color:yellow}
</style>
<link rel="stylesheet" type="text/css" href="one.css">
</head>
<body>
<ul style="list-style-type:filled square">
<li class="bg" style="color:red">fruits</li>
<ol style="list-style-type:upper-roman">
<li class="fruits">banana</li>
<li class="fruits">apple</li>
<li class="fruits">mango</li>
</ol>
</ul>
<ol style="list-style-type:lower-alpha">
<li class="bg" style="color:red">vegetables</li>
<ol class="grp1" start="4" style="list-style-type:uppper-roman">
<li class="vegetables">tomato</li>
<li class="vegetables">potato</li>
<li class="vegetables">carrot</li>
</ol>
</ol>
</body>
</html>
One.css
.bg {background-color:pink}
OUTPUT:

2. Image maps, Tables, Forms and Media

a. Design a web page with Imagemaps.

CODE:
<!DOCTYPE html>
<html>
<head>
<title>ImageMap</title>
</head>

6
<body>
<map name="m1">
<area shape="poly" coords="355,367,390,262,424,367" href="first.html">
<area shape="circle" coords="305,216,44" href="one.html">
<area shape="circle" coords="496,213,38" href="two.html">
<area shape="rectangle" coords="278,407,473,443" href="three.html">
</map>
<img src="face.png" usemap="#m1">

</body>
</html>

OUTPUT:

b. Design a web page demonstrating different semantics

CODE:
<!DOCTYPE html>
<html>
<head>
<title>Semantic Example</title>
</head>
<body>
<header>

7
<img src="garden.jpg" style="float:left; height:80px;width:60">
<h1>The Garden Company</h1>
<h5 style="clear:left">Helping your garden grow</h5>
</header>
<nav>
<hr>
<a href="one.html"><img src="home.jpg"></a>
<a href="lists.html"><img src="about.jpg"></a>
<a href="link.html"><img src="contact.jpg"></a>
</hr>
</nav>
<article>Welcome to Home Page<br>
</article>
We are expert in Gardening.
</article>
<aside>
<b>what does<i>mean?</i></b>
run into an unfamiliar gardening term?
</aside>
<footer>
<hr style="height:5px">
copyright &copy; 2012
</footer>
</body>
</html>
OUTPUT:

c. Design a web page with different tables. Design a webpages using table so that the
content appears well placed.
CODE:
<!DOCTYPE html>
<html>
<head> <title>Layout with table</title>
</head>
<body>
<a href="http://www.contoso.com" title="Home page">
<img src="images/leaf.gif" class="logo"></a>

8
<h1>The Garden Company</h1>
<h5><i>Helping you help your gardens grow since 1975</i></h5>
<hr>
<table>
<tr> <td style="width: 150px">
<p style="margin:0px">
<a href="index.htm"><img src="images/btn_home.gif"
style="border:none"></a>
<a href="tips.htm"><img src="images/btn_tips.gif"
style="border:none"></a>
<a href="contact.htm"><img src="images/btn_contact.gif"
style="border:none"></a></p>
</td>
<td> <p><img src="images/peaches.jpg"
style="float:right;padding:10px">
<b>Fruit trees are now in stock! </b>We have just received a large
Shipment of peach, pear, apple, and plum trees with sturdy root
systems and healthy foliage, with prices as low as $29.99. Visit the
<a href="products.htm"> Products</a> page for details.</p>
</td>
</tr>
</table>
</html>
OUTPUT:

d. Design a web page with a form that uses all types of controls
CODE:
<!DOCTYPE html>
<html>
<head>
<title>Password Input Control</title>
</head>
<body>
<form >
9
User ID : <input type="text" name="user_id" />
<br>
Password: <input type="password" name="password" />
<br> Description : <br />
<textarea rows="5" cols="50"
name="description"> Enter description
here...
</textarea>
<br>
<input type="checkbox" name="maths" value="on"> Maths
<input type="checkbox" name="physics" value="on"> Physics
<br>
<input type="radio" name="subject" value="maths"> Maths
<input type="radio" name="subject" value="physics"> Physics
<br>
<select name="dropdown">
<option value="Maths" selected>Maths</option>
<option value="Physics">Physics</option>
</select>
<br>
<input type="submit" name="submit" value="Submit" />
<input type="reset" name="reset" value="Reset" />
<input type="button" name="ok" value="OK" />
<input type="image" name="imagebutton" src="/html/images/logo.png" />
</form>
</body>
</html>
OUTPUT:

e. Design a web page embedding with multimedia features.

CODE:
<!DOCTYPE html>
<html>
<head>
<title>Multimedia</title>

10
</head>
<body>
Video File: <video width="240" height="320" controls>
<source src="myvideo.ogv" type="video/ogg">
<source src="myvideo.mp4" type="video/mp4">
<source src="myvideo.avi" type="video/avi">
<embed src="myvideo.mp4">
</video>
<br>
Audio File: <audio width="240" height="320" controls>
<source src="myaudio.ogv">
<source src="myaudio.mp4">
<source src="myaudio.avi">
<embed src="myaudio.mp4">
</audio>
</body>
</html>

OUTPUT:

3. JAVA SCRIPT
a. Using JavaScript design, a web page that prints factorial/Fibonacci series/any given
series.
CODE:
1. Factorial:
<!DOCTYPE html>
<html>
<head>
<title>Factorial Demo</title>
11
<script language="javascript">
var x=parseInt(prompt("Enter a number",""));
var fact=1,i;
for(i=1;i<=x;i++)
fact*=i;
document.write("<h1>Factorial of "+x+" is : "+fact+"</h1>");
</script>
</head>
<body>
</body>
</html>

OUTPUT:

2. Fibonacci:
<!DOCTYPE html>
<html>
<head>
<title>Fibonacci series Demo</title>
<script
language="javascript"
> var a=0,b=1,c,n,i;
n=parseInt(prompt("Enter limit for fibonacci
series:","")); document.write("<h2> Fibonacci series:
</h2><br>"); document.write(a+" "+b+" ");
for(i=2;i<n;i++)
{
c=a+b;
document.write(c+" ");
a=b;
b=c;}
</script>
</head>
<body>
</body>
</html>
OUTPUT:

b. Design a form and validate all the controls placed on the form using Java Script.
CODE:
<!DOCTYPE html>
<html>
<head>
<title>Form Validation</title>

12
<script type="text/javascript">
function validate()
{
if (document.myform.Name.=="");
{
alert("please provide your name!");
document.myform.Name.focus();
return false;
}
if (document.myform.Email.value=="")
{
alert("please provide your Email!");
document.myform.Email.focus();
return false;
}
if (document.myform.Zip.value==""||isNaN(document.myform.Zip.value)||
document.myform.Zip.value.length!=5)
{
alert("please provide a zip in the format #####.");
document.myform.Zip.value.focus();
return false;
}
if( document.myForm.Country.value == "-1" )
{
alert( "Please provide your country!" );
return false;
}

return (true); }
</script>
</head>
<body>

13
<form name="myform" onsubmit="return(validate());">
<table cellspacing="2" cellpadding="2" border="1">
<tr>
<td align="right">Name </td>
<td><input type="text" name="Name"></td>
</tr>
<tr>
<td align="right"> Email</td>
<td><input type="text" name="Email"></td>
</tr>
<tr>
<td align="right">Zip</td>
<td><input type="text" name="Zip"></td>
</tr>
<tr>
<td align="right"> Country </td>
<td>
<select name=""Country">
<option value="-1" selected>[Choose Yours]</option>
<option value="1"> USA</option>
<option value="2"> UK</option>
<option value="3"> INDIA</option>
</select>
</td>
</tr>
<tr>
<td align="right"></td>
<td><input type="submit" value="Sumbit"/></td>
</tr>
</table>

14
</form>
</body>
</html>
OUTPUT:

c. Write a JavaScript program to display all the prime numbers between 1 and 100.

CODE:
<!DOCTYPE html>
<html>
<head>
<title>prime number</title>
<script>

15
for(var i=1;i<=100;i++)
{
var flag=0;
for(var j=2;j<=i/2;j++)
{
if(i%j==0)
{
flag=1;
break;
}
}
if(flag==0)
{
document.write(i+"<br>");
}
}
</script>
</head>
</html>
OUTPUT:

d. Write a JavaScript program to accept a number from the user and display the sum of
its digits.

CODE:
<!DOCTYPE html>
<html>
<head>

16
<title>sum of digits</title>
<script>
var n=parseInt(prompt("Enter the
number"," ")); var p=0,y;
while(n>0)
{
y=n%10;
n=parseInt(n/10); p=p+y;
}
document.write("Sum of digits is: "+p);
</script>
</head>
</html>
OUTPUT:

e. Write a program in JavaScript to accept a sentence from the user and display the
number of words in it. (Do not use split () function).

CODE:
<!DOCTYPE html>
<html>
<head>
<title>Without using split function</title>

17
<script>
var str=prompt("Enter the
sentence=",""); var count=0;
for(i=0;i<str.length;i++)
{
if(str.charAt(i,1)==" " && str.charAt(i+1,1)!=" ")
count++;
}
document.write("Number of words are="+(count+1));
</script>
</head>
<body>
</body>
</html>

INPUT: Vinayak Kumawat


OUTPUT:

f. Write a java script program to design simple calculator.

CODE:
<!DOCTYPE html>
<html>
<head>
<title>Calculator</title>

18
<script
language="javascript">
function calc()
{
var n1,n2,opr,x; n1=parseInt(f1.s1.value);
n2=parseInt(f1.s3.value);
opr=f1.s2.value;
if(opr=="add")
x=n1+n2;
else
if(opr=="sub")
x=n1-n2;
else
if(opr=="multi")
x=n1*n2;
else
if(opr=="div")
x=n1/n2;
else
alert("please select operator");
document.getElementById("ans").innerHTML="answer
is:"+x;
}
</script>
</head>
<body>
<form name="f1">
<table width=50%>
<tr>
<td>Number1<br>
<select name="s1" size=1>
<option>Select</option>
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
</select>
</td>
<td>Operator<br>
<select name="s2" size=1>
<option>Select</option>
<option value="add">+</option>
<option value="sub">-</option>
<option value="multi">*</option>

19
<option value="div">/</option>
</select>
</td>
<td>Number 2<br>
<select name="s3" size=1>
<option>Select</option>
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
</select>
</td>
<td>
<input type="button" value="calculate" onclick="calc()">
</td>
</tr>
</table>
</form>
<p id="ans"> </p>
</body>
</html>

OUTPUT:

4. Control and looping statements and Java Script references

a. Design a web page demonstrating different conditional statements.

CODE:
<!DOCTYPE html>
<html>
<head>

20
<title>Conditional Statements</title>
<script
language="javascript"
> var age = 20;
if( age > 18 )
{
document.write("<b>Qualifies for driving</b>");
}
</script>
</head>
<body>
</body>
</html>

OUTPUT:

b. Design a web page demonstrating different looping statements.

CODE:
<!DOCTYPE html>
<html>
<head> <title>Looping Statements</title>

21
<script language="javascript"> document.write("For Loop demo:<br>");
for (i = 0; i < 5; i++)
{
text1 += "The number is " + i + "<br>";
document.write(text1+" ");
}
document.write("<br>While Loop
demo:<br>"); while (i < 10)
{
text2 += "The number is " +
i; document.write(text2+" ");
i++;
}
document.write("<br>Do-While Loop
demo:<br>"); do
{
text3 += "The number is " +
i; document.write(text3+" ");
i++;
}while (i < 10);
</script>
</head>
<body>
</body>
</html>
OUTPUT:

c. Design a web page demonstrating different Core JavaScript references (Array,


Boolean, Date, Function, Math, Number, Object, String, reg Exp).

CODE:

22
Array object code:

<!DOCTYPE html>
<html>
<head>
<title>Array object Demo</title>
<script language="javascript">
var cars=["Saab","Volvo","BMW"];
document.write("Array length is:
"+cars.length+"<br>"); document.write(cars.join["*"]);
cars.push("Opel");
document.write("After insert: "+cars.toString());
cars.pop();
document.write("After delete: "+cars.toString());
cars.sort();
document.write("After sort: "+cars.toString());
cars.reverse();
document.write("After reverse: "+cars.toString());
</script>
</head>
<body>
</body>
</html>

OUTPUT:

Boolean object code:

<!DOCTYPE html>
<html>
<head>
<title>Boolean object demo</title>

23
<script
language="javascript">
function myFunction()
{
document.getElementById("demo").innerHTML = Boolean(10 > 9);
}
</script>
</head>
<body>
<p>Display the value of Boolean(10 > 9):</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
</body>
</html>

OUTPUT:

Date object code:

<!DOCTYPE html>
<html>
<head> <title> Date object demo </title>
<script language="javascript">
var currentDate =new Date();
document.write("Date is:");
document.write(currentDate.getMonth()+"/"+currentDate.getDate()+"/"+currentDate.g
etFullYear()+"<br>");
document.write("Time is:");
document.write(currentDate.getHours()+":"+currentDate.getMinutes()
+":"+currentDate.getSeconds());
</script>
<head>
<body>
</body>
</html>

OUTPUT:

Function object code:

<!DOCTYPE html>
<html>
<head> <title> function object demo</title>
<script language="javascript">

24
addBraces=new
Function("s","return'['+s+']'");
document.write(addBraces("this"));
document.write(addBraces("is"));
document.write(addBraces("a"));
document.write(addBraces("test"));
</script>
</head>
<body>
</body>
</html>

OUTPUT:

Math object code:

<!DOCTYPE html>
<html>
<head> <title>math object</title>
<script language="javascript">
document.write("Random number: " +Math.random(3)+"<br>");
document.write("Minimum number: "+Math.min(150,0,-29,60)+"<br>");
document.write("Maximum number: "+Math.max(600,120,129,89)+"<br>");
document.write("Round(47.6): "+Math.round(47.6)+"<br>");
document.write("Ceil(4.4): "+Math.ceil(4.4)+"<br>");
document.write("Floor(4.7): "+Math.floor(4.7)+"<br>");
document.write("Square root of 2: "+Math.SQRT2+"<br>");
document.write("Square root of 1/2: "+Math.SQRT1_2+"<br>");
document.write("2 raise to 3: "+Math.pow(2,3)+"<br>");
</script>
</head>
<body>
</body>
</html>

OUTPUT:

Number object demo:

<!DOCTYPE html>
<html>
<head>
<title>Number object Demo</title>

25
<script language="javascript">
document.write("Maximum value:
"+Number.MAX_VALUE;); document.write("Minimum
value: "+Number.MIN_VALUE;); var num1 = 5.56789;
document.write(num1.toFixed(2));
var num2 = 13.3714;
document.write(num2.toPrecision(2));
document.write(num1.toString());
</script>
</head>
<body>
</body>
</html>

OUTPUT:

Object object code:

<!DOCTYPE html>
<html>
<head>
<title> object demo </title>
<script language="javascript">
myobj1=new
Object(123);
document.write(myobj1.toString()
+"<br>"); myobj2=new Object("hello
world");
document.write(myobj2.valueOf());
</script>
</head>
<body>
</body>
</html>

OUTPUT:

String object demo:

<!DOCTYPE html>
<html>

26
<head>
<title>String object Demo</title>
<script language="javascript">
var txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
document.write(txt.length);
var x = 'It\'s alright';
var y = "We are the so-called \"Vikings\" from the north.";
document.write(x + "<br>" + y);
var str = "Please locate where 'locate'
occurs!"; var pos = str.indexOf("locate");
document.write(pos);
var str = "Please locate where 'locate'
occurs!"; var pos = str.lastIndexOf("locate");
document.write(pos);
var str = "Please locate where 'locate'
occurs!"; var pos = str.search("locate");
document.write(pos);
var str = "Apple, Banana, Kiwi";
var res = str.slice(7, 13);
document.write(res);
var str = "Apple, Banana, Kiwi";
var res = str.substring(7, 13);
document.write(res);
var str = "Apple, Banana, Kiwi"; var res =
str.substr(7, 6); document.write(res);
var str = "Please visit Microsoft!";
var n = str.replace("Microsoft", "W3Schools");
document.write(n);
var text1 = "Hello World!";
var text2 = text1.toUpperCase();
document.write(text2);
var text1 = "Hello World!";
var text2 = text1.toLowerCase();
document.write(text2);
var text1 = "Hello";
var text2 = "World";
text3 = text1.concat(" ", text2);
document.write(text3);
var str = "HELLO WORLD";
documet.write(str.charAt(0));
var str = "HELLO WORLD";
documet.write(str.charCodeAt(0));
var txt = "a,b,c,d,e";
var arr=txt.split(" ");
document.write(arr[0]+"

"+arr[2]);
</script>
</head>
<body>
27
</body>
</html>

OUTPUT:

RegEx object demo:

<!DOCTYPE html>
<html>
<head>
<title>RegExp object Demo</title>
<script language="javascript">
var str = "Visit W3Schools!";
var n = str.search(/w3Schools/i);
document.write(n);
var str = "Visit Microsoft!";
var res = str.replace(/microsoft/i, "W3Schools");
document.write(res);
text = "The best things in life are free!";
document.write(/e/.test(text));
text = "The best things in life are free!";
document.write(/e/.exec(text));
</script>
</head>
<body>
</body>
</html>
OUTPUT:

5. Basic PHP I
a. Write a PHP Program to accept a number from the user and print it factorial.

28
CODE:
Input.html
<html>
<head>
<title>Factorial of a number</title>
</head>
<body>
<form method="post" action=”Fact.php">
Enter a number: <input type="text" name="n1"><br>
<input type="submit" value="Factorial">
</form>
</body>
</html>

Fact.php
<?php
$n1=(int)$_POST['n1'];
$fact=1;
for($i=1;$i<=$n1;$i+
+)
{
$fact=$fact*$i;
}
echo "Factorial of ".$n1." is: ".$fact;
?>
OUTPUT:

b. Write a PHP program to accept a number from the user and print whether it is
prime or not.

29
CODE:
Input.html
<html>
<head>
<title>Prime Number</title>
</head>
<body>
<form method="post" action="checkPrime.php">
Enter a number: <input type="text" name="n1"><br>
<input type="submit" value="CheckPrime">
</form>
</body>
</html>

checkPrime.php
<?php
$n1=(int)$_POST['n1'];
$flag=0; for($i=2;$i<=$n1/2;$i++)
{
if($n1%$i==0)
{
$flag=1;
break;
}
}
if($flag==0)
echo "Number is prime";
else
echo "Number is not prime";
?>
OUTPUT:

6. Basic PHP II
a. Write a PHP code to find the greater of 2 numbers. Accept the no. from the user.

30
CODE:
Input.html
<html>
<head>
<title>Greater of two no.s</title>
</head>
<body>
<form method="post" action="check.php">
1st Number: <input type="text" name="n1"><br>
2nd Number: <input type="text" name="n2"><br>
<input type="submit" value="Check">
</form>
</body>
</html>

check.php
<?php
$n1=(int)$_POST['n1'];
$n2=(int)$_POST['n2'];
if($n1>$n2)
echo $n1." is greater than ".$n2;
else if($n2>$n1)
echo $n2." is greater than ".$n1;
else
echo "Both the no.s are equal";
?>

OUTPUT:

b. Write a PHP program to display the following


Binary Pyramid:
1

31
0 1
1 0 1
0 1 0 1

CODE:

<?php
for($i=0;$i<4;$i++)
{
for($j=0;$j<=$i;$j++)
{
if(($i+$j)%2==0)
echo "1 ";
else
echo "0 ";
}
echo "<br>";
}
?>

OUTPUT:

7. String Functions and arrays


a. Write a PHP program to demonstrate different string functions.

32
CODE:
<?php
echo strlen("Hello");
echo"<br>";
echo strchr("Hello world!","world");
echo"<br>";
echo str_replace("world","Peter","Hello world!"); echo str_word_count("Hello world!");
echo"<br>";
echo strpos("I love php, I love php too!","php");
echo"<br>";
echo substr_count("Hello world. The world is nice","world"); echo substr("Hello world",6);
echo"<br>";
echo strtolower("Hello WORLD.");
echo"<br>";
echo strtoupper("Hello WORLD.");
echo"<br>";
echo strcmp("Hello world!","Hello world!"); echo strcasecmp("Hello world!","Hello world!");
?>
OUTPUT:

b. Write a PHP program to create one dimensional array.

33
CODE:
<?php
$cars = array("Volvo", "BMW", "Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] .
"."; echo count($cars);
$arrlength = count($cars);
for($x = 0; $x < $arrlength; $x++)
{
echo $cars[$x];
echo "<br>";
}
//Associative arrays
$age = array("Peter"=>"35", "Ben"=>"37",
"Joe"=>"43"); echo "Peter is " . $age['Peter'] . " years
old.";
?>

OUTPUT:

8. PHP and Database


a. Write a PHP code to create:
• Create a database College

34
• Create a table Department (Dname, Dno, Number_Of_faculty)

CODE:
<?php
$con=mysql_connect("localhost","root","")
; if(!$con)
die('could not connect:'.mysql_error());
if(mysql_query("create database College",$con))
echo"Database Created successfully";
else
echo "Error creating database:".mysql_error();
mysql_select_db("College",$con);
$query="create table department(dname varchar(50),dno smallint,no_of_faculty
smallint)";
if(mysql_query($query,$con))
echo"Table Created successfully";
else
echo "Error creating
table:".mysql_error(); mysql_close($con);
?>

b. Write a PHP program to create a database named “College”. Create a table named

35
“Student” with following fields (sno, sname, percentage). Insert 3 records of your
choice. Display the names of the students whose percentage is between 35 to 75 in
a tabular format.

CODE:
<html>
<head>
<title>Database insert and select</title>
</head>
<body>
<?php
$con=mysql_connect("localhost","root","")
; if(!$con)
die('could not connect:'.mysql_error());
if(mysql_query("create database College",$con))
echo"Database Created successfully";
else
echo "Error creating database:".mysql_error();
mysql_select_db("College",$con);
$query="create table student(sno smallint,sname varchar(50),percentage
decimal(7,2))";
if(mysql_query($query,$con))
echo"Table Created successfully";
else
echo "Error creating table:".mysql_error();
$query1="insert into student values(101,'Allen',78)";
if(mysql_query($query1,$con))
echo"Record 1 inserted successfully";
else
echo "Error inserting record 1:".mysql_error();
$query2="insert into student values(102,'Smith',45)";
if(mysql_query($query2,$con))
echo"Record 2 inserted successfully";
else
echo "Error inserting record 2:".mysql_error();
$query3="insert into student values(103,'Scott',63.2)";
if(mysql_query($query3,$con))
echo"Record 3 inserted successfully";
else
echo "Error inserting record 3:".mysql_error();
$sql="select * from student where percentage>=35 and percentage<=75";
$result=mysql_query($sql,$con);
if(mysql_num_rows($result)>0)
{
echo "<table
border='1'><tr><th>Sno</th><th>Sname</th><th>Percentage</th></tr>";
while($row=mysql_fetch_assoc($result))
{

36
echo "<tr>";
echo "<td>".$row['sno']."</td>";
echo "<td>".$row['sname']."</td>";
echo
"<td>".$row['percentage']."</td>";
echo "</tr>";
}
echo "</table>";
}
else
{
echo "Table is empty";
}
mysql_close($con);
?>
</body>
</html>
c. Design a PHP page for authenticating a user.

CODE:
Input.html
<html>
<head> <title>User authentication</title>
</head>
<body>
<form method="post" action="validate.php"> Username: <input type="text"
name="t1"><br>
Password: <input type="password" name="p1"><br>
<input type="submit" value="Submit">&nbsp;
<input type="reset" value="Clear">
</form>
</body>
</html>

validate.php:
<?php
$user=$_POST['t1'];
$pass=$_POST['p1'];
if($user=="abc" &&
$pass=="1234")
echo "Welcome ".$user;
else
echo "Invalid username and password";

9. Email

37
a. Write a program to send email with attachment.

Code:

<?php
$to="[email protected]";
$subject="test mail";
$message="Hello! This is simple email";
$from="[email protected]"
$header="From:$from"; mail($to,
$subject,$message,$header); echo
"Email sent";
?>

10. Sessions and Cookies

38
a. Write a program to demonstrate use of sessions and cookies.

CODE:

<?php
session_start();
if(isset($_SESSION['views']
))
$_SESSION[' views '] += 1;
else
$_SESSION[' views '] = 1;
$msg = "You have visited this page ". $_SESSION[' views '];
$msg .= "in this session";
?>

Cookie code (Setting and Retrieving):


<?php
$cookie_name = "user";
$cookie_value = "John Doe";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1
day
?>

<html>
<body>
<?php
if(!isset($_COOKIE[$cookie_name]))
{
echo "Cookie named '" . $cookie_name . "' is not set!";
}
else
{
echo "Cookie '" . $cookie_name . "' is set!
<br>"; echo "Value is: " .
$_COOKIE[$cookie_name];
}
?>
</body>
</html>

39

You might also like