Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-16-2012, 06:16 AM   PM User | #1
Mayhem30
New Coder

 
Join Date: Jan 2010
Posts: 63
Thanks: 2
Thanked 5 Times in 4 Posts
Mayhem30 is an unknown quantity at this point
Clean Sales Report Code

I have a script that keeps track of sales - but the code is really messy.

Is there a way to rewrite it so it only uses one mysql query - then parse the results in the different sections with php?

PHP Code:
$now date("Y-m-d");
$y date("Y-m-d"strtotime("-1 day"));
$w date("Y-m-d"strtotime("-7 day"));
$ww date("Y-m-d"strtotime("-14 day"));
$m date("Y-m-d"strtotime("-30 day"));
$this_month date("Y-m");
$last_month date("Y-m"strtotime("-1 month"));

// For Friendly printing
$today date("M d, Y");


// Today
$result mysql_query("SELECT date FROM payment WHERE date = '$now'");
$count mysql_num_rows($result);

// Yesterday
$result mysql_query("SELECT date FROM payment WHERE date = '$y'");
$count2 mysql_num_rows($result);

// Last 7 days
$result mysql_query("SELECT date FROM payment WHERE date >= '$w'");
$count3 mysql_num_rows($result);

// Last 14 days
$result mysql_query("SELECT date FROM payment WHERE date >= '$ww'");
$count4 mysql_num_rows($result);

// Last 30 Days
$result mysql_query("SELECT date FROM payment WHERE date >= '$m'");
$count5 mysql_num_rows($result);

// All Time
$result mysql_query("SELECT date FROM payment");
$count6 mysql_num_rows($result);

// This month
$result mysql_query("SELECT date FROM payment WHERE date LIKE '$this_month-%'");
$count7 mysql_num_rows($result);

// Last Month
$result mysql_query("SELECT date FROM payment WHERE date LIKE '$last_month-%'");
$count8 mysql_num_rows($result);

echo 
"<table width=\"700px\"><tr><td>";
echo 
"Product Sales<br><br>Today : $count<br>";
echo 
"Yesterday : $count2<br><br>Last 7 days : $count3<br>Last 14 days : $count4<br> Last 30 days : $count5<br><br>This Month : $count7<br>Last Month : $count8<br><br>All Time Sales : $count6</td></tr></table>"
Also, the This Month" and "Last Month" doesn't see to be working correctly .. "This Month" resets to 0 a couple days before the month is even over.

My server date and time is set correctly, so I'm not sure what is causing that.
Mayhem30 is offline   Reply With Quote
Old 06-16-2012, 04:37 PM   PM User | #2
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,042
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
You have a column named "date".

Tell us exactly what column type that is, and an example of your date format.



.
mlseim is offline   Reply With Quote
Old 06-16-2012, 07:28 PM   PM User | #3
Mayhem30
New Coder

 
Join Date: Jan 2010
Posts: 63
Thanks: 2
Thanked 5 Times in 4 Posts
Mayhem30 is an unknown quantity at this point
The column type is "Date" and the format is "0000-00-00".

Thanks.
Mayhem30 is offline   Reply With Quote
Old 06-17-2012, 03:32 AM   PM User | #4
Mayhem30
New Coder

 
Join Date: Jan 2010
Posts: 63
Thanks: 2
Thanked 5 Times in 4 Posts
Mayhem30 is an unknown quantity at this point
Yyyy-mm-dd
Mayhem30 is offline   Reply With Quote
Old 06-17-2012, 06:37 PM   PM User | #5
Mayhem30
New Coder

 
Join Date: Jan 2010
Posts: 63
Thanks: 2
Thanked 5 Times in 4 Posts
Mayhem30 is an unknown quantity at this point
Is anyone available to help me with this?
Mayhem30 is offline   Reply With Quote
Old 06-17-2012, 07:04 PM   PM User | #6
sunfighter
Senior Coder

 
Join Date: Jan 2011
Location: Missouri
Posts: 2,362
Thanks: 18
Thanked 347 Times in 346 Posts
sunfighter is on a distinguished road
I have not tried this so it just might be BS. Give it a go and see what you get:
Code:
SELECT COUNT(*) FROM payment GROUP BY date
sunfighter is offline   Reply With Quote
Old 06-17-2012, 08:19 PM   PM User | #7
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,042
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
I'm thinking you do one query (for all) and use
strtotime() to do compares with the current time().

But, as Sunfighter has mentioned, maybe there's a way to group.
It's the comparisons of ranges that will be the tough part, but MySQL
is powerful ... there might be a way that I'm not aware of.
mlseim is offline   Reply With Quote
Old 06-19-2012, 04:20 AM   PM User | #8
Mayhem30
New Coder

 
Join Date: Jan 2010
Posts: 63
Thanks: 2
Thanked 5 Times in 4 Posts
Mayhem30 is an unknown quantity at this point
I managed to figure it out, thanks guys.
Mayhem30 is offline   Reply With Quote
Old 06-19-2012, 02:43 PM   PM User | #9
sunfighter
Senior Coder

 
Join Date: Jan 2011
Location: Missouri
Posts: 2,362
Thanks: 18
Thanked 347 Times in 346 Posts
sunfighter is on a distinguished road
Please tell us what you did.
sunfighter is offline   Reply With Quote
Old 06-20-2012, 07:36 PM   PM User | #10
Mayhem30
New Coder

 
Join Date: Jan 2010
Posts: 63
Thanks: 2
Thanked 5 Times in 4 Posts
Mayhem30 is an unknown quantity at this point
Here is how I did it - it probably can be improved, but it works.

PHP Code:
$today date("Y-m-d"strtotime("now"));
$date2 date("Y-m"strtotime($date));
$y date("Y-m-d"strtotime("-1 day"));
$w date("Y-m-d"strtotime("-7 day"));
$ww date("Y-m-d"strtotime("-14 day"));
$m date("Y-m-d"strtotime("-30 day"));
$this_month date("Y-m"strtotime('this month'));
$last_month date("Y-m"strtotime("-1 month"));

$result mysql_query("SELECT date FROM payment FORCE INDEX(PRIMARY) WHERE ID <> 0");
$count mysql_num_rows($result);

for (
$t 0$t $count$t++)
{
    
$row mysql_fetch_array($result);
    
$date $row['date'];

    If (
$date == $today)
    {
        
$TodayCnt++;
    }
    If (
$date == $y)
    {
        
$YesterdayCnt++;
    }
    If (
$date >= $w)
    {
        
$Last7Cnt++;
    }
    If (
$date >= $ww)
    {
        
$Last14Cnt++;
    }
    If (
$date >= $m)
    {
        
$Last30Cnt++;
    }
    If (
$date2 == $this_month)
    {
        
$ThisMonthCnt++;
    }
    If (
$date2 == $last_month)
    {
        
$LastMonthCnt++;
    }
}

echo 
"<table width=\"700px\"><tr><td>";
echo 
"Product Sales<br><br>Today : $TodayCnt<br>";
echo 
"Yesterday : $YesterdayCnt<br><br>Last 7 days : $Last7Cnt<br>Last 14 days : $Last14Cnt<br> Last 30 days : $Last30Cnt<br><br>This Month : $ThisMonthCnt<br>Last Month : $LastMonthCnt<br><br>All Time Sales : $count</td></tr></table>"
Mayhem30 is offline   Reply With Quote
Reply

Bookmarks

Tags
mysql, php

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:24 PM.


Advertisement
Log in to turn off these ads.