Results 1 to 6 of 6

Thread: Problem with code!

  1. #1
    Join Date
    Sep 2007
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem with code!

    Can someone please help?!

    I need to add a calling statement to the end of the script that calls the checkGrade() function and passes to it the value from the grade form variable. Nothing I tried works!

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Letter Grades</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    </head>
    <body>
    <?php
    function checkGrade($Grade) {
    switch ($Grade) {
    case "A":
    echo "Your grade is excellent.";
    break;
    case "B":
    echo "Your grade is good.";
    break;
    case "C":
    echo "Your grade is fair.";
    break;
    case "D":
    echo "Your are barely passing.";
    break;
    case "E":
    echo "You failed.";
    break;
    default:
    echo "You did not enter a valid letter grade.";
    }
    }

    /* Is this the calling statement that is suppose to be here? It sure doesn't work. $Grade = checkGrade(); */

    ?>
    </body>
    </html>

  2. #2
    Join Date
    Jan 2007
    Location
    The stage
    Posts
    568
    Thanks
    23
    Thanked 6 Times in 6 Posts

    Default

    ok, put in code box and its in a function
    you got to call the function... its sorta like calling your friend and telling him to do something but only when you say so, but you never say so, so he never does it... if you called the have then I don't know what it could be
    to call the function do this
    Code:
    function checkGrade($Grade)
    if that don't work then try this
    Code:
    Code:
    function checkGrade()
    if that don't work then I don't know what it is...

  3. #3
    Join Date
    Sep 2007
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Still no go...

    Thanks for your reply but it seems you are helping me "define" the function. I already did that a the beginning of the script. Now I need help to call the function and pass to it the grade form variable. Thanks in advance to anyone that can help.

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    A function isn't necessary for this at all. PHP's built-in associative arrays can handle it quite nicely.

    I don't see how you've called the function, since a variable to pass as a grade isn't defined anywhere. I suggest sending it via GET:
    Code:
    <?php
      $gradeMessages = array(
        'A' => 'Your grade is excellent.',
        'B' => 'Your grade is good.',
        'C' => 'Your grade is fair.',
        'D' => 'You are barely passing.',
        'E' => 'You failed.'
      );
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
      <head>
        <title>Grade Meanings</title>
      </head>
      <body>
        <div>
          <h1><?php echo $gradeMessages[@$_GET['grade']]; ?></h1>
          <form action="<?php echo $_SERVER['REQUEST_URI']; ?>">
            <label>
              Grade:
              <select>
              <?php foreach(array_keys($gradeMessages) as $grade) { ?>
                <option
                    value="<?php echo $grade; ?>"
                    <?php if($grade === @$_GET['grade']) echo 'selected="selected"'; ?>>
                  <?php echo $grade; ?>
                </option>
              <?php } ?>
              </select>
            </label>
            <input type="submit" value="Submit">
          </form>
        </div>
      </body>
    </html>
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Jan 2007
    Location
    The stage
    Posts
    568
    Thanks
    23
    Thanked 6 Times in 6 Posts

    Default

    Ok sorry I didn't get that in the first post...
    Now If I'm reading your words correctly your saying either 1 of 2 things...
    1. You want to make a form that does a grade check *I don't think so...*
    2. You need help to call the script so that it shows *in that case do this:
    Code:
    function checkGrade()
    That will tell the script IN the function to start! or "call the function"...

    Edit: Twey posted before i did, I hope he's right if he is, then read his thing instead...
    Last edited by Rockonmetal; 09-12-2007 at 11:00 PM. Reason: Someone posted at the same time i did... grr...

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    2. You need help to call the script so that it shows *in that case do this:
    Code:
    function checkGrade()
    That will tell the script IN the function to start! or "call the function"...
    No it won't: the function requires a parameter, which you haven't passed, and PHP functions are called simply as the function name with parameters in brackets: using the "function" keyword here is incorrect.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •