TecBrat Posted November 29, 2006 Share Posted November 29, 2006 Can anyone tell me if this logic is correct?Given a number D that is the numeric day of the year (where"jan 1" is "1" and "dec 31" of a non leap year is "365"Given a list size L I need to cycle through the list starting over every L days.cycle_postion= (D mod L)+1 Link to comment https://forums.phpfreaks.com/topic/28856-actually-a-math-question/ Share on other sites More sharing options...
Psycho Posted November 29, 2006 Share Posted November 29, 2006 If I am understanding this correctly, you have a list of items, and you want to have a way to cycle through each item in the list starting at the 1st item on day 1 until the list is empty and then start over.For example, if you hav a list of threeitems:Day 1 - Item 1Day 2 - Item 2Day 3 - Item 3Day 4 - Item 1Day 5 - Item 2....If that is the case, then the (+1) in your formula is not needed. Using the same example:(5 mod 3) = Item 2However, if these items are in an array or another 0 based index you would want to subtract 1 to get the correct index number. Link to comment https://forums.phpfreaks.com/topic/28856-actually-a-math-question/#findComment-132133 Share on other sites More sharing options...
TecBrat Posted November 29, 2006 Author Share Posted November 29, 2006 [quote]If that is the case, then the (+1) in your formula is not needed. Using the same example:(5 mod 3) = Item 2However, if these items are in an array or another 0 based index you would want to subtract 1 to get the correct index number.[/quote]thank you.what was messing me up was knowing that I would have a zero every L number of days. Now I realize that I need something like cycle_postion= (D mod L); if cycle_position==0 then cycle_position=L;I understand about array indexes, I just needed the core math before I went any further. Link to comment https://forums.phpfreaks.com/topic/28856-actually-a-math-question/#findComment-132193 Share on other sites More sharing options...
TecBrat Posted November 29, 2006 Author Share Posted November 29, 2006 In case anyone is interested, here's the implementation. I decided to use a simple switch/case instead of putting the agents in an array. I could replace the switch with include('agentfile'.$cycle_position.'.php');[code]<?$number_of_agents=5;$daycount=date('z');$cycle_position= ($daycount % $number_of_agents);if ($cycle_position==0) {$cycle_position=$number_of_agents;}switch ($cycle_position){case 1:echo("<a href=http://www.agent1.com>Agent 1 </a>");break;case 2:echo("<a href=http://www.agent2.com>Agent 2</a>");break;case 3:echo("<a href=http://www.agent3.com>Agent 3</a>");break;case 4:echo("<a href=http://www.agent4.com>Agent 4</a>");break;case 5:echo("<a href=http://www.agent5.com>Agent 5</a>");break;default:break;}?>[/code] Link to comment https://forums.phpfreaks.com/topic/28856-actually-a-math-question/#findComment-132219 Share on other sites More sharing options...
kenrbnsn Posted November 29, 2006 Share Posted November 29, 2006 There's a much easier way to do this:[code]<?php$number_of_agents=5;$daycount=date('z');$cycle_position= ($daycount % $number_of_agents);if ($cycle_position==0) {$cycle_position=$number_of_agents;}echo '<a href="http://www.agent' . $cycle_position . '.com">Agent ' . $cycle_position . ' </a>';?>[/code]Ken Link to comment https://forums.phpfreaks.com/topic/28856-actually-a-math-question/#findComment-132228 Share on other sites More sharing options...
TecBrat Posted November 29, 2006 Author Share Posted November 29, 2006 [quote author=kenrbnsn link=topic=116710.msg475772#msg475772 date=1164826133]There's a much easier way to do this:[/quote]You're right. That is roughly equivilent to what I said about using an include statement. But the echo() version only works as long as the agent link and agent anchor text are always a numeric progression. (What if agent 2 has a domain of www.agent2isreallycool.com and wants anchor text of "Agent 2 Cool"). In the switch version, you can hard code whatever you want for either of these, or in the include() version you can have numericly progressive filenames and put whatever you want in them. Link to comment https://forums.phpfreaks.com/topic/28856-actually-a-math-question/#findComment-132289 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.