a php and mysql loop challenging me...

This is a discussion on "a php and mysql loop challenging me..." within the PHP Forum section. This forum, and the thread "a php and mysql loop challenging me... are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Feb 8th, 2008, 00:12
Up'n'Coming Member
Join Date: May 2007
Location: northern nsw, au
Age: 27
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
a php and mysql loop challenging me...

hi everyone... I have a php and mysql loop that retrieves the number of hits to a page for each day over the past 7 days, and also for each month over the past 12 months... it 'kinda' works but there is a problem in that it doesn't just pull out the stats that are on the day of today, and then the day of yesterday, but hits within the last 24 hours...

Code: Select all
//loop for 7 days,counts
for( $i=0; $i<7; $i++ )
{
                
    $dayafter = $i + 1;
    // the query            
    $daySQL = "
    	SELECT COUNT(*) 
       	FROM site_refer 
       	WHERE thing='" . $row['thing'] . "'
       	AND (DATE(site_refer.when) <= DATE_SUB(CURRENT_DATE(), INTERVAL " . $i . " DAY)) 
      	AND (DATE(site_refer.when) > DATE_SUB(CURRENT_DATE(), INTERVAL " . $dayafter . " DAY)) 
    ";
                
    $resthisday = mysql_query($daySQL);
    $rowthisday = mysql_fetch_array($resthisday);
    
    // results for each day   
    echo '<span style="font-size: 7pt;">' . $rowthisday[0] . '</span>, ';
                
}

echo '<br />';
            
// loop count for 12 months
for( $i=0; $i<12; $i++ )
{
    
    // this is all the same code as above for the 7 days except months instead of days
            
    $monthafter = $i + 1;
               
    $daySQL = "
       	SELECT COUNT(*) 
      	FROM site_refer 
       	WHERE thing='" . $row['thing'] . "'
       	AND (DATE(site_refer.when) <= DATE_SUB(CURRENT_DATE(), INTERVAL " . $i . " MONTH)) 
       	AND (DATE(site_refer.when) > DATE_SUB(CURRENT_DATE(), INTERVAL " . $monthafter . " MONTH)) 
    ";
                
    $resthisday = mysql_query($daySQL);
    $rowthisday = mysql_fetch_array($resthisday);
                
    echo '<span style="font-size: 7pt;">' . $rowthisday[0] . '</span>, ';
                
}

In its full glory, it produces results like:

eguide(12)
0, 12, 0, 0, 0, 0, 0,
12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,


Thanks for any pointers you can give on this,

B

p.s the field site_refer.when is a timestamp

Last edited by cosmicbdog; Feb 8th, 2008 at 00:57. Reason: easier to understand now
Reply With Quote

  #2 (permalink)  
Old Feb 8th, 2008, 00:51
Up'n'Coming Member
Join Date: May 2007
Location: northern nsw, au
Age: 27
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
Re: a php and mysql loop challenging me...

In other words, how does one go about "select entries from last month" instead of "select entries within the last month" AND "select entries from yesterday" instead of "select entries within the last day"?
Reply With Quote
  #3 (permalink)  
Old Feb 8th, 2008, 03:46
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: a php and mysql loop challenging me...

I think if you're using Mysql timestamps, you don't need to use the <= comparison. You would just have to check if the day is the same using only the date part of the stamp.

Code: Select all
WHERE

DATE(site_refer.when) = DATE(DATE_SUB(CURRENT_DATE(), INTERVAL " . $i . " DAY))
ANd then for the month part, just compare the month part of the stamp

Code: Select all
WHERE

MONTH(site_refer.when) = MONTH(DATE_SUB(CURRENT_DATE(), INTERVAL " . $i . " MONTH))
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Reply With Quote
  #4 (permalink)  
Old Feb 8th, 2008, 04:31
Up'n'Coming Member
Join Date: May 2007
Location: northern nsw, au
Age: 27
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
Re: a php and mysql loop challenging me...

so easy... ! a whole rule removed from each... thank you heaps
Reply With Quote
Reply

Thread Tools

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
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Do a while loop once? Jack Franklin PHP Forum 2 Feb 15th, 2008 11:07
The Loop (Again) Blake121 Free Web Site Critique 16 Sep 7th, 2007 13:57
The Loop V2 Blake121 Free Web Site Critique 8 May 15th, 2007 09:37
The Loop Blake121 Free Web Site Critique 5 May 1st, 2007 14:25
Loop??? tazek0 Classic ASP 0 Jan 27th, 2006 07:38


All times are GMT. The time now is 11:56.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43