Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Rounding a variable

Posted on 2008-06-13
8
Medium Priority
?
236 Views
Last Modified: 2008-06-13
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.
<?
$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
}
?>

Open in new window

0
Comment
Question by:lvollmer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 82

Expert Comment

by:hielo
ID: 21781151
If you want two decimal places use:
: <? echo round( $row->average,2); ?>
0
 
LVL 82

Accepted Solution

by:
hielo earned 2000 total points
ID: 21781161
If you do not want any decimals then use:
<? echo round( $row->average,0); ?>
0
 

Author Comment

by:lvollmer
ID: 21781171
great thats what I need. any idea how I would display the number of records?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 82

Expert Comment

by:hielo
ID: 21781209
>>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);
...
0
 

Author Comment

by:lvollmer
ID: 21781265
i did not know that, sorry.
0
 

Author Comment

by:lvollmer
ID: 21781284
I will create a  new question. that solution did not work. keep an eye out for it if you want to help
0
 
LVL 82

Expert Comment

by:hielo
ID: 21781382
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.
0
 

Author Comment

by:lvollmer
ID: 21781392
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
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question