Wednesday, June 6, 2012

If ElseIf ...ElseIf Else In Search php

If ElseIf ...ElseIf Else In Search php

First create the fallowing table(employee) in your my data base



HTML file here I have changed than the previous lessens because we have 3 categories to be checked with this example.when you create a web application we have to combine HTML tags, CSS, Java Scripts etc.In here also I have used java script file to populate the option field automatically to make the application user friendly one.
HTML file with the search categories  is displayed bellow.

 
IF Else IF...Else Search

Select Each Categories
Select DivisionSelect NameSelect Appointed Year

Codes for above HTML is

<html>
<head>
<title>IF Else IF...Else Search</title>

  <link type="text/css" rel="stylesheet" href="content.css">
<script language="javascript" src="pop.js"></script>
</head>

<body onload="fillCategory();">
<table align="center"  bgcolor=#efd62e>
<tr bgcolor=#efd62e><td colspan=4 ><center><font size=5 color="white">Select Each Categories</font></center></td></tr>
<FORM name="drop_list" action="pop_view.php" method="POST" >

<tr bgcolor=#ffffff><td colspan=2>Select Division</td><td>Select Name</td><td>Select  Appointed Year</td></tr>
<tr bgcolor=#ffffff><td colspan=2><SELECT  NAME="Category" onChange="SelectSubCat();" >
<Option value=""></option>
</SELECT></td>
<td><SELECT id="SubCat" NAME="SubCat" onChange="SelectSubCat2();" >
<Option value=""></option>
</SELECT></td>
<td><SELECT id="SubCat2" NAME="SubCat2">
<Option value=""></option>
</SELECT></td></tr>
<tr bgcolor=#efd62e><td colspan=4><center><input type="submit" name="submit" value="Search" /></center></td></tr>
</form>
</table>
</body>
</html>

pop.js for populating data with the above option boxes is bellow.

function fillCategory(){
addOption(document.drop_list.Category, "Production", "Production", "");
addOption(document.drop_list.Category,  "Service", "Service", "");
addOption(document.drop_list.Category, "Marketting", "Marketting", "");
addOption(document.drop_list.Category, "Human resource", "Human resource", "");
}

function SelectSubCat(){
removeAllOptions(document.drop_list.SubCat);
addOption(document.drop_list.SubCat, "", "--Select District--", "");

if(document.drop_list.Category.value == 'Production')
{

addOption(document.drop_list.SubCat,"Jhones", "Jhones");

addOption(document.drop_list.SubCat,"Jennifer", "Jennifer");
}

if(document.drop_list.Category.value == 'Service')
{

addOption(document.drop_list.SubCat,"Taylor", "Taylor");

addOption(document.drop_list.SubCat,"Lopez", "Lopez");

}

if(document.drop_list.Category.value == 'Marketting')
{

addOption(document.drop_list.SubCat,"Bayker", "Bayker");

}

if(document.drop_list.Category.value == 'Human resource')
{

addOption(document.drop_list.SubCat,"Steven", "Steven");

}
}

function SelectSubCat2(){
removeAllOptions(document.drop_list.SubCat2);


if(document.drop_list.SubCat.value == 'Jhones')
{

addOption(document.drop_list.SubCat2,"1965", "1965");


}
if(document.drop_list.SubCat.value == 'Jennifer')
{

addOption(document.drop_list.SubCat2,"1990", "1990");


}
if(document.drop_list.SubCat.value == 'Taylor')
{

addOption(document.drop_list.SubCat2,"1970", "1970");


}
if(document.drop_list.SubCat.value == 'Lopez')
{

addOption(document.drop_list.SubCat2,"2000", "2000");


}
if(document.drop_list.SubCat.value == 'Bayker')
{

addOption(document.drop_list.SubCat2,"1980", "1980");

addOption(document.drop_list.SubCat2,"2000", "2000");

addOption(document.drop_list.SubCat2,"2010", "2010");

}
if(document.drop_list.SubCat.value == 'Steven')
{

addOption(document.drop_list.SubCat2,"1988", "1988");

addOption(document.drop_list.SubCat2,"2008", "2008");
}
}
function removeAllOptions(selectbox)
{
    var i;
    for(i=selectbox.options.length-1;i>=0;i--)
    {
        //selectbox.options.remove(i);
        selectbox.remove(i);
    }
}


