We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

sql query module to find entries not older than 31 days

Development and Programming

Peter Hampus's avatar
Peter Hampus
30 posts
15 years ago
Peter Hampus's avatar Peter Hampus

I want to display a list of categories and also the number of entries in them (as described here But the entries must not be older than 31 days.

So I used the exp:query module like this:

{exp:query sql="SELECT count( exp_category_posts.entry_id ) AS total_items, 
exp_category_posts.cat_id, exp_categories.cat_name as category_name, exp_categories.cat_url_title as category_url_title
FROM exp_categories, exp_category_posts, exp_weblog_titles
WHERE exp_category_posts.cat_id = exp_categories.cat_id 
AND exp_categories.site_id = 1
AND exp_categories.group_id = 4
AND exp_weblog_titles.entry_id = exp_category_posts.entry_id
AND exp_weblog_titles.weblog_id = 69
AND exp_weblog_titles.entry_date > <?php echo time() - (31 * 24 * 60 * 60); ?>
GROUP BY exp_categories.cat_name"}  

<a href="http://{path=weblog/page}C{cat_id}/">{cat_name}</a> ({count}) 

 
{/exp:query}

But I get a SQL error so I guess the row

AND exp_weblog_titles.entry_date > <?php echo time() - (31 * 24 * 60 * 60); ?>

is the problem.

Can someone please explain what I am doing wrong? Or give me another solution?

[Mod Edit: Moved to the Development and Programming forum]

       
Adrienne L. Travis's avatar
Adrienne L. Travis
213 posts
15 years ago
Adrienne L. Travis's avatar Adrienne L. Travis

Patrick,

First of all, if PHP isn’t enabled in your template on Input, then that code won’t work. HOWEVER, you don’t need PHP to do what you’re trying to do at all! MySQL has a perfectly find set of date functions built in.

Just replace your problem line with:

AND exp_weblog_titles.entry_date >= UNIX_TIMESTAMP(CURDATE() - INTERVAL 31 DAY)

That says, “take the current date, subtract 31 days, and then turn it into an epoch date (otherwise known as a UNIX timestamp).”

       
Peter Hampus's avatar
Peter Hampus
30 posts
15 years ago
Peter Hampus's avatar Peter Hampus

Thanks alot! It works just the way I want!

       
Adrienne L. Travis's avatar
Adrienne L. Travis
213 posts
15 years ago
Adrienne L. Travis's avatar Adrienne L. Travis

You’re welcome! If you have any more mySQL/query questions, ask them in the forum (because it’s important for other people to be able to see the answers!) – but please feel free to ALSO PM me to tell me “hey, i’ve got an SQL question, here’s the thread link.” Because i <3 SQL queries, and i’m always happy to help, but i don’t always see all the forum posts.

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.