Own function, not giving right output

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!

Moderator: General Moderators

Post Reply
rongdhonu
Forum Newbie
Posts: 4
Joined: Mon Oct 01, 2007 10:45 am

Own function, not giving right output

Post 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";
}
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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.
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.
rongdhonu
Forum Newbie
Posts: 4
Joined: Mon Oct 01, 2007 10:45 am

Post 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:
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post 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");
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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?
rongdhonu
Forum Newbie
Posts: 4
Joined: Mon Oct 01, 2007 10:45 am

Post 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.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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.
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Post 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 ....
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
Hemlata
Forum Commoner
Posts: 35
Joined: Mon Sep 10, 2007 5:40 am
Location: India
Contact:

Post 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,
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

The previous solutions would be much preferred over a temporary fix like that. :wink:
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.
Post Reply