My current project is for a delivery sheet creator.
Whilst I have not yet implemented it, I have to have it so that upon entering the order, you must select whether the order is a one-off, or whether it should be repeated weekly on the same day, or whether it should be repeated fortnightly.
What I think I will do is have a 'recur' field in my 'orders' table. The data from the form (e.g. a combo box) will go in this form, and be interpreted as follows:
0 = One-off order
1 = Weekly order
2 = Fortnightly order
I have an input box with a calendar, which is used to select the required date. This then redirects to a page with a query, which displays the results.
For testing purposes, I have used the following query, which works for weekly orders:
PHP Code:
$result = mysql_query ("SELECT * FROM orders LEFT JOIN customers ON (customers.ID = orders.customer) WHERE DAYOFWEEK(date)=DAYOFWEEK(' . $DelDate . ')");
This works great - it shows items on the correct day.
I cannot work out how to do it for fortnightly orders, though.
I'll clarify what I mean here.
Say I have the following orders:
Order 1: 29 April, Weekly
Order 2: 6 April, Fortnightly
Order 3: 13 April, Weekly
These are all Tuesdays. If I were to search the 13 April, currently it would show me all 3. However, I don't want it to show me order 2 - that should appear for the 6th or the 20th, but not the 13th.
How can I go about this?
Someone suggested I use the following, but it gives me an error:
Sounds to me like you need a set of AND and OR clauses in your where:
Code:
SELECT * FROM orders
LEFT JOIN customers
ON (customers.ID = orders.customer)
WHERE (`recur` = 1 AND DAYOFWEEK(date)=DAYOFWEEK(' . $DelDate . '))
OR (`recur` = 2 AND DATEDIFF(date, ' . $DelDate . ')%14=0)
Seems to work for me when I tested locally with some very very raw data.
Here's a table dump for you:
Code:
DROP TABLE IF EXISTS `orders`;
CREATE TABLE `orders` (
`id` int(10) unsigned NOT NULL auto_increment,
`recur` int(10) unsigned NOT NULL,
`date` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;
And my result as I run the query based on two different dates:
Code:
QUERY:
SELECT * FROM orders
WHERE (`recur` = 1 AND DAYOFWEEK(date)=DAYOFWEEK('2008-04-20'))
OR (`recur` = 2 AND DATEDIFF(date, '2008-04-20')%14=0)
RESULT:
2, 2, '2008-04-06 12:30:00'
3, 1, '2008-04-13 12:30:00'
QUERY:
SELECT * FROM orders
WHERE (`recur` = 1 AND DAYOFWEEK(date)=DAYOFWEEK('2008-04-13'))
OR (`recur` = 2 AND DATEDIFF(date, '2008-04-13')%14=0)
RESULT:
3, 1, '2008-04-13 12:30:00'
Hope that helps.
Oh, and the "%" is a modulo operator. I think what they forgot to tell you was that MOD is a function and is to be used like:
Code:
SELECT * FROM orders
WHERE (`recur` = 1 AND DAYOFWEEK(date)=DAYOFWEEK(' . $DelDate . '))
OR (`recur` = 2 AND MOD(DATEDIFF(date, ' . $DelDate . '), 14)=0)
SELECT *
FROM orders
LEFT JOIN customers ON ( customers.id = orders.customer )
WHERE DAYOFWEEK( date ) = DAYOFWEEK( '2008-04-24' )
This returned results.
I then changed the query to:
Code:
SELECT *
FROM orders
LEFT JOIN customers ON ( customers.id = orders.customer )
WHERE DATEDIFF(date, '2008-04-24')%14=0
This threw up an error:
#1064 - You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '( date , '2008-04-24' ) % 14 = 0
LIMIT 0, 30' at line 1
Oops, I am an idiot! I put $DelDate in the query on phpMyAdmin. Of course, I meant to change that to '2008-04-24' also. I did so (updated the above) and it still threw up the error.
I changed the code as you suggested. I still got the error. So I put your code into the phpMyAdmin query, changing $DelDate for a date, and still got the error.
I noticed that date is changed to DATE in the query. Is it a reserved keyword? I tried changing it, but it made no difference.
Date is a reserved keyword, meaning you should encase it in "`" backticks. Of course, to mitigate future issues, you should always encase column names in back-ticks It's just good practice.
Well, I did that, but it still didn't help. I then noticed that in phpMyAdmin, DATEDIFF was in red. I thought that was just because they coloured everything differently, but it got me wondering. So I looked up DATEDIFF, and it needs v4.1.1. My host are running 4.0.26! So I asked them why that was, and they said they have now implemented a MySQL 5 server, set up a database, I copied the data across and hey presto, it works!!
Why they didn't update to the last version of 4, I don't know! Grr!
Bookmarks