Hello all,
I've been a big fan of PHP for some time - developed some sites and I must say it is my favorite language. I'm not too advanced of a developer, and this is my first post because it is the first time I couldn't find the help I needed online. Mostly because I just don't know how to say what I'm trying to do.... hoping someone here can help me out.
ANYWAYS....
Basic shopping cart functionality - I have a coupon code that the user enters in a form - it checks the database and returns the actual code I want to process from the database.
Example, if the coupon code was coupon1, it might pull something out of the database like this:
=(.10*$Cart->total)
What I want is to actually execute this code (and return the actual float)instead of reading it as a string. I'm sure there is an easy way to do this - and I'm even more sure that there is an easier way to process "coupons". Let me know your thoughts, I appreciate your help.
K
PHP code stored in DB - need to execute
Moderator: General Moderators
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
eval() is what you are looking for.. although I'd suggest creating an interpretter that converts the text into the proper equation.. just in case someone hacks into the database and is able to make everything free for themselves..
How to do this, I don't know for sure, as I have yet to create a coupon system for anything, but basically, I'd use keywords of some form and numbers, or system wide globals of a fashion to tell me what math to actually run.
so do note, that using eval() can be very dangerous.
so do note, that using eval() can be very dangerous.
Thanks!
Thanks for the help - that was exactly what I was looking for. However, after your reading your post I took your advice and used an interpreter from values in the db - so I didn't need the eval() function after all.
Thanks again.
K
Thanks again.
K