Jump to content

Recommended Posts

I have a simple MYSQL DB listing 30 articles.

We would like to display ten different articles every six months.

How could we do this?

 

This is the code which I have so far which isn't near completion but my hang was to set the pointer back to the start of the articles when reached 30, also to increment the start by 10 every six months.  

$unitStart ='1474657057';
$nowTime = time();
$interPassed = round(    $nowTime - $unitStart   );
$secToChange = '15552000';
$intv=  $interPassed / $secToChange;
 
$str = 'SELECT * FROM tc_articals LIMIT 0,6';
$result = mysql_query($str)or die(mysql_error());
Edited by cyberRobot
removed duplicate code / post
Link to comment
https://forums.phpfreaks.com/topic/302232-get-next-from-array-every-six-months/
Share on other sites

You say 10 articles but your query says 6...

 

There's a general approach to take with this sort of problem.

 

1. Calculate a number that increases by 1 for every unit of time you care about. That's a month. Yes, you said you want every six months, but the unit you're using is a month. Include the year so that January 2017 = December 2016 + 1.

year * 12 + month
2016 * 12 + 9  = 24201
2016 * 12 + 10 = 24202
2016 * 12 + 11 = 24203
2016 * 12 + 12 = 24204
2017 * 12 + 1  = 24205
2. Scale that number down according to how many intervals of that unit you want. Here is where the six months matters.

floor((year * 12 + month) / 6)
floor((2016 * 12 + 9 ) / 6) = 4033
floor((2016 * 12 + 10) / 6) = 4033
floor((2016 * 12 + 11) / 6) = 4033
floor((2016 * 12 + 12) / 6) = 4034
floor((2017 * 12 + 1 ) / 6) = 4034
Note that it changes in December, and will again in June. If you want Jan-Jun and Jul-Dec then alter the formula from step 1 to offset by however much you need (ie, -1).

 

3. Use modulus to reduce that number to choose which group of articles you want. If you have 30 articles and want 10 at a time then that's 3 groups.

floor((year * 12 + month - 1) / 6) % 3
floor((2016 * 12 + 9  - 1) / 6) % 3 = 1
floor((2016 * 12 + 10 - 1) / 6) % 3 = 1
floor((2016 * 12 + 11 - 1) / 6) % 3 = 1
floor((2016 * 12 + 12 - 1) / 6) % 3 = 1
floor((2017 * 12 + 1  - 1) / 6) % 3 = 2
4. For your query, you need to determine a starting offset given a group number. Since the first group will be 0 (0-2 not 1-3), the offset is just group * 10.

use your imagination
So you're left with

$offset = (floor((date("Y") * 12 + date("m") - 1) / 6) % 3) * 10;
$str = "SELECT * FROM tc_articals LIMIT {$offset}, 6";
By the way, your idea won't work unless you have an exact multiple of 10 articles. Are you sure you will always have that?
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.