PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
function showDropdown($value){
global $attributes;
$wordArray;
$z=0;
$word="";
for($i=0;$i<=strlen($value);$i++){
$oneChar=substr($value,$i,1);
//Until come accross a comma
if($oneChar!=',' ){
//add one character at a time to $word
$word=$word."$oneChar";
}
//when comma is encountered, add $word to $wordArray[]. $word now holds a value.
else{
$wordArray[$z]=$word;
// next value will be added to $wordArray[1] and so on
$z++;
// make $word empty so it can hold the next value
$word="";
}
}
echo"<option value=$word>Select a value</option>\n";
foreach($wordArray as $value){
echo"<option>$value</option>\n";
}
echo"</select>\n";
}
EDIT| Anyways, the reason it's not working is because there is no comma after the last value. Your script is looking for a comma before it adds the word to $wordArray.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
But it doesn't serve my purpose. I must call the function as showDropdown("red","blue","brown") and not showDropdown(array("red","blue","brown")) . It will be used by non-techy person who doesnot know what array is! So cant include that in the function call.
Also, I used your example and it outputs the value outside the dropdown menu.
But it doesn't serve my purpose. I must call the function as showDropdown("red","blue","brown") and not showDropdown(array("red","blue","brown")) . It will be used by non-techy person who doesnot know what array is! So cant include that in the function call.
Also, I used your example and it outputs the value outside the dropdown menu.
I would explain it to them then, its just a few more letters they will have to type..
I think you are going to have to go with something like what Zoxive posted, but this may defy your logic a bit. It sounds like what you are after is a way to take any number of passed values and make a list of them. That means that you will need to dynamically figure out what was passed to the function and turn that into an array then loop the array to get your output.
Zoxive, I dont know why but your last bit of code outputs something that looks strange in IE7, it shows the values outside of the dropdown box (In Mozila however, it shows fine)
rongdhonu wrote:But it doesn't serve my purpose. I must call the function as showDropdown("red","blue","brown") and not showDropdown(array("red","blue","brown")) . It will be used by non-techy person who doesnot know what array is! So cant include that in the function call.
That's the worst reason for writing bad code I've ever heard.
rongdhonu wrote:Zoxive, I dont know why but your last bit of code outputs something that looks strange in IE7, it shows the values outside of the dropdown box (In Mozila however, it shows fine)
It wasn't meant to be your exact code. It was an example, and is simple enough that you should be able to change the output html.
showDropdown('"red","blue","brown",');// Added additional comma after last element
function showDropdown($value)
{
global $attributes;
$wordArray = array();
$z=0;
$word="";
for($i=0;$i<=strlen($value);$i++)
{
$oneChar=substr($value,$i,1);
//Until come accross a comma
if($oneChar!=',' )
{
//add one character at a time to $word
$word=$word."$oneChar";
}
//when comma is encountered, add $word to $wordArray[]. $word now holds a value.
else
{
$wordArray[$z]=$word;
// next value will be added to $wordArray[1] and so on
$z++;
// make $word empty so it can hold the next value
$word="";
}
}
echo '<select>';// You missed this in your script
echo"<option value=$word>Select a value</option>\n";
foreach($wordArray as $value)
{
echo"<option>$value</option>\n";
}
echo"</select>\n";
}
The previous solutions would be much preferred over a temporary fix like that.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.