Page 1 of 1

Simple PHP Prob

Posted: Fri Mar 28, 2003 4:10 pm
by Ricky
HI Guys,

<?php
switch(mt_rand(0,2)) {
case 0: print "link1";
case 1: print "link2";
case 2: print "link3";
}
?>

I am signed up with a number of affiliate schemes and i am using (trying to) get this to randomly display which one is shown but it gives this error

Parse error: parse error, unexpected T_STRING in /home/collyer/public_html/front.php on line 23

line 23 is the case 0 line. I know php works as i have php bb on the server working.

Any help appreciated..

Edit: worked out its becuase the links have a " in...any ideas?

Edit2: ok i have used the escape charater to stop the errors but now the script will print link1, or link 1 and 2, or link 1 and 2 and 3

It seems that if the case is 2 then it is printing 1 and 2 not just 2 any ideas on this.

Cheers
Richard

Posted: Fri Mar 28, 2003 5:11 pm
by volka
try something like

Code: Select all

<?php
switch(mt_rand(0,2)) {
	case 0:
		print '<a href="link1" ....';
		break;
	case 1:
		print '<a href="link2" ....';
		break;
	case 2:
		print '<a href="link3" ....';
		break;
	default:
		print '<a href="default_link" ....'; // shouldn't happen 
}
?>

Posted: Fri Mar 28, 2003 5:13 pm
by Ricky
thnaks for that it worked a treat.