Php <option>

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
Jack9
Forum Newbie
Posts: 6
Joined: Tue Jul 01, 2008 3:15 am

Php <option>

Post by Jack9 »

I m newbie
Want to do some change in php script code.

Code: Select all

                    $weather = '<select name="bx11" size="1">';
 
                        // need wet & dry option here
                        for ($k = 1; $k < 3; $k++) 
                        {
                            $selected            = ( $k == $tipp_array[10]) ? 'selected' : '';
                            $weather        .= '<option value="' . $k . '" ' . $selected . '>' . $k . '</option>';
                        }
                        $weather .= '</select>';                        
                        
                    }
Above code generates select option box with option 1 & 2.
Instead of 1 & 2 want to display Dry(value=1) & Wet (value=2).

Thanks in advance
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Php <option>

Post by jaoudestudios »

as it is just 2 options (wet & dry) why dont you just hard code them in?

otherwise...

// create an array
$a_ = array('Dry', 'Wet');

// and do a foreach instead of a for loop
foreach ($a_ as $key=>$value) {

}
// $key will equal Dry/Wet
// and $value will equal 0/1

If you want 1 and 2, then you could create the array like...
$a_ = array(1=>'Dry', 2=>'Wet');

Eddie
Jack9
Forum Newbie
Posts: 6
Joined: Tue Jul 01, 2008 3:15 am

Re: Php <option>

Post by Jack9 »

so what will be the final code?

(sorry don't know php)
:x
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Php <option>

Post by jaoudestudios »

if it is just for those two options I would just hard code it, it will save you so much time.

$weather = "<select name='bx11' size='1' id='bx11'>";
$weather .= "<option value='1'>Wet</option>";
$weather .= "<option value='2'>Dry</option>";
$weather .= "</select>";

Hope this helps,
Eddie
Jack9
Forum Newbie
Posts: 6
Joined: Tue Jul 01, 2008 3:15 am

Re: Php <option>

Post by Jack9 »

No but particular option is shown 'selected' according to value from db mysql.
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Php <option>

Post by jaoudestudios »

yes you are right, sorry I did that code on my blackberry.

with the auto selected included it is:

if ($tipp_array[10] == 1) { $selected_1 = "selected='selected'"; }
elseif ($tipp_array[10] == 2) { $selected_2 = "selected='selected'"; }
$weather = "<select name='bx11' size='1' id='bx11'>";
$weather .= "<option value='1' ".$selected_1.">Wet</option>";
$weather .= "<option value='2' ".$selected_2.">Dry</option>";
$weather .= "</select>";

that should do it :)
Jack9
Forum Newbie
Posts: 6
Joined: Tue Jul 01, 2008 3:15 am

Re: Php <option>

Post by Jack9 »

Code: Select all

if ($tipp_array[10] == 1) { $selected_1 = "selected='selected'"; }
elseif ($tipp_array[10] == 2) { $selected_2 = "selected='selected'"; }
$weather = "<select name='bx11' size='1' id='bx11'>";
$weather .= "<option value='1' ".$selected_1.">Wet</option>";
$weather .= "<option value='2' ".$selected_2.">Dry</option>";
$weather .= "</select>";
didn't work :(
Jack9
Forum Newbie
Posts: 6
Joined: Tue Jul 01, 2008 3:15 am

Re: Php <option>

Post by Jack9 »

Undefined variable: selected_1

and sometimes .. Undefined variable: selected_2 :banghead:
i placed this above your code
$selected_1 = '';
$selected_2 = '';
and now no error .. is this correct way?

have one more ...

Code: Select all

        $combo_weather = '<select name="place11" size="1">';
            //need wet & dry option
            for ($k = 1; $k < 3; $k++) 
            {
                if (isset($result_array[10]))
                {
                    $selected = ( $k == $result_array[10]) ? 'selected' : '';
                }
                else
                {
                    $selected = '';
                }           
                $combo_weather .= '<option value="' . $k . '" ' . $selected . '>' . $k . '</option>';
            }
            $combo_weather .= '</select>';
thanks :D
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Php <option>

Post by jaoudestudios »

hey,

sorry for not getting back to you soon.

The notice is just a notice/advice from php, the code will work as long as there are no warnings or errors. It is very hard to remove all notices as you must declare everything officially. Php is very forgiving so if you dont declare it it will still use the variable and auto declare it for you, but then give a notice. On your testing server you must be in development mode which displays notices which is good as it helps when debugging.

Glad it works, well done, you understood the code.

Code: Select all

$selected_1 = '';
$selected_2 = '';
The above is a way of declaration - but you will soon see this is not always an option as if data is coming from another page then you would reset it. Its all a beautiful learning curve :)

I am just about to run out so I will take a look at your other code tonight.
Eddie
Jack9
Forum Newbie
Posts: 6
Joined: Tue Jul 01, 2008 3:15 am

Re: Php <option>

Post by Jack9 »

thanks Eddie ..your really very helpful :D

the code is from phpBB mod & running in debug mode.

I was looking around ... just read that phpBB suggest not to use html code in php file but instead use it template file.

Do you know phpBB also ?

Going to read about it soon .. template functions <!-- If S_ .....!
Post Reply