...

View Full Version : array behavior



sparrow91
02-15-2006, 01:18 AM
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.):

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?

shyam
02-15-2006, 09:59 AM
you should try the echo without the double quotes otherwise the variable interpolation will treat it as 2 different variables

echo $quotees[$numdisplayed];

funhunter
02-15-2006, 10:25 AM
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 :)

Kurashu
02-15-2006, 09:11 PM
If you REALLY want to do it inside double quotes then use this..


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

But a better way would be...


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.



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

sparrow91
02-15-2006, 09:21 PM
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?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum