Author Topic: Location Problem  (Read 944 times)

Offline Sweet

  • Level 3
  • *
  • Posts: 9
  • Reputation: +0/-0
    • View Profile
Location Problem
« on: July 11, 2010, 11:31:22 PM »
So the problem I'm having is, is I need different location screens for when my players are in the different locations. So as you can see below.. when the players are in Ovendale I want them to see the Rusty Doorknob and the Western Gate, and when they are in the Gremish forest I want them to see The Mossy Knowle. However, the problem I am having is that, when the players are in Ovendale, the script is showing both the Ovendale and the Gremish Forest location.
Code: [Select]
<?php
include_once 'connect.php';
session_start();

include_once 
'logo.php';
?>

 <link href="style.css" rel="stylesheet" type="text/css" />
<div id="login2" div align="center">






<?php
if (isset($_SESSION['player']))
{
  
$player=$_SESSION['player'];
}
else
{
  echo 
"Not Logged in <br><br> <A href='login.php'>Login</a>";
  exit;
}

$bypass 0;
?>


</div>

<?php
$playerinfo
="SELECT * from players where name='$player'";
$playerinfo2=mysql_query($playerinfo) or die("could not get player stats!");
$playerinfo3=mysql_fetch_array($playerinfo2);
include_once 
'statpanel.php';
 
?>

 <div id ="locations">
 <?php
$playerhp 
$playerinfo3['hpoints'];
if (
$playerhp 1)
{
  echo 
"You are dead!" ;
  echo 
"<br><a href='useitem.php'>Use an Item";
  exit;
}
?>

<?php
if(isset($_GET['map']))
{
$bypass=1;
echo 
"<a href='index.php?mapchange=1&mapname=Ovendale'>Ovendale</a><br>";


}

if(isset(
$_GET['mapchange']))
{
$bypass=1;
$mapname $_GET['mapname'];

$updateplayer="update players set location='$mapname' where name='$playerinfo3[name]'";
  
mysql_query($updateplayer) or die("Could not update player");
  
echo 
"You have traveled to " $mapname ".<br>";
echo 
"<a href='index.php?id=7162533'>To location</a><br>";
}

if(
$bypass != 1)

{
echo 
"<b><big><u>" $playerinfo3['location'] . "</u></big></b><br>";
echo 
"<a href='store.php'>Grocery Store</a><br>";
echo 
"<a href='weaponshop.php'>Weapon Shop</a><br>";
echo 
"<a href='armorshop.php'>Armor Shop</a><br>";
echo 
"<a href='inn.php'>The Rusty Doorknob</a><br>";
echo 
"<a href='spelltrainer.php'>Spell Trainer</a><br>";
/////echo "<a href='battle.php'>Battle in Arena</a><br>"; ///////
/////echo "<a href='index.php?map=1'>Go to Map</a><br>";  ///////
echo "<a href='travelattack1.php'>The West Gate</a><br>";

}
?>


<?php
if(isset($_GET['map']))
{
$bypass=2;
echo 
"<a href='index.php?mapchange=2&mapname=GremishForest'>Gremish Forest</a><br>";


}
if(isset(
$_GET['mapchange']))
{
$bypass=2;
$mapname $_GET['mapname'];

$updateplayer="update players set location='$mapname' where name='$playerinfo3[name]'";
  
mysql_query($updateplayer) or die("Could not update player");
  

}

if(
$bypass != 2)
{
echo 
"<b><big><u>" $playerinfo3['location'] . "</u></big></b><br>";
echo 
"<a href='store.php'>Grocery Store</a><br>";
echo 
"<a href='weaponshop.php'>Weapon Shop</a><br>";
echo 
"<a href='armorshop.php'>Armor Shop</a><br>";
echo 
"<a href='inn.php'>The Mossy Knowle</a><br>";
echo 
"<a href='spelltrainer.php'>Spell Trainer</a><br>";

}
?>



 </div>
<div id="logout">
<?php
echo "<br><a href='logout.php'><img src='images/logout.bmp'></a>";
?>

</div>


Offline Shrapnel

  • Level 9
  • *
  • Posts: 46
  • Reputation: +0/-0
    • View Profile
Re: Location Problem
« Reply #1 on: July 16, 2010, 11:51:40 AM »
Looks like it's because of your "if" conditions.  You're telling it to print condition A if bypass is not equal to 1 and condition B if bypass is not equal to 2.  So bypass must not be equal to either 1 or 2.  Since you set it to zero at the very beginning of the script, maybe it's still 0?
"Never compromise. Not even in the face of Armageddon" -Rorschach, Watchmen (2009)

Offline JGadrow

  • Level 35
  • **
  • Posts: 1,133
  • Reputation: +23/-2
    • View Profile
Re: Location Problem
« Reply #2 on: July 17, 2010, 09:59:42 AM »
Agreed, change your conditionals. In a case like this, you're wanting to pull data for a specific entry. So, rather than testing that it's not everything else, just check whether it is the correct id for that entry.

For example:
Code: (php) [Select]
<?php

