Battle Problem [Resolved]

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Battle Problem [Resolved]

Post by Epiales »

Okay, last post for the night. Got to rest sometime :D

I've looked over and over. I've tried redoing, but always come up with the same result.

Code: Select all

Parse error: syntax error, unexpected 'else' (T_ELSE)
It's the last else in the battle.php! I have to have that there, so I don't know why I can't fix it. Probably b/c I've been up for 20 hours coding LOL. Anyway, if anyone can notice what's wrong, that's great. If not, no worries. I'll be up later to check it out again. Thanks for the help all. At one time I had it working, but it kept saying I visited the page wrong, which shouldn't of said that, but I messed it up somehow. Probably something small. Night everyone!

Code: Select all


<?php
session_start();
include("header.php");
if(!isset($_SESSION['uid'])){
    echo "You must be logged in to view this page!";
}else
   
if (isset($_GET['characters']))
{
	$characters=$_GET['characters'];
	$charactersinfo = "SELECT * from characters where name = '$characters'";
	$charactersinfo2 = mysql_query($charactersinfo) or die ("Could not get the character you were fighting!");
	$charactersinfo3 = mysql_fetch_array($charactersinfo2);

} else {

	$charactersinfo = "SELECT * from characters order by rand() limit 1";
	$charactersinfo2 = mysql_query($charactersinfo) or die ("Could not get a character!");
	$charactersinfo3 = mysql_fetch_array($charactersinfo2);
	
	$characters = $charactersinfo3['name'];
	$charactershp = $charactersinfo3['hpoints'];
	$charactersattack = $charactersinfo3['attack'];
	$charactersdefense = $charactersinfo3['defense'];
	
	
	echo "<u> " . $user['username'] . "</u>";
	echo "<br>";
	echo "Hit Points = " . $stats['hpoints'] . "<br>";
	echo "Attack = " . $stats['attack'] . "<br>";	   
	echo "Defense = " . $stats['defense'] . "<br>"; 
	echo "<br><br>";
	
	echo "<u> " . $charactersinfo3['name'] . "</u><br>";
	echo "Hit Points = " . $charactershp . "<br>";
	echo "Attack = " . $charactersattack . "<br>";	   
	echo "Defense = " . $charactersdefense . "<br>"; 
	echo "<br>";
	echo "<a href='attack.php?characters=$characters'>Attack!";
           }
       
    else{
        output("You have visited this page incorrectly!");
    }
}
include("footer.php");
?>
Last edited by Epiales on Fri Aug 23, 2013 7:35 am, edited 1 time in total.
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Battle Problem

Post by Jackolantern »

You can only have 1 ELSE clause for each IF statement. It doesn't have to be there because it makes no sense ;)

Else what? You have to create an IF statement for it to be an ELSE against or the idea of the ELSE makes no sense :)

To fix it, you need to think of the reason why someone would get the message "You have visited this page incorrectly" and put it into an IF statement to reflect that condition.
The indelible lord of tl;dr
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Battle Problem

Post by Epiales »

*EDITED*

I have it fixed. Wasn't thinking straight :) Thx
Last edited by Epiales on Fri Aug 23, 2013 12:30 am, edited 2 times in total.
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
Xaos
Posts: 940
Joined: Wed Jan 11, 2012 4:01 am

Re: Battle Problem

Post by Xaos »

Code: Select all

    <?php
    session_start();
    include("header.php");
    if(!isset($_SESSION['uid'])){
        echo "You must be logged in to view this page!";
    }else   //missing the { here. 
       
    if (isset($_GET['characters']))
    {
       $characters=$_GET['characters'];
       $charactersinfo = "SELECT * from characters where name = '$characters'";
       $charactersinfo2 = mysql_query($charactersinfo) or die ("Could not get the character you were fighting!");
       $charactersinfo3 = mysql_fetch_array($charactersinfo2);

    } else {

       $charactersinfo = "SELECT * from characters order by rand() limit 1";
       $charactersinfo2 = mysql_query($charactersinfo) or die ("Could not get a character!");
       $charactersinfo3 = mysql_fetch_array($charactersinfo2);
       
       $characters = $charactersinfo3['name'];
       $charactershp = $charactersinfo3['hpoints'];
       $charactersattack = $charactersinfo3['attack'];
       $charactersdefense = $charactersinfo3['defense'];
       
       
       echo "<u> " . $user['username'] . "</u>";
       echo "<br>";
       echo "Hit Points = " . $stats['hpoints'] . "<br>";
       echo "Attack = " . $stats['attack'] . "<br>";      
       echo "Defense = " . $stats['defense'] . "<br>";
       echo "<br><br>";
       
       echo "<u> " . $charactersinfo3['name'] . "</u><br>";
       echo "Hit Points = " . $charactershp . "<br>";
       echo "Attack = " . $charactersattack . "<br>";      
       echo "Defense = " . $charactersdefense . "<br>";
       echo "<br>";
       echo "<a href='attack.php?characters=$characters'>Attack!";
               }
           //running double else here. You're saying }else{else}. I don't see where the "if" part of this is. What is this the else of? 
        else{
            output("You have visited this page incorrectly!");
        }
    }
    include("footer.php");
    ?>
