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 12-12-2012, 06:27 PM   PM User | #1
mharrison
New Coder

 
Join Date: Dec 2012
Posts: 52
Thanks: 12
Thanked 0 Times in 0 Posts
mharrison is an unknown quantity at this point
Show results for the week

I'm trying to get my code to display all results entered into the table for the current week.

Right now my code looks like this:
Code:
$past = date('Y-m-d', strtotime('-1 weeks');
$result = mysql_query("SELECT * FROM patterns WHERE datetxt >= '$past' ORDER BY dsptxt");
however it keeps giving me errors no matter how I try to change the code. Right now the error is
Quote:
Parse error: syntax error, unexpected ';' in /homepages/22/d292640728/htdocs/crochet/phptest/new.php on line 9
I would like it to use Monday as the start day no matter what day of the week the query is run.
mharrison is offline   Reply With Quote
Old 12-12-2012, 06:41 PM   PM User | #2
mharrison
New Coder

 
Join Date: Dec 2012
Posts: 52
Thanks: 12
Thanked 0 Times in 0 Posts
mharrison is an unknown quantity at this point
I've also tried it this way:
Code:
$result = mysql_query("SELECT * FROM patterns WHERE YEARWEEK(datetxt = YEARWEEK(CURDATE())) ORDER BY dsptxt");
It doesn't error out, but doesn't display any results either and I have a test db it's connecting to where I have put 3 records in with the date being todays date.
mharrison is offline   Reply With Quote
Old 12-12-2012, 06:45 PM   PM User | #3
mharrison
New Coder

 
Join Date: Dec 2012
Posts: 52
Thanks: 12
Thanked 0 Times in 0 Posts
mharrison is an unknown quantity at this point
Figured it out...had my ()'s wrong.

Correctly works with
Code:
$result = mysql_query("SELECT * FROM patterns WHERE YEARWEEK(datetxt) = YEARWEEK(CURDATE()) ORDER BY dsptxt");
mharrison is offline   Reply With Quote
Old 12-12-2012, 07:19 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Glad to be of help! Hehehe.
The initial problem was here: $past = date('Y-m-d', strtotime('-1 weeks');. You're simply missing the ending ); however, using the functions within the query directly is IMO a much better option.
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
mharrison (12-13-2012)
Old 12-13-2012, 01:03 PM   PM User | #5
mharrison
New Coder

 
Join Date: Dec 2012
Posts: 52
Thanks: 12
Thanked 0 Times in 0 Posts
mharrison is an unknown quantity at this point
Quote:
Originally Posted by Fou-Lu View Post
Glad to be of help! Hehehe.
The initial problem was here: $past = date('Y-m-d', strtotime('-1 weeks');. You're simply missing the ending ); however, using the functions within the query directly is IMO a much better option.
Thanks! It always seems to be a missing element somewhere. Now, I do like the code I did end up getting working better, the question remains if I can use that code to display items in the database added 2 weeks ago, and 3 weeks ago.

It's basically for a "Recently Added" type page that shows "New this week", "New Last Week", and "New 3 Weeks Ago".

The 1st bit of code works, but when you get to the 2 and 3 weeks ago, it also displays everything from this week as well. Would like to isolate the query to just those items from 2 and 3 weeks in the past, if possible.
mharrison is offline   Reply With Quote
Old 12-13-2012, 02:52 PM   PM User | #6
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
That's because of the operator in use. If you want to do that using PHP code, you need to select a date that is BETWEEN two other dates (one three weeks ago and one two weeks ago). Otherwise you get everything from three weeks ago to current.
MySQL can do that too, just use the date_add/date_sub or the overloads of the -/+ operators: WHERE datetxt BETWEEN CURDATE() - INTERVAL 3 WEEK AND CURDATE() - INTERVAL 2 WEEK looks like it would work.

YEARWEEK would work, except you need to check for the YEAR and you need to check when the current week is <= the provided week in the year (since then you have to go back and check last years records). This is easier to do if you manipulate the field itself, but you will slow down the query in doing so. So I think using BETWEEN is a better option.
Fou-Lu is offline   Reply With Quote
Old 12-14-2012, 01:48 AM   PM User | #7
mharrison
New Coder

 
Join Date: Dec 2012
Posts: 52
Thanks: 12
Thanked 0 Times in 0 Posts
mharrison is an unknown quantity at this point
So are you saying it should look something like this?

Code:
$result = mysql_query("SELECT * FROM patterns WHERE datetxt BETWEEN CURDATE() - INTERVAL 2 WEEK AND CURDATE() ORDER BY dsptxt");
When I use that for both 2 and 3 week, it displays the same as my new this week query does.
mharrison is offline   Reply With Quote
Old 12-14-2012, 01:34 PM   PM User | #8
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Your missing the interval on the until date. That is the same as using datetxt >= CURDATE() - INTERVAL 2 WEEK. If you just want records from two weeks ago, but not from the current week, then you need to add the second interval condition.
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
mharrison (12-14-2012)
Old 12-14-2012, 03:50 PM   PM User | #9
mharrison
New Coder

 
Join Date: Dec 2012
Posts: 52
Thanks: 12
Thanked 0 Times in 0 Posts
mharrison is an unknown quantity at this point
Quote:
Originally Posted by Fou-Lu View Post
Your missing the interval on the until date. That is the same as using datetxt >= CURDATE() - INTERVAL 2 WEEK. If you just want records from two weeks ago, but not from the current week, then you need to add the second interval condition.
Ok, I understand what you are saying but at the same time I don't. At least, when it comes to how to write the query for it, I'm not sure what I need to do and where I need to do it. Heading back to google for some help on this query.
mharrison is offline   Reply With Quote
Old 12-14-2012, 04:04 PM   PM User | #10
mharrison
New Coder

 
Join Date: Dec 2012
Posts: 52
Thanks: 12
Thanked 0 Times in 0 Posts
mharrison is an unknown quantity at this point
GOT IT! I was re-reading the post and you specifically stated the answer, but I completely missed it.

Changed my code to look like this:
Code:
$result = mysql_query("SELECT * FROM patterns WHERE datetxt BETWEEN CURDATE() - INTERVAL 3 WEEK AND CURDATE() - INTERVAL 

2 WEEK
and BAM, works great!

Thanks!
mharrison is offline   Reply With Quote
Old 12-14-2012, 04:06 PM   PM User | #11
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
That'll do it yep.
You can use variables to represent the intervals as well, which may be beneficial as you can calculate them based on the request.
Fou-Lu is offline   Reply With Quote
Old 12-14-2012, 04:08 PM   PM User | #12
mharrison
New Coder

 
Join Date: Dec 2012
Posts: 52
Thanks: 12
Thanked 0 Times in 0 Posts
mharrison is an unknown quantity at this point
Quote:
Originally Posted by Fou-Lu View Post
That'll do it yep.
You can use variables to represent the intervals as well, which may be beneficial as you can calculate them based on the request.
lol, one thing at a time...still very new to PHP/MySQL at least outside of what I have to do at work where I can just query the table with a GUI
mharrison is offline   Reply With Quote
Reply

Bookmarks

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 06:57 AM.


Advertisement
Log in to turn off these ads.