DeanWhitehouse Posted November 11, 2008 Share Posted November 11, 2008 Can anyone see a way to make a better way of making sure that the same avatar doesn't appear in a row. e.g. pic1 then pic2 then pic2. My code work's but i think there are better ways to do it. <?php session_start(); if(isset($_SESSION['random']))// check for the session that contains the random number used for the previous avatar then unset it. { unset($_SESSION['random']);//unset the session } // fill the list with avatars, each seperated by a comma $avatars = array( "http://www.avatarity.com/avatars/7/78/7865.jpg", "http://www.avatarity.com/avatars/6/66/6687.gif", "http://www.avatarity.com/avatars/4/45/4578.gif", "http://www.avatarity.com/avatars/13/137/13746.gif", "http://i9.tinypic.com/6su42nt.gif", "http://i8.tinypic.com/6ob6qv8.gif" ); $avatar_amount = count($avatars);//set the amount of avatars there are in the list $_SESSION['random'] = rand(0,$avatar_amount);//generate the random avatar number if(!isset($_SESSION['num']))//check if there was a previous avatar number { $_SESSION['num'] = $_SESSION['random'];//if not then create one } function random_check() { if($_SESSION['num'] == $_SESSION['random'])//check if the previous avatar number is the same as the current one, if so generate a new one, { $_SESSION['random'] = rand(0,$avatar_amount); random_check(); } return true; } header("content-type: image/gif");//set the header for gifs, the common avatar image type if(random_check())//run the function to check the avatar numbers { echo readfile($avatars[$_SESSION['random']]);//print the avatar } $_SESSION['num'] = $_SESSION['random'];// set the old session number to the current one. ?> Link to comment https://forums.phpfreaks.com/topic/132245-avatar-random-ness-code/ Share on other sites More sharing options...
Third_Degree Posted November 11, 2008 Share Posted November 11, 2008 that's a lot of code <?php $avatars = array( "http://www.avatarity.com/avatars/7/78/7865.jpg", "http://www.avatarity.com/avatars/6/66/6687.gif", "http://www.avatarity.com/avatars/4/45/4578.gif", "http://www.avatarity.com/avatars/13/137/13746.gif", "http://i9.tinypic.com/6su42nt.gif", "http://i8.tinypic.com/6ob6qv8.gif" ); shuffle($array); header("content-type: image/gif"); foreach($avatars as $tar) { echo readfile($tar); } ?> I do believe that's all you need. If that's not what you're looking for let me know. Link to comment https://forums.phpfreaks.com/topic/132245-avatar-random-ness-code/#findComment-687500 Share on other sites More sharing options...
DeanWhitehouse Posted November 11, 2008 Author Share Posted November 11, 2008 OK, first off i believe your foreach loop will cause an error, as i only want one image shown each time, and second off that is what my code did look like, but i found it was showing the same image after it's self a lot, that's what my code now stops, but i think it is inefficient. Link to comment https://forums.phpfreaks.com/topic/132245-avatar-random-ness-code/#findComment-687648 Share on other sites More sharing options...
Third_Degree Posted November 26, 2008 Share Posted November 26, 2008 ah, i understand now. um, you could try a long pre-set random list. also, if the same image was appearing over and over, the browser may have cached the file. Just add the lines: header('Cache-Control: no-store, no-cache, must-revalidate'); header('Cache-Control: pre-check=0, post-check=0, max-age=0'); Link to comment https://forums.phpfreaks.com/topic/132245-avatar-random-ness-code/#findComment-699283 Share on other sites More sharing options...
chmpdog Posted November 26, 2008 Share Posted November 26, 2008 This is an easy fix. just use $_SESSION if ($_SESSION['ID'] == $_SESSION['random']) { $_SESSION['ID'] = $_SESSION['ID'] - 1; } Link to comment https://forums.phpfreaks.com/topic/132245-avatar-random-ness-code/#findComment-699301 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.