Page 1 of 1

Own function, not giving right output

Posted: Mon Oct 01, 2007 10:54 am
by rongdhonu
Hi,

Can somebody please help me find a solution to this problem. The last value is not being printed. I just get red and blue in the dropdown list.

Code: Select all

showDropdown("red","blue","brown");

Code: Select all

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";
}

Posted: Mon Oct 01, 2007 11:00 am
by s.dot
Wouldn't something like the following be much easier? :P

Code: Select all

function showDropdown($values)
{
    echo '<select>';
    foreach ($values AS $value)
    {
        echo '<option value="' . $value .'">' . $value . '</option>';
    }
    echo '</select>';
}

showDropdown(array("red","blue","brown"));
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.

Posted: Mon Oct 01, 2007 11:15 am
by rongdhonu
Thanks :D

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. :roll:

Posted: Mon Oct 01, 2007 11:33 am
by Zoxive
rongdhonu wrote:Thanks :D

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. :roll:
I would explain it to them then, its just a few more letters they will have to type..

OR if you must

Code: Select all

function showDropdown(){
  $values = func_get_args();
  echo '<select>';
  foreach ($values AS $value){
    echo '<option value="' . $value .'">' . $value . '</option>';
  }
  echo '</select>';
} 

showDropdown("red","blue","brown");

Posted: Mon Oct 01, 2007 12:40 pm
by RobertGonzalez
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.

Is this what you are after?

Posted: Tue Oct 02, 2007 5:40 am
by rongdhonu
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)

Everah, yes you are right.

Posted: Tue Oct 02, 2007 6:35 am
by onion2k
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.

Posted: Tue Oct 02, 2007 6:47 am
by Stryks
Really? I'm sure we could think of worse.

My cat doesn't speak to me if I don't write bad code. Or ... I'd code better but I don't really feel like it.

But I suppose, it's just the worst you've ever heard, not the worst we could come up with, so ....

Posted: Tue Oct 02, 2007 8:22 am
by Zoxive
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.

Posted: Tue Oct 02, 2007 10:21 am
by RobertGonzalez
Try something like this...

Code: Select all

<?php
function showDropdown() { 
  $values = func_get_args(); 
  $return = '';
  if (!empty($values)) {
    $return .= '<select name="formFieldName">';
    foreach ($values AS $value) { 
      $return .= '<option value="' . $value .'">' . $value . '</option>'; 
    } 
    $return .= '</select>'; 
  }
  return $return;
}


// Test it
echo showDropdown('Orange', 'Yellow', 'Green', 'Blue');
?>
It should output:

Code: Select all

<select name="formFieldName"><option value="Orange">Orange</option><option value="Yellow">Yellow</option><option value="Green">Green</option><option value="Blue">Blue</option></select>
At least it does on my machine.

Posted: Wed Oct 03, 2007 12:34 am
by Hemlata
Hello,

Lots of discussion were held on this topic. But here is the temporary fixed for your script. Hope this might solve your problem.

Code: Select all

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";
}
Regards,

Posted: Wed Oct 03, 2007 1:08 am
by s.dot
The previous solutions would be much preferred over a temporary fix like that. :wink: