Results 1 to 8 of 8

Thread: This is sad. I need help :P

  1. #1
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default This is sad. I need help :P

    Code:
    <?php
    session_start();
    $answer = $_SESSION["answer"];
    $user = $_POST["user"];
    $pass = $_POST["pass"];
    $cpass = $_POST["cpass"];
    $email = $_POST["email"];
    $cemail = $_POST["cemail"];
    $valid = $_POST["valid"];
    $error = "";
    $count = 0;
    function add($txt) {
      global $count, $error;
      $count++;
      $error .= "\n<br>".$count.". ".$txt;
    };
    if ($cpass != $pass) {
      add("Your second password did not match the original.");
    };
    if ($cemail != $email) {
      add("Your second e-mail address did not match the original.");
    };
    if ($valid != $answer) {
      add("Your Roman Numeral answer did not match! Please click the link regarding Roman Numerals on the previous page.");
    };
    if (!eregi("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$]", $email)) {
      add("You did not enter a valid e-mail.");
    };
    echo $error;
    ?>
    For some reason the add() function won't add the string to the $error variable. I've been poking around with this for a while now, and I just can't get it. It's probably just some stupid mistake I'm overlooking.
    - Mike

  2. #2
    Join Date
    Oct 2006
    Posts
    183
    Thanks
    0
    Thanked 11 Times in 11 Posts

    Default

    Try this:
    Code:
    <?php
    session_start();
    $answer = $_SESSION["answer"];
    $user = $_POST["user"];
    $pass = $_POST["pass"];
    $cpass = $_POST["cpass"];
    $email = $_POST["email"];
    $cemail = $_POST["cemail"];
    $valid = $_POST["valid"];
    
    
    if ($cpass != $pass) {
      $error[] = "Your second password did not match the original.";
    };
    if ($cemail != $email) {
      $error[] = "Your second e-mail address did not match the original.";
    };
    if ($valid != $answer) {
      $error[] = "Your Roman Numeral answer did not match! Please click the link regarding Roman Numerals on the previous page.";
    };
    if (!eregi("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$]", $email)) {
      $error[] = "You did not enter a valid e-mail.";
    };
    
    
    if(isset($errors))
    {
      for($i = 0; $i < count($errors); $i++)
      {
        echo "$i. " . $errors[$i] . "<br>";
      }
    }
    ?>

  3. The Following User Says Thank You to motormichael12 For This Useful Post:

    mburt (08-07-2008)

  4. #3
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Yeah I know that will work, it's just I wanted to use a function. Oh well, I can live with it.
    - Mike

  5. #4
    Join Date
    Feb 2006
    Posts
    236
    Thanks
    8
    Thanked 3 Times in 3 Posts

    Default

    Could it be the ';' after the closing '}' used in various places?

  6. #5
    Join Date
    Oct 2006
    Posts
    183
    Thanks
    0
    Thanked 11 Times in 11 Posts

    Default

    I testede this with some conditions that I knew would work or not and it worked fine:
    Code:
    <?php
    function add($txt) {
      global $count, $error;
      $count++;
      $error .= "\n<br>".$count.". ".$txt;
    };
    if (1==1) {
      add("Your second password did not match the original.");
    };
    if (2 != 1) {
      add("Your second e-mail address did not match the original.");
    };
    if ("blah" == "blah") {
      add("Your Roman Numeral answer did not match! Please click the link regarding Roman Numerals on the previous page.");
    };
    if (1 != 1) {
      add("You did not enter a valid e-mail.");
    };
    echo $error;
    ?>
    I see no reason why the original wasn't working.

  7. #6
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    while PHP doesn't have an add() function to any other developer that might appear that you attempting to do a mathematical equation.

    There are a couple of other coding practices that could be implemented differently.
    It's generally good habit to stay away from global variables as much as possible, because as stated, they are global, meaning available whereever and whenever... and can cause about as big of a headache as a missing semi colon LOL.

    It is also good practice to separate the code from the output.

    Also, there is no way of scaling this function into other application, which is one of the main reasons functions are created.

    Below is a function I created a while ago that will address the issues above.

    PHP Code:
    function &addToArray($input$array$clear=false)
    {
        if(
    $clear)
        {
            unset(
    $array);
            
    $array = array();
        }
        
        if(
    is_array($input))
        {
             foreach(
    $input as $val)
             {
                  
    $array[] = $val;
             }
        }
        else
        {
             
    $array[] = $input;
        }
        return 
    $array;


  8. #7
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Thanks boogyman, I appreciate the advice. However, I have the problem solved now.
    - Mike

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

    Default

    I made a post recently detailing three fairly critical points of good coding style. It's now in my sig under 'how to code', but I'll link to it here too: How to Code. These guidelines apply to any language, to the extent permissible by that language.
    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
  •