flyer automated system

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Smartus
Forum Newbie
Posts: 3
Joined: Sat Sep 11, 2010 9:57 am

flyer automated system

Post by Smartus »

Hi everyone

I have a huge problem, i'm stuck with my code :( I was already posting my problem on an other forum but they can't solve the problem :-(
But first let me explain the main problem from the beginning.

There's a guy with his salsa-site (http://www.sabortropical.be/flyers.htm). He has a flyer section as you can see in the url. Now he has to input every flyer manually, by copy-pasting html and edit the things that have to be edited.
He came to me, if it could be automated. So we had a talk about the features etc. I started coding a small system. In the system he can upload a flyer with data (data + image uploading), this works all fine (http://gyazo.com/33c229f8ac3fd46e9d7dbbaf42cca02e.png). (In the dropdown you can chose every week, every 2 weeks, every month)
He can edit, delete, etc. Now I'm running a cronjob every night to see if the date of the flyer is expired and then I'm putting it in an archive. If it is in the archive, you can put it back by just editing the date and the data + image is back online.

Now he told me, he didn't like it because he wants to upload it all at once (in one day for whole year). There are flyers which have to be repeated. Every first sunday of the month, every second sunday of the month, every month, etc.

First I used a unix timestamp-field in my database for the date, they (guys on the other forum) suggested me to use date-field because you can use mysql's functions.
Now they came with a couple solutions that didn't work:

-

Code: Select all

$query = "SELECT ADDDATE("2010-09-05, INTERVAL 4 WEEK) FROM test";
, this didn't work because every month doesn't have 4 weeks, sometimes 5. So I made something in php that's checking for when i add 4 weeks and it is still in the same month then add a week extra. This solved my problem for every 1ste sunday of the month. But when i try to upload a flyer which is every second saturday, then there's a bug showing up.

- another solutions that someone gave was:
http://gyazo.com/60d496759f933250e899e2a47643ca38.png

This is actually the same as i coded in php, but now all in SQL. He suggested this (it's PostgreSQL) and that I had to convert it into Mysql, but I can't get it working.
But I think it will give me the same bug with every second saturday.

Example of correct dates:
september 2nd saturday = 2010-09-11
october 2nd saturday = 2010-10-09
november 2nd saturday = 2010-11-13

Example of my system's return values
september : 2010-09-11 (input date)
october: 2010-10-09 (It adds to 4 weeks, checks if the month is different from the previous, which is true, so adds no extra week -> answer correct)
november: 2010-11-06 (Here it does the same as in october, checks the month, it's not the same month, now it's november, so it doesn't add extra week.)
BUT NOW IT SHOULD, this is my problem, can't get this fixed :(

Hopefully you guys can help me. Greetz
User avatar
mecha_godzilla
Forum Contributor
Posts: 375
Joined: Wed Apr 14, 2010 4:45 pm
Location: UK

Re: flyer automated system

Post by mecha_godzilla »

Just a quick thought - and apologies if I missed anything - but if you need to do a "first Saturday of the month" thing, you could just use PHP functions:

1. Get the month that you want
2. Create a for() loop, starting from the 1st of the month to the last day in the month
3. Check what day the 1st falls on (let's say it's a Friday)
4. Check what day the 2nd falls on (it's a Saturday) so store this value somewhere and exit the loop

For completeness, you'd want to check what the last valid day of the month was as well before you put the numbers in the for() loop - the function I created to do this looks like:

Code: Select all

function check_valid_date($month,$year) {
    
    // Format for arguments:
    // $month = '12'
    // $year = '2010'

    for ($day = 31; $day >= 28; $day--) {
        
        if (checkdate($month, $day, $year)) {
                return $day;
        }
            
    }
    
}
To get the "second saturday of the month" you'd just need to create a variable that counts how many Saturdays have been found and exit the loop when it gets to the second one.

Is that what you're after? I'd need to re-read your post in detail to suggest any MySQL examples though :)

HTH,

Mecha Godzilla
Smartus
Forum Newbie
Posts: 3
Joined: Sat Sep 11, 2010 9:57 am

Re: flyer automated system

Post by Smartus »

Hey mecha

You're the first one that comes with this idea, and I think it might work. I'm going to try it, and if I have a working solution, i'll post it here.
Actually it's quite simple, why didn't I get this idea :D anyway thanks for your quick respons.

This post is already on 3 forums now, and you're the first to reply, many thanks.

Greetz
User avatar
mecha_godzilla
Forum Contributor
Posts: 375
Joined: Wed Apr 14, 2010 4:45 pm
Location: UK

Re: flyer automated system

Post by mecha_godzilla »

No problem - if you need any help with the scripting just say so :)

M_G
Smartus
Forum Newbie
Posts: 3
Joined: Sat Sep 11, 2010 9:57 am

Re: flyer automated system

Post by Smartus »

Sorry deleted my previous post, programmatically this is more right :)
With this you don't loop too much ass necessary.

Code: Select all

//Values from form
$day = 11;
$month = 9;
$year = 2010;
$weekNeeded = 2;

//script that calculates the correct date
//init values
$week = 1;
$nextMonth = $month + 1;
$dayNameNeeded = date("l", mktime(0,0,0,$month, $day, $year));
$firstDayMonth = date("l", mktime(0,0,0,$nextMonth, 1, $year));
$daysInMonth = date("t", mktime(0,0,0,$nextMonth, 1, $year));

//correct date search
$i = 1;
while($i <= $daysInMonth && !$dateFound)
{
    $currentDayName = date("l", mktime(0,0,0,$nextMonth, $i, $year));
    $nextDayName = date("l", mktime(0,0,0,$nextMonth, $i+1, $year));
    
    if($nextDayName == 'Monday')
        $week ++;
    
    if($week == $weekNeeded)
    {
        if($currentDayName == $dayNameNeeded)
        {
            $correctDate = "$year-$nextMonth-$i";
            $dateFound = true;
        }
    }
    $i++;
}

//output correct date
echo "This day is returned: $currentDayName $correctDate";
you're my hero :drunk:
User avatar
mecha_godzilla
Forum Contributor
Posts: 375
Joined: Wed Apr 14, 2010 4:45 pm
Location: UK

Re: flyer automated system

Post by mecha_godzilla »

Thanks for posting the code - I'll take a look at it later on. As long as it does what you need then it's good enough :)

Just a quick point - if you want to find out what today's date is you can use this bit of code in your script:

Code: Select all

$today = getdate();
$day = $today['mday'];
$month = $today['mon'];
$year = $today['year'];
Good luck with the rest of the site,

M_G
Post Reply