/    Sign up×
Community /Pin to ProfileBookmark

loop through sequence command?

Hi, im looking for a command that will replace the “rand” command below to force the sequence to loop through in order rather than randomise.

Here is the code.

[CODE]<?php
$img = array();
$img[] = ‘<img src=”img1.jpg” />’;
$img[] = ‘<img src=”img2.jpg” />’;
$img[] = ‘<img src=”img3.jpg” />’;
$img[] = ‘<img src=”img4.jpg” />’;
$img[] = ‘<img src=”img5.jpg” />’;
$max = count($img) – 1;
$count = [B]rand[/B](0,$max);
echo $img[$count];
?>[/CODE]

Is this possible?

Thanks

to post a comment
PHP

10 Comments(s)

Copy linkTweet thisAlerts:
@sstalderNov 03.2008 — Your question doesn't really make sense to me.

What are you trying to accomplish in the end?
Copy linkTweet thisAlerts:
@dbrandonNov 03.2008 — [code=php]
<?php

$img = array();
$img[] = '<img src="img1.jpg" />';
$img[] = '<img src="img2.jpg" />';
$img[] = '<img src="img3.jpg" />';
$img[] = '<img src="img4.jpg" />';
$img[] = '<img src="img5.jpg" />';

foreach ( $img as $k => $v )
{
echo $v;
}

?>
[/code]


Does that help at all ?
Copy linkTweet thisAlerts:
@rodd1000authorNov 04.2008 — Hi and thanks for the help.

@dbrandon. Thanks for the code but it makes all the images display at the same time.

What i am trying to do here is each time a visitor comes to the site they see a different image, at the moment it is random and sometimes it falls on the same image 2 or 3 times in a row. So i figured the best way around that would be to have the image order in sequence. Then when it gets to last image it loops back to image 1 again and so on.

basically, is there an alternative to the "rand" attribute that will make a set of 5 images display in order rather than randomly.
Copy linkTweet thisAlerts:
@dbrandonNov 04.2008 — Ah i see what you mean now. You could do that like this ...

Put this at the top of every page:

[code=php]
session_start();
[/code]


And then use this code to display the images:

[code=php]
$img = array();
$img[] = '<img src="img1.jpg" />';
$img[] = '<img src="img2.jpg" />';
$img[] = '<img src="img3.jpg" />';
$img[] = '<img src="img4.jpg" />';
$img[] = '<img src="img5.jpg" />';


$_SESSION['img'] = ( !isset($_SESSION['img']) || ( $_SESSION['img'] + 1 ) >= count($img) ) ? 0 : $_SESSION['img'] + 1;

echo $img[$_SESSION['img']];

[/code]


EDIT: this would display a different image while the session is active. sessions are destroyed when the users browser is quit. if this is a problem, you will need to store the image to display in a cookie
Copy linkTweet thisAlerts:
@rodd1000authorNov 04.2008 — Right thanks for the reply.

Storing the image to display in a cookie, won't that just take me back to image 1 everytime when i clear my cookies? ( and so every person that visits the page will also land on image 1 as its the start of the session or first visit)?

Is there a way to store cookies/sessions on the server to display a different image.

Say

visitor 1 sees image 1,

visitor 2 sees image 2,

etc.... (all visits from different browsers on different machines)

I don't know if im asking the impossible or not. I'm certainly glad for the help.

Thanks
Copy linkTweet thisAlerts:
@dbrandonNov 04.2008 — You'd need to store it in a database or flat file. Probably flat file would be easier for something like this.
Copy linkTweet thisAlerts:
@rodd1000authorNov 04.2008 — Thanks,

I think thats a little way over my head, i wouldnt know where to begin with a flat file or database. Im only just coming to terms with html...lol

Is there a tutorial or could you tell me how i might do this?

I already feel like i'm getting in to deep.

I really appreciate the help though. ?

Cheers!
Copy linkTweet thisAlerts:
@dbrandonNov 04.2008 — Make a file called [B]imgKeyData.txt[/B] in the same directory as the PHP script (you will need to CHMOD it to allow script access), then use this code ...

[code=php]
$img = array();
$img[] = '<img src="img1.jpg" />';
$img[] = '<img src="img2.jpg" />';
$img[] = '<img src="img3.jpg" />';
$img[] = '<img src="img4.jpg" />';
$img[] = '<img src="img5.jpg" />';

$theFile = 'imgKeyData.txt';
$lastIndex = intval(@file_get_contents($theFile));
$newIndex = ( ( $lastIndex + 1 ) >= count($img) ) ? 0 : $lastIndex + 1;

echo $img[$newIndex];

$handle = @fopen($theFile,'w');
@fwrite($handle,$newIndex);
@fclose($handle);

[/code]


All errors are surpressed, so if it's not working, double check the file exists, and it is CHMODed correctly. If in doubt, 777
Copy linkTweet thisAlerts:
@rodd1000authorNov 04.2008 — Hi, dbrandon

I understand everything except the CHMOD to allow script access" bit....

How would i do that.


Thankyou so much for putting you time in to this.
Copy linkTweet thisAlerts:
@rodd1000authorNov 05.2008 — Hi, just to let you know, i managed to sort this using this code with a count.txt file with a value of 0 on the host directory. If anyone else should want to do something similar.

[CODE]
<?php
$img = array();
$img[] = '<img src="PLUMBER1.jpg" />';
$img[] = '<img src="PLUMBER2.jpg" />';
$img[] = '<img src="PLUMBER3.jpg" />';
$img[] = '<img src="PLUMBER4.jpg" />';

$count = (int)file_get_contents('count.txt');

echo $img[$count];

if ($count == count($img) - 1){
$count = 0;
}else{
$count++;
}
$file = 'count.txt';
$f = fopen($file, 'w');

fwrite($f, $count);
fclose($f);
?>
[/CODE]



Thanks for your help!
×

Success!

Help @rodd1000 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...