Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: array behavior

  1. #1
    New to the CF scene
    Join Date
    Jan 2006
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    array behavior

    I am trying to create a dropdown menu with options pulled from a MySQL database. I count the number of entries in the database, assign that to a variable, and then insert each entry into an array. I then try to echo the HTML for each option using this loop(where $numdisplayed is the number of times the loop has run, and $quotees is the array that the options for the dropdown menu are stored in.):
    PHP Code:
    while($numdisplayed $numquotees){
    echo 
    '<option value="';
    echo
    "$quotees[$numdisplayed]";
    echo
    '">';
    echo
    "$quotees[$numdisplayed]";
    echo
    "</option>";
    $numdisplayed++;

    It seems as though you can't use a variable to refer to an array key. Any alternatives?

  • #2
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    you should try the echo without the double quotes otherwise the variable interpolation will treat it as 2 different variables
    Code:
    echo $quotees[$numdisplayed];

  • #3
    New Coder
    Join Date
    Jan 2006
    Posts
    43
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Oh, I got same problem sometime ago, through I dont know what is variable interpolation but i used $temp=$quotees[$numdisplayed]; then echo "$temp";
    ...something like that ..It worked for me but I didnt know why

  • #4
    Regular Coder
    Join Date
    Aug 2004
    Location
    The US of A
    Posts
    767
    Thanks
    1
    Thanked 0 Times in 0 Posts
    If you REALLY want to do it inside double quotes then use this..

    PHP Code:
    while($numdisplayed $numquotees){
    echo 
    '<option value="';
    echo 
    "{$quotees[$numdisplayed]}";
    echo 
    '">';
    echo 
    "{$quotees[$numdisplayed]}";
    echo 
    "</option>";
    $numdisplayed++;

    But a better way would be...

    PHP Code:
    while($numdisplayed $numquotees){
    echo 
    '<option value="' $quotees[$numdisplayed] . '">' $quotees[$numdisplayed]. '</option>';
    $numdisplayed++;

    Or something similar.

    I myself prefer to use sprintf for this type of thing. However, you could use just printf in this case.

    PHP Code:
    printf('<option value="%1$s">%1$s</option>'$quotees[$numdisplayed]); 

  • #5
    New to the CF scene
    Join Date
    Jan 2006
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up

    none of that produces any output... I suppose I am lucky enought to have a strange configeration or something. Does anybody know of a place where I can learn some more about quotes, double quotes, and no quotes?
    Last edited by sparrow91; 02-15-2006 at 10:29 PM.


  •  

    Posting Permissions

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