SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Member
    Join Date
    May 2011
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP Dynamic dropdown

    I'm having some problems getting a dynamic drop box to work in PHP. The $mem is stored in a previous .php file, this page is included in that file ( hope that makes sense). When i load up the page all that i see is the drop down box with -- Please select -- in it. It is not pulling from the database. Any suggestions?



    PHP Code:
    <?php
    mysql_connect
    ("localhost""login""password") or die(mysql_error());
    mysql_select_db("database") or die(mysql_error());

    echo 
    "<br>";
    $data mysql_query("select * from Character WHERE owner='$mem'");
    echo 
    "<select name=\"queryTypeCombo\" onchange=submitForm()>\n";
    echo 
    "<option>-- Please select --</option>\n";

    while (
    $row mysql_fetch_assoc($data))
    {
    $ID $row['CName'];
    echo 
    "<option value ='$ID'>"
    }
    echo 
    "</select>\n";
    echo 
    "</p>";

    ?>

  2. #2
    SitePoint Zealot Cute Tink's Avatar
    Join Date
    Apr 2009
    Posts
    152
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What is $mem and where does it come from? Are you sure that this variable is getting populated correctly? You should echo "select * from Character WHERE owner='$mem'" and make sure that it says what you expect it to.

    Also, if all you need is 'CName', why are you getting all of the columns from the database? If you use more columns elsewhere in your script, ignore this question.

  3. #3
    SitePoint Member
    Join Date
    May 2011
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $tta mysql_query("SELECT * FROM `Character` WHERE owner='$mem'") or die(mysql_error());  
    $test mysql_fetch_array $tta );
    echo 
    $test['CName']; 
    Placed this above and i was able to get it to display exactly what i needed it to, however i still cannot get it to populate the drop down box. I was having problems with it displaying at all until i added the `'s around Character.. So it appears that is now working correctly it seems something is wrong with the rest of the code.

    PHP Code:
    echo "<select name=\"queryTypeCombo\" onchange=submitForm()>\n";

    echo 
    "<option>-- Please select --</option>\n";



    while (
    $row mysql_fetch_assoc($data))

    {

    $ID $row['CName'];

    echo 
    "<option value ='$ID'>"

    }

    echo 
    "</select>\n";

    echo 
    "</p>";



    ?> 

  4. #4
    SitePoint Member
    Join Date
    May 2011
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Using the following code i am able to get it to display blank spaces in the drop down bar in the exact amount i'd expect however there is no text. Appears to be something wrong with the echo "<option value =\$ID\>";
    line. I've tried '$ID'

    PHP Code:
    <?php
    mysql_connect
    ("localhost""""") or die(mysql_error());
    mysql_select_db("") or die(mysql_error());

    echo 
    "<br>";


    $tta mysql_query("SELECT * FROM `Character` WHERE owner='$mem'") or die(mysql_error());  

    echo 
    "<br>";
    echo 
    "<select name=\"queryTypeCombo\" onchange=submitForm()>\n";
    echo 
    "<option>-- Please select --</option>\n";

    while (
    $row mysql_fetch_array($tta))
    {
    $ID $row['CName'];
    echo 
    "<option value =\$ID\>"
    }
    echo 
    "</select>\n";
    echo 
    "</p>";

    ?>
    Thank you for any help!

  5. #5
    SitePoint Zealot
    Join Date
    Feb 2005
    Location
    Aveiro, Portugal
    Posts
    164
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I think you got almost everything right, except for the HTML.

    Each <option> line should be:
    Code HTML4Strict:
    <option value=""></option>

    Thus:
    PHP Code:
    while ($row mysql_fetch_array($tta))
    {
    $ID $row['CName'];
    echo 
    "<option value =\"$ID\">$ID</option>"

    Of course you can place any other field value between <option> and </option> given you extract it inside your while loop.

    HTH
    Yours truely
    Mário Ramos

  6. #6
    SitePoint Enthusiast derokorian's Avatar
    Join Date
    Jan 2011
    Location
    Ohio
    Posts
    57
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Note you are only setting the value for the option... you should probably do something like

    PHP Code:
    while ($row mysql_fetch_array($tta)) 

       
    $ID $row['CName']; 
       echo 
    "<option value =\"$ID\">$ID</option>";  


    *damn you mjpr
    Last edited by derokorian; May 16, 2011 at 17:38. Reason: slow

  7. #7
    SitePoint Zealot Cute Tink's Avatar
    Join Date
    Apr 2009
    Posts
    152
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Alteredd View Post
    Placed this above and i was able to get it to display exactly what i needed it to, however i still cannot get it to populate the drop down box. I was having problems with it displaying at all until i added the `'s around Character..
    "character" is probably a special word to mysql. That's why you would need the backquotes around it. Good catch.

    The other two probably have the reason why your options aren't displaying.

  8. #8
    SitePoint Member
    Join Date
    May 2011
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That did the trick, thank you everyone!

    I haven't looked into it yet, plan on researching more tomorrow, but how can i use the value thats been selected. This is way above my knowledge of PHP I still have a TON to learn, i would guess how it's setup that the <option value =\"$ID\"> the $ID is the value? Should i change that to something different? Like i said i'll look around more tomorrow but if it's something easy would love to know.

    Again, thanks for all the help!

  9. #9
    SitePoint Enthusiast derokorian's Avatar
    Join Date
    Jan 2011
    Location
    Ohio
    Posts
    57
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <option value="--XX--">--YY--</option>

    --YY-- is what you would see in the drop down menu on the page with the form.

    --XX-- is what the processing script will see when it access the select item.

  10. #10
    Non-Member bronze trophy
    Join Date
    Nov 2009
    Location
    Keene, NH
    Posts
    3,760
    Mentioned
    23 Post(s)
    Tagged
    0 Thread(s)
    This is a perfect example of what I like to call "double quotes and extra echo's for nothing". Double quotes for echo just make the code more confusing through the use of unneccessary escapes and/or just plain long code. Likewise if you use single quotes you don't NEED to say "/n" or "/t" since single quotes preserve whitespace. Also no clue why you felt the need for the extra variable inside the loop.

    Code:
    $result=mysql_query("select * from Character WHERE owner='$mem'"); 
    
    echo '<br />
    <select name="queryTypeCombo" onchange="submitForm()">
    	<option>-- Please select --</option>';
    
    while ($row = mysql_fetch_assoc($data)) { 
    	echo '
    	<option value ="',$row['CName'],'">';  
    } 
    
    echo '
    </select>';
    I'd also suspect other code issues, since a select is NOT a paragraph, and doesn't belong in a paragraph... (neither is a label)

    I really wonder why anyone bothers with double quotes 99% of the time they use them.

  11. #11
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    PHP Code:
    $result=mysql_query("select * from Character WHERE owner='$mem'"); 

    echo 
    '<br /><select name="queryTypeCombo" onchange="submitForm()">
        <option>-- Please select --</option>'
    ;

    while (
    $row mysql_fetch_assoc($data)) { 
        echo 
    '<option value ="',$row['CName'],'">' PHP_EOL ;  


    echo 
    '</select>' PHP_EOL
    I find that even tidier - don't use hard returns to add new lines in your html source code, use the OS-neutral constant PHP_EOL - and as said don't pander to feeling to you have to use double quotes just to insert all this "\n" nonsense.

    (Having said that yesterday I had to add "\n" to an xml stream test, so never say never eh?)

    Use double quotes when it suits you. e.g when interpolating a scalar variable like so:
    PHP Code:
    echo "This $post_name makes me $body_function, no really."
    There are other ways too, just thought I'd add my 2c.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •