View Full Version : Using eval()

04-06-2005, 09:01 PM
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:

$my_name_1="Micky Mouse";
$my_name_2="Donald Duck";
$my_name_3="Minnie Mouse";
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().

04-06-2005, 09:23 PM
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.


04-06-2005, 09:24 PM

$str2 = "$var foo";


04-06-2005, 09:37 PM
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?
<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 :thumbsup:

04-07-2005, 01:31 PM
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?

04-07-2005, 01:38 PM
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].

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum