HTML Tutorial


 Forum HomeForum Home   FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
RegisterRegister - Not registered yet? Got something to say? Join HTML Code Tutorial!
Sequential image upon each reload (cycle vs. random)
Post new topic   Reply to topic    HTML Help Forum Index -> PHP
View previous topic :: View next topic  
Author Message
mypointofview



Joined: 18 Mar 2005
Posts: 32
Location: Los Angeles

PostPosted: Sat Dec 30, 2006 7:19 pm     Sequential image upon each reload (cycle vs. random) Reply with quote

Is there a way to get a sequential (not random) new image upon each reaload ?

I have let's say 10 images. I want that the user upon first visit sees image 1, then when the visitor reloads the page, he'd see picture 2 and so forth. When seen image 10 he'd then see upon renewed reload image 1.

I tried a random script and the problem is that sometimes, especially when there are not so many images in the pile, that the same image shows the next time again. Like throwing a dice -- the liklyhood to have the same result twice exists Sad

How can I achieve my task? I've checked everywhere -- is it even possible ??

Thanks,


Martin

PS: I want to simply make a website with a nice photo splashscreen that is guaranteed to be different with each visit.
Thor Erik



Joined: 30 Dec 2006
Posts: 8

PostPosted: Sun Dec 31, 2006 5:39 am     Reply with quote

something like this may be something you like:

Code:

<?php
session_start();
// Use $HTTP_SESSION_VARS with PHP 4.0.6 or less
if (!isset($_SESSION['count'])) {
   $_SESSION['count'] = 0;
} else {
   $_SESSION['count']++;
   echo '<img src="'.$_SESSION['count'].'.jpg" \>';
   if ($_SESSION['count] == "10"
    {
     $_SESSION['count'] = 0;
     }
}
?>

this was a example i found here:
http://no.php.net/manual/en/ref.session.php

it will use Sessions to know witch image to get, it's werry simple but it works Smile
now all images will have to be named
1.jpg, 2.jpg up to 10.
when it reatch 10 it will reset $_SESSION['count'] to 0
mypointofview



Joined: 18 Mar 2005
Posts: 32
Location: Los Angeles

PostPosted: Sun Dec 31, 2006 5:37 pm     Reply with quote

Thanks Thor, this works basically -- I had to correct some spelling errors - see below. There are two problems though:

1. Upon first visit: page is blank Shocked

2. If the visitor visits once and comes back the next day and has his browser turned off in the meantime - he starts at zero again.

So, how can I (a) eliminate the "blank" with the first visit (I tried to set the zero in the code to 1 but this did not work) and how can I (b) get the visitor's browser to remember where he left off last time -- somehow use cookies perhaps ?

Your code:

Code:
<?php
session_start();
// Use $HTTP_SESSION_VARS with PHP 4.0.6 or less

if (!isset($_SESSION['count'])) { $_SESSION['count'] = 0; }

else {
   $_SESSION['count']++;
   echo '<img src="'.$_SESSION['count'].'.jpg">';

   if ($_SESSION['count'] == "10") { $_SESSION['count'] = 0; }
     
   }
?>


All together:

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
   <meta http-equiv="content-type" content="text/html; charset=utf-8">
   <title>Untitled</title>
</head>
<body>

test

<?php
session_start();
// Use $HTTP_SESSION_VARS with PHP 4.0.6 or less

if (!isset($_SESSION['count'])) { $_SESSION['count'] = 0; }

else {
   $_SESSION['count']++;
   echo '<img src="'.$_SESSION['count'].'.jpg">';

   if ($_SESSION['count'] == "10") { $_SESSION['count'] = 0; }
     
   }
?>


</body>
</html>


PS: when reaching 10, then it indeed continues with 1 so this works Smile
Display posts from previous:   
Post new topic   Reply to topic    HTML Help Forum Index -> PHP All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
HTML Help Archive
Powered by phpBB © 2001, 2005 phpBB Group
HTML Help topic RSS feed 

 
HOSTING / DESIGN
MAKE MONEY

Home
  |   Tutorials   |   Forum   |   Quick List   |   Link Directory   |   About
Copyright ©1997-2002 Idocs and ©2002-2007 HTML Code Tutorial