MySQL table operations (multiply/divide)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
maxhudson
Forum Newbie
Posts: 2
Joined: Mon Jan 10, 2011 11:20 pm

MySQL table operations (multiply/divide)

Post by maxhudson »

Basically, I have two colums.

What I want it to do is this:

if column (topic_rating_total)'s value = 0 {
$ratingtotal = "0" }
else {
$ratingtotal = column (topic_rating_total)'s value divided by column (topic_num_rating)'s value}

Heres my code attemt (snippet):

Code: Select all

while($row = mysql_fetch_assoc($result))
{				
if($row['topic_rating_total'] == 0){
$ratingtotal = "0";
} else {
$ratingtotal = ($row['topic_rating_total'] / $row['topic_num_ratings']);
}

echo "<td class='ratingcat' > $ratingtotal </td>";
Here is how it displays:

http://componentadvice.com/category.php?id=1

Any ideas?
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: MySQL table operations (multiply/divide)

Post by social_experiment »

Move this code inside the else statement

Code: Select all

echo "<td class='ratingcat' > $ratingtotal </td>"; 
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply