Populating <options> tag with SQL data?

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

    Populating <options> tag with SQL data?

    Hello everyone,

    I have a problem that I cannot seem to figure out. If any of you have
    an answer or suggestion, I would appreciate it.

    My HTML/PHP code needs to pull options from a database to populate the
    <optionstag for a drop down menu and I cannot get it to work.

    Basically, there is a form on the website with a drop down box. I need
    the options in this box to be pulled from a preexisting table in a
    database. That's all. Sounds very simple but I'm totally lost on how
    to accomplish it.

    Any help would be greatly appreciated.

    Thank you,

    Cresh

  • ZeldorBlat

    #2
    Re: Populating &lt;options& gt; tag with SQL data?

    On May 3, 10:37 am, cresh <davebow...@gma il.comwrote:
    Hello everyone,
    >
    I have a problem that I cannot seem to figure out. If any of you have
    an answer or suggestion, I would appreciate it.
    >
    My HTML/PHP code needs to pull options from a database to populate the
    <optionstag for a drop down menu and I cannot get it to work.
    >
    Basically, there is a form on the website with a drop down box. I need
    the options in this box to be pulled from a preexisting table in a
    database. That's all. Sounds very simple but I'm totally lost on how
    to accomplish it.
    >
    Any help would be greatly appreciated.
    >
    Thank you,
    >
    Cresh
    Difficult to say. You haven't told us anything about the database
    (like the tables you are using or even the server software). You
    haven't told us what you've tried or done so far, and you haven't
    shown us any code you've written. We can't help you without some
    additional details.

    Comment

    • cresh

      #3
      Re: Populating &lt;options& gt; tag with SQL data?

      On May 3, 9:51 am, ZeldorBlat <zeldorb...@gma il.comwrote:
      Difficult to say. You haven't told us anything about the database
      (like the tables you are using or even the server software). You
      haven't told us what you've tried or done so far, and you haven't
      shown us any code you've written. We can't help you without some
      additional details

      I am using PHP and HTML to make the form. The server software is MySQL
      database. I can pull information from the database fine, I just don't
      know how to put the information into an HTML form as options in a drop
      down box.

      For example:

      The table contains five records: OPTION1, OPTION2, OPTION3, OPTION4,
      OPTION5.

      I can pull the data from the table using an array, no problems there.
      I can display the data, etc., just can't figure out how to put the
      data into a form <optiontag.

      Maybe something like this:
      <form action="process .php" method="post">
      <p>Select An Option:
      <select name="program" style="color:#F F0000">
      <option value="<?echo $program[ ] ; ?>" selected="selec ted"><?echo
      $program[ ] ; ?></option>
      <option value="<?echo $program[ ] ; ?>"><?echo $program[ ] ; ?></
      option>
      <option value="<?echo $program[ ] ; ?>"><?echo $program[ ] ; ?></
      option>
      <option value="<?echo $program[ ] ; ?>"><?echo $program[ ] ; ?></
      option>
      <option value="<?echo $program[ ] ; ?>"><?echo $program[ ] ; ?></
      option>
      </select>

      Do you see what I'm trying to do now? I'm not sure if I'm making
      myself clear enough. $program[ ] is the variable to hold the
      information pulled from the database table.

      Thanks again,

      Cresh

      Comment

      • =?ISO-8859-15?Q?Iv=E1n_S=E1nchez_Ortega?=

        #4
        Re: Populating &lt;options& gt; tag with SQL data?

        cresh wrote:
        I can pull the data from the table using an array, no problems there.
        I can display the data, etc., just can't figure out how to put the
        data into a form <optiontag.
        [...]
        Do you see what I'm trying to do now? I'm not sure if I'm making
        myself clear enough. $program[ ] is the variable to hold the
        information pulled from the database table.
        In order to iterate through an array, do this:

        <?php

        foreach($progra m as $item)
        {
        echo "<option value='$item'>$ item</option>";
        }

        ?>


        Or maybe you'd prefer something like this:


        <?php

        $r = mysql_query("se lect foo from baaz where foobar");

        while($row = mysql_fetch_ass oc($r))
        {
        echo "<option value='{$row['foo']}'>{$row['foo']}</option>";
        }

        ?>


        I hope you get the idea.

        By the way, try to make your PHP blocks larger, and fewer in number - code
        is more readable that way.

        --
        ----------------------------------
        Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

        El clavo que sobresale siempre recibe un martillazo.
        --Proverbio Chino

        Comment

        • cresh

          #5
          Re: Populating &lt;options& gt; tag with SQL data?

          <?php
          >
          $r = mysql_query("se lect foo from baaz where foobar");
          >
          while($row = mysql_fetch_ass oc($r))
          {
          echo "<option value='{$row['foo']}'>{$row['foo']}</option>";
          >
          }
          >
          ?>
          >
          I hope you get the idea.
          >
          By the way, try to make your PHP blocks larger, and fewer in number - code
          is more readable that way.
          Thank you! This is -exactly- what I was trying to do.
          I'm sorry, next post I'll try to get the PHP blocks larger and easier
          to read.

          Thanks again, this worked perfectly.

          Cresh

          Comment

          • cresh

            #6
            Re: Populating &lt;options& gt; tag with SQL data?

            Just in case anyone else needs this type of code, here is my small
            example, thanks to the help received here. The reference to
            "require.ph p" is where I store my database and server information,
            such as dbase name, user, password, etc.

            Thanks again for the assistance, it works great.

            Cresh

            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://
            www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
            <html xmlns="http://www.w3.org/1999/xhtml">
            <head>
            <meta http-equiv="Content-Type" content="text/html;
            charset=iso-8859-1" />
            <title>Retrie ve Information</title>
            </head>
            <body>
            <? require('requir e.php');
            $link = mysql_connect($ dbasehost, $dbaseuser, $dbasepass);
            if (!$link) {
            die('Could not connect to the database. Error is: ' .
            mysql_error());
            }
            mysql_select_db ($dbasename);
            ?>
            <form name"myform" action="file.ph p" method="post">
            <p>Make a selection: <select name="program">

            <?
            $r = mysql_query("se lect information from info");
            while($row = mysql_fetch_ass oc($r))
            {
            echo "<option
            value='{$row['information']}'>{$row['information']}</option>";
            }
            ?>
            </select></form>
            </body>
            </html>

            Comment

            • cresh

              #7
              Re: Populating &lt;options& gt; tag with SQL data?

              If you are getting results back from your database (e.g. MySQL), you can use a
              "while" loop to collect the information. This link at the PHP site would cover
              it in more detail
              >

              >
              but once you have the query setup you can built that into a variable such as...
              >
              $option = '';
              while ($row = mysql_fetch_ass oc($result))
              {
              $option .= "<option>" . $row['ColumnName'] . "</option>";
              >
              }
              >
              Tom

              Thank you very much. It's great to find out information like this. I
              appreciate it much.

              Cresh

              Comment

              Working...