Jump to content

Recommended Posts

OK so I'm new to the site, as well as PHP altogether (Trying my hand at managing an online RPG) and so far it's been quite smooth. However whilst trying to change an aspect of the game from giving the user the location of another user into giving the user the continent another user is in I have become a bit stuck. Here is the original piece of code:

 

if ($action=="choose")

        {

          $infoskill=$currentuser["infoskill"];

          $infoskill=$infoskill+makeBonus($user,"i",$db);

          if ($find=="location")

          {

            if ($infoskill > 14)

            {

              $cost=round((15/$infoskill)*300);

              if ($cost <20)

              {

                $cost=20;

              }

              if ($currentuser["cash"]<$cost)

              {

                echo "        You can't afford that information.<br>\n";

              }

              else

              {

                $sql="SELECT * FROM `users` WHERE `name` = '$usertofind' AND `dead` = 'n'";

                $userlocationresult = runSQL($sql, $currentuser["admin"]);

                if ($userlocation = mysql_fetch_array($userlocationresult))

                {

                  echo "        $usertofind is in ".$userlocation["location"].".<br>\n";

                  runSQL("UPDATE `users` SET `cash`=`cash`-'$cost' WHERE `name`='$user'", $currentuser["admin"]);

                }

                else

                {

                  echo "        The user you entered doesn't exist or is dead.<br>\n";

                }

              }

            }

            else

            {

              echo "        You don't have the contacts to find that information.<br>\n";

            }

          }

          elseif ($find=="skills")

          {

            if ($infoskill > 19)

            {

              $cost=round((20/$infoskill)*400);

              if ($cost <20)

              {

                $cost=20;

              }

              if ($currentuser["cash"]<$cost)

              {

                echo "        You can't afford that information.<br>\n";

 

As you can see in the middle of that piece the user can pay to see the location of another user, providing the target is not dead and the user has enough cash. Now instead of giving the exact location of the target I am trying to group the cities into continents, like so:

 

if ($action=="choose")

        {

          $infoskill=$currentuser["infoskill"];

          $infoskill=$infoskill+makeBonus($user,"i",$db);

          if ($find=="location")

          {

            if ($infoskill > 14)

            {

              $cost=round((15/$infoskill)*300);

              if ($cost <20)

              {

                $cost=20;

              }

              if ($currentuser["cash"]<$cost)

              {

                echo "        You can't afford that information.<br>\n";

              }

              else

              {

                $sql="SELECT * FROM `users` WHERE `name` = '$usertofind' AND `dead` = 'n'";

                $userlocationresult = runSQL($sql, $currentuser["admin"]);

                if ($userlocation = London || Milan || Paris || Dublin || Moscow)

                {

                  echo "        $usertofind is in Europe.";

                  runSQL("UPDATE `users` SET `cash`=`cash`-'$cost' WHERE `name`='$user'", $currentuser["admin"]);

                }

                elseif ($userlocation = Lagos || Johannesburg || Nairobi)

                {

                  echo "        $usertofind is in Africa";

                  runSQL("UPDATE `users` SET `cash`=`cash`-'$cost' WHERE `name`='$user'", $currentuser["admin"]);

                }

                elseif ($userlocation = New York || Los Angeles || Dallas)

                {

                  echo "        $usertofind is in North America";

                  runSQL("UPDATE `users` SET `cash`=`cash`-'$cost' WHERE `name`='$user'", $currentuser["admin"]);

                }

                elseif ($userlocation = Bogota || Rio De Janeiro || Buenos Aires)

                {

                  echo "        $usertofind is in South America";

                  runSQL("UPDATE `users` SET `cash`=`cash`-'$cost' WHERE `name`='$user'", $currentuser["admin"]);

                }

                elseif ($userlocation = Mexico City || Havana || Kingston)

                {

                  echo "        $usertofind is in Central America";

                  runSQL("UPDATE `users` SET `cash`=`cash`-'$cost' WHERE `name`='$user'", $currentuser["admin"]);

                }

                else

                {

                  echo "        $usertofind is in Asia";

                  runSQL("UPDATE `users` SET `cash`=`cash`-'$cost' WHERE `name`='$user'", $currentuser["admin"]);

                }

              }

            }

            else

            {

              echo "        You don't have the contacts to find that information.<br>\n";

            }

          }

          elseif ($find=="skills")

          {

            if ($infoskill > 19)

            {

              $cost=round((20/$infoskill)*400);

              if ($cost <20)

              {

                $cost=20;

              }

              if ($currentuser["cash"]<$cost)

              {

                echo "        You can't afford that information.<br>\n"

 

This however gives the following error message:

 

Parse error: syntax error, unexpected T_STRING in /home/a7098669/public_html/getinfo.php on line 158

 

What can I do to solve this? And instead of giving just text answers could I group the locations into continents and just have one response which would be something like:

 

"$usertofind is in $continent"

 

I hope someone can help!

 

Thanks in advance,

 

Matt

Link to comment
https://forums.phpfreaks.com/topic/182664-elseif-help/
Share on other sites

actually,

 

if ($userlocation = London || Milan || Paris || Dublin || Moscow)

this is probably setting $userlocation to 0, you are probably trying to do...

if ($userlocation == "London" || $userlocation == "Milan" || $userlocation == "Paris" || $userlocation == "Dublin" || $userlocation == "Moscow")

raw strings need to have quotation marks around them, and = is to set something, == is comparison

Link to comment
https://forums.phpfreaks.com/topic/182664-elseif-help/#findComment-964098
Share on other sites

Actually its not quite solved, I'm no longer getting any errors but every continent is coming up as "Africa". Am I using my elseif function right? Here's the code:

 

{

                $sql="SELECT * FROM `users` WHERE `name` = '$usertofind' AND `dead` = 'n'";

                $userlocationresult = runSQL($sql, $currentuser["admin"]);

                if ($userlocation == "London" || $userlocation == "Milan" || $userlocation == "Paris" || $userlocation == "Dublin" || $userlocation == "Moscow")

 

                {

                  echo "        $usertofind is in Europe.";

                  runSQL("UPDATE `users` SET `cash`=`cash`-'$cost' WHERE `name`='$user'", $currentuser["admin"]);

                }

                elseif ($userlocation = "Lagos" || $userlocation == "Johannesburg" || $userlocation == "Nairobi")

                {

                  echo "        $usertofind is in Africa";

                  runSQL("UPDATE `users` SET `cash`=`cash`-'$cost' WHERE `name`='$user'", $currentuser["admin"]);

                }

                elseif ($userlocation = "New York" || $userlocation == "Los Angeles" || $userlocation == "Dallas")

                {

                  echo "        $usertofind is in North America";

                  runSQL("UPDATE `users` SET `cash`=`cash`-'$cost' WHERE `name`='$user'", $currentuser["admin"]);

                }

                elseif ($userlocation = "Bogota" || $userlocation == "Rio De Janeiro" || $userlocation == "Buenos Aires")

                {

                  echo "        $usertofind is in South America";

                  runSQL("UPDATE `users` SET `cash`=`cash`-'$cost' WHERE `name`='$user'", $currentuser["admin"]);

                }

                elseif ($userlocation = "Mexico City" || $userlocation == "Havana" || $userlocation == "Kingston")

                {

                  echo "        $usertofind is in Central America";

                  runSQL("UPDATE `users` SET `cash`=`cash`-'$cost' WHERE `name`='$user'", $currentuser["admin"]);

                }

                else

                {

                  echo "        $usertofind is in Asia";

                  runSQL("UPDATE `users` SET `cash`=`cash`-'$cost' WHERE `name`='$user'", $currentuser["admin"]);

                }

 

**EDIT: Sloppy syntax again. Fixed!**

Link to comment
https://forums.phpfreaks.com/topic/182664-elseif-help/#findComment-964240
Share on other sites

OK, it's still not working. There are no syntax errors, just every response gives (User is in Europe). Even made up users that don't exist on the database are in Europe, apparently. I've shortened it to just one if, one elseif, and one else for simplicity, can anyone please help me discover why only the first result is given, no matter what?

 

Here's the code:

 

              else

              {

                $sql="SELECT * FROM `users` WHERE `name` = '$usertofind' AND `dead` = 'n'";

                $userlocationresult = runSQL($sql, $currentuser["admin"]);

                if ($userlocation = "London")

                {

                  echo "        $usertofind is in Europe.";

                  runSQL("UPDATE `users` SET `cash`=`cash`-'$cost' WHERE `name`='$user'", $currentuser["admin"]);

                }

                elseif ($userlocation = "Kabul")

                {

                  echo "        $usertofind is in Asia.";

                  runSQL("UPDATE `users` SET `cash`=`cash`-'$cost' WHERE `name`='$user'", $currentuser["admin"]);

                }

                else

                {

                  echo "        The user you entered doesn't exist or is dead.<br>\n";

                }

              }

 

Thank you!

Link to comment
https://forums.phpfreaks.com/topic/182664-elseif-help/#findComment-964306
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.