Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setting a maximum limit on a php/mysql result?

Status
Not open for further replies.

RottNKorpse

Technical User
Aug 8, 2006
13
Hi,
I have a question I can't seem to figure out on my own using tutorials or documentations so I was hoping someone here could help me out. Here is my problem.

I am using PHP and MySQL to do this script. I want to have a table that contains only numbers for data and then add them all up as a result to display and that I have accomplished with no problem and then I want to display a percentage of that number to the public however that also I have accomplished with no problem using round()...

The problem is I am trying to set a maximum number that it displays.

I want it to display 1-500 no matter what the number is or the decimal it may have is provided it doesnt exceed 500 but once it does I want it to display 500 as the limit so even if it is say 674 I want it to only display 500.

How would I go about doing this?

The sql I am using for the adding is SELECT SUM which works just fine and I am using round( $total_number *.15, 2) to display the percentage number of 15%. But not I am completely at a loss at what to do for the maximum number thing so any ideas?
 
You want to show the number unless it exceeds 500. After the 15 percent has been applied correct?


how about:

Code:
$numbertodisplay=round($total_number*.15)>500
if($numbertodisplay>500){
$numbertodisplay=500;
}
echo numbertodispla

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
ignore the firsy [red]>500[/red]

it should be:
Code:
$numbertodisplay=round($total_number*.15);
if($numbertodisplay>500){
$numbertodisplay=500;
}
echo $numbertodisplay;

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
i think what you are trying to do is to apply an average across the top 500 records. i.e. a sort of clipping.

in which case you need to use the order by clause of sql to make sure you are returning the 'right' 500 records and then add the following to the end of your sql statement

Code:
LIMIT 500

this will limit the results to the top 500 records

you can use this in group by clauses as well as plain selects.
 
vacunita, thank you very much...that worked. I can't believe I didn't realize to use an if statement with a greater than symbol...silly me. Thanks again.

jpadie, thank you for your assistance but vacunita was right...I wanted the limit to be set after the round runs the percentage but again thank you for trying to help.
 
Glad I could help.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top