Complex Buttons Posted May 17, 2010 Share Posted May 17, 2010 I'm trying to make a PHP feature on my Wordpress site that would randomly display one of three messages in the header, and the message displayed would be randomized each time the page is loaded. The message would contain a link. I'm this far: In index.php, after <?php get_header(); ?>, I have written or copy pasted: $i = mt_rand(1,3); switch ($i) { case 1: echo "message 1"; break; case 2: echo "message 2"; break; case 3: echo "message 3"; break; } ?> But this whole code prints out as text. Apparently I'm missing something really basic here. Could someone help? Thank you a thousand times! Link to comment https://forums.phpfreaks.com/topic/202025-noob-problem-code-printed-out-as-text/ Share on other sites More sharing options...
hyster Posted May 17, 2010 Share Posted May 17, 2010 im no means an expert but the echo are plain text. u need to make them a hyperlink <a href="www.link1.com/index.php" title="link 1">message 1</a> echo "<a href=www.link1.com/index.php" title="link 1">message 1</a>; break; something like that Link to comment https://forums.phpfreaks.com/topic/202025-noob-problem-code-printed-out-as-text/#findComment-1059399 Share on other sites More sharing options...
scvinodkumar Posted May 17, 2010 Share Posted May 17, 2010 Yeah you have missed php open tag <?php after <?php get_header(); ?> this line or remove this closing tag in the header function '?>' Link to comment https://forums.phpfreaks.com/topic/202025-noob-problem-code-printed-out-as-text/#findComment-1059400 Share on other sites More sharing options...
Complex Buttons Posted May 17, 2010 Author Share Posted May 17, 2010 hyster: Thanks! That worked. scvinodkumar: Now when I try to load the page, I get: "Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in C:\xampp\htdocs\wordpress\wp-content\themes\tutorial\index.php on line 6" Line 6 is: echo "<a href=www.link1.com/index.php" title="link 1">message 1</a>; Entire code is: <?php $i = mt_rand(1,3); switch ($i) { case 1: echo "<a href=www.link1.com/index.php" title="link 1">message 1</a>; break; case 2: echo "<a href=www.link1.com/index.php" title="link 1">message 2</a>; break; case 3: echo "<a href=www.link1.com/index.php" title="link 1">message 3</a>; break; } ?> Link to comment https://forums.phpfreaks.com/topic/202025-noob-problem-code-printed-out-as-text/#findComment-1059406 Share on other sites More sharing options...
Complex Buttons Posted May 17, 2010 Author Share Posted May 17, 2010 The first " in echo "<a href=www.link1.com/index.php" title="link 1">message 1</a>; looked wrong as there is no closing ". Now I get: "Parse error: syntax error, unexpected '<' in C:\xampp\htdocs\wordpress\wp-content\themes\tutorial\index.php on line 6" For some reason I could not edit my previous post to tell this. Link to comment https://forums.phpfreaks.com/topic/202025-noob-problem-code-printed-out-as-text/#findComment-1059415 Share on other sites More sharing options...
scvinodkumar Posted May 17, 2010 Share Posted May 17, 2010 You should use like this, echo "<a href='www.link1.com/index.php' title='link 1'>message 1</a>"; whenever you open quote, you need to close quote. Link to comment https://forums.phpfreaks.com/topic/202025-noob-problem-code-printed-out-as-text/#findComment-1059421 Share on other sites More sharing options...
Complex Buttons Posted May 17, 2010 Author Share Posted May 17, 2010 Brilliant! You rule, scvinodkumar! Link to comment https://forums.phpfreaks.com/topic/202025-noob-problem-code-printed-out-as-text/#findComment-1059425 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.