Page 1 of 1
Only accepting Numeric from a VARCHAR column
Posted: Mon Aug 03, 2009 9:52 am
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??
Re: Only accepting Numeric from a VARCHAR column
Posted: Mon Aug 03, 2009 10:05 am
by jackpf
Re: Only accepting Numeric from a VARCHAR column
Posted: Mon Aug 03, 2009 10:10 am
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";
}
Re: Only accepting Numeric from a VARCHAR column
Posted: Mon Aug 03, 2009 10:12 am
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.
Re: Only accepting Numeric from a VARCHAR column
Posted: Mon Aug 03, 2009 10:23 am
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
Re: Only accepting Numeric from a VARCHAR column
Posted: Mon Aug 03, 2009 10:25 am
by jackpf
Oh right, in that case, scrap everything I've just told you
Try running the value through this first. It should strip all non-numeric characters:
Code: Select all
$var = preg_replace('/[^0-9]/', '', $var);
Re: Only accepting Numeric from a VARCHAR column
Posted: Mon Aug 03, 2009 10:28 am
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";
}
Re: Only accepting Numeric from a VARCHAR column
Posted: Mon Aug 03, 2009 10:30 am
by jackpf
You'll want to replace $var with $ad['Price']

Re: Only accepting Numeric from a VARCHAR column
Posted: Mon Aug 03, 2009 10:59 am
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?
Re: Only accepting Numeric from a VARCHAR column
Posted: Mon Aug 03, 2009 11:50 am
by REwingUK
all sorted, thank you for your help jack.
Re: Only accepting Numeric from a VARCHAR column
Posted: Mon Aug 03, 2009 12:24 pm
by Mark Baker
So can you explain why you were using a VARCHAR2 for a price value anyway?
Re: Only accepting Numeric from a VARCHAR column
Posted: Mon Aug 03, 2009 1:05 pm
by jackpf
That's true actually...you should just keep the numerical value in the database, and manipulate it on display.