Setting a maximum limit on a php/mysql result?

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
RottNKorpse
Forum Newbie
Posts: 10
Joined: Sun Aug 05, 2007 3:04 pm

Setting a maximum limit on a php/mysql result?

Post by RottNKorpse »

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?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

Code: Select all

SELECT
IF (number > 500, 500, number)
FROM table
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
iknownothing
Forum Contributor
Posts: 337
Joined: Sun Dec 17, 2006 11:53 pm
Location: Sunshine Coast, Australia

Post by iknownothing »

The Following can be used, so it counts up 500 every page..(If maybe you wanted to do that)

URL = "http:www.yousite.com/index.php?pageID=1" (First Page)

Code: Select all

$pageID = $_GET['pageID'];


$limitLow = ($pageID * 500) - 500; //Page 1 (0)
$limitHigh = $pageID * 500;  //Page 1 (500)


$sql = "SELECT * FROM your_table LIMIT $limitLow, $limitHigh";
RottNKorpse
Forum Newbie
Posts: 10
Joined: Sun Aug 05, 2007 3:04 pm

Post by RottNKorpse »

VladSun wrote:

Code: Select all

SELECT
IF (number > 500, 500, number)
FROM table
thank you for your assistance...although that didn't exactly solve my problem it did lead me in the direction that I needed to be. I realized that a simple if statement would do what I wanted...silly me.

Code: Select all

if ($variable > 500) {
echo "this";
} else {
echo "that";
}
iknownothing wrote:The Following can be used, so it counts up 500 every page..(If maybe you wanted to do that)

URL = "http:www.yousite.com/index.php?pageID=1" (First Page)

Code: Select all

$pageID = $_GET['pageID'];


$limitLow = ($pageID * 500) - 500; //Page 1 (0)
$limitHigh = $pageID * 500;  //Page 1 (500)


$sql = "SELECT * FROM your_table LIMIT $limitLow, $limitHigh";
Although this isn't what I was looking for it will actually come in handy because I do need this for another project so thank you for this.
Post Reply