Added comments in a few places that are error spots. Also, if you type

Code: Select all

[/ code], it makes it show in PHP !  :D
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Battle Problem

Post by Epiales »

Code: Select all

Notice: Undefined variable: stat in C:\xampp\htdocs\game\attack.php on line 22

Notice: Undefined variable: stat in C:\xampp\htdocs\game\attack.php on line 23

Notice: Undefined variable: stat in C:\xampp\htdocs\game\attack.php on line 24

Code: Select all

<?php
session_start();
include("header.php");
if(!isset($_SESSION['uid'])){
    echo "You must be logged in to view this page!";
}else{
   
if (isset($_GET['characters']))
{
	$characters=$_GET['characters'];
	$charactersinfo = "SELECT * from characters where name = '$characters'";
	$charactersinfo2 = mysql_query($charactersinfo) or die ("Could not get the character you were fighting!");
	$charactersinfo3 = mysql_fetch_array($charactersinfo2);

} else {

	echo "<a href='battle1.php>No Character selected. Go Back!";
	exit;
}   


	$playershp = $stat['hpoints'];
	$playersattack = $stat['attack'];
	$playersdefense = $stat['defense'];
   
	$characters = $charactersinfo3['name'];
	$charactershp = $charactersinfo3['hpoints'];
	$charactersattack = $charactersinfo3['attack'];
	$charactersdefense = $charactersinfo3['defense'];

Errors in question:


$playershp = $stat['hpoints'];
$playersattack = $stat['attack'];
$playersdefense = $stat['defense'];
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
Xaos
Posts: 940
Joined: Wed Jan 11, 2012 4:01 am

Re: Battle Problem

Post by Xaos »

Epiales wrote:

Code: Select all

Notice: Undefined variable: stat in C:\xampp\htdocs\game\attack.php on line 22

Notice: Undefined variable: stat in C:\xampp\htdocs\game\attack.php on line 23

Notice: Undefined variable: stat in C:\xampp\htdocs\game\attack.php on line 24

Code: Select all

<?php
session_start();
include("header.php");
if(!isset($_SESSION['uid'])){
    echo "You must be logged in to view this page!";
}else{
   
if (isset($_GET['characters']))
{
	$characters=$_GET['characters'];
	$charactersinfo = "SELECT * from characters where name = '$characters'";
	$charactersinfo2 = mysql_query($charactersinfo) or die ("Could not get the character you were fighting!");
	$charactersinfo3 = mysql_fetch_array($charactersinfo2);

} else {

	echo "<a href='battle1.php>No Character selected. Go Back!";
	exit;
}   


	$playershp = $stat['hpoints'];
	$playersattack = $stat['attack'];
	$playersdefense = $stat['defense'];
   
	$characters = $charactersinfo3['name'];
	$charactershp = $charactersinfo3['hpoints'];
	$charactersattack = $charactersinfo3['attack'];
	$charactersdefense = $charactersinfo3['defense'];

Errors in question:


$playershp = $stat['hpoints'];
$playersattack = $stat['attack'];
$playersdefense = $stat['defense'];

Where are you defining $stat?
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Battle Problem

Post by Epiales »

All of my stats are through a stat field in the database. I found it easier to do it that way. That way I can include all the links in the header.php and just call the stats from the stats field. The only thing in my users field is the username, password, and email.
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
Xaos
Posts: 940
Joined: Wed Jan 11, 2012 4:01 am

Re: Battle Problem

Post by Xaos »

Epiales wrote:All of my stats are through a stat field in the database. I found it easier to do it that way. That way I can include all the links in the header.php and just call the stats from the stats field. The only thing in my users field is the username, password, and email.
Are you defining the variable in the header.php? You'd have to actually define $stat as a field in the DB
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Battle Problem

Post by Epiales »

OH! ((slap in face))

You asked WHERE not WHY lmao! No, I see I don't have them defined. LOL.
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
Xaos
Posts: 940
Joined: Wed Jan 11, 2012 4:01 am

Re: Battle Problem

Post by Xaos »

Epiales wrote:OH! ((slap in face))

You asked WHERE not WHY lmao! No, I see I don't have them defined. LOL.
Haha. That would solve that issue. Make sure you have $stat itself defined a row in the DB.
Post Reply

Return to “Beginner Help and Support”