Tuesday, 10 April 2012

Php Tutorial 1.3

 

Session 2

Loops:

1) while - loops through a block of code while a specified condition is true
2) do...while - loops through a block of code once, and then repeats the loop as long as a specified condition is true
3) for - loops through a block of code a specified number of times
4) foreach - loops through a block of code for each element in an array

Conditional Statements:

1) if statement - use this statement to execute some code only if a specified condition is true
2) if...else statement - use this statement to execute some code if a condition is true and another code if the condition is false
3) if...elseif....else statement - use this statement to select one of several blocks of code to be executed
4) switch statement - use this statement to select one of many blocks of code to be executed

Examples

1) Display Array Data in HTML Table Using While Loop:


<?php
echo"<h1>Table Using While Loop</h1>";
$firstname=array();
$firstname[0]="Vishakha";
$firstname[1]="Abhishek";
$firstname[2]="Rucha";
$firstname[3]="Rahul";
$firstname[4]="Riddhi";
$firstname[5]="Ajay";
$firstname[6]="Charvi";
$firstname[7]="Rizvin";
$firstname[8]="Radhika";
$firstname[9]="Hatim";

$email=array();
$email[0]="vishakha@gmail.com";
$email[1]="charvi@gmail.com";
$email[2]="rucha@gmail.com";
$email[3]="radhika@gmail.com";
$email[4]="riddhi@gmail.com";
$email[5]="abhishek.rajput28@gmail.com";
$email[6]="rahul12@gmail.com";
$email[7]="ajay@gmail.com";
$email[8]="rizvin32@gmail.com";
$email[9]="hatim11@gmail.com";


$gender=array();
$gender[0]="F";
$gender[1]="M";
$gender[2]="F";
$gender[3]="M";
$gender[4]="F";
$gender[5]="M";
$gender[6]="F";
$gender[7]="M";
$gender[8]="F";
$gender[9]="M";

$age=array();
$age[0]="41";
$age[1]="55";
$age[2]="66";
$age[3]="70";
$age[4]="68";
$age[5]="40";
$age[6]="52";
$age[7]="69";
$age[8]="68";
$age[9]="67";
echo"<h2>List Of Males</h2>";
echo "<table border=\"8\">
<tr>
<th>First Name</th>
<th>Email</th>
<th>Gender</th>
<th>Age</th>
</tr>";
$i=0;
while($i<=9)
{
if($gender[$i]=='M' && $age[$i]>55)
{
echo "<tr><td>".$firstname[$i]."</td>
<td>".$email[$i]."</td>
<td>".$gender[$i]."</td>
<td>".$age[$i]."</td>
</tr>";
}
$i++;
}
echo "</table>";
echo"<h2>List Of Females</h2>";
echo "<table border=\"8\">
<tr>
<th>First Name</th>
<th>Email</th>
<th>Gender</th>
<th>Age</th>
</tr>";
$i=0;
while($i<=9)
{
if($gender[$i]=='F' && $age[$i]>55)
{
echo "<tr><td>".$firstname[$i]."</td>
<td>".$email[$i]."</td>
<td>".$gender[$i]."</td>
<td>".$age[$i]."</td>
</tr>";
}
$i++;
}
echo "</table>";
?>
//===============================================================

2) Display Array Data Using For Loop

<?php
echo"<h1>Table Using Loop</h1>";
$firstname=array();
$firstname[0]="Vishakha";
$firstname[1]="Charvi";
$firstname[2]="Rucha";
$firstname[3]="Radhika";
$firstname[4]="Riddhi";
$lastname=array();
$lastname[0]="Bandivadekar";
$lastname[1]="Jani";
$lastname[2]="Solanki";
$lastname[3]="Lal";
$lastname[4]="Vyas";
$mobilenumber=array();
$mobilenumber[0]=6785234265;
$mobilenumber[1]=5893214785;
$mobilenumber[2]=1423879654;
$mobilenumber[3]=7851236549;
$mobilenumber[4]=4582316578;
$email=array();
$email[0]="vishakha@gmail.com";
$email[1]="charvi@gmail.com";
$email[2]="rucha@gmail.com";
$email[3]="radhika@gmail.com";
$email[4]="riddhi@gmail.com";
$city=array();
$city[0]="Mumbai";
$city[1]="Delhi";
$city[2]="Kolkata";
$city[3]="Pune";
$city[4]="Banglore";
echo "<table border=\"8\">
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Mobile Number</th>
<th>Email</th>
<th>City</th>
</tr>";
for ($i=0; $i<5; $i++)
{
echo "<tr><td>$firstname[$i]</td>
<td>$lastname[$i]</td>
<td>$mobilenumber[$i]</td>
<td>$email[$i]</td>
<td>$city[$i]</td>
</tr>";
}
echo "</table>";
?>
//=================================================================

3) Display array data in HTML table using switch case:

