Only accepting Numeric from a VARCHAR column

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
REwingUK
Forum Commoner
Posts: 26
Joined: Wed Jul 29, 2009 8:46 pm

Only accepting Numeric from a VARCHAR column

Post by REwingUK »

Code: Select all

                    
                    if ($ad['Price']  <= 120) {
    
                    echo "£".$ad['Price']." PPPW";
    
                    }
    
                    else {
    
                    echo "£".($ad['Price'] / 4)." PPPW";
        
                    }
I want to display the contents in the Price Column, but only numeric. Anyone know how i can do this??
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Only accepting Numeric from a VARCHAR column

Post by jackpf »

REwingUK
Forum Commoner
Posts: 26
Joined: Wed Jul 29, 2009 8:46 pm

Re: Only accepting Numeric from a VARCHAR column

Post by REwingUK »

Do you mena like this ? because this displays 0.

Thanks for your help

Code: Select all

                    
                    if (is_numeric($ad['Price']  <= 120)) {
    
                    echo "£".$ad['Price']." PPPW";
    
                    }
    
                    else {
    
                    echo "£".($ad['Price'] / 4)." PPPW";
        
                    }
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Only accepting Numeric from a VARCHAR column

Post by jackpf »

No, I mean like this:

Code: Select all

if(is_numeric($ad['Price']) && $ad['Price'] <= 120)
Btw, your logic doesn't make sense - if it isn't numeric, you're trying to divide it by 4. That obviously won't work.
REwingUK
Forum Commoner
Posts: 26
Joined: Wed Jul 29, 2009 8:46 pm

Re: Only accepting Numeric from a VARCHAR column

Post by REwingUK »

Basically i have data in the table for example like £530 PCM. I want to display it as per person per week, so i want to take that value remove all the non-numeric data and then if the value is over 120 to divide it by 4.

How else could i write that?

Thanks mate
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Only accepting Numeric from a VARCHAR column

Post by jackpf »

Oh right, in that case, scrap everything I've just told you :P

Try running the value through this first. It should strip all non-numeric characters:

Code: Select all

$var = preg_replace('/[^0-9]/', '', $var);
REwingUK
Forum Commoner
Posts: 26
Joined: Wed Jul 29, 2009 8:46 pm

Re: Only accepting Numeric from a VARCHAR column

Post by REwingUK »

sorry mate, where am i displaying this?

Code: Select all

                    $var = preg_replace('/[^0-9]/', '', $var);
                    
                    if ($ad['Price']  <= 120) {
                     
                    echo "£".$ad['Price']." PPPW";
                    
                    }
                    
                    else {
                    
                    echo "£".($ad['Price'] / 4)." PPPW";
                    
                    }
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Only accepting Numeric from a VARCHAR column

Post by jackpf »

You'll want to replace $var with $ad['Price'] :wink:
REwingUK
Forum Commoner
Posts: 26
Joined: Wed Jul 29, 2009 8:46 pm

Re: Only accepting Numeric from a VARCHAR column

Post by REwingUK »

thats works great, but has shown another problem.

If the value in the column is say 585 it will take that value and divide by 4 and display the value fine.

But if the value was 585.00 with the decimal and the 2 zeros it will display 14625

Any ideas how i can get around this?
REwingUK
Forum Commoner
Posts: 26
Joined: Wed Jul 29, 2009 8:46 pm

Re: Only accepting Numeric from a VARCHAR column

Post by REwingUK »

all sorted, thank you for your help jack.
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Only accepting Numeric from a VARCHAR column

Post by Mark Baker »

So can you explain why you were using a VARCHAR2 for a price value anyway?
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Only accepting Numeric from a VARCHAR column

Post by jackpf »

That's true actually...you should just keep the numerical value in the database, and manipulate it on display.
Post Reply