Quick PHP / MYSQL drop down question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • stealthbristol
    New Member
    • Mar 2008
    • 5

    Quick PHP / MYSQL drop down question

    Hi,

    I've created a drop down menu that gets it's values (ID and SCHOOL) from a MYSQL database. I've created a variable for the ID value, but can't figure out how to do this for the SCHOOL value.

    The code ...
    [php]<?php
    $sql = "SELECT ID, SCHOOL FROM SCHOOL ORDER BY ID";
    $users = mysql_query($sq l,$conn);
    while($row = mysql_fetch_arr ay($users))
    {
    echo ("<option value=\"$row[ID]\" " . ($schooladd == $row["ID"] ? " selected" : "") . ">$row[SCHOOL]</option>"); }
    ?>[/php]
    I'm sure it's something simple, but I've been looking at it for so long I just can't see it - any ideas ?

    Please enclose your posted code in [code] tags (See How to Ask a Question).

    This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

    Please use [code] tags in future.

    MODERATOR
    Last edited by ronverdonk; Mar 24 '08, 10:45 AM. Reason: code tags
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    Welcome to The Scripts!

    When you mean by 'add a variab'le' to add an option to the select list, then the following will suffice. I cannot fill in the variable you use to check the checked attribute, so it is named $variable.[php]<?php
    $sql = "SELECT ID, SCHOOL FROM SCHOOL ORDER BY ID";
    $users = mysql_query($sq l,$conn);
    while($row = mysql_fetch_arr ay($users))
    {
    echo ("<option value=\"$row[ID]\" " . ($schooladd == $row["ID"] ? " selected" : "") . ">$row[SCHOOL]</option>");
    echo ("<option value=\"$row[SCHOOL]\" " . ($value == $row["SCHOOL"] ? " selected" : "") . ">$row[SCHOOL]</option>");
    }
    ?>[/php]If you mean something else, then please explain a bit further.

    Ronald

    Comment

    • stealthbristol
      New Member
      • Mar 2008
      • 5

      #3
      Whoops - sorry Mods, will remember in future !

      @ Ronald: Thanks for the welcome, I'll explain a bit further ...

      The variable $schooladd contains the row ID on the value list selection and I need a variable to contain the actual text displayed based on the value list selection (called $schoolname for example)

      Unfortunately your solution creates a duplicate in the drop down list.

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        Does not make it any clearer. Let me see if I understand correctly. You want the value in $row['SCHOOL'], which is displayed in your drop down list text, to be stored from the POST array into a variable after the form has been submitted. Correct?

        Ronald

        Comment

        • stealthbristol
          New Member
          • Mar 2008
          • 5

          #5
          Originally posted by ronverdonk
          Does not make it any clearer. Let me see if I understand correctly. You want the value in $row['SCHOOL'], which is displayed in your drop down list text, to be stored from the POST array into a variable after the form has been submitted. Correct?

          Ronald
          Yup, that sounds about right !

          This is where the school ID appears a bit further down the script:

          Code:
          <?php echo "School id = $school "; ?>
          I've realised in my stupidity that I've forgot to mention that a piece of Javascript 'auto-submits' the form (and populates the variable) when the drop down menu is used. So all I need is another variable (for the school name) added somewhere in the piece of code in a similar way to the $school variable shown in my first post .. if possible ?

          Comment

          • ronverdonk
            Recognized Expert Specialist
            • Jul 2006
            • 4259

            #6
            There are 2 ways of doing this: the easy and the hard way.
            The easy way is to concat, with a separator such as underscore ()_), the $row[SCHOOL] value to the $orw[ID] value in the option. Like[php]echo ("<option value=\"$row[ID]_$row[SCHOOL]\" " . ($schooladd == $row["ID"] ? " selected" : "") . ">$row[SCHOOL]</option>");[/php]In the POST you can easily take that out, using[php]$str=explode('_ ', $_POST[select_name]);
            // $str[0] = the ID
            // $str[1] = the school name[/php]
            The hard way is to use JavaScript to get the text from the selected drop down button and insert it into the value of a hidden field in the form before submitting it.

            Ronald

            Comment

            • stealthbristol
              New Member
              • Mar 2008
              • 5

              #7
              Originally posted by ronverdonk
              There are 2 ways of doing this: the easy and the hard way.
              The easy way is to concat, with a separator such as underscore ()_), the $row[SCHOOL] value to the $orw[ID] value in the option. Like[php]echo ("<option value=\"$row[ID]_$row[SCHOOL]\" " . ($schooladd == $row["ID"] ? " selected" : "") . ">$row[SCHOOL]</option>");[/php]In the POST you can easily take that out, using[php]$str=explode('_ ', $_POST[select_name]);
              // $str[0] = the ID
              // $str[1] = the school name[/php]
              The hard way is to use JavaScript to get the text from the selected drop down button and insert it into the value of a hidden field in the form before submitting it.

              Ronald
              Aaah ok, I think I'll try the easier option then !

              I was actually hoping that I could add a $schoolname == $row["SCHOOL"] somewhere in the code similar to where it says $schooladd == $row["ID"] - I assume this isn't possible for some reason ?

              No worries though, at least it CAN be done - thanks for all your help mate.

              Nath

              Comment

              • ronverdonk
                Recognized Expert Specialist
                • Jul 2006
                • 4259

                #8
                Originally posted by stealthbristol
                Aaah ok, I think I'll try the easier option then !
                I was actually hoping that I could add a $schoolname == $row["SCHOOL"] somewhere in the code similar to where it says $schooladd == $row["ID"] - I assume this isn't possible for some reason ?
                No worries though, at least it CAN be done - thanks for all your help mate.

                Nath
                [php]$schooladd == $row["ID"] [/php] is NOT an assignment, it is a comparison. And you use it with a ternary function, i.e.[php]$schooladd == $row["ID"] ? " selected" : "")[/php]meaning: if $schooladd equals $row['ID'] then set attribute 'selected 'in option statement, otherwise set '' in option statement. Nothing to do with assignment to a variable!

                Ronald

                Comment

                • stealthbristol
                  New Member
                  • Mar 2008
                  • 5

                  #9
                  Originally posted by ronverdonk
                  [php]$schooladd == $row["ID"] [/php] is NOT an assignment, it is a comparison. And you use it with a ternary function, i.e.[php]$schooladd == $row["ID"] ? " selected" : "")[/php]meaning: if $schooladd equals $row['ID'] then set attribute 'selected 'in option statement, otherwise set '' in option statement. Nothing to do with assignment to a variable!

                  Ronald
                  That would explain your confusion earlier !

                  Sorry, but I'm still finding my feet with PHP / MYSQL and we all gotta start somewhere right ?

                  So, in conclusion, apart from what you suggested earlier I cannot assign a variable with the 'text' part of a drop down menu selection ?

                  (Last question then I'll leave you alone)

                  Comment

                  • ronverdonk
                    Recognized Expert Specialist
                    • Jul 2006
                    • 4259

                    #10
                    There are 2 ways: see post no 6

                    Ronald

                    Comment

                    Working...