Exercise 23 Play Block Lottery

Imagine how many visitors your site would get if every time they viewed a page, they had a chance to win the lottery. It doesn't matter which page is viewed—if the lucky number is drawn, a winner has been chosen. Here's how to make a random You Win! block for your site.

1. Create a new block (admin/block/add) named Block Lottery. Use the following message for the body (or create your own):

<p><strong>You win!</strong></p>

<p>You have won block lottery. Congratulations, and enjoy the site.</p>

2. Enter a description and save the block.

3. Select administer> block (admin/block), and then click Configure for the Block Lottery block. For the Show Block on Specific Pages field, choose the third option, "Show if the following PHP code returns TRUE."

4. In the Pages field, enter the following code: <?php if (rand(1, 10) === 10) return TRUE; ?>

5. Save the block configuration. This returns you to the block administration page. Activate the block and start playing Block Lottery.

The code you entered in step 4 chooses a random number between 1 and 10, and if it is equal to 10, returns the value TRUE (the return value FALSE is implicit). If TRUE is returned, the block is shown; otherwise, it is not shown.

Now on every page view, there is a 10% chance that the block will be shown. If you have bad luck with lotteries in general, you may need to visit more than ten pages before you see the block appear.

Was this article helpful?

0 0

Post a comment