Jump to content

How To Rotate In Sequence?


PTS

Recommended Posts

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

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.