blastin311 Posted September 3, 2009 Share Posted September 3, 2009 Hello, I am having an issue getting the eval() to work. In my database I am storing a select box with options. I name the select box using a variable ($options): <select id="required1-$id2" name="required1-$id2" disabled='disabled' style='margin-top:5px'> <option>Choose Option</option></select> I am trying to pull this from the database and still retain use of that variable. In my while loop I am grabbing the data from the database and putting it into a variable: $options = $myrow["options"]; eval("\$options = \"$options\";"); I guess I don't understand how the eval() does its magic cause I keep getting Parse error: syntax error, unexpected T_STRING on the eval()'d code. Can anyone shine some light on this for me? Thanks in advance! Link to comment https://forums.phpfreaks.com/topic/173041-eval-with-php-variables-in-database/ Share on other sites More sharing options...
rhodesa Posted September 3, 2009 Share Posted September 3, 2009 when you substitute everything in...this is what is being eval'd: $options = "<select id="required1-$id2" name="required1-$id2" disabled='disabled' style='margin-top:5px'> <option>Choose Option</option></select>"; As you can see...your double quotes won't work....you can escape them though: eval("\$options = \"".str_replace('"','\"',$options)."\";"); Link to comment https://forums.phpfreaks.com/topic/173041-eval-with-php-variables-in-database/#findComment-912077 Share on other sites More sharing options...
blastin311 Posted September 4, 2009 Author Share Posted September 4, 2009 That works perfectly! Thanks for the explanation too. I understand eval() a little better now. Someone can mark as Solved. Link to comment https://forums.phpfreaks.com/topic/173041-eval-with-php-variables-in-database/#findComment-912448 Share on other sites More sharing options...
ToonMariner Posted September 4, 2009 Share Posted September 4, 2009 eval is a very expensive way of going about things - I can't really think of a situation where I would use it on purpose. I'd always look to avoid it if possible... Link to comment https://forums.phpfreaks.com/topic/173041-eval-with-php-variables-in-database/#findComment-912456 Share on other sites More sharing options...
rhodesa Posted September 4, 2009 Share Posted September 4, 2009 eval is a very expensive way of going about things - I can't really think of a situation where I would use it on purpose. I'd always look to avoid it if possible... agreed...expensive and dangerous...you may want to look into doing simple substitution instead with str_replace(). or, check out something like Smarty (PHP templating engine) Link to comment https://forums.phpfreaks.com/topic/173041-eval-with-php-variables-in-database/#findComment-912463 Share on other sites More sharing options...
blastin311 Posted September 4, 2009 Author Share Posted September 4, 2009 I appreciate the suggestions. I am storing a drop-down list of "Options" for a product in a database. They are being pulled for display because each one is either required or optional. I don't think it will effect it much as it is a small app with little else to do than configure the the options to a product. What do you think? Too expensive for such a small app or too expensive in general. Would this work the same (or better)? $options = str_replace('"$options"', '\"$options\"', $options); Link to comment https://forums.phpfreaks.com/topic/173041-eval-with-php-variables-in-database/#findComment-912590 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.