mysql - PHP / Save math formula in table row -
i managing online store. every products has own fees calculation (1% + 0.10€ / 1% + 0.20€ / 1% + 0.30€ etc). i'm not able guess right fees because follow complex calculations , can't integrate them directly in php class.
i like, in back-end, able add formulas , assign them every product. example: product a, create fee calculation "x/100+0.10" x product price. how can store kind of formula in sql row? safe? how can tell php use row as:
$fee = x/100 + 0.1; thanks!
so rule of thumb, can store whatever want in database - don’t worried can store in there, need worry how store - database understands types of data, not meaning of data - answer question, yes safe store string in database (assuming put stuff in database using correct escape methods etc.)
to honest without more knowledge of how many different calculations talking about, quite hard 1 - in ideal world have field in database each different variable / exponent in calculation , assemble @ php end, given have stated can complicated - might work generic case, although leaves lot desired:
lets store stuff in database this
$formula = ‘x/100 + 0.1’; insertintodatabase($formula); —— time later $resultsofquery = somedatabasemethod(); foreach($resultsofquery $row) { $calculation = str_replace(‘x’, $row[‘price’], $row[‘formula’]); //here $calculation contains ’22.1/100 + 1’; } now, in our foreach loop $calculation contains ’22.1/100 +1’ needs evaluation - can in number of ways - post explains it: php function evaluate string "2-1" arithmetic 2-1=1
this 1 way, there many - have fiddle , see works you.
Comments
Post a Comment