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
REwingUK
Forum Commoner
Posts: 26 Joined: Wed Jul 29, 2009 8:46 pm
Post
by REwingUK » Mon Aug 03, 2009 9:52 am
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??
jackpf
DevNet Resident
Posts: 2119 Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK
Post
by jackpf » Mon Aug 03, 2009 10:05 am
REwingUK
Forum Commoner
Posts: 26 Joined: Wed Jul 29, 2009 8:46 pm
Post
by REwingUK » Mon Aug 03, 2009 10:10 am
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";
}
jackpf
DevNet Resident
Posts: 2119 Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK
Post
by jackpf » Mon Aug 03, 2009 10:12 am
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
Post
by REwingUK » Mon Aug 03, 2009 10:23 am
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
jackpf
DevNet Resident
Posts: 2119 Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK
Post
by jackpf » Mon Aug 03, 2009 10:25 am
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);
REwingUK
Forum Commoner
Posts: 26 Joined: Wed Jul 29, 2009 8:46 pm
Post
by REwingUK » Mon Aug 03, 2009 10:28 am
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";
}
jackpf
DevNet Resident
Posts: 2119 Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK
Post
by jackpf » Mon Aug 03, 2009 10:30 am
You'll want to replace $var with $ad['Price']
REwingUK
Forum Commoner
Posts: 26 Joined: Wed Jul 29, 2009 8:46 pm
Post
by REwingUK » Mon Aug 03, 2009 10:59 am
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
Post
by REwingUK » Mon Aug 03, 2009 11:50 am
all sorted, thank you for your help jack.
Mark Baker
Forum Regular
Posts: 710 Joined: Thu Oct 30, 2008 6:24 pm
Post
by Mark Baker » Mon Aug 03, 2009 12:24 pm
So can you explain why you were using a VARCHAR2 for a price value anyway?
jackpf
DevNet Resident
Posts: 2119 Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK
Post
by jackpf » Mon Aug 03, 2009 1:05 pm
That's true actually...you should just keep the numerical value in the database, and manipulate it on display.