diff --git a/scripts/weekly_report.php b/scripts/weekly_report.php
index 1a7ecd6..86f82d2 100644
--- a/scripts/weekly_report.php
+++ b/scripts/weekly_report.php
@@ -9,6 +9,19 @@ $enddate = strtotime('last sunday') - (1*86400);
$debug = false;
+function safe_percentage($count, $prior_count) {
+ if ($prior_count !== 0) {
+ $percentagediff = round((($count - $prior_count) / $prior_count) * 100);
+ } else {
+ if ($count > 0) {
+ $percentagediff = INF;
+ } else {
+ $percentagediff = 0;
+ }
+ }
+ return $percentagediff;
+}
+
if(isset($_GET['ascii'])) {
$db = new SQLite3('./scripts/birds.db', SQLITE3_OPEN_READONLY);
@@ -38,15 +51,14 @@ if(isset($_GET['ascii'])) {
$result7= $statement7->execute();
$priortotalspeciestally = $result7->fetchArray(SQLITE3_ASSOC)['COUNT(DISTINCT(Com_Name))'];
- $percentagedifftotal = round( (($totalcount - $priortotalcount) / $priortotalcount) * 100 );
-
+ $percentagedifftotal = safe_percentage($totalcount, $priortotalcount);
if($percentagedifftotal > 0) {
$percentagedifftotal = "+".$percentagedifftotal."%";
} else {
$percentagedifftotal = "-".abs($percentagedifftotal)."%";
}
- $percentagedifftotaldistinctspecies = round( (($totalspeciestally - $priortotalspeciestally) / $priortotalspeciestally) * 100 );
+ $percentagedifftotaldistinctspecies = safe_percentage($totalspeciestally, $priortotalspeciestally);
if($percentagedifftotaldistinctspecies > 0) {
$percentagedifftotaldistinctspecies = "+".$percentagedifftotaldistinctspecies."%";
} else {
@@ -80,9 +92,7 @@ if(isset($_GET['ascii'])) {
$priorweekcount = $totalcount['COUNT(*)'];
// really percent changed
- if($priorweekcount > 0){
- $percentagediff = round( (($scount - $priorweekcount) / $priorweekcount) * 100 );
-
+ $percentagediff = safe_percentage($scount, $priorweekcount);
if($percentagediff > 0) {
$percentagediff = "+".$percentagediff."%";
} else {
@@ -90,9 +100,6 @@ if(isset($_GET['ascii'])) {
}
echo $com_name." - ".$scount." (".$percentagediff.")
";
- } else {
- echo $com_name." - ".$scount ."
";
- }
}
}
@@ -177,12 +184,7 @@ while($detection=$result1->fetchArray(SQLITE3_ASSOC))
$totalcount = $result2->fetchArray(SQLITE3_ASSOC);
$priorweekcount = $totalcount['COUNT(*)'];
- if ($priorweekcount > 0) {
- $percentagediff = round( (($scount - $priorweekcount) / $priorweekcount) * 100 );
- } else {
- $percentagediff = 0;
- }
-
+ $percentagediff = safe_percentage($scount, $priorweekcount);
if($percentagediff > 0) {
$percentagediff = "+".$percentagediff."%";
} else {