if statement help

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
jasondavis
Forum Commoner
Posts: 60
Joined: Sat Feb 04, 2006 5:35 pm

if statement help

Post by jasondavis »

Below is my code I am using on a page it worked fine for showing my flash chat if name was set in a session and showing a message instead if name wasnt set yet, Now though I need to show a different chat if there age is over a certain again in the middle of the code you will see where I need this added I am lost any help greatly appreciated

Code: Select all

<?PHP
$age ='12';
session_start(); // need to be called once 
 if ($l=$_SESSION['mycribname']) { ?>
     
<html>
<head>
<title>MyCribTrain.com - MyCrib Chat</title>
<META HTTP-EQUIV="Expires" CONTENT="Tue, 01 Jan 1980 1:00:00 GMT">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="100%" height="100%">
<param name=movie value="123flashchat.swf?init_listroom=1001,3,4,5,6">
<param name=quality value=high>
<embed src="123flashchat.swf?init_listroom=1001,3,4,5,6" quality=high pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="100%" height="100%" >
</embed>
</object>

</body>
</html>
<? need code here if session for name is set and $age > 18 ?>
<html>
<head>
<title>MyCribTrain.com - MyCrib Chat</title>
<META HTTP-EQUIV="Expires" CONTENT="Tue, 01 Jan 1980 1:00:00 GMT">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="100%" height="100%">
<param name=movie value="123flashchat.swf?init_user=<?=$_SESSION['mycribname'];?>&init_password=">
<param name=quality value=high>
<embed src="123flashchat.swf?init_user=<?=$_SESSION['mycribname'];?>&init_password=" quality=high pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="100%" height="100%" >
</embed>
</object>

</body>
</html>
 <? } else { ?>
You must come from Mycrib.net to enter the chat <a href="http://mycrib.net/?pageid=mycrib.member.profile.home">Click here</a>
<? } ?>
blackbeard
Forum Contributor
Posts: 123
Joined: Thu Aug 03, 2006 6:20 pm

Post by blackbeard »

You mean something like this:

Code: Select all

$age = 12;  //  Use an integer instead of a string