<html>
<body>
<?php
echo"<h1>Table Using Switch Case</h1>";
$firstname=array();
$firstname[0]="Vishakha";
$firstname[1]="Charvi";
$firstname[2]="Rohit";
$firstname[3]="Ruchita";
$firstname[4]="Rahul";
$firstname[5]="Radhika";
$firstname[6]="Riddhi";
$firstname[7]="Siddhesh";
$firstname[8]="Supriya";
$firstname[9]="Darshit";
$firstname[10]="Krishna";
$firstname[11]="Madhuri";
$firstname[12]="Hiren";
$firstname[13]="Dipika";
$firstname[14]="Sanket";
$firstname[15]="Tejas";
$firstname[16]="Shraddha";
$firstname[17]="Anu";
$firstname[18]="Rohan";
$firstname[19]="Akhil";
$firstname[20]="Rashmi";
$firstname[21]="Priya";
$firstname[22]="Harshil";
$firstname[23]="Parag";
$firstname[24]="Charmi";
$marks=array();
$marks[0]="70";
$marks[1]="40";
$marks[2]="76";
$marks[3]="34";
$marks[4]="91";
$marks[5]="55";
$marks[6]="65";
$marks[7]="31";
$marks[8]="58";
$marks[9]="79";
$marks[10]="30";
$marks[11]="80";
$marks[12]="92";
$marks[13]="32";
$marks[14]="93";
$marks[15]="70";
$marks[16]="85";
$marks[17]="33";
$marks[18]="95";
$marks[19]="94";
$marks[20]="47";
$marks[21]="65";
$marks[22]="48";
$marks[23]="76";
$marks[24]="72";
$status=array();
$status[0]="Fail";
$status[1]="Pass Class";
$status[2]="1st Class";
$status[3]="Distinction";
$status[4]="Board Rankers";
echo"<h2>List Of Students Marks</h2>";
echo "<table border=\"8\">
<tr>
<th>First Name</th>
<th>Marks</th>
<th>Status</th>
</tr>";
for($i=0;$i<=24;$i++)
{
switch($marks)
{
case $marks:
{
if($marks[$i]<35)
{
echo "<tr bgcolor=\"red\"><td>".$firstname[$i]."</td>
<td>".$marks[$i]."</td>
<td>".$status[0]."</td></tr>";
break;
}
if($marks[$i]>=35 && $marks[$i]<=60)
{
echo "<tr><td>".$firstname[$i]."</td>
<td>".$marks[$i]."</td>
<td>".$status[1]."</td></tr>";
break;
}
if($marks[$i]>=60 && $marks[$i]<=75)
{
echo "<tr><td>".$firstname[$i]."</td>
<td>".$marks[$i]."</td>
<td>".$status[2]."</td></tr>";
break;
}
if($marks[$i]>=75 && $marks[$i]<90)
{
echo "<tr><td>".$firstname[$i]."</td>
<td>".$marks[$i]."</td>
<td>".$status[3]."</td></tr>";
break;
}
if($marks[$i]>90)
{
echo "<tr bgcolor=\"green\"><td>".$firstname[$i]."</td>
<td>".$marks[$i]."</td>
<td>".$status[4]."</td></tr>";
break;
}
}
}
}
?>
</body>
</html>
//===============================================================

 3) Display array data in HTML table using switch case:

<html>
<body>
<?php
echo"<h1>Table Using Switch Case</h1>";
$firstname=array();
$firstname[0]="Vishakha";
$firstname[1]="Charvi";
$firstname[2]="Rohit";
$firstname[3]="Ruchita";
$firstname[4]="Rahul";
$firstname[5]="Radhika";
$firstname[6]="Riddhi";
$firstname[7]="Siddhesh";
$firstname[8]="Supriya";
$firstname[9]="Darshit";
$firstname[10]="Krishna";
$firstname[11]="Madhuri";
$firstname[12]="Hiren";
$firstname[13]="Dipika";
$firstname[14]="Sanket";
$firstname[15]="Tejas";
$firstname[16]="Shraddha";
$firstname[17]="Anu";
$firstname[18]="Rohan";
$firstname[19]="Akhil";
$firstname[20]="Rashmi";
$firstname[21]="Priya";
$firstname[22]="Harshil";
$firstname[23]="Parag";
$firstname[24]="Charmi";
$marks=array();
$marks[0]="70";
$marks[1]="40";
$marks[2]="76";
$marks[3]="34";
$marks[4]="91";
$marks[5]="55";
$marks[6]="65";
$marks[7]="31";
$marks[8]="58";
$marks[9]="79";
$marks[10]="30";
$marks[11]="80";
$marks[12]="92";
$marks[13]="32";
$marks[14]="93";
$marks[15]="70";
$marks[16]="85";
$marks[17]="33";
$marks[18]="95";
$marks[19]="94";
$marks[20]="47";
$marks[21]="65";
$marks[22]="48";
$marks[23]="76";
$marks[24]="72";
$status=array();
$status[0]="Fail";
$status[1]="Pass Class";
$status[2]="1st Class";
$status[3]="Distinction";
$status[4]="Board Rankers";
echo"<h2>List Of Students Marks</h2>";
echo "<table border=\"8\">
<tr>
<th>First Name</th>
<th>Marks</th>
<th>Status</th>
</tr>";
for($i=0;$i<=24;$i++)
{
switch($marks)
{
case $marks:
{
if($marks[$i]<35)
{
echo "<tr bgcolor=\"red\"><td>".$firstname[$i]."</td>
<td>".$marks[$i]."</td>
<td>".$status[0]."</td></tr>";
break;
}
if($marks[$i]>=35 && $marks[$i]<=60)
{
echo "<tr><td>".$firstname[$i]."</td>
<td>".$marks[$i]."</td>
<td>".$status[1]."</td></tr>";
break;
}
if($marks[$i]>=60 && $marks[$i]<=75)
{
echo "<tr><td>".$firstname[$i]."</td>
<td>".$marks[$i]."</td>
<td>".$status[2]."</td></tr>";
break;
}
if($marks[$i]>=75 && $marks[$i]<90)
{
echo "<tr><td>".$firstname[$i]."</td>
<td>".$marks[$i]."</td>
<td>".$status[3]."</td></tr>";
break;
}
if($marks[$i]>90)
{
echo "<tr bgcolor=\"green\"><td>".$firstname[$i]."</td>
<td>".$marks[$i]."</td>
<td>".$status[4]."</td></tr>";
break;
}
}
}
}
?>
</body>
</html>

Related Post:

1.Basics of Php:Variables,Operators
2.Important String Functions


No comments:

Post a Comment