Foreach Probems [All Fixed]

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
watson516
Forum Contributor
Posts: 198
Joined: Mon Mar 20, 2006 9:19 pm
Location: Hamilton, Ontario

Foreach Probems [All Fixed]

Post by watson516 »

Hello,

I seem to be running into a bit of an issue that I could really use the help with. I have probably just missed something simple but I am starting to get frustrated with it so I thought it best to see if anyone can help.

What it is suppose to do:
Return a fully working select box for an html form. It is also suppose to check to see if the index of an array item is 'SELECTED' and if it is then add SELECTED to that option and only that option. It should also only allow for one item to be selected.

What does not work:
The check to see if a SELECTED has already been placed ($selected). If I get rid of that if, there are two SELECTED printed (the if prints the incorrect item).

Code:

Code: Select all

class Page {
    
    public $userSex1 = array('SELECTED' => 'Male','Female','Other');
    public $userSex2 = array('Male','SELECTED' => 'Female','Other');
    
    public function renderSelect($name,$options=0,$min=18,$max=60,$action='none') {
        $source = '<select name="'.$name.'">';
        if(is_array($options)) {
            $selected = 0;
            foreach($options as $key => $value) {
                if($key == 'SELECTED' AND $selected == 0) {
                    $source .= '<option value="'.$value.'" SELECTED>'.$value.'</option>';
                    $selected = 1;
                }else{
                    $source .= '<option value="'.$value.'">'.$value.'</option>';
                }
            }
        }else{
            $count = $min;
            while($count<$max) {
                $source .= '<option value="'.$count.'">'.$count.'</option>';
                $count++;
            }
            $source .= '<option value="'.$count.'+">'.$count.'+</option>';
        }
        $source .= '</select>';
        return $source;
    }
    
}
One last thing...

When I call this function, is there a way to pass the class's member array and change one of the keys in that array all in one statement? I would like to keep the page coding to a minimum and having a ton of different member arrays inside the class just seems like a waste when most of them will be the same (except for the selected one).

Thanks.
Last edited by watson516 on Thu Aug 27, 2009 7:41 pm, edited 1 time in total.
watson516
Forum Contributor
Posts: 198
Joined: Mon Mar 20, 2006 9:19 pm
Location: Hamilton, Ontario

Re: Foreach Probems

Post by watson516 »

I have just been trying to figure it out and it appears to be something to do with the

Code: Select all

if($key == 'SELECTED')
part of the code. I replaced it with

Code: Select all

if(!is_int($key))
and the appropriately selected array element is properly set as default when the page is loaded.

If I place in something like...

Code: Select all

array('SELECTED'=>'Male','Female','SELECTED'=>'Other')
I end up completely missing the 'Male' option and the 'Female' and 'Other' are in reverse order. Any ideas?
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Foreach Probems

Post by Darhazer »

You are comparing an integer value (0, 1) with a string
In that case string is casted to integer, and 'SELECTED' casted to integer is exactly 0
Using == instead of === will resolve the issue
See:
http://us3.php.net/manual/en/language.o ... arison.php
http://us3.php.net/manual/en/types.comparisons.php
watson516
Forum Contributor
Posts: 198
Joined: Mon Mar 20, 2006 9:19 pm
Location: Hamilton, Ontario

Re: Foreach Probems

Post by watson516 »

Thank you sir. Completely forgot about the ===
watson516
Forum Contributor
Posts: 198
Joined: Mon Mar 20, 2006 9:19 pm
Location: Hamilton, Ontario

Re: Foreach Probems [All Fixed]

Post by watson516 »

Well, I feel like a complete idiot.

Just for future reference, if you have an array with two keys the same, the last one (with the same name) will overwrite all previous ones when the array is created. Seems pretty obvious eh?
Post Reply