Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
Bom dia à todos. Eu estou com um pequeno problema: preciso transformar uma string que recebo da DB em uma operação matemática.
O código é o seguinte:
$slotquery = row(query("SELECT `attribute` FROM {{table}} WHERE id='{$userrow["slot1id"]}' LIMIT 1", "drops"));
$newvalue = $userrow["attack"] . $slotquery[1]["attribute"];
query("UPDATE {{table}} SET `attack`='{$newvalue}' WHERE `uid`='{$userrow["id"]}';", "drops");
No caso, o valor de "$newvalue" seria uma algo semelhante à "5+3", porém preciso transforma-la em um número (no caso, 8). Já tentei usar o (int) antes da string, porém nada.
Caso alguem possa me ajudar agradeço.
Observação: query() e row() funcionam de forma idêntica à mysql_query() e mysql_fetch_assoc().
Já havia tentado, porém está retornando isso:
Parse error: parse error in C:\wamp\www\maps.php(4) : eval()'d code on line 1
mostre como você fez.
$slotquery = row(query("SELECT `attribute` FROM {{table}} WHERE id='{$userrow["slot1id"]}' LIMIT 1", "drops"));
$newvalue = eval($userrow["attack"] . $slotquery[1]["attribute"]);
query("UPDATE {{table}} SET `attack`='{$newvalue}' WHERE `uid`='{$userrow["id"]}';", "drops");do manual:
<?php
function matheval($equation)
{
$equation = preg_replace("/[^0-9+\-.*\/()%]/","",$equation);
// fix percentage calcul when percentage value < 10
$equation = preg_replace("/([+-])([0-9]{1})(%)/","*(1\$1.0\$2)",$equation);
// calc percentage
$equation = preg_replace("/([+-])([0-9]+)(%)/","*(1\$1.\$2)",$equation);
// you could use str_replace on this next line
// if you really, really want to fine-tune this equation
$equation = preg_replace("/([0-9]+)(%)/",".\$1",$equation);
if ( $equation == "" )
{
$return = 0;
}
else
{
eval("\$return=" . $equation . ";" );
}
return $return;
}
$str = '3+5';
echo matheval( $str );//saida: 8
http://php.net/evalConsegui resolver assim:
$slotquery = row(query("SELECT `attribute` FROM {{table}} WHERE id='{$userrow["slot1id"]}' LIMIT 1", "drops"));
eval("\$newvalue = {$userrow["attack"]}{$slotquery[1]["attribute"]};");
query("UPDATE {{table}} SET `attack`='{$newvalue}' WHERE `uid`='{$userrow["id"]}';", "drops");
Mas irei usar seu metodo também pois pelo que testei é muito bom..
Obrigado.
já tentou eval() ?