Post by along on Oct 23, 2005 23:05:21 GMT -5
Multi drop down search form...
<?php
/*------------------------------------------------------------------------
control codes
------------------------------------------------------------------------*/
if (isset($_POST['submit']))
{
search(); //call the search function
}else{
show_form(); //call the show form function
}//end if
/*------------------------------------------------------------------------
show the search form
------------------------------------------------------------------------*/
function show_form()
{
//call the dropdown function which creates an html string to build a select box for each element
$cuenta = dropdown('cuenta','notas');
$idclase = dropdown('idclase','notas');
$anio = dropdown('anio','notas');
$periodo = dropdown('periodo','notas');
$nota = dropdown('nota','notas');
echo "<form name='search' action='".$_SERVER['PHP_SELF']."' method='post'>
<table width='50%' align='center' valign='center'>
<tr>
<td colspan='2' align='center'>Search Form</td>
</tr>
<tr>
<td align='right'>Cuenta:</td><td>$cuenta</td>
</tr>
<tr>
<td align='right'>Idclase:</td><td>$idclase</td>
</tr>
<tr>
<td align='right'>Anio:</td><td>$anio</td>
</tr>
<tr>
<td align='right'>Periodo:</td><td>$periodo</td>
</tr>
<tr>
<td align='right'>Nota:</td><td>$nota</td>
</tr>
<tr>
<td colspan='2' align='center'> </td>
</tr>
<tr>
<td colspan='2' align='center'><input type='submit' name='submit' value='Go!'></td>
</tr>
</table>
</form>";
}//end function
/*------------------------------------------------------------------------
run the search and show the results
------------------------------------------------------------------------*/
function search()
{
//base sql
$sql = "select * from notas where 1 ";
//get the values from the form
//NOTE: You should do way more valdation on the values before you attempt to process anything
if ((!empty($_POST['cuenta']))&&($_POST['cuenta'] != 'all'))
{
$sql .= " and cuenta like '". addslashes($_POST['cuenta'])."%' ";
}
if ((!empty($_POST['idclase']))&&($_POST['idclase'] != 'all'))
{
$sql .= " and idclase like '". addslashes($_POST['idclase'])."%' ";
}
if ((!empty($_POST['anio']))&&($_POST['anio'] != 'all'))
{
$sql .= " and anio = '". addslashes($_POST['anio'])."' ";
}
if ((!empty($_POST['periodo']))&&($_POST['periodo'] != 'all'))
{
$sql .= " and periodo = '". addslashes($_POST['periodo'])."' ";
}
if ((!empty($_POST['nota']))&&($_POST['nota'] != 'all'))
{
$sql .= " and nota = '". addslashes($_POST['nota'])."' ";
}
//add more elements (or take away) as you desire...follow the same code structure as above
//run query
$result = conn($sql);
if (!$result){ die("No results due to database error.<br>".mysql_error()); }
if (mysql_num_rows($result)==0)
{
echo "No Results found!";
}else{
echo "<table border='1'><th>Cuenta</th><th>idclase</th><th>anio</th><th>periodo</th><th>nota</th></tr>";
while ($rows= mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>". $rows['cuenta'] ."</td>";
echo "<td>". $rows['idclase'] ."</td>";
echo "<td>". $rows['anio'] ."</td>";
echo "<td>". $rows['periodo'] ."</td>";
echo "<td>". $rows['nota'] ."</td>";
echo "</tr>";
}
echo "</table>";
}//end if
}//end function
/*------------------------------------------------------------------------
create the drop downs
------------------------------------------------------------------------*/
function dropdown($field, $table)
{
//initialize variables
$oHTML = '';
$result = '';
//check to see if the field is passed correctly
if (($field == "")||($table == ""))
{
die("No column or table specified to create drop down from!");
}
$sql = "select distinct($field) from $table";
//call the db function and run the query
$result = conn($sql);
//if no results are found to create a drop down return a textbox
if ((!$result) ||(mysql_num_rows($result)==0))
{
$oHTML .= "<input type='text' name='$field' value='' size='15'>";
}elseif (($result)&&(mysql_num_rows($result)>0)){
//build the select box out of the results
$oHTML .= "<select name='$field'>\n<option value='all'>All</option>\n";
while ($rows = mysql_fetch_array($result))
{
$oHTML .= "<option value='".$rows[$field]."'>".$rows[$field]."</option>\n";
}
$oHTML .= "</select>\n";
}
//send the value back to the calling code
return $oHTML;
}//end function
/*------------------------------------------------------------------------
database connection function
------------------------------------------------------------------------*/
function conn($sql)
{
$username = "some_user";
$pwd = "some_pass";
$host = "localhost";
$dbname = "test";
//echo "commnecing connection to local db<br>";
if (!($conn=mysql_connect($host, $username, $pwd))) {
printf("error connecting to DB by user = $username and pwd=$pwd");
exit;
}
$db3=mysql_select_db($dbname,$conn) or die("Unable to connect to local database");
$result = mysql_query($sql) or die ("Can't connect because ". mysql_error());
return $result;
}//end function
?>
<?php
/*------------------------------------------------------------------------
control codes
------------------------------------------------------------------------*/
if (isset($_POST['submit']))
{
search(); //call the search function
}else{
show_form(); //call the show form function
}//end if
/*------------------------------------------------------------------------
show the search form
------------------------------------------------------------------------*/
function show_form()
{
//call the dropdown function which creates an html string to build a select box for each element
$cuenta = dropdown('cuenta','notas');
$idclase = dropdown('idclase','notas');
$anio = dropdown('anio','notas');
$periodo = dropdown('periodo','notas');
$nota = dropdown('nota','notas');
echo "<form name='search' action='".$_SERVER['PHP_SELF']."' method='post'>
<table width='50%' align='center' valign='center'>
<tr>
<td colspan='2' align='center'>Search Form</td>
</tr>
<tr>
<td align='right'>Cuenta:</td><td>$cuenta</td>
</tr>
<tr>
<td align='right'>Idclase:</td><td>$idclase</td>
</tr>
<tr>
<td align='right'>Anio:</td><td>$anio</td>
</tr>
<tr>
<td align='right'>Periodo:</td><td>$periodo</td>
</tr>
<tr>
<td align='right'>Nota:</td><td>$nota</td>
</tr>
<tr>
<td colspan='2' align='center'> </td>
</tr>
<tr>
<td colspan='2' align='center'><input type='submit' name='submit' value='Go!'></td>
</tr>
</table>
</form>";
}//end function
/*------------------------------------------------------------------------
run the search and show the results
------------------------------------------------------------------------*/
function search()
{
//base sql
$sql = "select * from notas where 1 ";
//get the values from the form
//NOTE: You should do way more valdation on the values before you attempt to process anything
if ((!empty($_POST['cuenta']))&&($_POST['cuenta'] != 'all'))
{
$sql .= " and cuenta like '". addslashes($_POST['cuenta'])."%' ";
}
if ((!empty($_POST['idclase']))&&($_POST['idclase'] != 'all'))
{
$sql .= " and idclase like '". addslashes($_POST['idclase'])."%' ";
}
if ((!empty($_POST['anio']))&&($_POST['anio'] != 'all'))
{
$sql .= " and anio = '". addslashes($_POST['anio'])."' ";
}
if ((!empty($_POST['periodo']))&&($_POST['periodo'] != 'all'))
{
$sql .= " and periodo = '". addslashes($_POST['periodo'])."' ";
}
if ((!empty($_POST['nota']))&&($_POST['nota'] != 'all'))
{
$sql .= " and nota = '". addslashes($_POST['nota'])."' ";
}
//add more elements (or take away) as you desire...follow the same code structure as above
//run query
$result = conn($sql);
if (!$result){ die("No results due to database error.<br>".mysql_error()); }
if (mysql_num_rows($result)==0)
{
echo "No Results found!";
}else{
echo "<table border='1'><th>Cuenta</th><th>idclase</th><th>anio</th><th>periodo</th><th>nota</th></tr>";
while ($rows= mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>". $rows['cuenta'] ."</td>";
echo "<td>". $rows['idclase'] ."</td>";
echo "<td>". $rows['anio'] ."</td>";
echo "<td>". $rows['periodo'] ."</td>";
echo "<td>". $rows['nota'] ."</td>";
echo "</tr>";
}
echo "</table>";
}//end if
}//end function
/*------------------------------------------------------------------------
create the drop downs
------------------------------------------------------------------------*/
function dropdown($field, $table)
{
//initialize variables
$oHTML = '';
$result = '';
//check to see if the field is passed correctly
if (($field == "")||($table == ""))
{
die("No column or table specified to create drop down from!");
}
$sql = "select distinct($field) from $table";
//call the db function and run the query
$result = conn($sql);
//if no results are found to create a drop down return a textbox
if ((!$result) ||(mysql_num_rows($result)==0))
{
$oHTML .= "<input type='text' name='$field' value='' size='15'>";
}elseif (($result)&&(mysql_num_rows($result)>0)){
//build the select box out of the results
$oHTML .= "<select name='$field'>\n<option value='all'>All</option>\n";
while ($rows = mysql_fetch_array($result))
{
$oHTML .= "<option value='".$rows[$field]."'>".$rows[$field]."</option>\n";
}
$oHTML .= "</select>\n";
}
//send the value back to the calling code
return $oHTML;
}//end function
/*------------------------------------------------------------------------
database connection function
------------------------------------------------------------------------*/
function conn($sql)
{
$username = "some_user";
$pwd = "some_pass";
$host = "localhost";
$dbname = "test";
//echo "commnecing connection to local db<br>";
if (!($conn=mysql_connect($host, $username, $pwd))) {
printf("error connecting to DB by user = $username and pwd=$pwd");
exit;
}
$db3=mysql_select_db($dbname,$conn) or die("Unable to connect to local database");
$result = mysql_query($sql) or die ("Can't connect because ". mysql_error());
return $result;
}//end function
?>