Forum Moderators: coopster

Message Too Old, No Replies

Simplify random snippet includes

         

Patrick Taylor

10:18 pm on Nov 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I am using this code to introduce random snippets of content into the page:


$nummer = rand(1,8);
if ($nummer == 1) {
include (TEMPLATEPATH . "/folder/1.php");
} else if ($nummer == 2) {
include (TEMPLATEPATH . "/folder/2.php");
} else if ($nummer == 3) {
include (TEMPLATEPATH . "/folder/3.php");
} else if ($nummer == 4) {
include (TEMPLATEPATH . "/folder/4.php");
} else if ($nummer == 5) {
include (TEMPLATEPATH . "/folder/5.php");
} else if ($nummer == 6) {
include (TEMPLATEPATH . "/folder/6.php");
} else if ($nummer == 7) {
include (TEMPLATEPATH . "/folder/7.php");
} else if ($nummer == 8) {
include (TEMPLATEPATH . "/folder/8.php");
}

The site is built using WordPress. There are actually many more than eight snippets and I would like to shorten the code, if possible with just one line of "include". I have tried various things without success.

A pointer would be appreciated.

jatar_k

10:30 pm on Nov 22, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



try this

$nummer = rand(1,8);
include (TEMPLATEPATH . "/folder/" . $nummer . ".php");

since you named your files well it should be fine

Patrick Taylor

11:47 pm on Nov 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, it works fine. Thanks for your help.

Patrick

(I'm seeing my code example above as a very tall narrow box in the post. As far as I know, I didn't do anything unusual, and it wasn't like that when I first posted it.)

jatar_k

2:16 am on Nov 23, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> tall narrow box

it does that sometimes