openskies2009 Posted September 23, 2016 Share Posted September 23, 2016 (edited) 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 September 23, 2016 by cyberRobot removed duplicate code / post Quote Link to comment https://forums.phpfreaks.com/topic/302232-get-next-from-array-every-six-months/ Share on other sites More sharing options...
requinix Posted September 24, 2016 Share Posted September 24, 2016 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 = 242052. 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) = 4034Note 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 = 24. 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 imaginationSo 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? Quote Link to comment https://forums.phpfreaks.com/topic/302232-get-next-from-array-every-six-months/#findComment-1537773 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.