I have a MySQL table of error codes with three columns:
error_code, page, and error_msg.
On checking for errors I create an array called $error with all the error codes generated. I then use a

foreach ($error as $error_code) {get_error($error_code, $page); }

The get_error function connects to the db and echos the error message. I also need to have the function create a variable named after the error code. ie $error1, $error2, $error3, etc... that the script can look for in order to modify the output to show where the error is. ie.

$errorstyle = "background:yellow;";
// many lines later
echo"
Address:&nbsp;<INPUT TYPE='text' NAME='address' VALUE='$address' STYLE='";
if ($error1) {echo $errorstyle;}
echo"'>";

inside of HTML tags to change the background color for highlighting purposes.

This is where I am lost... is there a way to create a variable named after another variable...?
The only option I have right now if to use an if, else if, and specify every single error in the code (if this is the case I could just get rid of the table in the db for the error codes and put it all hard coded.... but I would really like to avoid this as it is really long coding and it basically defeats the purpose of dynamic content...

Any help on this issue is much appreciated, Thanks,

-Jesse

    I dont see where in your code you are connecting to the db.

    Have you considered using a loop to print out the error codes
    something like:

    if ($error) {
    $result = mysql_query("SELECT * FROM table WHERE error_code='$error_code'");
     while ($row = mysql_fetch_array($result)) 
    {
    echo $row[error_msg];
    echo $row[error_code];
    echo $row[page];
    }
    echo $errorstyle;
    }
    

      well if you want to see the actual code I can copy and paste it, but there is a lot more than I am showing... about 20 or so form fields and another 20 or so lines of code involved with checking for errors, the connection to the db is inside the get_error function. which is, so far, another 10 or 15 lines... and it is actually

      SELECT error_msg FROM error_codes WHERE error_code = $error_code AND page = %page
      (SELECT * is a rather sloppy way to do things in my opinion, unless its absolutely needed)

      but the issue is creating variables named after the error code. such as error code 1 produces a variable names $error1, error code produces a variable named $error2, etc...
      I suppose I could include the code to create the variable inside the table... like an additional field with something like $error1 = 1; in it for error code 1... and then just echo that... after SELECTing it...

      What I need to do is display the error message above the form and then turn the erant fields yellow... I have 10 error messages based on required fields and double confirmation of email and password.... (I know I can use javascript, but I would like to keep everything server side so I know it gets done)

        Check for variable variables.

        It's like this:

        $i = 1;
        $test = 'error'.$i;
        $$test = 100; // the same as $error1 = 100
        

          This will produce a variable named $error1 ?

          So I could write

          function get_error($error_code, $page)
          {
          $query ="
          SELECT
          error_msg
          FROM
          errors
          WHERE
          error_code = $error_code
          AND
          page = $page
          ";
          echo mysql_query($query);
          $1 = $error_code;
          $test = $error .$1;
          }
          
          //  outside of the function
          
          $errorstyle = "background:yellow;";
          echo "
          <INPUT TYPE='text' NAME='name_first' VALUE='$name_first' STYLE='
          ";
          if ($error1) {echo $errorstyle;}
          echo "
          '>blah blah whatever else is after this
          ";

          ??

          I was under the understanding that the $variable .$variable was a string operator and merely concatenate the value of on to the value of the other.
          Im not concerned with the value of $error1, $error2, $error3, etc... just creating them to use in a conditional outside of the function... I thought of using the error array and havaing some sorf of

          if (1 isin $error_array)(do this)

          but I cannot find any info on using an isin. Is there such an operator?

            Write a Reply...