Page 1 of 1

Php <option>

Posted: Tue Jul 01, 2008 3:30 am
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

Re: Php <option>

Posted: Tue Jul 01, 2008 4:56 am
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

Re: Php <option>

Posted: Tue Jul 01, 2008 7:53 am
by Jack9
so what will be the final code?

(sorry don't know php)
:x

Re: Php <option>

Posted: Tue Jul 01, 2008 1:06 pm
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

Re: Php <option>

Posted: Wed Jul 02, 2008 2:51 am
by Jack9
No but particular option is shown 'selected' according to value from db mysql.

Re: Php <option>

Posted: Wed Jul 02, 2008 3:18 am
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 :)

Re: Php <option>

Posted: Wed Jul 02, 2008 12:52 pm
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 :(

Re: Php <option>

Posted: Thu Jul 03, 2008 2:15 am
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

Re: Php <option>

Posted: Fri Jul 04, 2008 1:53 am
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

Re: Php <option>

Posted: Sun Jul 06, 2008 2:18 pm
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_ .....!