WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Poker Tournaments Poker Tournaments
Poker Guide for Developers Poker Guide for Developers
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Resources
Web Development Content
Internet Security Software
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
פרייסז - הכח לקנות עובר לידיים שלך
Texas Holdem Poker Evangelists

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Multi drop down search form...
Categories : PHP, HTML and PHP, Databases, MySQL
bastien koert
Date : Oct 13th 2005
Grade : 3 of 5 (graded 9 times)
Viewed : 20486
File : No file for this code example.
Images : No Images for this code example.
Search : More code by bastien koert
Action : Grade This Code Example
Tools : My Examples List

Submit your own code examples  Submit your own code examples 
 

Here is a simple way to create a multi drop down search form for your site/records. There is a function which writes the drop down based on a field/table name that gets passed to ti. It returns an html string with the drop down in it.

<?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     

?>


As always, you can make all sorts of improvements and changes as you see fit...

Lostboy



html split bar used to split in multiple pages a database result
Categories : HTML and PHP, Databases, MySQL, PHP
dynamic table columns
Categories : PHP, HTML and PHP, Arrays, Databases, MySQL
This program will take data from a user via a web based form, validate it, show it to the user for re-validation, and finally insert it into the database. Plenty of sanity checking on the fields in the form.
Categories : MySQL, HTML and PHP, PHP, Complete Programs, Databases
Automatically printing the contents of an sql table in MySQL.
Categories : MySQL, PHP, HTML and PHP, Databases
bookmarker - PHP, PHPLIB, MySQL WWW based bookmark manager
Categories : MySQL, PHP, MySQL, Complete Programs, Databases
Complex paging with no resultset limit
Categories : PHP, MySQL, Databases, Output Control, HTML and PHP
A very simple way to build and do a hierarchical html categories browser without javascript , just using html php and mySql
Categories : HTML and PHP, Databases, Algorithms, PHP, MySQL
How can i Preload a 'SELECT MULTIPLE'?
Categories : HTML and PHP, PHP, MySQL, Databases
phpFormGenerator for Dynamic Form Generation from MySQL
Categories : PHP, PHP Classes, MySQL, Databases, HTML and PHP
DDN FFA Network Script
Categories : PHP, MySQL, Complete Programs, HTML and PHP, Databases
This program allows you to upload an ODBC ressource - i.e. an MS-Access database to a MySQL server.
Categories : Databases, MySQL, Complete Programs, PHP, Databases
A PHP useradmin for MySQL. Uploading is easy. The only thing you need is PHP and MySQL installed!
Categories : MySQL, Databases, PHP, HTML and PHP
Database resultset navigation
Categories : PHP, HTML and PHP, Databases, MySQL, Navigation
Record Set Paging with PHP (RSP)
Categories : PHP, MySQL, Navigation, Databases, HTML and PHP
Pull Down Surfing - Surf on Change
Categories : Java Script, MySQL, HTML and PHP, PHP, Databases
 Mick Scott wrote :1925
This is EXACTALLY what I was looking for, ...had a problem with my form which was doing my head in, till I found this and saw where I was going wrong.

MANY thanks! :D