Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 Programming Discussions
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 01-25-2003, 12:26 PM
Hwulex's Avatar
Hwulex Hwulex is offline
 
Join Date: Mar 2002
Location: Manchester, UK
Posts: 97
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default Crazy SQL scripts involving DATELINE and DATE()

Ok.
I'm in the process of writing a new hack, and I'm hoping to include a 'member of the month' function to it. I've been thinking of different ways to do this (preferably without Cron Jobs), and thought I'd come up with a solution, but I'm having problems.
The motm is determined by a condition attached to each post.

Basically, the way I thought I'd do it is (pseudocode):
Code:
if (currentmonth != storedmonth)
  query_first(
    SELECT username, userid
    FROM user
    WHERE dateline <= todaysdate
    AND dateline >= a month ago
    ORDER BY condition
    DESC
  )
}
Basically, I need a way (within MySQL) of retrieving ONLY the posts that were made in the last month. Now, I thought I could embed the date() function within it, but that doesn't work.

Anyone any ideas on how I can do this?

As a temp mock-up, I've used:
Code:
    WHERE p.dateline >= $today-(60*60*24*30)
    AND p.dateline <= $today-(60*60*24)
But that's not terribly accurate, depending on when it gets executed, and the fact that not every month has 30 days etc.

I did try:
Code:
    WHERE ".date('mY',."p.dateline".)." = $currentmonth
But that throws out a parse error.


I hope this makes some sort of sense. I know I've been a little vague, but I don't wish to give too much away at this point. But if people need more info I will do my best to comply

Hwu
Reply With Quote
  #2  
Old 01-25-2003, 07:00 PM
Icheb's Avatar
Icheb Icheb is offline
 
Join Date: Oct 2002
Location: Germany
Posts: 86
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

First, get the number of the current month with PHP's date, then get the UNIX time of the first day in the current month and query the table for all entries which have a higher value in the time column.
There you go :-) .


PHP Code:
WHERE ".date('mY',."p.dateline".)." $currentmonth 
The reason why this doesn't work is the fact that p.dateline is no valid timestamp. Even it were, it wouldn't work since the query gets parsed by PHP and then it's being sent to the MySQL server, which returns the values for the queried fields. But date() needs the timestamp *before* the query is sent.
And btw, it doesn't work for a second reason: The value to the left has to be the name of a column, but i doubt date() will return the name of an already existing column.
Reply With Quote
  #3  
Old 01-26-2003, 02:21 AM
Hwulex's Avatar
Hwulex Hwulex is offline
 
Join Date: Mar 2002
Location: Manchester, UK
Posts: 97
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Thanks for the suggestion, but I still don't think that will work, for the same reason I post above. AFAIC tell, that's the same logic as I was trying to use first time.
The problem is, I need to retrieve all posts from the previous month to this one, and I can't see a short way of doing it without much code writing.


I understand what you're saying about the quoted code. I never expected it to work, was just a wild stab in the dark out of desperation.
Btw, the p.dateline that you refer to is valid, but I haven't quoted the rest of the query. p is an alias for the post table which is set earlier in the query.

Cheers.
Reply With Quote
  #4  
Old 01-26-2003, 08:23 AM
Icheb's Avatar
Icheb Icheb is offline
 
Join Date: Oct 2002
Location: Germany
Posts: 86
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I never said that p.dateline isn't valid. What i said was that the where-clause is not valid. You have to supply the name of a column there, but date() sure won't give you the name of an existing column.
Reply With Quote
  #5  
Old 01-26-2003, 10:49 AM
Hwulex's Avatar
Hwulex Hwulex is offline
 
Join Date: Mar 2002
Location: Manchester, UK
Posts: 97
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Ah right, I see what you mean.
Sorry

Looks like I might have to forget this without cron jobs. Shame
Reply With Quote
  #6  
Old 01-26-2003, 01:19 PM
Icheb's Avatar
Icheb Icheb is offline
 
Join Date: Oct 2002
Location: Germany
Posts: 86
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Why shouldn't this work? Just try it!

"First, get the number of the current month with PHP's date, then get the UNIX time of the first day in the current month and query the table for all entries which have a higher value in the time column."
Reply With Quote
  #7  
Old 01-27-2003, 05:58 PM
Hwulex's Avatar
Hwulex Hwulex is offline
 
Join Date: Mar 2002
Location: Manchester, UK
Posts: 97
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally posted by Icheb
Why shouldn't this work? Just try it!

"First, get the number of the current month with PHP's date, then get the UNIX time of the first day in the current month and query the table for all entries which have a higher value in the time column."
Hi Icheb.
Thanks for your pointers. I didn't quite follow what you were getting at the first time I read it, but looked again last night with a clear head, did some research, and have solved the problem.

Code looks something like this:
PHP Code:
// Member of the month stuff
$thismonth mktime (0,0,0,date("m"),1,  date("Y"));
$lastmonth mktime (0,0,0,date("m")-1,1,  date("Y"));
$motm $DB_site->query_first("
    SELECT
        u.userid,
        u.username
    FROM post p
    LEFT JOIN user u ON (p.userid = u.userid)
    LEFT JOIN usergroup g ON (u.usergroupid=g.usergroupid)
    WHERE p.dateline >= 
$lastmonth
    AND p.dateline <= 
$thismonth
    GROUP BY u.userid
    ORDER BY condition
    DESC
"
); 
Cheers.
Reply With Quote
Reply


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. The time now is 09:06 PM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.03758 seconds
  • Memory Usage 2,234KB
  • Queries Executed 13 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (3)bbcode_code
  • (2)bbcode_php
  • (1)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (7)post_thanks_box
  • (7)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (7)post_thanks_postbit_info
  • (7)postbit
  • (7)postbit_onlinestatus
  • (7)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete