Jump to content


Photo

switch/case


  • Please log in to reply
6 replies to this topic

#1 Tandem

Tandem
  • Members
  • PipPipPip
  • Advanced Member
  • 251 posts

Posted 16 July 2006 - 01:56 PM

I'm having a problem using switch/case. I've probably missed something obvious, but i can't work it out for the life of me.

I'm getting this error:
Parse error: parse error, unexpected T_SL in C:\Program Files\Apache Group\Apache2\htdocs\Train_station.php on line 97
Here's the relevant code from the page:
<?php
$travel = $_POST['travel'];

if (empty($travel)) {
travel_form();
}

if (!empty($travel)){
switch ($travel){
case "NY":
$sql = mysql_query("UPDATE USERS SET LOCATION='New York' WHERE USERID='$_SESSION[userid]'");
/*(line 97)*/print <<<HERE 
<div class="whitetext" align="center">Welcome to New York!</div>\n
HERE;
break;
case "LA":
$sql = mysql_query("UPDATE USERS SET LOCATION='Los Angeles' WHERE USERID='$_SESSION[userid]'");
print <<<HERE 
<div class="whitetext" align="center">Welcome to Los Angeles!</div>\n
HERE;
break;
case "CH":
$sql = mysql_query("UPDATE USERS SET LOCATION='Chicago' WHERE USERID='$_SESSION[userid]'");
print <<<HERE 
<div class="whitetext" align="center">Welcome to Chicago!</div>\n
HERE;
break;
case "HN":
$sql = mysql_query("UPDATE USERS SET LOCATION='Houston' WHERE USERID='$_SESSION[userid]'");
print <<<HERE 
<div class="whitetext" align="center">Welcome to Houston!</div>\n
HERE;
break;
case "PH":
$sql = mysql_query("UPDATE USERS SET LOCATION='Philadelphia' WHERE USERID='$_SESSION[userid]'");
print <<<HERE 
<div class="whitetext" align="center">Welcome to Philadelphia!</div>\n
HERE;
break;
case "DT":
$sql = mysql_query("UPDATE USERS SET LOCATION='Detroit' WHERE USERID='$_SESSION[userid]'");
print <<<HERE 
<div class="whitetext" align="center">Welcome to Detroit!</div>\n
HERE;
break;
default:
print "Error!\n";
}
travel_form();
}
?>
<?php
function travel_form {
print <<<HERE
<table cellpadding="0" cellspacing="0" width="30%" align="center">
<tr><td class="tablehead" colspan="2">TRAVEL TO</td></tr>
<form type="POST" action="train_station.php">
<tr><td class="radio"><input type="radio" name="travel" value="NY"></td><td class="topcity">New York</td></tr>
<tr><td class="radio"><input type="radio" name="travel" value="LA"></td><td class="city">Los Angeles</td></tr>
<tr><td class="radio"><input type="radio" name="travel" value="CH"></td><td class="city">Chicago</td></tr>
<tr><td class="radio"><input type="radio" name="travel" value="HN"></td><td class="city">Houston</td></tr>
<tr><td class="radio"><input type="radio" name="travel" value="PH"></td><td class="city">Philadelphia</td></tr>
<tr><td class="radio"><input type="radio" name="travel" value="DT"></td><td class="city">Detroit</td></tr>
<tr><td class="s" colspan="2"><input type="submit" name="submit" value="Travel!">
</form>
</table>
HERE;
}
?>

In case you're wondering what on earth is going on, i'm building a text-based MMO. This is my project to help me learn php, and i kinda always wanted to do it too :P.
This is the relevant code on the train_station.php page. This page changes the user's location to a different city in the game.
I've labeled line 97 for you.

Would appreciate it if somebody could point out to me what i've done wrong.

Thanks in advance

-Tandem

#2 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 16 July 2006 - 02:21 PM

This:
<div class="whitetextW align="center">Welcome to New York!</div>\n

Should be:
<div class="whitetextW" align="center">Welcome to New York!</div>\n

Orio.
Think you're smarty?

(Gone until 20 to November)

#3 Tandem

Tandem
  • Members
  • PipPipPip
  • Advanced Member
  • 251 posts

Posted 16 July 2006 - 02:26 PM

I've since changed that, but the error is still happening...

#4 Tandem

Tandem
  • Members
  • PipPipPip
  • Advanced Member
  • 251 posts

Posted 16 July 2006 - 02:51 PM

Anyone?

#5 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 16 July 2006 - 03:20 PM

Using this:
print <<<HERE 
<div class="whitetext" align="center">Welcome to Chicago!</div>n
HERE;
is a lot more work that it needs to be, and is probably what is causing your error.

Do this instead:
echo "<div class="whitetext" align="center">Welcome to Chicago!</div>\n";

You can also format your code to make it more readable when you use the echo statements...or if you don't like echo, you can still use print with the same syntax:

print "<div class="whitetext" align="center">Welcome to Chicago!</div>\n";


#6 Tandem

Tandem
  • Members
  • PipPipPip
  • Advanced Member
  • 251 posts

Posted 16 July 2006 - 03:31 PM

Still getting an error.

I had it with the syntax you said, before, but i changed it to the '<<HERE' etc in the hope that it would somehow fix the problem.

I'm getting a different error now though, perhaps you might be able to tell whats wrong from that. It's:
Parse error: parse error, unexpected T_STRING, expecting ',' or ';' in C:\Program Files\Apache Group\Apache2\htdocs\Train_station.php on line 103

Heres a few line of the relevant code:
if (!empty($travel)){
$location_check = mysql_query("SELECT LOCATION FROM USERS WHERE USERID='$_SESSION[userid]'");
$location = mysql_result($location_check,0);
print "You are currently in $location\n";
switch ($travel){
case "NY":
$sql = mysql_query("UPDATE USERS SET LOCATION='New York' WHERE USERID='$_SESSION[userid]'");
/*(line 103)*/echo "<div class="whitetext" align="center">Welcome to New York!</div>\n";
break;


#7 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 16 July 2006 - 03:40 PM

You have to escape the double quotes when they are inside of double quotes(I missed that you had them in there when I posted above)...or wrap the statement in single quotes rather than double:

Either
echo "<div class=\"whitetext\" align=\"center\">Welcome to New York!</div>\n";
or
echo '<div class="whitetext" align="center">Welcome to New York!</div>\n';

However, with the latter it will not substitute a new line for \n, it will put \n in the output.

So the latter should be:
[code]echo '<div class="whitetext" align="center">Welcome to New York!</div>' . "\n";[/code]





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users