Jump to content

Recommended Posts

please i have an error message as viz(Notice: Undefined variable: select in C:\xampp\htdocs\interest.php) running this code

<?php
//lets select options from a dropdown list using php functions
$month = array("jan" => "Jan", "feb" => "Feb", "mar" => "Mar", "apr" => "Apr", "may" => "May", "jun" => "Jun", "jul" => "Jul", "aug" => "Aug", "sep" => "Sep", "oct" => "Oct", "nov" => "Nov", "dec" => "Dec");
		 $default = "jan";
$select = generate_menu("month", $month, $default);
function generate_menu($name, $month, $default="") {
$select .= "<SELECT NAME=\"$name\">";						<<<<------- this is where the error is located
foreach($month as $value => $label) {
$select .= "<OPTION ";
if ($value == $default)
$select .= "SELECTED ";
$select .= "VALUE=\"$value\">$label</OPTION>";
}
$select .= "</SELECT>";
return($select);
}
?>
<FORM ACTION="month.php" METHOD=POST>
<?php print $select;?>
<INPUT TYPE=SUBMIT VALUE="Continue">
</FORM>

Edited by Zane

Please use the code tags and indent your code.

 

Within your function, $select is not defined. You probably want to just create it the first time, rather than trying to append to it.

$select = "<select...

rather than

$select .= "<select...

Post your updated code. In code tags. Use the plain text editor if you need to.

If you changed it EVERYWHERE and not just the first time you try to use that variable, that won't work. Think about it.

the updated code took off the error but wasnt giving me the dropdown list i coded

this is the updated code

<?php
//lets select options from a dropdown list using php functions
$month = array("jan" => "Jan", "feb" => "Feb", "mar" => "Mar", "apr" => "Apr", "may" => "May", "jun" => "Jun", "jul" => "Jul", "aug" => "Aug", "sep" => "Sep", "oct" => "Oct", "nov" => "Nov", "dec" => "Dec");
		 $default = "jan";
$select = generate_menu("month", $month, $default);
function generate_menu($name, $month, $default="") {
$select= "<SELECT NAME=\"$name\">";
foreach($month as $value => $label) {
$select= "<OPTION ";
if ($value == $default)
$select= "SELECTED ";
$select= "VALUE=\"$value\">$label</OPTION>";
}
$select= "</SELECT>";
return($select);
}
?>
<FORM ACTION="month.php" METHOD=POST>
<?php print $select;?>
<INPUT TYPE=SUBMIT VALUE="Continue">
</FORM>

and more over the editor am using is notepad+++

please help me out i've been sick of this code for sum days now

Edited by Zane

Using the advice that Jesi initially gave you, and with a little cleanup added, this code should work for you:

 

$month = array("jan" => "Jan", "feb" => "Feb", "mar" => "Mar", "apr" => "Apr", "may" => "May", "jun" => "Jun", "jul" => "Jul", "aug" => "Aug", "sep" => "Sep", "oct" => "Oct", "nov" => "Nov", "dec" => "Dec");
$default = "jan";
$menu = generate_menu("month", $month, $default);
function generate_menu($name, $month, $default="")
{
$select = "<select name='$name'>";
foreach($month as $value => $label)
{
$select .= "<option ";
if ($value == $default)
$select .= "selected='selected' ";
$select .= "value='$value'>$label</option>";
}
$select .= "</select>";
return $select;
}
echo $menu;

 

Also, the function returned $select from the initial post.

Edited by AyKay47

Post your updated code. In code tags. Use the plain text editor if you need to.

If you changed it EVERYWHERE and not just the first time you try to use that variable, that won't work. Think about it.

You did like maybe 1/5th of what this post says. 

much tanx to Jessica, Aykay47 & Barand 4 helping me out with my prob i used Aykay47's anwser and it worked out well....

much tanx 2 Jessica for ur's 2...... like i've said before am new to php and programming environment wish to be creative using php

You didn't post using the code tags. You apparently didn't read the entire second sentence, because it explains what's wrong with your code. I'm guessing you don't understand what .= and = do. 

You didn't post using the code tags. You apparently didn't read the entire second sentence, because it explains what's wrong with your code. I'm guessing you don't understand what .= and = do.

haha i forgot adding the tag

tanx a lot Jessy wil love u helping out next time on any prob

Like Jessica stated, this forum isn't like Twitter. Writing in shortened internet lingo only makes it harder for us to understand what you're saying, which ultimately makes it harder for us to help you. We don't have a 140 character limit, so slow down, write complete sentences, and be as clear and concise as you can about your problem.

 

When posting code, place it within


tags. That will format it and apply syntax highlighting so we can more easily see what's going on.

 

In short, the easier it is for us to understand you and your problem, the faster it will be addressed.

As for what Jess said on like the first or second post she did - about the indenting - when you copy and paste code into the text editor, the text editior takes out all of the indenting thereby making it all just a plain list.

As for what Jess said on like the first or second post she did - about the indenting - when you copy and paste code into the text editor, the text editior takes out all of the indenting thereby making it all just a plain list.

Not if you use the plain text editor. 

function test(){
echo 'hello world';
}

 

I c&p that from my text editor into the plain text here.

but please @jessy what are diff btw .= & = and what are their functions to the code

 

You may have figured it out by now but replying to your question '=' means you're assigning a value to a variable. For example:

 $a = 5; //means your variable $a receives the value 5

When you use '.=' it will append a string to the value already contained in the variable like

$a = "This is the first sentence."; $a .= " This is the second sentence.";

After this your variable $a will contain the string "This is the first sentence. This is the second sentence.".

 

I hope it's clear enough for you to understand.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.