» Quick Links
You can make money on these forums
We Share profits with you
Google
Google Adsense
Google Adwords
YPN
Yahoo
MSN Search
Web Directories
Web Hosting
Web Hosting Offers
Hosting News
Suggestions
Link Building
Domain Names
PHP Forums
MySQL Forums
» More Links
OSP News
Reseller Hosting
Shared Hosting
Dedicated Servers
Google Adsense
Search Engine Marketing
Link Development
Affiliate Marketing


» Advertising
Multiple DC PR Check

Free SEO Tools


Go Back   Webmaster Forums > Website Developement / Programming > PHP Programming and Tips

PHP Programming and Tips Discuss about PHP programming and Share Tips. Ask questions about Scripting and Errors.

Reply
 
Thread Tools Display Modes
  #1  
Old 09-13-2006, 11:05 AM
jaffer jaffer is offline
OSP Starters
 
Join Date: Sep 2006
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
jaffer is on a distinguished road
Default How would I do this in PHP?

hi all.
I need to divide two elements in my array to reach a percentage. Normally this is easy to do but one of the elements is sometimes zero which generates an error message. I'm not sure how to overcome this in PHP.
In SQL I'd do something like this

CASE WHEN $row[sales] <> 0
THEN $row[gp]/$row[sales]
ELSE '0'
END AS gp percent
How would I do this in PHP?

Thanks
BJ -- This message may have been cut off and the rest will only be shown to members. To become a member, click here --
Reply With Quote
Revenue Sharing Ads ( ?):
  #2  
Old 09-13-2006, 11:11 AM
stranger stranger is offline
OSP Starters
 
Join Date: Sep 2006
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
stranger is on a distinguished road
Default

I think I'can help you, except your data above isn't coming from an array, but anyways:
PHP Code:
<?php if ($row['sales']!=0){
$gp=$row['gp']/$row['sales'];
}
else {$gp=0;} ?>

we still need to change $gp into a percentage...how many decimal places do you want? Here it is rounded to two decimals of a percentage:
PHP Code:
<?php
$gp=round($gp,4);
$gp=$gp*100;
echo $gp . "%";
?> -- This message may have been cut off and the rest will only be shown to members. To become a member, click here --
Reply With Quote
  #3  
Old 09-13-2006, 11:18 AM
CORMAC CORMAC is offline
OSP Starters
 
Join Date: Sep 2006
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
CORMAC is on a distinguished road
Default

Just giving you an idea (not checked):
PHP Code:
<?php
foreach($rows as $row)
{
$gp_percent[] = ($row['sales']!=0)?$row['gp']/$row['sales']:0;
}
?>
bye -- This message may have been cut off and the rest will only be shown to members. To become a member, click here --
Reply With Quote
  #4  
Old 09-13-2006, 11:27 AM
NEILLE NEILLE is offline
OSP Starters
 
Join Date: Sep 2006
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
NEILLE is on a distinguished road
Default

In fact if you are using mysql you could even do this:
Code:
select
coalesce(gp/sales,0) as gp_percent
from yourtable
the coalesce says that if your result would be null then just show 0 in the gp_percent output. -- This message may have been cut off and the rest will only be shown to members. To become a member, click here --
Reply With Quote
  #5  
Old 09-13-2006, 11:33 AM
TAFFE TAFFE is offline
OSP Addicts
 
Join Date: Aug 2006
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
TAFFE is on a distinguished road
Default

Thanks a lot everyone.
I got it to work, thanks to all your help. -- This message may have been cut off and the rest will only be shown to members. To become a member, click here --
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
i can help with php averagejoe Advertise Programming Services 1 02-25-2007 03:48 AM
Modwest Offers Live PHP Tool outsourcingplans Web Hosting News 0 12-16-2006 02:24 AM
Is PHP loosing out to .NET? garcia PHP Programming and Tips 1 08-10-2006 01:44 PM
Recompiling PHP, Fedora 4 jhingalala PHP Programming and Tips 1 07-17-2006 01:47 PM
Getting MySQL & PHP to communicate JCH MySQL Forums 0 05-29-2006 01:52 PM


All times are GMT. The time now is 01:10 PM.