Jump to content

Recommended Posts

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:
[code]
<?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;
}
?>
[/code]

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
Link to comment
https://forums.phpfreaks.com/topic/14769-switchcase/
Share on other sites

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

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

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:

[code]print "<div class="whitetext" align="center">Welcome to Chicago!</div>\n";[/code]
Link to comment
https://forums.phpfreaks.com/topic/14769-switchcase/#findComment-58972
Share on other sites

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:
[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;
[/code]
Link to comment
https://forums.phpfreaks.com/topic/14769-switchcase/#findComment-58973
Share on other sites

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
[code]echo "<div class=\"whitetext\" align=\"center\">Welcome to New York!</div>\n";[/code]
or
[code]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][/code]
Link to comment
https://forums.phpfreaks.com/topic/14769-switchcase/#findComment-58976
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.