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
Share on other sites

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

Link to comment
Share on other sites

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
Share on other sites

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.