A little help with php

Kaos

[H]ard|Gawd
Joined
Oct 14, 2003
Messages
1,328
I just started learning php on monday for something fun to do.

I'm working on a little "tool" that will basically take information from a user and format it into a block of text they can copy and paste.

Pretty simple.

I learned enough css to apply it to my program but now it seems to be fudged (which I'm sure is something I did)
It simply tells me "Parse error: syntax error, unexpected $end in /homepages/43/d156462996/htdocs/php/defense_dev.php on line 257"

But I know I probably forgot to close a bracket or something retarded, I've just been staring at it for about 2 hours and can't see it.

Line 257 is "</html>"

So my questions are

1. Is there a better way of debugging than by searching through guessing for errors?
2. Can you take a look and see if you see anything obvious?

Code:
<html>
<head>
<title>Pack Defense Request Builder v0.1</title>
<script language="javascript" type="text/javascript" src="datetimepicker.js"></script>
<link rel=stylesheet href="style.css" type="text/css">
</head>

<?php
/*
Will be used to generate properly formatted defense requests and some form of catapult detection
*/
?>

<body>
<?php
if(isset($_POST['user']))
{
	//User Information
	$user = $_POST['user'];
	$usercity = $_POST['usercity'];
	$user_xcord = $_POST['user_xcord'];
	$user_ycord = $_POST['user_ycord'];
	$grancap = $_POST['grancap'];
	$wheat = $_POST['wheat'];
	$wall = $_POST['wall'];
	$curD = $_POST['curD'];
	$sitter1 = $_POST['sitter1'];
	$sitter2 = $_POST['sitter2'];
	$platoon = $_POST['platoon'];
	$division = $_POST['Division'];
	
	//Attack Information
	$attacktype = $_POST['attacktype'];
	$attname = $_POST['attacker'];
	$alliance = $_POST['alliance'];
	$attcity = $_POST['attcity'];
	$att_xcord = $_POST['att_xcord'];
	$att_ycord = $_POST['att_ycord'];
	$breport= $_POST['breport'];
		
	//Catapult detection
	$atttime1 = $_POST['atttime1'];
	$landtime = $_POST['landtime'];
	
	
}
?>
<h3 class="header" align="center">Pack Defense Request Builder</h3>
<table border="1" align="center">
	<tr>
	<td>
		<table>
			<form method="post" action="<?=$_SERVER['PHP_SELF']?>">
				<td>	
					<tr>
						<td><h5 class="header">User Information<h5></td>
   					</tr>
					
					<tr>
						<td class="sText">User name:</td>
						<td>
							<input type="text" class="form"  name="user" size="30" value="<?php echo $user; ?>">
						</td>
					</tr>
	
					<tr>
						<td class="sText">Your city name & coords</td>
						<td class="sText">
				   			<input type="text" class="form"  name="usercity" size="30" value="<?php echo $usercity; ?>"> 
								X:<input type="text" class="form"  name="user_xcord" size="4" value=<?php echo $user_xcord; ?>> 
								Y:<input type="text" class="form"  name="user_ycord" size="4" value=<?php echo $user_ycord; ?>>
						</td>
   					</tr>
	
					<tr>
						<td class="sText">Granary Capacity</td>
						<td class="sText">
							<input type="text" class="form"  name="grancap" size="12" value="<?php echo $grancap; ?>">
			    			Current amount of wheat: <input type="text" class="form"  name="wheat" size="10" value="<?php echo $wheat; ?>">
						</td>
   					</tr>
   
   					<tr>
   						<td class="sText">Wall Level</td>
   						<td><input type="text" class="form"  name="wall" size="12" value="<?php echo $wall; ?>"></td>
   					</tr>
   
   					<tr>
   						<td class="sText">Current Defenses</td>
   						<td><textarea class="textarea" name="curD" rows="3" cols="50"><?php echo $curD; ?></textarea></td>
   					</tr>
   
   					<tr>
   						<td class="sText">Sitters</td>
   						<td>
   							<input type="text" class="form"  name="sitter1" value=<?php echo $sitter1; ?>>
   							<input type="text" class="form"  name="sitter2" value=<?php echo $sitter2; ?>>
   						</td>
  					</tr>
 
 					<tr>
  							<td class="sText">Division</td>
  							<td>
  								<select class="form" name="Division">
									<option value=<? echo $division; ?>><? echo $division; ?></option>
									<option value=1>1</option>
									<option value=2>2</option>		
									<option value=3>3</option>	
									<option value=4>4</option>	
									<option value=5>5</option>	
									<option value=6>6</option>	
								</select>
  							</td>
  					</tr>
  
  					<tr>
  						<td class="sText">Platoon</td>
  						<td>
  								<select class="form" name="platoon">
									<option value=<? echo $platoon; ?>><? echo $platoon; ?></option>
									<option value=1>1</option>
									<option value=2>2</option>		
									<option value=3>3</option>	
									<option value=4>4</option>	
									<option value=5>5</option>	
									<option value=6>6</option>	
								</select>
  						</td>
  					</tr>
  			</td>
  		</table>
  		<td>
  		  	<table>
  			<tr>
				<td><h5 class="header">Attack Information<h5></td>
   			</tr>
    
          <tr>
   		        <td class="sText">Attack Type</td>
   		        <td>
   							<select class="form" name="attacktype">
								<option value=<? echo $attacktype; ?>><? echo $attacktype; ?></option>
								<option value=Attacked>Attacked</option>
								<option value=Attack>Attack</option>		
							</select>
   				</td>
 			</tr>
  			<tr>
 				<td class="sText">Time attacks were first noticed by you</td>
 				<td>
 						<input class="form" id="atttime1" type="text" name="atttime1" value="<?php echo $atttime1; ?>">
 						<a href="javascript:NewCal('atttime1','mmddyyyy',true,24)"><img src="images/cal.gif" width="16" height="16" border="0" alt=Pick a date"></a>
 				</td>
 			</tr>
 
 			<tr>
 				<td class="sText">Time attack will land (EST)</td>
 				<td>
 						<input type="text" class="form" id="landtime"  name="landtime" value="<?php echo $landtime; ?>">
 						<a href="javascript:NewCal('landtime','mmddyyyy',true,24)"><img src="images/cal.gif" width="16" height="16" border="0" alt=Pick a date"></a>
 				</td>
   			</tr>
   			
   			<tr>
				<td class="sText">Attacker Name</td>
				<td>
						<input type="text" class="form"  name="attacker" size="30" value="<?php echo $attname; ?>">
				</td>
   			</tr>
   
   			<tr>
   	   			<td class="sText">Alliance</td>
				<td>
						<input type="text" class="form"  name="alliance" size="30" value="<?php echo $alliance; ?>">
				</td>
   			</tr>
   
   			<tr>
				<td class="sText">Attacker city name & coords</td>
				<td class="sText">
				   		<input type="text" class="form"  name="attcity" size="30" value="<?php echo $attcity; ?>"> 
					X:<input type="text" class="form"  name="att_xcord" size="4" value=<?php echo $att_xcord; ?>> 
					Y:<input type="text" class="form"  name="att_ycord" size="4" value=<?php echo $att_ycord; ?>>
				</td>
   			</tr>

   			<tr>
   				<td class="sText">Battle Reports</td>
   				<td>
   						<textarea class="textarea" name="breport" rows="5" cols="50"><?php echo $breport; ?></textarea>
   				</td>
   			</tr>
  	</td>
</table>
</tr>
</table>
   			
 
<tr>
	<table align="center">
			<td  class="form" width=300 align="center"><?php $currentFile = $_SERVER["SCRIPT_NAME"];
  								$parts = Explode('/', $currentFile);?>
  							
				<input type="button" value="Restart" onClick="parent.location='<?php echo $parts[count($parts) - 1];?>'"> 
					<input type="submit" value="Build Defense Request"></td>
	</table>
</tr>

	
</tr>
</form>

<?php
if(isset($_POST['user']))
{
?>

<table border="3" align="center" cellpadding="3" cellspacing="0">
	<tr>
		<h5 align=center class="header">Subject Line: <?php echo $user . " - " . $usercity ." (". $user_xcord ."|". $user_ycord .") ". $attacktype ." by ". $attname ." - ". $attcity ." (". $att_xcord ."|". $att_ycord .") of ". $alliance; ?></h5>
	</tr>
	<tr>
			<td  class="output">
			<?php echo $user ."'s village ".  $usercity ." (". $user_xcord ."|". $user_ycord .") ";?><br>
			<?php echo $attacktype ." by ". $attname ." - ". $attcity ." (". $att_xcord ."|". $att_ycord .") of ". $alliance; ?><br>
			<?php echo "Attack(s) land at " . $landtime . " (or battle report posted below.)"; ?><br>
			<?php echo $breport; ?><br>
			<?php echo "Granary Information (". $wheat ."/". $grancap .")"; ?><br>
			<?php echo "Wall level: " . $wall; ?><br>
		   <?php if(empty($sitter1) and empty($sitter2)){}
					  else{echo "Sitters: " . $sitter1 .", " . $sitter2;}?>
			<?php echo "Division " . $division; ?><br>
			<?php echo "Platoon " . $platoon; ?><br>
			<?php echo "Current Defenses: " . $curD; ?><br>			
			<?php $distance = sqrt((($user_xcord - $att_xcord)*($user_xcord - $att_xcord))+(($user_ycord - $att_ycord)*($user_ycord - $att_ycord)));?>
			 
			<?php echo "Distance From " . " (". $att_xcord ."|". $att_ycord .") to (". $user_xcord ."|". $user_ycord .") =". $distance;?><br>
			<?php $tsquare =0;
						$unitspeed = 3; ?>
						<?php if($distance < 30) {$triptime = $distance / $unitspeed; }
						else{
					          $triptime = ((30 / $unitspeed)+(($distance-30) / ($unitspeed*(1+($tsquare/10)))));}
					          ?>
			          					
	   		<?php echo "Travel time by catapult from " . "(" . $att_xcord ."|". $att_ycord . ") to (" . $user_xcord . "|" . $user_ycord . ") is " . $triptime;?><br>	
	   		<?php $unix_launchtime = strtotime($landtime) - (60 * $triptime * 60);?>
	   		<?php echo "Launch time if catapults are present: " . date("m/d @ H:i:s", $unix_launchtime);?>
</td>
</tr>
</table>
</body>
</html>

I did everything by hand in a text editor (ultraedit) so if the formatting is odd, I am quite sorry.

Thanks!
 
notepad++ is great - you can set the color coding to many different languages which can help spot errors.

In your case, I noticed a couple things:

<link rel=stylesheet href="style.css" type="text/css">

This tag is not closed. And stylesheet is not in quotes.

should be: <link rel="stylesheet" href="style.css" type="text/css" />


And <script language="javascript" type="text/javascript" src="datetimepicker.js"></script>

Isn't language deprecated? I'm not sure if it will cause errors leaving it there, but you might try omitting it.

Good luck!
 
If you want something to help with syntax errors take a peek at zend. Also your code technique gives me a giant headache when debugging. You'd be much better off without all the <?php open and close tags. Also it's very poor practice to use short open close tags. Some servers don't support short tags so if you need to move your code you might need to do a lot of editing. I'll go through your code a bit see if I can clean it up.

Code:
<html>
<head>
<title>Pack Defense Request Builder v0.1</title>
<script language="javascript" type="text/javascript" src="datetimepicker.js"></script>
<link rel=stylesheet href="style.css" type="text/css">
</head>

<?php
/*
Will be used to generate properly formatted defense requests and some form of catapult detection
*/
?>

<body>
<?php
if(isset($_POST['user']))
{
	//User Information
	$user = $_POST['user'];
	$usercity = $_POST['usercity'];
	$user_xcord = $_POST['user_xcord'];
	$user_ycord = $_POST['user_ycord'];
	$grancap = $_POST['grancap'];
	$wheat = $_POST['wheat'];
	$wall = $_POST['wall'];
	$curD = $_POST['curD'];
	$sitter1 = $_POST['sitter1'];
	$sitter2 = $_POST['sitter2'];
	$platoon = $_POST['platoon'];
	$division = $_POST['Division'];
	
	//Attack Information
	$attacktype = $_POST['attacktype'];
	$attname = $_POST['attacker'];
	$alliance = $_POST['alliance'];
	$attcity = $_POST['attcity'];
	$att_xcord = $_POST['att_xcord'];
	$att_ycord = $_POST['att_ycord'];
	$breport= $_POST['breport'];
		
	//Catapult detection
	$atttime1 = $_POST['atttime1'];
	$landtime = $_POST['landtime'];
	
	
}
?>
<h3 class="header" align="center">Pack Defense Request Builder</h3>
<table border="1" align="center">
	<tr>
	<td>
		<table>
			<form method="post" action="<?=$_SERVER['PHP_SELF']?>">
				<td>	
					<tr>
						<td><h5 class="header">User Information<h5></td>
   					</tr>
					
					<tr>
						<td class="sText">User name:</td>
						<td>
							<input type="text" class="form"  name="user" size="30" value="<?php echo $user; ?>">
						</td>
					</tr>
	
					<tr>
						<td class="sText">Your city name & coords</td>
						<td class="sText">
				   			<input type="text" class="form"  name="usercity" size="30" value="<?php echo $usercity; ?>"> 
								X:<input type="text" class="form"  name="user_xcord" size="4" value=<?php echo $user_xcord; ?>> 
								Y:<input type="text" class="form"  name="user_ycord" size="4" value=<?php echo $user_ycord; ?>>
						</td>
   					</tr>
	
					<tr>
						<td class="sText">Granary Capacity</td>
						<td class="sText">
							<input type="text" class="form"  name="grancap" size="12" value="<?php echo $grancap; ?>">
			    			Current amount of wheat: <input type="text" class="form"  name="wheat" size="10" value="<?php echo $wheat; ?>">
						</td>
   					</tr>
   
   					<tr>
   						<td class="sText">Wall Level</td>
   						<td><input type="text" class="form"  name="wall" size="12" value="<?php echo $wall; ?>"></td>
   					</tr>
   
   					<tr>
   						<td class="sText">Current Defenses</td>
   						<td><textarea class="textarea" name="curD" rows="3" cols="50"><?php echo $curD; ?></textarea></td>
   					</tr>
   
   					<tr>
   						<td class="sText">Sitters</td>
   						<td>
   							<input type="text" class="form"  name="sitter1" value=<?php echo $sitter1; ?>>
   							<input type="text" class="form"  name="sitter2" value=<?php echo $sitter2; ?>>
   						</td>
  					</tr>
 
 					<tr>
  							<td class="sText">Division</td>
  							<td>
  								<select class="form" name="Division">
									<option value=<?php echo $division." >".$division; ?></option>
									<option value=1>1</option>
									<option value=2>2</option>		
									<option value=3>3</option>	
									<option value=4>4</option>	
									<option value=5>5</option>	
									<option value=6>6</option>	
								</select>
  							</td>
  					</tr>
  
  					<tr>
  						<td class="sText">Platoon</td>
  						<td>
  								<select class="form" name="platoon">
									<option value=<? echo $platoon." >".$platoon; ?></option>
									<option value=1>1</option>
									<option value=2>2</option>		
									<option value=3>3</option>	
									<option value=4>4</option>	
									<option value=5>5</option>	
									<option value=6>6</option>	
								</select>
  						</td>
  					</tr>
  			</td>
  		</table>
  		<td>
  		  	<table>
  			<tr>
				<td><h5 class="header">Attack Information<h5></td>
   			</tr>
    
          <tr>
   		        <td class="sText">Attack Type</td>
   		        <td>
   							<select class="form" name="attacktype">
								<option value=<? echo $attacktype." >".$attacktype; ?></option>
								<option value=Attacked>Attacked</option>
								<option value=Attack>Attack</option>		
							</select>
   				</td>
 			</tr>
  			<tr>
 				<td class="sText">Time attacks were first noticed by you</td>
 				<td>
 						<input class="form" id="atttime1" type="text" name="atttime1" value="<?php echo $atttime1; ?>">
 						<a href="javascript:NewCal('atttime1','mmddyyyy',true,24)"><img src="images/cal.gif" width="16" height="16" border="0" alt=Pick a date"></a>
 				</td>
 			</tr>
 
 			<tr>
 				<td class="sText">Time attack will land (EST)</td>
 				<td>
 						<input type="text" class="form" id="landtime"  name="landtime" value="<?php echo $landtime; ?>">
 						<a href="javascript:NewCal('landtime','mmddyyyy',true,24)"><img src="images/cal.gif" width="16" height="16" border="0" alt=Pick a date"></a>
 				</td>
   			</tr>
   			
   			<tr>
				<td class="sText">Attacker Name</td>
				<td>
						<input type="text" class="form"  name="attacker" size="30" value="<?php echo $attname; ?>">
				</td>
   			</tr>
   
   			<tr>
   	   			<td class="sText">Alliance</td>
				<td>
						<input type="text" class="form"  name="alliance" size="30" value="<?php echo $alliance; ?>">
				</td>
   			</tr>
   
   			<tr>
				<td class="sText">Attacker city name & coords</td>
				<td class="sText">
				   		<input type="text" class="form"  name="attcity" size="30" value="<?php echo $attcity; ?>"> 
					X:<input type="text" class="form"  name="att_xcord" size="4" value=<?php echo $att_xcord; ?>> 
					Y:<input type="text" class="form"  name="att_ycord" size="4" value=<?php echo $att_ycord; ?>>
				</td>
   			</tr>

   			<tr>
   				<td class="sText">Battle Reports</td>
   				<td>
   						<textarea class="textarea" name="breport" rows="5" cols="50"><?php echo $breport; ?></textarea>
   				</td>
   			</tr>
  	</td>
</table>
</tr>
</table>
   			
 
<tr>
	<table align="center">
			<td  class="form" width=300 align="center"><?php $currentFile = $_SERVER["SCRIPT_NAME"];
  								$parts = Explode('/', $currentFile);?>
  							
				<input type="button" value="Restart" onClick="parent.location='<?php echo $parts[count($parts) - 1];?>'"> 
					<input type="submit" value="Build Defense Request"></td>
	</table>
</tr>

	
</tr>
</form>

<?php
if(isset($_POST['user']))
{
?>

<table border="3" align="center" cellpadding="3" cellspacing="0">
	<tr>
		<h5 align=center class="header">Subject Line: <?php echo $user . " - " . $usercity ." (". $user_xcord ."|". $user_ycord .") ". $attacktype ." by ". $attname ." - ". $attcity ." (". $att_xcord ."|". $att_ycord .") of ". $alliance; ?></h5>
	</tr>
	<tr>
			<td  class="output">
			<?php echo $user ."'s village ".  $usercity ." (". $user_xcord ."|". $user_ycord .") <br>";
			echo $attacktype ." by ". $attname ." - ". $attcity ." (". $att_xcord ."|". $att_ycord .") of ". $alliance. "<br>";
			echo "Attack(s) land at " . $landtime . " (or battle report posted below.) <br>";
			echo $breport."<br>";
			echo "Granary Information (". $wheat ."/". $grancap .") <br>";
			echo "Wall level: " . $wall ."<br>";
			if(empty($sitter1) and empty($sitter2)){}
			else{echo "Sitters: " . $sitter1 .", " . $sitter2;}
			echo "Division " . $division ."<br>";
			echo "Platoon " . $platoon ."<br>";
			echo "Current Defenses: " . $curD."<br>";
			$distance = sqrt((($user_xcord - $att_xcord)*($user_xcord - $att_xcord))+(($user_ycord - $att_ycord)*($user_ycord - $att_ycord)));

			echo "Distance From " . " (". $att_xcord ."|". $att_ycord .") to (". $user_xcord ."|". $user_ycord .") =". $distance."<br>";
			$tsquare =0;
			$unitspeed = 3;
			if($distance < 30) {$triptime = $distance / $unitspeed; }
			else{
				$triptime = ((30 / $unitspeed)+(($distance-30) / ($unitspeed*(1+($tsquare/10)))));}
					          
			          					
	   		echo "Travel time by catapult from " . "(" . $att_xcord ."|". $att_ycord . ") to (" . $user_xcord . "|" . $user_ycord . ") is " . $triptime."<br>";	
	   		$unix_launchtime = strtotime($landtime) - (60 * $triptime * 60);
	   		echo "Launch time if catapults are present: " . date("m/d @ H:i:s", $unix_launchtime);
}?>
	   		</td>
	   		</tr>
	   		</table>
	   		</body>
	   		</html>
 
well this won;t fly
Code:
if(empty($sitter1) and empty($sitter2))
it should be
Code:
if(empty($sitter1) && empty($sitter2))
 
well this won;t fly
Code:
if(empty($sitter1) and empty($sitter2))
it should be
Code:
if(empty($sitter1) && empty($sitter2))

I will def change it, but oddly enough, it did work the first few times I tested it.

Im not a programmer by any means (everything I wrote above I've learned since monday (even the html) so I really appreciate all the tips.
 
Dyslexic_Dog,

One fo your formatting changes has resolved my issue, I thank you very much indeed sir!

Thanks to everyone else for their reccomendations which are also greatly appreciated!
 
Back
Top