#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Posts
    36
    Rep Power
    15
    I am trying to query all of the unique values from a table in mysql. I want to create a drop down box with the results. (ie.let's say I had a table and each row was a country, and I was trying to create a drop down box with countries)

    I know how to do this if I wanted to create one drop down box (I would just run the query once and then cycle through the results).

    However I want to use this drop down box multiple times on the same page. (Say I wanted to ask someone what state they were born in, what state their parents were born in, etc.) But I figure it would be very inefficient to run the every time I needed the results. So, I want to run it once, and somehow store the values into an array and then call them in each drop down box.

    Any help
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2000
    Posts
    669
    Rep Power
    15
    Well if you are using classes you can run a constructor function whcih is the same name as the class it sits in and runs each timethe page is laoded atuomaitcally then you would have access to the array anywhere in your page, I might do something like this.

    I used two files in this example the first holds the class definition and should be modified to mirror your db config, you should just have to change the $db varaibale and the table and field names in the query
    Here is classtest.php3
    <?php

    $dbhost = "";
    $dbuser = "";
    $dbpass = "";
    $dbname = "";

    if (!($db = mysql_connect($dbhost, $dbuser, $dbpass))) echo "error connecting to ". $dbname ."on ". $dbhost;
    mysql_select_db($dbname,$db);


    class Test {

    var $countrylist;

    function Test() {
    global $db;
    $result = mysql_query("SELECT DISTINCT city from listings order by city");
    while ($row = mysql_fetch_array($result)) {
    $this->countrylist[] = $row["city"];
    }

    }

    function create_dropdown() {
    reset($this->countrylist);
    $dd .= sprintf('<select name="country">%s', "n");
    $dd .= sprintf('<option value="ZZ">Select a country%s', "n");
    while (list($key, $val) = each ($this->countrylist)) {
    $dd .= sprintf('<option value="%s">%s %s', rawurlencode($val), $val, "n");
    }
    $dd .= sprintf('%s', "n");
    $dd .= sprintf('</select>%s', "n");
    return $dd;
    }

    }//End class definition

    ?>


    Now here is the page that calls on the class and uses the dropdown function, you will notice that I used the function three times and only queried the db once at the start:
    Here is test.php3

    <?php
    include("./classtest.php3");
    $test = new Test;
    ?>
    <html>
    <head>
    <title</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <link rel="stylesheet" href="./style.css">
    </head>
    <body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
    <form>
    <?php
    //Added on 8/31 I just realized you can just print the varaible $dd whereevr you need it
    $dd = $test->create_dropdown();
    print $dd ."<br>";
    print $dd ."<br>";
    print $dd ."<br>";


    ?>
    </form>
    </body>
    </html>


    [This message has been edited by freddydoesphp (edited August 31, 2000).]

Similar Threads

  1. MySQL query not getting array properly [PHP]
    By adz32 in forum MySQL Help
    Replies: 1
    Last Post: February 11th, 2004, 03:51 PM
  2. MySQL Select query
    By MrTee1 in forum MySQL Help
    Replies: 0
    Last Post: January 30th, 2004, 11:16 AM
  3. MySQL Fulltext Search Query
    By txchou in forum MySQL Help
    Replies: 4
    Last Post: January 27th, 2004, 05:42 PM
  4. MySQL query inside an array?
    By enzo250gto in forum PHP Development
    Replies: 3
    Last Post: January 26th, 2004, 10:08 AM
  5. MySQL query using DB driven drop down menu
    By msg270 in forum PHP Development
    Replies: 3
    Last Post: January 22nd, 2004, 04:41 PM

IMN logo majestic logo threadwatch logo seochat tools logo