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
Jack9
Forum Newbie
Posts: 6 Joined: Tue Jul 01, 2008 3:15 am
Post
by Jack9 » Tue Jul 01, 2008 3:30 am
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
jaoudestudios
DevNet Resident
Posts: 1483 Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey
Post
by jaoudestudios » Tue Jul 01, 2008 4:56 am
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
Post
by Jack9 » Tue Jul 01, 2008 7:53 am
so what will be the final code?
(sorry don't know php)
jaoudestudios
DevNet Resident
Posts: 1483 Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey
Post
by jaoudestudios » Tue Jul 01, 2008 1:06 pm
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
Post
by Jack9 » Wed Jul 02, 2008 2:51 am
No but particular option is shown 'selected' according to value from db mysql.
jaoudestudios
DevNet Resident
Posts: 1483 Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey
Post
by jaoudestudios » Wed Jul 02, 2008 3:18 am
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
Post
by Jack9 » Wed Jul 02, 2008 12:52 pm
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
Post
by Jack9 » Thu Jul 03, 2008 2:15 am
Undefined variable: selected_1
and sometimes .. Undefined variable: selected_2
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
jaoudestudios
DevNet Resident
Posts: 1483 Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey
Post
by jaoudestudios » Fri Jul 04, 2008 1:53 am
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
Post
by Jack9 » Sun Jul 06, 2008 2:18 pm
thanks Eddie ..your really very helpful
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_ .....!