Larry Vollmer
asked on
Rounding a variable
I am using a query to calculate the average of some numbers in a table.
the problem is that when i use this: <? echo $row->average; ?>
I get numbers with decimals. Is there some way I can round the <? echo $row->average; ?> to the nearest whole number?
I also need to grab the number of rows used to calculate the average and echo it like <? echo $row->average; ?> - <? echo $row->total; ?>
IE: if Mike's Restaurant had 10 records, <? echo $row->total; ?> would say 10.
the problem is that when i use this: <? echo $row->average; ?>
I get numbers with decimals. Is there some way I can round the <? echo $row->average; ?> to the nearest whole number?
I also need to grab the number of rows used to calculate the average and echo it like <? echo $row->average; ?> - <? echo $row->total; ?>
IE: if Mike's Restaurant had 10 records, <? echo $row->total; ?> would say 10.
<?
$query = "SELECT a.venuename, a.venueid, b.stars, b.reviewtext, AVG( stars ) AS average
FROM venue a, venuereview b
WHERE a.venueid = b.venueid
AND b.switch = '1'
GROUP BY b.venueid
ORDER BY average DESC";
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_object($result))
{
?>
<p> <strong><? echo $row->venuename; ?></strong> - <img src = "<? echo $row->average; ?>"/></p>
<?php
}
?>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
great thats what I need. any idea how I would display the number of records?
>>any idea how I would display the number of records?
You should not be asking more than 1 question per post. That's in the user agreement. I'll assume you didn't know that and let it slide this time.
To answer your question, you need to do so right after your query:
...
$result = mysql_query($query) or die(mysql_error());
$totalRecords = mysql_num_rows( $result);
...
You should not be asking more than 1 question per post. That's in the user agreement. I'll assume you didn't know that and let it slide this time.
To answer your question, you need to do so right after your query:
...
$result = mysql_query($query) or die(mysql_error());
$totalRecords = mysql_num_rows( $result);
...
ASKER
i did not know that, sorry.
ASKER
I will create a new question. that solution did not work. keep an eye out for it if you want to help
Ahh. I see you are using AVG( stars ) AS average
Try adding count(*) as totalRows to your query so that you get an extra field named totalRows. What I gave you:
$totalRecords = mysql_num_rows( $result);
will give you the total number of rows returned for the current query, but that is NOT what you need.
Try adding count(*) as totalRows to your query so that you get an extra field named totalRows. What I gave you:
$totalRecords = mysql_num_rows( $result);
will give you the total number of rows returned for the current query, but that is NOT what you need.
ASKER
I actually have a field that just contains teh number 1 as a default, so I am using the SUM function for that - thanks for the help
: <? echo round( $row->average,2); ?>