if ((isset($_SESSION['mycribname'])  && ($age >= 18)) {

//  output html for people 18 and over

}

else if ((isset($_SESSION['mycribname'])  && ($age < 18)) {

// output html for people under 18

}

else {

// Output message to make them go to url

}
jasondavis
Forum Commoner
Posts: 60
Joined: Sat Feb 04, 2006 5:35 pm

Post by jasondavis »

exactly what i need but im getting this

Parse error: parse error, unexpected '{' in /home/mycrib/public_html/chat/index2.php on line 4

line 4=

Code: Select all

if ((isset($_SESSION['mycribname'])  && ($age >= 88)) {


my whole code now

Code: Select all

<?PHP
$age =12;
session_start(); // need to be called once 
if ((isset($_SESSION['mycribname'])  && ($age >= 88)) {
?>

<html>
<head>
<title>MyCribTrain.com - MyCrib Chat</title>
<META HTTP-EQUIV="Expires" CONTENT="Tue, 01 Jan 1980 1:00:00 GMT">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>	
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="100%" height="100%">
<param name=movie value="123flashchat.swf?init_user=<?=$_SESSION['mycribname'];?>&init_password=&init_listroom=1001,1002,1003,1004,1005">
<param name=quality value=high>
<embed src="123flashchat.swf?init_user=<?=$_SESSION['mycribname'];?>&init_password=&init_listroom=1001,1002,1003,1004,1005" quality=high pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="100%" height="100%" >
</embed>
</object>
</body>
</html>
<? } else if ((isset($_SESSION['mycribname'])  && ($age < 18)) { ?>
<html>
<head>
<title>MyCribTrain.com - MyCrib Chat</title>
<META HTTP-EQUIV="Expires" CONTENT="Tue, 01 Jan 1980 1:00:00 GMT">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>	
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="100%" height="100%">
<param name=movie value="123flashchat.swf?init_user=<?=$_SESSION['mycribname'];?>&init_password=&init_listroom=1001,1002,1003,1004">
<param name=quality value=high>
<embed src="123flashchat.swf?init_user=<?=$_SESSION['mycribname'];?>&init_password=&init_listroom=1001,1002,1003,1004" quality=high pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="100%" height="100%" >
</embed>
</object>
</body>
</html>
<? } else { ?>
You must come from Mycrib.net to enter the chat <a href="http://mycrib.net/?pageid=mycrib.member.profile.home">Click here</a>
<? } ?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

pair up the parens... you're missing a few.
blackbeard
Forum Contributor
Posts: 123
Joined: Thu Aug 03, 2006 6:20 pm

Post by blackbeard »

Personally, I don't like using multiple <?php ?> tags, it's too easy for me to miss things like brackets. You may want to use the heredoc syntax, IMHO, less trouble, and easier to maintain. Your code using heredoc syntax would look like this:

Of course, you don't have to, do it however you're comfortable with.

edit: didn't notice that other embed php tags, look at the next post for a better way.
Last edited by blackbeard on Mon Aug 28, 2006 5:34 pm, edited 2 times in total.
jasondavis
Forum Commoner
Posts: 60
Joined: Sat Feb 04, 2006 5:35 pm

Post by jasondavis »

feyd wrote:pair up the parens... you're missing a few.
a raen is ( correct?
if so i tried paring them with no luck
jasondavis
Forum Commoner
Posts: 60
Joined: Sat Feb 04, 2006 5:35 pm

Post by jasondavis »

thanks I do like this method better but I still get error with that code

Parse error: parse error, unexpected '{' in /home/mycrib/public_html/chat/index2.php on line 5

blackbeard wrote:Personally, I don't like using multiple <?php ?> tags, it's too easy for me to miss things like brackets. You may want to use the heredoc syntax, IMHO, less trouble, and easier to maintain. Your code using heredoc syntax would look like this:

Code: Select all

<?PHP
session_start();
$age =12;
session_start(); // need to be called once
if ((isset($_SESSION['mycribname'])  && ($age >= 18)) {

echo<<<SECTION1
<html>
<head>
<title>MyCribTrain.com - MyCrib Chat</title>
<META HTTP-EQUIV="Expires" CONTENT="Tue, 01 Jan 1980 1:00:00 GMT">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head> 
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="100%" height="100%">
<param name=movie value="123flashchat.swf?init_user=<?=$_SESSION['mycribname'];?>&init_password=&init_listroom=1001,1002,1003,1004,1005">
<param name=quality value=high>
<embed src="123flashchat.swf?init_user=<?=$_SESSION['mycribname'];?>&init_password=&init_listroom=1001,1002,1003,1004,1005" quality=high pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="100%" height="100%" >
</embed>
</object>
</body>
</html>
SECTION1;

} 
else if ((isset($_SESSION['mycribname'])  && ($age < 18)) { 

echo<<<SECTION2
<html>
<head>
<title>MyCribTrain.com - MyCrib Chat</title>
<META HTTP-EQUIV="Expires" CONTENT="Tue, 01 Jan 1980 1:00:00 GMT">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head> 
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="100%" height="100%">
<param name=movie value="123flashchat.swf?init_user=<?=$_SESSION['mycribname'];?>&init_password=&init_listroom=1001,1002,1003,1004">
<param name=quality value=high>
<embed src="123flashchat.swf?init_user=<?=$_SESSION['mycribname'];?>&init_password=&init_listroom=1001,1002,1003,1004" quality=high pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="100%" height="100%" >
</embed>
</object>
</body>
</html>
SECTION2;
} else { 

echo "You must come from Mycrib.net to enter the chat <a href="http://mycrib.net/?pageid=mycrib.member.profile.home">Click here</a>";
 } 

?>
Of course, you don't have to, do it however you're comfortable with.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

This is a great example of why you should separeate your programming from your presentation. It makes it much easier to see what is going on and promotes reuse.

Here is the actual program:

Code: Select all

<?PHP
$age =12;
session_start(); // need to be called once 
if ((isset($_SESSION['mycribname'])  && ($age >= 88)) {
     $params = 'init_user=' . $_SESSION['mycribname'] . '&init_password=&init_listroom=1001,1002,1003,1004';
     include 'templates/chat.php';
} else if ((isset($_SESSION['mycribname'])  && ($age < 18 )) {
     $params = 'init_user=&init_listroom=1001,1002,1003,1004';
     include 'templates/chat.php';
} else {
     echo 'You must come from Mycrib.net to enter the chat <a href="http://mycrib.net/?pageid=mycrib.member.profile.home">Click here</a>';
}
 ?>
This is the presentation stuff in chat.php:

Code: Select all

<html>
<head>
<title>MyCribTrain.com - MyCrib Chat</title>
<META HTTP-EQUIV="Expires" CONTENT="Tue, 01 Jan 1980 1:00:00 GMT">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>	
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="100%" height="100%">
<param name=movie value="123flashchat.swf?<?php echo $params; ?>">
<param name=quality value=high>
<embed src="123flashchat.swf?<?php echo $params; ?>" quality=high pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="100%" height="100%" >
</embed>
</object>
</body>
</html>
(#10850)
jasondavis
Forum Commoner
Posts: 60
Joined: Sat Feb 04, 2006 5:35 pm

Post by jasondavis »

interesting way to do it, but this still creates same error as other ways tried

Parse error: parse error, unexpected '{' in /home/mycrib/public_html/chat/index2.php on line 4

arborint wrote:This is a great example of why you should separeate your programming from your presentation. It makes it much easier to see what is going on and promotes reuse.

Here is the actual program:

Code: Select all

<?PHP
$age =12;
session_start(); // need to be called once 
if ((isset($_SESSION['mycribname'])  && ($age >= 88)) {
     $params = 'init_user=' . $_SESSION['mycribname'] . '&init_password=&init_listroom=1001,1002,1003,1004';
     include 'templates/chat.php';
} else if ((isset($_SESSION['mycribname'])  && ($age < 18 )) {
     $params = 'init_user=&init_listroom=1001,1002,1003,1004';
     include 'templates/chat.php';
} else {
     echo 'You must come from Mycrib.net to enter the chat <a href="http://mycrib.net/?pageid=mycrib.member.profile.home">Click here</a>';
}
 ?>
This is the presentation stuff in chat.php:

Code: Select all

<html>
<head>
<title>MyCribTrain.com - MyCrib Chat</title>
<META HTTP-EQUIV="Expires" CONTENT="Tue, 01 Jan 1980 1:00:00 GMT">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>	
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="100%" height="100%">
<param name=movie value="123flashchat.swf?<?php echo $params; ?>">
<param name=quality value=high>
<embed src="123flashchat.swf?<?php echo $params; ?>" quality=high pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="100%" height="100%" >
</embed>
</object>
</body>
</html>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

still missing the same parens..

yes, parens are "(" and ")"
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

You are a patient soul feyd. :D
(#10850)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

For the program part of it, you could do something like...

Code: Select all

<?php
session_start(); // need to be called once

$age = 12;
$msg = '';
if ( isset($_SESSION['mycribname']) )
{
    if ( $age >= 88 )
	{
	    $params = 'init_user=' . $_SESSION['mycribname'] . '&init_password=&init_listroom=1001,1002,1003,1004';
	}
	else if ( $age < 18 )
	{
	    $params = 'init_user=&init_listroom=1001,1002,1003,1004';
	}
	
	include 'templates/chat.php';
}
else
{
    $msg = 'You must come from Mycrib.net to enter the chat <a href="http://mycrib.net/?pageid=mycrib.member.profile.home">Click here</a>';
}

echo $msg; // Or use this in your HTML somewhere
?>
Post Reply