dropdowns

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

    dropdowns

    OK, total noob here trying to teach himself PHP/mySQL by reading books
    and playing with code from the web and from trying to write increasingly
    difficult (for me) code. Here's what I'm trying to do:

    I have a db with a few tables. I have managed to write a simple query to
    one of these tables that then displays all the fields of each record
    that matches the query. Now I want to take it one step further, I want
    to create a drop down selection box where the user can select any of
    these records. I managed to create the box, but can only display one
    field of the records (so if the fields were firstname and lastname, I
    have only figured out how to show one of the two in the dropdown).

    Any hints would be appreciated,or pointers to good web resources for
    examples.

    Here's the query code. I have deleted the form part as it is crap and
    I'd like to see how it is properly done.
    -----------------------------
    $query = "SELECT ID, first, last FROM master WHERE fhl is null";
    $result = mysql_query($qu ery) or die('Error, query failed');

    $tsv = array();
    $html = array();

    while($row = mysql_fetch_arr ay($result, MYSQL_NUM))
    {
    $tsv[] = implode("\t", $row);
    $html[] = "<tr><td>" .implode("</td><td>", $row) .
    "</td></tr>";
    }

    $tsv = implode("\r\n", $tsv);
    $html = "<table>" . implode("\r\n", $html) . "</table>";
  • Jerry Stuckle

    #2
    Re: dropdowns

    finbogey jones wrote:
    OK, total noob here trying to teach himself PHP/mySQL by reading books
    and playing with code from the web and from trying to write increasingly
    difficult (for me) code. Here's what I'm trying to do:
    >
    I have a db with a few tables. I have managed to write a simple query to
    one of these tables that then displays all the fields of each record
    that matches the query. Now I want to take it one step further, I want
    to create a drop down selection box where the user can select any of
    these records. I managed to create the box, but can only display one
    field of the records (so if the fields were firstname and lastname, I
    have only figured out how to show one of the two in the dropdown).
    >
    Any hints would be appreciated,or pointers to good web resources for
    examples.
    >
    Here's the query code. I have deleted the form part as it is crap and
    I'd like to see how it is properly done.
    -----------------------------
    $query = "SELECT ID, first, last FROM master WHERE fhl is null";
    $result = mysql_query($qu ery) or die('Error, query failed');
    >
    $tsv = array();
    $html = array();
    >
    while($row = mysql_fetch_arr ay($result, MYSQL_NUM))
    {
    $tsv[] = implode("\t", $row);
    $html[] = "<tr><td>" .implode("</td><td>", $row) .
    "</td></tr>";
    }
    >
    $tsv = implode("\r\n", $tsv);
    $html = "<table>" . implode("\r\n", $html) . "</table>";
    Well, first of all I don't just implode the data; I prefer to access the
    individual columns myself.

    But you can do something like:

    <select name="myselect" >
    <?php
    while ($row = mysql_fetch_arr ay($result)) {
    echo "<option value=$row['id']>" . $row[firstname] . " "
    ..$row[lastname] . "</option>\n";
    ?>
    </select>
    Of course you'll need to adjust based on your particular column names in
    your database.

    --
    =============== ===
    Remove the "x" from my email address
    Jerry Stuckle
    JDS Computer Training Corp.
    jstucklex@attgl obal.net
    =============== ===

    Comment

    • finbogey jones

      #3
      Re: dropdowns

      Jerry Stuckle wrote:
      finbogey jones wrote:
      >OK, total noob here trying to teach himself PHP/mySQL by reading books
      >and playing with code from the web and from trying to write increasingly
      >difficult (for me) code. Here's what I'm trying to do:
      >>
      >I have a db with a few tables. I have managed to write a simple query to
      >one of these tables that then displays all the fields of each record
      >that matches the query. Now I want to take it one step further, I want
      >to create a drop down selection box where the user can select any of
      >these records. I managed to create the box, but can only display one
      >field of the records (so if the fields were firstname and lastname, I
      >have only figured out how to show one of the two in the dropdown).
      >>
      >Any hints would be appreciated,or pointers to good web resources for
      >examples.
      >>
      >Here's the query code. I have deleted the form part as it is crap and
      >I'd like to see how it is properly done.
      >-----------------------------
      >$query = "SELECT ID, first, last FROM master WHERE fhl is null";
      >$result = mysql_query($qu ery) or die('Error, query failed');
      >>
      >$tsv = array();
      >$html = array();
      >>
      >while($row = mysql_fetch_arr ay($result, MYSQL_NUM))
      >{
      > $tsv[] = implode("\t", $row);
      > $html[] = "<tr><td>" .implode("</td><td>", $row) .
      >"</td></tr>";
      >}
      >>
      >$tsv = implode("\r\n", $tsv);
      >$html = "<table>" . implode("\r\n", $html) . "</table>";
      >
      Well, first of all I don't just implode the data; I prefer to access the
      individual columns myself.
      >
      But you can do something like:
      >
      <select name="myselect" >
      <?php
      while ($row = mysql_fetch_arr ay($result)) {
      echo "<option value=$row['id']>" . $row[firstname] . " "
      .$row[lastname] . "</option>\n";
      ?>
      </select>
      Of course you'll need to adjust based on your particular column names in
      your database.
      >
      Thanks for the info, I'll try that out tonight.

      Comment

      • obiron

        #4
        Re: dropdowns

        Except that it is the form bit we need :)

        you have generated the HTML for the <FORM method=\"post\"
        action=\"somepa ge.php\"and for the <SELECT value=\"recordI D\">....

        You now use the object / array to generate the option bit of the code.

        $HTML = "<OPTION value=\"$result['value']\"$result['Fname']
        $result['Sname']</OPTION>";

        PRINT $HTML;

        and then end the </SELECT >

        You will now see in the HTML

        <OPTION value="17">Fred Smith</OPTION>
        <OPTION value="18">John Jones</OPTION>
        <OPTION value="19">Pete r Parker</OPTION>

        Basically whatever you put between <OPTIONand </OPTIONwill show in
        the drop down list but the value is what gets passed back to the server
        as part of the form submit and will end up in $_POST['recordID']

        Hope this helps

        Comment

        • finbogey jones

          #5
          Re: dropdowns

          obiron wrote:
          Except that it is the form bit we need :)
          >
          you have generated the HTML for the <FORM method=\"post\"
          action=\"somepa ge.php\"and for the <SELECT value=\"recordI D\">....
          >
          You now use the object / array to generate the option bit of the code.
          >
          $HTML = "<OPTION value=\"$result['value']\"$result['Fname']
          $result['Sname']</OPTION>";
          >
          PRINT $HTML;
          >
          and then end the </SELECT >
          >
          You will now see in the HTML
          >
          <OPTION value="17">Fred Smith</OPTION>
          <OPTION value="18">John Jones</OPTION>
          <OPTION value="19">Pete r Parker</OPTION>
          >
          Basically whatever you put between <OPTIONand </OPTIONwill show in
          the drop down list but the value is what gets passed back to the server
          as part of the form submit and will end up in $_POST['recordID']
          >
          Hope this helps
          >
          Thank you very much. I now have the form looking right and am moving on
          to the page that takes the value and changes the db with the selection.


          fin

          Comment

          • obiron

            #6
            Re: dropdowns

            glad to be of help and thankyou for saying thankyou.. Buts lets leave
            it there or we could be going at it for a week.

            Hopefully if the not quite noobees help the noobees, the xperts will
            help the not quite noobees and the gurus will help the xperts. that
            way everyone gets to contribute and we all get anwers

            Comment

            Working...