I found a problem in the rows but that didn't fix it. if I test for
votes, total, and percentages it work perfect. To do this I have to cut
part of the code and rearrange the braces. I beginning to think it is a
problem with braces being wrong or id
test code
<?php
include("calls.php");
include("common.php");
$db = DB::connect("mysql://$user:$pass@$host/$dname");
$sql = "SELECT question, responce1, responce2, responce3,
vote1, vote2, vote3, date FROM poll WHERE id = id";
$res = $db->query($sql);
if (DB::isError($res)) {
die ($res->getMessage());
}
while ($row = $res->fetchRow()){
$question = $row[0];
$responce1 = $row[1];
$responce2 = $row[2];
$responce3 = $row[3];
$vote1 = $row[4];
$vote2 = $row[5];
$vote3 = $row[6];
$date = $row[7];
/* Count the votes*/
$total = $vote1 + $vote2 + $vote3;
/* Calculate and round to 2 decimals*/
$perc_vote1 = round(($vote1/$total)*100,2);
$perc_vote2 = round(($vote2/$total)*100,2);
$perc_vote3 = round(($vote3/$total)*100,2);
echo" $vote1 $vote2 $vote3 Total $total Percent1 $perc_vote1 Percent2
$perc_vote2 Percent3 $perc_vote3"";
}
?>
Owen Prime wrote:
> Make sure that vote1, vote2, and vote3 are not null. Maybe you need to use
> the IFNULL(vote1, 0) function.
>
> Cheers,
>
> Owen.
>
>
>
> Gary wrote:
>
>
>> I am trying t get the percent of votes form the table poll, but keep
>>getting Division by zero for the line commented below.
>>
>><snip>
>>if ($res) {
>>echo "bla bla bla";
>>$sql = "SELECT question, responce1, responce2, responce3,
>>vote1, vote2, vote3, date FROM poll WHERE id = {$_POST['id']}";
>> $res = $db->query($sql);
>> if (DB::isError($res)) {
>> die ($res->getMessage());
>> }
>>while ($row = $res->fetchRow());
>>$id = $row[0];
>> $question = $row[1];
>> $responce1 = $row[2];
>>$responce2 = $row[3];
>>$responce3 = $row[4];
>>$vote1 = $row[5];
>>$vote2 = $row[6];
>>$vote3 = $row[7];
>>$date = $row[8];
>>
>> /* Count the votes*/
>> $total = $vote1 + $vote2 + $vote3;
>>
>>/* Caluclate */
>>$perc_vote1 = round(($vote1/$total)*100,2); /* Division by zero*/
>> $perc_vote2 = round(($vote2/$total)*100,2); /* Division by zero*/
>>$perc_vote3 = round(($vote3/$total)*100,2); /* Division by zero*/
>>
>>
>><snip>
>>
>>TIA
>>Gary
>
>
>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php