PTS Posted March 27, 2007 Share Posted March 27, 2007 On a site I am working on, I have a right column area that I want to load content from an include file from. However, I have five different include files which I want to rotate in order. For example, visitor views home page with include page 1 as the right column. They refresh the page or click on a link and the following page has include page 2. They click again and now it's include page 3. And so on until include page 5 and then back to include page 1. Currently, I use rand() to randomly pick the include file but I want to find a way to make these go in order for a visitor. Is there an easy way to do this? Any suggestions/direction would be greatly appreciated. Link to comment https://forums.phpfreaks.com/topic/44547-how-to-rotate-in-sequence/ Share on other sites More sharing options...
per1os Posted March 27, 2007 Share Posted March 27, 2007 You want to use sessions. <?php session_start(); if (isset($_SESSION['viewing'])) $_SESSION['viewing']++; // add one else $_SESSION['viewing'] = 1; // start of the viewing include('columnpage' . $_SESSION['viewing'] . '.inc.php'); ?> Link to comment https://forums.phpfreaks.com/topic/44547-how-to-rotate-in-sequence/#findComment-216363 Share on other sites More sharing options...
PTS Posted March 27, 2007 Author Share Posted March 27, 2007 Thanks. I never thought it would be so simple. I gotta get myself to learn more about sessions. I did the following to get the number back down to 1 after it hits 5. session_start(); if (isset($_SESSION['viewing'])) $_SESSION['viewing']++; // add one else $_SESSION['viewing'] = 1; // start of the viewing if ($_SESSION['viewing'] >= 5) {$_SESSION['viewing'] = 0;} echo $_SESSION['viewing']; Is this the best way to do it? Seems to do the trick for what I'm looking for. Link to comment https://forums.phpfreaks.com/topic/44547-how-to-rotate-in-sequence/#findComment-216399 Share on other sites More sharing options...
per1os Posted March 27, 2007 Share Posted March 27, 2007 That works, there is really no "best way" but here is another option for you. session_start(); if (isset($_SESSION['viewing'])) { if ($_SESSION['viewing'] > 4) $_SESSION['viewing'] = 0; else $_SESSION['viewing']++; // add one }else { $_SESSION['viewing'] = 1; // start of the viewing } echo $_SESSION['viewing']; But as they say there is more than one way to skin a fat cat. Link to comment https://forums.phpfreaks.com/topic/44547-how-to-rotate-in-sequence/#findComment-216403 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.