$a 
1;
$b 2;
$c 3;
$var mt_rand (13);

// Wrong way!
if ($var != $b && $var != $c)
{
    echo 
'Must be a!';
}

if (
$var != $a && $var != $c)
{
    echo 
'Must be b!';
}

if (
$var != $a && $var != $b)
{
    echo 
'Must be c!';
}

// Correct way.
if ($var == $a)
{
    echo 
'It is a!';
}

if (
$var == $b)
{
    echo 
'It is b!';
}

if (
$var == $c)
{
    echo 
'It is c!';
}

?>

See the difference? In the first set of examples, maybe a month or three down the line, you realize that you need $d = 4. Well, now you've got to change every one of your conditions to account for this new possibility. However, with the second set, all you would need to do was to add its own if ($var == $d) conditional (this example would actually have been better with a case statement but I wanted to illustrate the point).

Hope that helps! :)
Idiocy - Never underestimate the power of stupid people in large groups.


Offline Sweet

  • Level 3
  • *
  • Posts: 9
  • Reputation: +0/-0
    • View Profile
Re: Location Problem
« Reply #3 on: July 20, 2010, 05:58:33 PM »
Thank you guys, I appreciate the help, I've finally fixed this annoying problem >.<

Offline andrewjbaker

  • Level 16
  • *
  • Posts: 151
  • Reputation: +2/-0
    • View Profile
    • Fleeting Fantasy
Re: Location Problem
« Reply #4 on: July 21, 2010, 04:12:44 AM »
Just as an aside, I'd also be tempted to use if..else to short circuit the evaluation of all those if statements. If the number of if statements grows to say 50 and the first if statement is true, as it stands at the moment your code would then perform 49 unnecessary comparisons.  :-\

Alternatively, you might consider using a switch statement.

If you need code examples... shout up. ;)

Hmm... will a switch work? Yes, because PHP expects the case keyword to be followed by an expr (expression)...

Code: [Select]
case_list:
        /* empty */
    |    case_list T_CASE expr case_separator  inner_statement_list
    |    T_DEFAULT case_separator inner_statement_list
    ;
« Last Edit: July 21, 2010, 04:42:54 AM by andrewjbaker »
Currently working on an HTML5 canvas 2.5D landscape renderer and a PBBG that uses it (http://fleetingfantasy.com/). The development blog's at http://fleetingfantasy.wordpress.com/.
What are BBGameZone members working on? See the game list.
irc.freenode.net, #bbg

Offline JGadrow

  • Level 35
  • **
  • Posts: 1,133
  • Reputation: +23/-2
    • View Profile
Re: Location Problem
« Reply #5 on: July 21, 2010, 09:35:00 AM »
Just as an aside, I'd also be tempted to use if..else to short circuit the evaluation of all those if statements. If the number of if statements grows to say 50 and the first if statement is true, as it stands at the moment your code would then perform 49 unnecessary comparisons.  :-\
Yup, you're perfectly correct there. I almost did an if-else-if construct, but I wanted the language flow of the if-if-if structure. It was meant to be an example so I wasn't worried about optimization.  The part that I wanted to get across was what the expression was saying about the code rather than the actual structure itself.

Alternatively, you might consider using a switch statement.
Yup, at the very bottom of my post I actually indicated that would have been the best case scenario for the example that I wrote but, again, I wanted to stress the importance of what your logical evaluations are doing rather than the specific language construct that should have been used to evaluate a variable.

Hmm... will a switch work? Yes, because PHP expects the case keyword to be followed by an expr (expression)...
Yup, it's possible to greatly abuse the switch/case construct to do some interesting things. Personally, however, I've found that the code would have been clearer if it was written another way. Indeed, sometimes it's both clearer and more concise!
Idiocy - Never underestimate the power of stupid people in large groups.


Offline andrewjbaker

  • Level 16
  • *
  • Posts: 151
  • Reputation: +2/-0
    • View Profile
    • Fleeting Fantasy
Re: Location Problem
« Reply #6 on: July 21, 2010, 06:34:22 PM »
Quote
It was meant to be an example so I wasn't worried about optimization.  The part that I wanted to get across was what the expression was saying about the code rather than the actual structure itself.

No worries mate, that's what I concluded; I wasn't picking holes in your use of selection statements. ;-)
Currently working on an HTML5 canvas 2.5D landscape renderer and a PBBG that uses it (http://fleetingfantasy.com/). The development blog's at http://fleetingfantasy.wordpress.com/.
What are BBGameZone members working on? See the game list.
irc.freenode.net, #bbg

Offline pixlepix

  • Level 12
  • *
  • Posts: 90
  • Reputation: +0/-0
    • View Profile
Re: Location Problem
« Reply #7 on: October 06, 2010, 06:54:10 AM »
if(isset($_GET['map']))
{
$bypass=1;
echo "<a href='index.php?mapchange=1&mapname=Ovendale'>Ovendale</a><br>";


}

Could this be part of the issue? Looks like it is redirecting you to ovendle always

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal