[Resolvido] Problema Em Calcular Porcentagem De Valores.
Olá Amigos,
Estou Com Probleminha...
Se eu por:
$positive = 1;
$neutral = 0;
negative = 0;
RESULTADO: ocorre tudo ok fica 100% e os de mais 0%.
Mais se eu por:
$positive = 10;
$neutral = 5;
negative = 2;
RESULTADO: Fica 2% perdido entre os valores (calculo) e não consigo corrigir.
**CODIGO PHP:**
<?php
$positive = count($conn->query("SELECT * FROM reviews WHERE type = '2'")->fetchAll());
$neutral = count($conn->query("SELECT * FROM reviews WHERE type = '1'")->fetchAll());
$negative = count($conn->query("SELECT * FROM reviews WHERE type = '0'")->fetchAll());
$total = count($conn->query("SELECT * FROM reviews")->fetchAll());
$porc = 100;
if($positive > 0){
$porc_positive = (($positive*$porc)/$total);
if(strlen($porc_positive) > 2){
$array = str_split($porc_positive);
$porc_positive = $array['0'].$array['1'].$array['2'];
$porc_positive = str_replace(",", "", $porc_positive);
}else{
$porc_positive = $porc_positive;
}
}else{
$porc_positive = 0;
}
if($neutral > 0){
$porc_neutral = (($neutral*$porc)/$total);
if(strlen($porc_neutral) > 2){
$array = str_split($porc_neutral);
$porc_neutral = $array['0'].$array['1'].$array['2'];
$porc_neutral = str_replace(",", "", $porc_neutral);
}else{
$porc_neutral = $porc_neutral;
}
}else{
$porc_neutral = 0;
}
if($negative > 0){
$porc_negative = (($negative*$porc)/$total);
if(strlen($porc_negative) > 2){
$array = str_split($porc_negative);
$porc_negative = $array['0'].$array['1'].$array['2'];
$porc_negative = str_replace(",", "", $porc_negative);
}else{
$porc_negative = $porc_negative;
}
}else{
$porc_negative = 0;
}
?>Discussão (6)
Carregando comentários...