I am working on a project in which i have to distribute different ad’s per day, the ad’s in form of array are:
$ad = array( 'attribute1_value' => "12",
'attribute2_value' => "xyz",
'attribute3_value' => 'http://example.com',
'attribute4_value' => 'data');
The logic i am using with switch case :
$day = date('w',time());
switch ($day) {
case '0':
if($day == '0') {
$count = 0;
echo $ad;
$count++;
}
else {
$count = 7;
echo $ad;
}
break;
case '1':
if($day == '1') {
$count = 1;
echo $ad;
$count++;
}
else {
$count = 8;
echo $ad;
}
break;
Problem is if i have ~15 ad’s then i want to distribute ad/day, date(‘w’) output’s the present day but after day 7 i.e saturday, on sunday ad number 8 initiate. I have to implement this scenario using date function. Also i have to send ad’s to those user who are not experience this ad before. I am not expert in php, as a beginner working in php/mysql. Kindly help me to improve this concept
There’s some issues with your current code. First, with logic:
Also, you’re echo’ing out
$ads, which is an array. You need to echo out an element from the array.If you want one Monday to use ad 1 and the following Monday to use ad 8,
date('w')isn’t going to be much help as that simply cycles through 0 to 6, so it is unsuitable for this. It has no memory of the overall day number of the month or year – its scope is only the current week.You could use the day of the month instead [edit – or, of course, the day of the year (
date('z')). See comment underneath).So on the 5th day of the month, ad 5 shows. On the 7th day, ad 7 shows. On the 14th day, ad 2 shows, as it starts round the ad cycle again.
As for how to ensure a user has not seen the ad before, that’s a whole other question and I suggest you post it as such. This would presumably involve some sort of cookie system, unless users are logged in, in which case you could track this sort of thing server-side.