Rookie problem in learning PHP

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
Wickings
Forum Newbie
Posts: 5
Joined: Tue Sep 28, 2010 11:29 am

Rookie problem in learning PHP

Post by Wickings »

Hi guys!

Short intro: I study at a university in denmark, and this semester includes PHP coding. So i've only been working with php for a couple of weeks, and I still run in to basic, stupid problems, as u will probably see, hehe.

Anyway, i come to you asking what mistake i made in the following code:

<?php
$day = range(1, 31);
foreach ($day as $value) {
echo "<option value=\"$value\"";
if ($_POST['day'] == $value) {
echo "selected=\"selected\"";
}
echo ">$value</option>\n";
}

?>

Now, what i'm trying to do: I'm currently working on a reference form for people to fill out on a web page. In this form they should enter their date of birth. And with the code above, as you might see, i'm trying to make the array sticky. However, nomatter what i try, it gives me an error when i test the site (using WAMP server).

Can anyone of you correct my code and shed light over what i did wrong? tried multiple things, nothing worked. :?

Also, since our teacher is not really any good at teaching, could someone explain me what "selected=\"selected\"" does? She sent out the command in a mail with the comment "just copy paste my code"...

Many thanks!
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Rookie problem in learning PHP

Post by Jonah Bron »

Code: Select all

<?php
$day = range(1, 31);
foreach ($day as $value) {
    echo '<option value="' . $value . '"';
    if ($_POST['day'] == $value) {
        echo ' selected="selected"';
    }
    echo '>' . $value . '</option>\n';
}
?> 
It looked okay, but try the above. 'selected="selected"' sets that item in the menu as the default option.
Wickings
Forum Newbie
Posts: 5
Joined: Tue Sep 28, 2010 11:29 am

Re: Rookie problem in learning PHP

Post by Wickings »

Hmm unfortunately that doesn't work either. I copypasted it to replace my current code, that was what you meant, right? :P

Could the error in my original code be that i at some point mix up the variables that i tell it to display?
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Rookie problem in learning PHP

Post by Jonah Bron »

Oh, you have an error? What is it? Errors have a reason for being there, and the always provide useful information. If you are asking for help with a problem, post errors.
Wickings
Forum Newbie
Posts: 5
Joined: Tue Sep 28, 2010 11:29 am

Re: Rookie problem in learning PHP

Post by Wickings »

well, its my browser that displays the error:
Parse error: parse error in C:\wamp\www\test\MP3_case_2010\make_references.php on line 247

and since i end the </html> in line 247, i guess its a syntax error somewhere. the script itself is in line 166 of the document
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Rookie problem in learning PHP

Post by twinedev »

When you get an error that reports on the last line, especially when that line isn't a PHP line, the first thing to look for is mismatched curly braces { }, or possibly forgotten closing php tag ( ?> ) before going back to HTML.

(ie. you opened one to start an if or some type of loop, and forgot to close it).

Also, the selected="selected" code will tell the select box which option to have selected by default when the page loads.

-Greg
Wickings
Forum Newbie
Posts: 5
Joined: Tue Sep 28, 2010 11:29 am

Re: Rookie problem in learning PHP

Post by Wickings »

Thanks guys:P i actually found the syntax error ( expendable { in line 79, d'oh)

however, now it gives me this when i select the menu:

Image

any suggestions?

also thanks for all ur patience =)
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Rookie problem in learning PHP

Post by Jonah Bron »

$_POST['day'] should be getting the input element "day" from the previous page. Are you sure you submitted form data to the page?
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Rookie problem in learning PHP

Post by twinedev »

Try this:

Code: Select all

if (isset($_POST['day']) && $_POST['day'] == $value) {
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Rookie problem in learning PHP

Post by McInfo »

It's unnecessary to check 31 times whether the variable is set. Move isset() out of the loop. Initialize another variable outside the loop and use that to store the posted value if there happens to be one; then use the new variable in the comparison within the loop. Otherwise, keep $_POST['day'] inside the loop, but outside the loop, if $_POST['day'] is not set, initialize it to something that won't be matched with any of the values within the loop.
Post Reply