Jump to content


Random Snippet


  • You cannot reply to this topic
7 replies to this topic

#1 Dean

    Loves Etomite Forums!

  • Admin
  • 4,758 posts
  • Gender:Male

Posted 07 June 2007 - 02:32 PM

Hi Folks, I have an idea for a snippet, but I need some advice on how to do it.

What I am doing is a charity event website that has sponsors.

Each sponsor has their own bit of code. Basically, I will use the following as an example.

<a href="sponsors/website/url/here/dot/com" title="sponsor_name_here"><img src="image/url/here.jpg" title="sponsor_name_here" /></a>

So I was thinking, I could put them all into their own chunks.

{{sponsor_sponsor1name}}
{{sponsor_sponsor2name}}
{{sponsor_sponsor3name}}
and so on

What I'd need would be for a snippet to randomly pick a chunk each time the page is loaded, so pull any chunk that is prefixed with sponsor_

It really would need to be random, unlike some of the other random problems I've seen recently on the forums.

So.. Is it possible?

Regards,
Dean

#2 Ralph

    Loves Etomite Forums!

  • Admin
  • 6,524 posts
  • Gender:Male

Posted 07 June 2007 - 03:05 PM

It's entirely possible... You would query for the chunks using either $rs = $etomite->dbQuery($sql) or $etomite->getIntTableRows(...) and then shuffle($rs) to randomize... You would use LIKE '%sponsor_%' in the query so dbQuery() might be simpler to use...

#3 mikef

    Loves Etomite Forums!

  • Member
  • PipPipPipPip
  • 1,551 posts

Posted 07 June 2007 - 03:22 PM

You can't get "really random" on a computer, but the problems that I've noticed discussed with 'random' selection recently are down to using the same code twice on a page in very quick succession, and so getting the same result (time often being used as the seed value needed by the 'randomising' routines).

For your purposes, I'd suggest get all into an array, use shuffle to give them a 'random' order in the array, and then take the first 'n' items from the array, depending on how many you want to display. This is of course not entirely random, if you picked from the whole list truly randomly each time, there'd be a chance that all the items you picked would be the same.

ps
just noticed Ralph's reply ... I agree with what he said

Edited by mikef, 07 June 2007 - 03:24 PM.


#4 Dean

    Loves Etomite Forums!

  • Admin
  • 4,758 posts
  • Gender:Male

Posted 07 June 2007 - 03:42 PM

Thanks guys - I'll look at it later.

#5 mikef

    Loves Etomite Forums!

  • Member
  • PipPipPipPip
  • 1,551 posts

Posted 07 June 2007 - 04:07 PM

The images that appear in the right hand column of many of the pages on the Rogue Gene Collective website are selected in exactly this fashion - a list of images is extracted from the database, pseudo-randomly sorted using shuffle, and the first four images in the resulting list displayed.

#6 Ed Headset

    Likes Etomite Forums!

  • Developers
  • PipPip
  • 194 posts
  • Gender:Male
  • Location:The Netherlands
  • Interests:Etomite, PHP, Ajax.
    3D imaging

Posted 07 June 2007 - 06:21 PM

Hi Dean,

I've got a snippet that draws a number between 1 and <$total>,

I use it to display a random picture, but you can always use it to display a random code

Something like:
	//start random code
	$total = "6";
	$file_type = ".jpg";
	$image_folder = "/images/";
	$start = "1";
	$random = mt_rand($start, $total);
	$image_name = $random . $file_type; 

return "<a href='$sponsorsurl". $random ."' title='$sponsor_name".$random."'><img src='$image_folder ."/".$image_name."'  /></a>

This code should generate something like:

Quote

<a href='$sponsorsurl1.' title='$sponsor_name1'><img src='$image_folder/1.jpg' /></a>

If you have filled the strings (1 to x ) you should be able to display the right things randomly ;)

#7 Cris D.

    Loves Etomite Forums!

  • Developers
  • PipPipPipPip
  • 1,104 posts
  • Gender:Male

Posted 08 June 2007 - 01:14 AM

Has anyone tried adding a random seed from the snippet call to produce different results with the same snippet used several times on a page?
[ !randomSnippet?seed=7654321!]
[ !randomSnippet?seed=1234567!]
[ !randomSnippet?seed=1000555!]
Just a thought...

OK I just finished testig this theory and it works like a charm.
Place in any random snippet:
$seed=isset($seed)? $seed:1000000;
srand( (double) microtime()*$seed );
Then for every use of the snippet on a page, set the seed variable to different numbers (as above) and each return will (most often than not) be different.

Edited by Cris D., 08 June 2007 - 05:39 AM.


#8 Cris D.

    Loves Etomite Forums!

  • Developers
  • PipPipPipPip
  • 1,104 posts
  • Gender:Male

Posted 12 August 2007 - 10:32 AM

I've been tinkering with JavaScript and found this solution to random images:

<script type="text/javascript">
var r=Math.random()
if (r>0.5) 
{
document.write("<img src='assets/images/image1.gif'>Example:Image 1")
}
else
{
document.write("<img src='assets/images/image2.gif'>Example:Image 2")
}
</script>

1)Save this as a chunk, 2)call it into your page where you want the image to show and 3)point the image names to the images required.
(Obviously you can add more images in nested "if, else if, else" or "switch" statements).

Edited by Cris D., 12 August 2007 - 10:36 AM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users