Iceman512 Posted November 20, 2007 Share Posted November 20, 2007 Hi everyone, I am trying to create a statistics script for my site. I am retrieving the data from a MySQL table and trying to calculate the results. The data displays perfectly as it is, however I have used if and else statements to check it against one another. The page has since become very long and it contains endless if and else queries which hurts me just to look at. I was wondering if there was a more efficient way to accomplish this? Perhaps through a better MySQL query? Here is the bulk of my code: <?php $mon = 0; $tue = 0; $wed = 0; $thu = 0; $fri = 0; $sat = 0; $sun = 0; $jan = 0; $feb = 0; $mar = 0; $apr = 0; $may = 0; $jun = 0; $jul = 0; $aug = 0; $sep = 0; $oct = 0; $nov = 0; $dec = 0; $getstats = mysql_query("SELECT * FROM $table ORDER by id DESC"); while($row = mysql_fetch_array($getstats)) { $stat_id = $row['id']; $stat_ip = $row['ip']; $stat_day = $row['day']; $stat_month = $row['month']; if ($stat_month == '01'){ $jan++; } elseif ($stat_month == '02'){ $feb++; } elseif ($stat_month == '03'){ $mar++; } elseif ($stat_month == '04'){ $apr++; } elseif ($stat_month == '05'){ $may++; } elseif ($stat_month == '06'){ $jun++; } elseif ($stat_month == '07'){ $jul++; } elseif ($stat_month == '08'){ $aug++; } elseif ($stat_month == '09'){ $sep++; } elseif ($stat_month == '10'){ $oct++; } elseif ($stat_month == '11'){ $nov++; } elseif ($stat_month == '12'){ $dec++; } $stat_year = $row['year']; $stat_date = $row['date']; $stat_time = $row['time']; $stat_dotw = $row['dotw']; if ($stat_dotw == 1){ $mon++; } elseif ($stat_dotw == 2){ $tue++; } elseif ($stat_dotw == 3){ $wed++; } elseif ($stat_dotw == 4){ $thu++; } elseif ($stat_dotw == 5){ $fri++; } elseif ($stat_dotw == 6){ $sat++; } elseif ($stat_dotw == 0){ $sun++; } } if ($mon>=$tue && $mon>=$wed && $mon>=$thu && $mon>=$fri && $mon>=$sat && $mon>=$sun){ $topDay = $mon; } elseif ($tue>=$mon && $tue>=$wed && $tue>=$thu && $tue>=$fri && $tue>=$sat && $tue>=$sun){ $topDay = $tue; } elseif ($wed>=$mon && $wed>=$tue && $wed>=$thu && $wed>=$fri && $wed>=$sat && $wed>=$sun){ $topDay = $wed; } elseif ($thu>=$mon && $thu>=$tue && $thu>=$wed && $thu>=$fri && $thu>=$sat && $thu>=$sun){ $topDay = $thu; } elseif ($fri>=$mon && $fri>=$tue && $fri>=$wed && $fri>=$thu && $fri>=$sat && $fri>=$sun){ $topDay = $fri; } elseif ($sat>=$mon && $sat>=$tue && $sat>=$wed && $sat>=$thu && $sat>=$fri && $sat>=$sun){ $topDay = $sat; } elseif ($sun>=$mon && $sun>=$tue && $sun>=$wed && $sun>=$thu && $sun>=$fri && $sun>=$sat){ $topDay = $sun; } ?> Obviously I have done this for months of the year and days of the month as well so you can imagine the length of the if and else's later on! Any suggestions would be appreciated. Well, thanks for taking a look! Regards, Iceman Quote Link to comment Share on other sites More sharing options...
PHP_PhREEEk Posted November 20, 2007 Share Posted November 20, 2007 Nothing gets echo'd, so it's hard to tell what the point is... so... what's the point? heh What is the end result you are trying to achieve? PhREEEk Quote Link to comment Share on other sites More sharing options...
thebadbad Posted November 20, 2007 Share Posted November 20, 2007 Dunno if this is solved, but it would be more efficient to use switch(). I guess that's exactly what you're looking for, actually Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 20, 2007 Share Posted November 20, 2007 Well, you can get the same date with a DB query and without all the IF/ELSE statements. <?php $months = array ('jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec'); $days = array ('sun','mon','tue','wed','thu','fri','sat'); //Query by months and get the total of each $query = "SELECT month, COUNT(month) as total FROM $table GROUP BY month"; $result = mysql_query($query) or die (mysql_error()); //put the results into an array in the format month => total while ($month_data = mysql_fetch_assosc($result)) { $month_name = $months[($month_data['month']-1)]; $stat_month[$month_name] = $month_data['total']; } //Query by DOTW and get the total of each $query = "SELECT dotw, COUNT(dotw) as total FROM $table GROUP BY dotw"; $result = mysql_query($query) or die (mysql_error()); //put the results into an array in the format dotw => total while ($dotw_data = mysql_fetch_assosc($result)) { $dotw_name = $days[($dotw_data['dotw'])]; $stat_dotw[$dotw_name] = $dotw_data['total']; } //Get the top day from the $stat_dotw array $top_day = max($stat_dotw); ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.