Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6

Thread: Using eval()

  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Posts
    626
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Using eval()

    I am developing a small Data Entry application that has a form with multiple lines where each line will represent a record on the database(MySql). Each line has a card number, cost center, hourly rate, earnings type, hours

    I have set up the element names so that for instance card number for the first line is card_num_1, second line card_num_2, etc. When they submit the form I will need to take each line (record) and update the data base. To cut down on the amount of coding that I have to do to access each line and create a record to update the table I found the function eval(). The following is a sample of what I used to test it:

    PHP Code:
    <?php
    $my_name_1
    ="Micky Mouse";
    $my_name_2="Donald Duck";
    $my_name_3="Minnie Mouse";
    for(
    $i=1;$i<=3;$i++){
       
    $my_name="\$my_name"."_$i";
       eval(
    "\$my_name = \"$my_name\";");
       print 
    "$my_name<br>";
      }
      
    ?>
    I used the above code to figure out how the eval() function worked. This method will work for me, but not sure if it is the best way. Does anyone have any other suggestions that would be better than using eval().

  • #2
    New to the CF scene
    Join Date
    Apr 2005
    Location
    Cape Town
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    there are two types of strings in PHP

    'string' and "string" (and a third actually - heredoc)
    read http://www.php.net/manual/en/language.types.string.php for more info

    here is a short piece to help you with your prob:

    $var = hello;
    $str1 = '$var foo.';
    $str2 = $var foo.';

    echo $str1;
    // echoes: $var foo.

    echo $str2;
    // echoes: hello foo.


  • #3
    New to the CF scene
    Join Date
    Apr 2005
    Location
    Cape Town
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hmmm...

    $str2 = "$var foo";

    anywayzz..)

  • #4
    New to the CF scene
    Join Date
    Apr 2005
    Location
    Cape Town
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    OK - well anyway - ignore the first two posts please (no, i haven't been drinking) i misunderstood the question..

    Why don't you post the vars in an array from HTML?
    use:
    <input name="var[0]">whatever</input>
    <input name="var[1]">whatever</input>
    <input name="var[2]">whatever</input>
    (this works on <select> too)

    If it is HTTP POST you can reference it with $_POST['var']['n'] where 'n' is the corresponding integer. (if i can remember it works with GET too)
    then you can either use 'implode()' or enter the vars directly into the mySQL statement.

    hope this helped

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Posts
    626
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi Atari,
    Thanks for your response. I will look into your suggestion. Do you see any drawbacks to using eval() to do what I want?

  • #6
    Regular Coder
    Join Date
    Jun 2002
    Posts
    626
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok a question. In your example would I use the same variable name in my form elements and then reference them using the index? If that is the case it probably would work better than using eval(). I am assuming that with javascript(which I use for validating) I could just reference them like so: document.forms[0].element[0].


  •  

    Posting Permissions

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