Jump to content

A different kind of weighted randomizer?


djfox

Recommended Posts

The weighted randomizer I currently have works fine. But as it currently stands, I`d have to update the page every time a new item comes along (and for my site, this is not efficient). Any way for me to have a weighted randomizer that automatically pulls the items from a table even when new items are added to the table?

Link to comment
https://forums.phpfreaks.com/topic/75937-a-different-kind-of-weighted-randomizer/
Share on other sites

Well if you really want my current code...:

 

<?php
$option = "link";

$sites[0] = array("javascript:location.reload()", "</a>You found nothing.<p><a href='javascript:location.reload()'>Keep Searching</a>", 1000);
$sites[1] = array("explore3.php?id=22&p=5", "</a>You Found a Glow Stick Tube! Click the image to add it to your inventory.<br><a href='explore3.php?id=22&p=5'><img src='collection/glowsticktube.png' border=0></a><p><a href='javascript:location.reload()'>Keep Searching Without Grabbing the Item.</a>", 30);
$sites[2] = array("explore3.php?id=23&p=1", "</a>You Found a String! Click the image to add it to your inventory.<br><a href='explore3.php?id=23&p=1'><img src='collection/string.png' border=0></a><p><a href='javascript:location.reload()'>Keep Searching Without Grabbing the Item.</a>", 100);
$sites[3] = array("explore3.php?id=24&p=4", "</a>You Found a Red Glow Stick Liquid! Click the image to add it to your inventory.<br><a href='explore3.php?id=24&p=4'><img src='collection/glowstickliquidred.png' border=0></a><p><a href='javascript:location.reload()'>Keep Searching Without Grabbing the Item.</a>", 40);
$sites[4] = array("explore3.php?id=25&p=3", "</a>You Found a Purple Glow Stick Liquid! Click the image to add it to your inventory.<br><a href='explore3.php?id=25&p=3'><img src='collection/glowstickliquidpurple.png' border=0></a><p><a href='javascript:location.reload()'>Keep Searching Without Grabbing the Item.</a>", 50);
$sites[5] = array("explore3.php?id=26&p=2", "</a>You Found a Blue Glow Stick Liquid! Click the image to add it to your inventory.<br><a href='explore3.php?id=26&p=2'><img src='collection/glowstickliquidblue.png' border=0></a><p><a href='javascript:location.reload()'>Keep Searching Without Grabbing the Item.</a>", 60);
$sites[6] = array("explore3.php?id=27&p=5", "</a>You Found a Green Glow Stick Liquid! Click the image to add it to your inventory.<br><a href='explore3.php?id=27&p=5'><img src='collection/glowstickliquidgreen.png' border=0></a><p><a href='javascript:location.reload()'>Keep Searching Without Grabbing the Item.</a>", 30);
$sites[7] = array("explore3.php?id=28&p=3", "</a>You Found a Pink Glow Stick Liquid! Click the image to add it to your inventory.<br><a href='explore3.php?id=28&p=3'><img src='collection/glowstickliquidpink.png' border=0></a><p><a href='javascript:location.reload()'>Keep Searching Without Grabbing the Item.</a>", 50);
$sites[8] = array("explore3.php?id=29&p=6", "</a>You Found a Gold Glow Stick Liquid! Click the image to add it to your inventory.<br><a href='explore3.php?id=29&p=6'><img src='collection/glowstickliquidpink.png' border=0></a><p><a href='javascript:location.reload()'>Keep Searching Without Grabbing the Item.</a>", 5);
$sites[9] = array("explore3.php?id=30&p=5", "</a>You Found a Glass Shard! Click the image to add it to your inventory.<br><a href='explore3.php?id=30&p=5'><img src='collection/glassshard.png' border=0></a><p><a href='javascript:location.reload()'>Keep Searching Without Grabbing the Item.</a>", 30);

$countsites = count($sites);

for($i=0; $i<$countsites; $i++)
{
for($x=0; $x<$sites[$i][2]; $x++)
	{
	if($option != null)
		{
		$mylist[] = array($sites[$i][0],$sites[$i][1],$sites[$i][3]);
		}
	else
		{
		echo("Error deciding option.");
		exit();
		}
	}
}

$countlist = count($mylist);

$countlist = $countlist - 1;

$picker = rand(0, $countlist);

if($option == "link")
{
echo("<a href='" . $mylist[$picker][0] . "'>" . $mylist[$picker][1] . "</a>");
}

else if($option == "banner")
{
echo("<a href='" . $mylist[$picker][0] . "'><img alt='" . $mylist[$picker][1] . "' src='" . $mylist[$picker][2] . "' /></a>");
}

else
{
echo("Error deciding option.");
exit();
}
?>

 

And the problem:

It reads from what I hand enter.

 

What I want:

I want it to read from the mysql database.

Archived

This topic is now archived and is closed to further replies.

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