function addOption(selectbox, value, text )
{
    var optn = document.createElement("OPTION");
    optn.text = text;
    optn.value = value;

    selectbox.options.add(optn);
}

pop_view.php which displays data from mysql table employee is bellow


<?php

if(($_POST['Category'] =='Production')&& $_POST['SubCat']=='Jhones'){
echo "<center><table bgcolor=#0b2d04>
<tr bgcolor=#e6f293><td colspan=9 align='center'>Production Division<font size=4 color='blue'><u>".$_POST['SubCat']."</u></font></td></tr>
<tr bgcolor=#327423>
<th><font color=#ffffff>ID</font></th>
<th><font color=#ffffff>Division</font></th>
<th><font color=#ffffff>Name</font></th>
<th><font color=#ffffff>Appointed Date</font></th>
</tr>";

$SubCat=$_POST['SubCat'];
$SubCat2= $_POST['SubCat2'];

$username="";
$password="";
$database="my_database";
mysql_connect(localhost,root,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query = "(SELECT * FROM employee
 WHERE  appointed='$SubCat2' AND name='$SubCat')";
$result = mysql_query($query);
mysql_close();

while ($row = mysql_fetch_assoc($result)){

echo "<tr  bgcolor=#e6f293>
<td>".$row['id']."</td>
<td>".$row['division']."</td>
<td>".$row['name']."</td>
<td>".$row['appointed']."</td>

</tr>";
}
echo "</table></center>";
}
else if(($_POST['Category'] =='Production')&& $_POST['SubCat']=='Jennifer'){
echo "<center><table bgcolor=#0b2d04>
<tr bgcolor=#e6f293><td colspan=9 align='center'>Production Division<font size=4 color='blue'><u>".$_POST['SubCat']."</u></font></td></tr>
<tr bgcolor=#327423>
<th><font color=#ffffff>ID</font></th>
<th><font color=#ffffff>Division</font></th>
<th><font color=#ffffff>Name</font></th>
<th><font color=#ffffff>Appointed Date</font></th>
</tr>";
$SubCat=$_POST['SubCat'];
$SubCat2= $_POST['SubCat2'];

$username="";
$password="";
$database="my_database";
mysql_connect(localhost,root,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query = "(SELECT * FROM employee
 WHERE  appointed='$SubCat2' AND name='$SubCat')";
$result = mysql_query($query);
mysql_close();

while ($row = mysql_fetch_assoc($result)){
echo "<tr  bgcolor=#e6f293>
<td>".$row['id']."</td>
<td>".$row['division']."</td>
<td>".$row['name']."</td>
<td>".$row['appointed']."</td>

</tr>";
}
echo "</table></center>";
}
else if(($_POST['Category'] =='Service')&& $_POST['SubCat']=='Taylor'){
echo "<center><table bgcolor=#0b2d04>
<tr bgcolor=#e6f293><td colspan=9 align='center'>Service Division<font size=4 color='blue'><u>".$_POST['SubCat']."</u></font></td></tr>
<tr bgcolor=#327423>
<th><font color=#ffffff>ID</font></th>
<th><font color=#ffffff>Division</font></th>
<th><font color=#ffffff>Name</font></th>
<th><font color=#ffffff>Appointed Date</font></th>
</tr>";
$SubCat=$_POST['SubCat'];
$SubCat2= $_POST['SubCat2'];

$username="";
$password="";
$database="my_database";
mysql_connect(localhost,root,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query = "(SELECT * FROM employee
 WHERE  appointed='$SubCat2' AND name='$SubCat')";
$result = mysql_query($query);
mysql_close();

while ($row = mysql_fetch_assoc($result)){
echo "<tr  bgcolor=#e6f293>
<td>".$row['id']."</td>
<td>".$row['division']."</td>
<td>".$row['name']."</td>
<td>".$row['appointed']."</td>

</tr>";
}
echo "</table></center>";
}
else if(($_POST['Category'] =='Service')&& $_POST['SubCat']=='Lopez'){
echo "<center><table bgcolor=#0b2d04>
<tr bgcolor=#e6f293><td colspan=9 align='center'>Service Division<font size=4 color='blue'><u>".$_POST['SubCat']."</u></font></td></tr>
<tr bgcolor=#327423>
<th><font color=#ffffff>ID</font></th>
<th><font color=#ffffff>Division</font></th>
<th><font color=#ffffff>Name</font></th>
<th><font color=#ffffff>Appointed Date</font></th>
</tr>";
$SubCat=$_POST['SubCat'];
$SubCat2= $_POST['SubCat2'];

$username="";
$password="";
$database="my_database";
mysql_connect(localhost,root,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query = "(SELECT * FROM employee
 WHERE  appointed='$SubCat2' AND name='$SubCat')";
$result = mysql_query($query);
mysql_close();

while ($row = mysql_fetch_assoc($result)){
echo "<tr  bgcolor=#e6f293>
<td>".$row['id']."</td>
<td>".$row['division']."</td>
<td>".$row['name']."</td>
<td>".$row['appointed']."</td>

</tr>";
}
echo "</table></center>";
}
else if(($_POST['Category'] =='Marketting')&& $_POST['SubCat']=='Bayker'){
echo "<center><table bgcolor=#0b2d04>
<tr bgcolor=#e6f293><td colspan=9 align='center'>Marketting Division<font size=4 color='blue'><u>".$_POST['SubCat']."</u></font></td></tr>
<tr bgcolor=#327423>
<th><font color=#ffffff>ID</font></th>
<th><font color=#ffffff>Division</font></th>
<th><font color=#ffffff>Name</font></th>
<th><font color=#ffffff>Appointed Date</font></th>
</tr>";
$SubCat=$_POST['SubCat'];
$SubCat2= $_POST['SubCat2'];

$username="";
$password="";
$database="my_database";
mysql_connect(localhost,root,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query = "(SELECT * FROM employee
 WHERE  appointed='$SubCat2' AND name='$SubCat')";
$result = mysql_query($query);
mysql_close();

while ($row = mysql_fetch_assoc($result)){
echo "<tr  bgcolor=#e6f293>
<td>".$row['id']."</td>
<td>".$row['division']."</td>
<td>".$row['name']."</td>
<td>".$row['appointed']."</td>

</tr>";
}
echo "</table></center>";
}
else if(($_POST['Category'] =='Human resource')&& $_POST['SubCat']=='Steven'){
echo "<center><table bgcolor=#0b2d04>
<tr bgcolor=#e6f293><td colspan=9 align='center'>Human resource Division<font size=4 color='blue'><u>".$_POST['SubCat']."</u></font></td></tr>
<tr bgcolor=#327423>
<th><font color=#ffffff>ID</font></th>
<th><font color=#ffffff>Division</font></th>
<th><font color=#ffffff>Name</font></th>
<th><font color=#ffffff>Appointed Date</font></th>
</tr>";
$SubCat=$_POST['SubCat'];
$SubCat2= $_POST['SubCat2'];

$username="";
$password="";
$database="my_database";
mysql_connect(localhost,root,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query = "(SELECT * FROM employee
 WHERE  appointed='$SubCat2' AND name='$SubCat')";
$result = mysql_query($query);
mysql_close();

while ($row = mysql_fetch_assoc($result)){
echo "<tr  bgcolor=#e6f293>
<td>".$row['id']."</td>
<td>".$row['division']."</td>
<td>".$row['name']."</td>
<td>".$row['appointed']."</td>

</tr>";
}
echo "</table></center>";
}
else
{
die("Query failed");
}
?>

No comments:

Post a Comment