Conditionally inserting HTML lines? Beating head against ||

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
steedvlx
Forum Contributor
Posts: 122
Joined: Wed Jun 11, 2003 10:54 pm
Location: Osaka, Japan

Conditionally inserting HTML lines? Beating head against ||

Post by steedvlx »

Hi,

I'm trying to figure out how to use PHP to conditionally determine which of two lines of HTML get used.

I thought I had the logic down, but the parser sputters and fails with my code. What I'm attempting is this: If a user is logged on, then the logout button with the appropriate variables is displayed. Otherwise, the login button is displayed instead.

Code: Select all

<?php
<?php 
	if($display_logout_btn ='FALSE') 
		{
		$line_output = htmlspecialcharacters('<a href="../forums/login.php" onMouseDown="MM_swapImage('btn_login','','../images/btn_login_down.gif',1)" onMouseUp="MM_swapImage('btn_login','','../images/btn_login_over.gif',1)" onMouseOver="MM_swapImage('btn_login','','../images/btn_login_over.gif',1)" onMouseOut="MM_swapImgRestore()"><img src="../images/btn_login.gif" alt="Proceed to the login page" name="btn_login" width="66" height="21" border="0"></a>');
		} 
	else
		{
		$line_output = htmlspecialcharacters('<a href="../forums/login.php?logout=true" onMouseDown="MM_swapImage('btn_logout','','../images/btn_logout_down.gif',1)" onMouseUp="MM_swapImage('btn_logout','','../images/btn_logout_over.gif',1)" onMouseOver="MM_swapImage('btn_logout','','../images/btn_logout_over.gif',1)" onMouseOut="MM_swapImgRestore()"><img src="../images/btn_logout.gif" alt="End Session" name="btn_logout" width="66" height="21" border="0"></a>');
		}
		echo ('$line_output');
		?>
?>
When I look at it, it's obvious from the code coloring that something is definately not right here. I have tried echo. printf, add/stripslashes and now htmlspecialcharacters.

Could someone point me in the right direction to learn how to fix this?

Thank you,
SteedVLX
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

you need to escape your single quotes!

Mark
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

...also, isn't the function called htmlspecialchars, not htmlspecialcharacters (unless it is your own function!)

..and why exactly are you using that function!? no need is there?

Mark
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

You could also jump in and out of PHP like this

Code: Select all

<?php 
   if($display_logout_btn ='FALSE') 
      { 
?>
      <a href="../forums/login.php" onMouseDown="MM_swapImage('btn_login','','../images/btn_login_down.gif',1)" onMouseUp="MM_swapImage('btn_login','','../images/btn_login_over.gif',1)" onMouseOver="MM_swapImage('btn_login','','../images/btn_login_over.gif',1)" onMouseOut="MM_swapImgRestore()"><img src="../images/btn_login.gif" alt="Proceed to the login page" name="btn_login" width="66" height="21" border="0"></a>
<?php
      } 
   else 
?>
      <a href="../forums/login.php?logout=true" onMouseDown="MM_swapImage('btn_logout','','../images/btn_logout_down.gif',1)" onMouseUp="MM_swapImage('btn_logout','','../images/btn_logout_over.gif',1)" onMouseOver="MM_swapImage('btn_logout','','../images/btn_logout_over.gif',1)" onMouseOut="MM_swapImgRestore()"><img src="../images/btn_logout.gif" alt="End Session" name="btn_logout" width="66" height="21" border="0"></a>
<?php
      } 

?>
Mark
User avatar
steedvlx
Forum Contributor
Posts: 122
Joined: Wed Jun 11, 2003 10:54 pm
Location: Osaka, Japan

Post by steedvlx »

Thanks Bech100,

I decided to try the jump in and out approach. I didn't know you could do that without a lot of problems with the parser. But, it works.

Code: Select all

<?php 
	if($display_logout_btn == 'FALSE') 
		{
		?><a href="../forums/login.php" onMouseDown="MM_swapImage('btn_login','','../images/btn_login_down.gif',1)" onMouseUp="MM_swapImage('btn_login','','../images/btn_login_over.gif',1)" onMouseOver="MM_swapImage('btn_login','','../images/btn_login_over.gif',1)" onMouseOut="MM_swapImgRestore()"><img src="../images/btn_login.gif" alt="Proceed to the login page" name="btn_login" width="66" height="21" border="0"></a><? } 
	else
		{
		?><a href="../forums/login.php?logout=true" onMouseDown="MM_swapImage('btn_logout','','../images/btn_logout_down.gif',1)" onMouseUp="MM_swapImage('btn_logout','','../images/btn_logout_over.gif',1)" onMouseOver="MM_swapImage('btn_logout','','../images/btn_logout_over.gif',1)" onMouseOut="MM_swapImgRestore()"><img src="../images/btn_logout.gif" alt="End Session" name="btn_logout" width="86" height="21" border="0"></a><?
		}
		?>
Why do things always seem to be a lot simpler than I tried to make them?

SteedVLX (Thinking Joy-Joy thoughts)
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

steedvlx wrote:Why do things always seem to be a lot simpler than I tried to make them?
:lol: It's called practice and I'm experiencing that learning curve pretty much every day... ;)
Post Reply