Query Question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris

    Query Question

    Can anyone advise me or point me to examples where I might figure out
    how to populate the pulldown below automatically with a query when the
    page is first opened?

    I have a database with the dogs names in there that I would very much
    like to populate through a db query of that table versus having to
    hardcode the box when changes occur. Database name=dogs and
    table=dogs_tbl

    <html>
    <head>
    <title>Dog Name Database Query</title>
    </head>
    <body>
    <tr>
    <td valign=top align=center width=400>
    <font color=white>.</font><br>
    <font color=black face=ARIEL><p>
    <form method="" action="">
    <center>
    <table border="1">
    <tr>
    <td>Dog Name:</td>
    <td><select name="dogname">
    <option value = "all">All</option>
    <option value = "spot">Spot </option>
    <option value = "snoopy">Snoopy </option>
    <option value = "oscar">Osc ar</option> etc...
    </select>
    </td>
    </tr>
    <tr>
    <td colspan="2" align="center"> <input type="submit"
    value="Submit"> </td>
    </tr>
    </table>
    </form><p>
    </td>
    </tr></table></center><p>
    </body>
    </html>
  • thehuby

    #2
    Re: Query Question

    Something like this should suffice - assuming you are using MySQL
    anyway.

    HTH

    Rick


    $conn = mysql_connect( "localhost" , "<USERNAME> ", "<PASSWORD> " );
    mysql_select_db ( "dogs", $conn );

    $sql = "SELECT * FROM dogs_tbl";
    $rs = mysql_query( $sql, $connection );

    $html = "<select name='dogname'> ";
    while($row = mysql_fetch_row ($rs)) {
    $dogname = $row['dogname']; //this assumes that the column name is
    'dogname'
    $html .= "<option value='" .$dogname. "'>" .$dogname. "</option>";
    }

    $html .= "</select>";

    Comment

    • cjo

      #3
      Re: Query Question

      Used some of the code you posted and got it working - thank you. Is
      there a way to keep duplicates out in a query? If you had three
      entrys for 'spot', five for 'oscar' and so on, how would you keep the
      duplicates out of the query?

      Also something I discovered, for the life of me, I couldn't make the
      php query code run properly within an html TABLE document (even though
      the extension is .php)

      I have been using a table type html doc because it looks neater with
      the three pulldowns within it.

      thanks again for any help...

      Chris

      Comment

      • Kimmo Laine

        #4
        Re: Query Question

        "cjo" <coverlandNOSPA M914@yahoo.com> wrote in message
        news:iijpj1h7r9 56omvhktpkfapjo qa2h9j727@4ax.c om...[color=blue]
        > Used some of the code you posted and got it working - thank you. Is
        > there a way to keep duplicates out in a query? If you had three
        > entrys for 'spot', five for 'oscar' and so on, how would you keep the
        > duplicates out of the query?
        >
        > Also something I discovered, for the life of me, I couldn't make the
        > php query code run properly within an html TABLE document (even though
        > the extension is .php)
        >
        > I have been using a table type html doc because it looks neater with
        > the three pulldowns within it.
        >
        > thanks again for any help...[/color]


        Use keyword DISTINCT for that. It reduces duplicates to a single entry.

        --
        Welcome to Usenet! Please leave tolerance, understanding
        and intelligence at the door. They aren't welcome here.
        antaatulla.sika nautaa@gmail.co m.NOSPAM.invalid


        Comment

        • cover

          #5
          Re: Query Question

          On Fri, 30 Sep 2005 12:40:35 +0300, "Kimmo Laine"
          <antaatulla.sik anautaa@gmail.c om.NOSPAM.inval id> wrote:
          [color=blue]
          >Use keyword DISTINCT for that. It reduces duplicates to a single entry.[/color]

          Worked great - thanks. Also, thanks ALL for emailed & posted replies.

          This little project leaves me with one remaining question... So far, I
          have a page that when loaded reads a MySQL database column and
          populates a dropdown (pulldown) menu. On that page there are input
          options for a couple of other things such as the dog's age and owners
          name so I end up with variables 'dogname', 'ownername', 'dogage'.

          So I have the page loading fine and then I'll type into a text box,
          ownername & dogage and click "enter" - for whatever reason, ownername
          & dogage show up but the origninal 'dogname' variable apparently has
          lost it's data or has not made it.

          On my second page that shows what was input to the database, I'm using
          $dogname = $_POST['dogname'];
          $dogage = $_POST['dogage'];
          $ownername = $_POST['ownername'];

          with a print command to successfully echo the data entry results to
          the screen but again, the original $dogname doesn't appear to make it
          since the contents are blank.

          After I've successfully queried the database for dogname, how do I
          successfully pass along that data with dogage and ownername.

          thanks very much for any help...

          Comment

          Working...