Hello:
Thank you for responding.
I modified the line as you suggested. I ran the script and I received warnings about a division by 0.
Here are the warnings that were generated:
Number of colors: 6 Color Tally (Actual votes cast) Total ($total variable) Percent ($percent variable)
Warning: Division by zero in /usr50/home/summitwe/public_html/poll.php on line 42
green 8 0 0
Warning: Division by zero in /usr50/home/summitwe/public_html/poll.php on line 42
blue 4 0 0
Warning: Division by zero in /usr50/home/summitwe/public_html/poll.php on line 42
yellow 9 0 0
Warning: Division by zero in /usr50/home/summitwe/public_html/poll.php on line 42
pink 8 0 0
Warning: Division by zero in /usr50/home/summitwe/public_html/poll.php on line 42
red 9 0 0
Warning: Division by zero in /usr50/home/summitwe/public_html/poll.php on line 42
purple 6 0 0
====================
My feeling is that the $total variable that is defined in the begining of the script is not being passed to the $percent calculation within the while structure.
I'm not quite sure how to fix this. I'm new to PHP and so far I haven't even attempted to write a script that uses two SELECT queries where info from one query is passed to the second query.
Can you help me?
Thank you so much for your time.
=================
Here's the script:
<?php
//connect to server and select database
$conn = mysql_connect("mysql.addr.com", "summitweb", "summitdb") or die(mysql_error());
mysql_select_db("summitweb",$conn) or die(mysql_error());
if (isset($POST['color']) && !empty($POST['color']))
mysql_query("UPDATE color SET tally=tally+1 WHERE name='{$_POST['color']}'");
//we're going to get the total here
$sql = "SELECT SUM(tally) FROM color";
$total = mysql_result(mysql_query($sql),0,0);
$result = mysql_query("SELECT name, tally FROM color") or die(mysql_error());
$num_rows = mysql_num_rows($result);
?>
<html>
<head>
<meta http-equiv="Content-Language" content="en-us">
<title></title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
<font size="2" face="Verdana">Number of colors: <?PHP echo $num_rows; ?></font>
<table width="457" border="1">
<tr>
<td width="50">Color</td>
<td width="50">Tally (Actual votes cast)</td>
<td width="50">Total ($total variable)</td>
<td width="50">Percent ($percent variable)</td>
</tr>
<?PHP
$total=0;
while ($row = mysql_fetch_array($result)) {
$percent=round((100*($row['tally']/$total)),2);
?>
<tr>
<td width="50">
<font size="2" face="Verdana"><?PHP echo $row['name']; ?></font>
</td>
<td width="50">
<font size="2" face="Verdana"><?PHP echo $row['tally']; ?></font>
</td>
<td width="50">
<font size="2" face="Verdana"><?PHP echo $total; ?></font>
</td>
<td width="50">
<font size="2" face="Verdana"><?PHP echo $percent; ?></font>
</td>
</tr>
<?PHP
}
?>
</table>
</body>
</html>