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! Quote Link to comment 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)."\";"); Quote Link to comment 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. Quote Link to comment 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... Quote Link to comment 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) Quote Link to comment 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); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.