#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2000
    Location
    Bangalore,Karnataka,India
    Posts
    1
    Rep Power
    0
    I have mysql database with 20 fields which includes Subject,Country fields. I want to search using those fields using from down menu. Please tell me the sample script for the same
    Waiting for your help
    Thank you very much...
  2. #2
  3. .Net Developer
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2000
    Location
    London
    Posts
    987
    Rep Power
    15
    Hi,

    here is a sample...


    html:

    <select name="country">
    <option value="india">india</option>
    <option value="pak">pak</option>
    </select>


    php:

    <?
    $con=mysql_connect('localhost','username','pwd');
    //database connectivity

    mysql_select_db('databasename',$con);
    //select your databae..

    $result=mysql_query("select * from tablename where country='$country'",$con);
    //issue the query..

    if(mysql_num_rows($result)>0){
    //result is their..
    echo "Search Results<br>";
    while($row=mysql_fetch_array($result)){
    //loop through and the print your results..
    echo "Subject :".$row["subject"];
    echo "<br>";
    }

    }else{
    //no resords
    echo " No Results";
    exit;
    }

    ?>

    Good Luck!!!

    ------------------
    SR -
    webshiju.com
    www.jobxyz.com-IT Career Portal
    ezipindia.com--WebStudio


    "The fear of the LORD is the beginning of knowledge..."
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Location
    Pune, Maharashtra, India
    Posts
    86
    Rep Power
    15
    Sorry Shivaram!
    Your question is not so clear.But I take that you want to make a dropdown from the fields in a table. You can do it this way:
    <?
    $database=mysql_connect('localhost','username','pwd') or Die("Cannot connect to the database plase try later");
    mysql_select_db('databasename',$database);

    $ssl="select * from tablename";
    $result=mysql_query($ssl);
    echo ("<select name="country">");
    while($row=mysql_fetch_array($result))
    {
    echo ("<option value=$row['subject;]>$row['subject']");
    }
    echo ("</select>");
    }
    ?>




    ------------------
    Nikunj
    MYSQL/PHP/XML
    ** Expertise comes with experiece ** Nikunj

    [This message has been edited by nikunj (edited September 07, 2000).]
  6. #4
  7. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2000
    Location
    Colchester, England
    Posts
    131
    Rep Power
    15
    Before you start with the dropdowns you should think about your database structure. Go through a normalistaion process. This at it's very basic level is the process of removing duplicate entries to a lookup tables. There is actually a lot more to it than that biut this a good way to start.

    The reason for this is that if your main table contains duplicate entries in the subject field that are 40 chars long, and the data is repeated 100 times through out the table you will have 4000 chars stored. whereas if the subject was stored in another table with key "ID" which has a value of "1" you will only store the value "1" 40 times. A saving of 3960 characters.

    The following table structure may work for you.

    CREATE TABLE tblCountry (
    ID INT AUTO_INCREMENT NOT NULL,
    Country CHAR (50),
    PRIMARY KEY (ID)

    CREATE TABLE tblSubject (
    ID INT AUTO_INCREMENT NOT NULL,
    Subject CHAR (50),
    PRIMARY KEY (ID)

    CREATE TABLE tblMainData (
    ID INT AUTO_INCREMENT NOT NULL,
    SubjectLookup INT NOT NULL DEFAULT 0,
    CountryLookup INT NOT NULL DEFAULT 0,
    Field3 CHAR (50),
    Field4 CHAR (50),
    Field20 CHAR (50),
    PRIMARY KEY (ID)

    This following piece of code is all you to create a dropdown to a lookup table:

    Now the code for the dropdown form, save this as whatever you want:

    <?php
    include("functions.php");
    OpenDb("localhost","user","pwd","databse") or die("Failed Opening Database");

    CreateDropDown("cboSubject","ID, subject", "tblSubject");

    // cboSubject is Form Control Name
    // ID is the name of key in lookup table
    // subject is the field that is looked up
    // tblCountry is the lookup table

    CreateDropDown("cboCountry","ID, country", "tblCountry");

    ?>

    The following code needs to be stored
    as functions.php

    <?php
    /* You can put this in other file and just include it */
    function OpenDb($hostname,$uid,$pwd,$dbname)
    {
    $link = @mysql_pconnect($hostname,$uid,$pwd);
    if($link && mysql_select_db($dbname)){
    //echo "$link && mysql_select_db($dbname)";
    return($link);
    }
    else{
    return(FALSE);
    }
    }
    ?>
    <?php
    function QueryIntoArray($query){
    settype($retval,"array");
    $result= mysql_query($query);

    if(!$result){
    print "Query Failed";
    }
    for($intRecordCount=0;$intRecordCount<mysql_numrows($result);$intRecordCount++)
    {

    for($intFieldCount=0;$intFieldCount<mysql_num_fields($result);$intFieldCount++)
    {
    $retval[$intRecordCount][mysql_field_name($result,$intFieldCount)] = mysql_result ($result,$intRecordCount,mysql_field_name($result,$intFieldCount));
    }//end inner loop
    }//end outer loop

    return $retval;
    }//end function
    ?>

    <?php
    function CreateDropDown($strLabel,$strFields, $strTable){
    settype($retval,"array");
    $strSql = "SELECT $strFields FROM $strTable";

    $result= mysql_query($strSql);

    if(!$result){
    print "Query Failed";
    }
    echo "<select name=$strLabel>";

    for($intRecordCount=0;$intRecordCount<mysql_numrows($result);$intRecordCount++)
    {

    $myrow = mysql_fetch_row($result);
    printf("<option value='$myrow[0]'>$myrow[1]");

    }

    echo "</select>";

    return $retval;
    }//end function



    // end of functions.php
    ?>

    I hope this useful.

    Andy Jackson

Similar Threads

  1. Replies: 0
    Last Post: February 9th, 2004, 03:38 PM
  2. MySQL output to table using phpBB
    By CCCMTech in forum PHP Development
    Replies: 0
    Last Post: February 2nd, 2004, 03:43 PM
  3. flash drop down menu over iframe
    By blueegg in forum HTML Programming
    Replies: 0
    Last Post: February 2nd, 2004, 12:59 PM
  4. flash drop down menu over iframe
    By blueegg in forum HTML Programming
    Replies: 0
    Last Post: February 2nd, 2004, 12:41 PM
  5. drop down php menu value wont show
    By alexmasters in forum PHP Development
    Replies: 2
    Last Post: January 19th, 2004, 06:31 AM

IMN logo majestic logo threadwatch logo seochat tools logo