Marfisa's randomizer

Created and explained by Marfisa. Please credit In the Cards if you use this script.

Marfisa's randomizer is very easy to customize and everything you need to randomize can go in ONE file. The folder your images are located in doesn't matter. There's no need to whip out your full PHP path every time you want to add a random image somewhere.

This is a PHP script.
Your host must support PHP, and EVERY page that uses the randomizer must have the .php extension in order to include the randomizer code!

simple reward.php

Create a blank file to use for your randomizer.
Let's call it reward.php because... that's what I've called it on all my TCGs. Humor me.

Start your file with this. All of the information you add must go BETWEEN these two lines.

<?php

?>

Now think about how you get a random card. This is a bit different from the conventional randomizer, where you have to think about putting all your images into specific folders. There are actually TWO random pieces - a random SET, and a random CARD NUMBER.

The easiest part is the NUMBER. How many cards do your regular decks have? (Don't worry if it's different between regular and special decks.) Let's say that it's 20 cards. The first part of reward.php can be the digits. Remember to put it between <?php and ?>!

$digits = array (
"01.gif",
"02.gif",
"03.gif",
"04.gif",
"05.gif",
"06.gif",
"07.gif",
"08.gif",
"09.gif",
"10.gif",
"11.gif",
"12.gif",
"13.gif",
"14.gif",
"15.gif",
"16.gif",
"17.gif",
"18.gif",
"19.gif",
"20.gif"
);

I've put .gif there because all of my TCG cards are GIF files. If your TCG uses JPG or PNG, change the extension to the one that's appropriate for you.

Now, you won't get too far with just this. These are just the digits, after all. Now we need the names of your regular decks. Let's say that your decks are named after common animals. They are sorted into folders according to where the animal lives - land, sea, or air - so we put the folder name, a backslash, then the name of the deck.

$regular = array (
"land/cat",
"land/dog",
"air/bird",
"sea/fish",
"land/cow"
);

Now your reward.php has enough information to call up a random GIF from the cat, dog, bird, fish, and cow sets, numbered 1-20. Sounds like a randomizer to me! How to display the random cards? Math.

I'm serious! Read on.

display your cards

Now let's find a place to display our random cards. Create a file called randomizer.php, since you'll probably want a randomizer for your TCG anyway. ^_^ Set it up with all your headers and footers as usual, and then add this line after the header.

<?php include('reward.php'); ?>

This is a regular include that adds the contents of reward.php to your page. This way, you can keep adding more things to reward.php and just include it wherever you want.

Here is the code that will display three random cards!
Pretend it fits nice in the box, okay?

Remember to add the code below on the page where you want the random cards to appear! DO NOT add these lines to your reward.php file, that is not where you want it! This is the code that goes on your game rewards pages, randomizer page, staff pay page, etc!

<?php 
echo "<img src=" . $regular[array_rand($regular,1)] . $digits[array_rand($digits,1)] . ">\n";
echo "<img src=" . $regular[array_rand($regular,1)] . $digits[array_rand($digits,1)] . ">\n";
echo "<img src=" . $regular[array_rand($regular,1)] . $digits[array_rand($digits,1)] . ">\n";
?>

What does this all MEAN? It's just math! Let's look at each piece.

1. echo "<img src=" means to output <img src=, the HTML image tag, in the browser.
2. . $regular[array_rand($regular,1)] means to ADD 1 randomly chosen item from the regular array.
3. . $digits[array_rand($digits,1)] means to ADD 1 randomly chosen item from the digits array.
4. . ">\n"; means to ADD the closing tag >\n (the optional \n makes a line break in the HTML output).

For example, we might get:
<img src= PLUS land/cat PLUS 20.gif PLUS >\n

In reality, that would be:
<img src=land/cat20.gif> (followed by an invisible line break made by \n)

If it's confusing, try thinking of the . as a plus sign. It just means add!

This is the most basic application of the randomizer. Let's get more advanced.

add special cards

Let's say your TCG has special cards, and each special deck has 10 cards. In addition, all of these special cards are in a folder called special. You can randomize these right along with the regulars even though there's a different amount of cards!

All you need to do is add more arrays.
Let's call the digit array $digits2 and the deck name array $special.

$digits2 = array (
"01.gif",
"02.gif",
"03.gif",
"04.gif",
"05.gif",
"06.gif",
"07.gif",
"08.gif",
"09.gif",
"10.gif"
);

$special = array (
"special/unicorn",
"special/dragon",
"special/griffin"
);

To display your specials, you'll just have to update your variable names in the display code. Using the example from above, change $regular to $special and $digits to $digits2.

<?php 
echo "<img src=" . $special[array_rand($special,1)] . $digits2[array_rand($digits2,1)] . ">\n";
echo "<img src=" . $special[array_rand($special,1)] . $digits2[array_rand($digits2,1)] . ">\n";
echo "<img src=" . $special[array_rand($special,1)] . $digits2[array_rand($digits2,1)] . ">\n";
?>

You can add additional arrays to add as many card classes as you want. For example, my TCG Trading Academy has cards separated into five subjects. There are arrays called $art, $history, and so on, so I can have a reward page show ONLY art cards if I want.

add more stuff!

What if your TCG has special decks, but they don't always have the same number of cards? What if you want to randomize currency or coupons or even sprites? No problem.

In this case, you would put the entire filename into reward.php. Since there is no need to add digits to the end of the randomized deck name (or item name, etc), the code to call up the random item is simpler too.

For example, if your special decks (all in a folder called special, by the way!) have different amount of cards, you could add this to reward.php.

$special = array (
"special/layout101.gif",
"special/layout102.gif",
"special/layout103.gif",
"special/season101.gif",
"special/season102.gif",
"special/season103.gif",
"special/season104.gif",
"special/season105.gif"
);

To make them appear, you'd use this code.

<?php 
echo "<img src=" . $special[array_rand($special,1)] . ">\n";
echo "<img src=" . $special[array_rand($special,1)] . ">\n";
echo "<img src=" . $special[array_rand($special,1)] . ">\n";
?>

You can do the same with anything you like. Just be sure to update the code to match your variable name, and you're good to go.

watch the comma!

The arrays in reward.php have long lists. At the end of each line is a comma, but there isn't one at the end of the LAST line. When you add more things to your array (if you add more decks, for example), you have to add the comma there or you will get a PHP error. If you like, you CAN put a comma after EVERY line, including the last, but it's not proper coding.

$special = array (
"special/layout101.gif",
"special/layout102.gif",
"special/layout103.gif"    <- uh oh, you forgot the comma!
"special/season101.gif",
"special/season102.gif",   <- it's okay to have one at the end
);

cheat like whoa

If you've been good and read through all that, here's something nice for you: a premade reward.php that you can easily customize! Simply take this reward.txt and rename it to reward.php, then customize as you wish! (You can REMOVE all the comments, they're just there to help you a bit!)

Not good enough? Okay. Try reward2.txt for samples of PHP code that will call up your random cards. Remember that you'll have to customize them to match any changes you've made to reward.php, though.

Please remember to credit Marfisa and to link back to In the Cards!