What's wrong with doing this??

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Matt Phelps
Forum Commoner
Posts: 82
Joined: Fri Jun 14, 2002 2:05 pm

What's wrong with doing this??

Post by Matt Phelps »

I've been fighting with a script I'm writing all day long and finally narrowed it down to a problem with one of my variables being empty for no apparent reason.

What on earth is wrong with doing this with a variable??

just in case it makes any difference, not all the $target variables have a value.

Code: Select all

<?php
		$target_num_array = array(1 => $target1, $target2, $target3, $target4, $target5, $target6, $target7, $target8, $target9, $target10,	$target11, $target12, $target13, $target14, $target15);

$target_num = '5';
			$torpedos_fired = $target_num_array[$target_num];

?>
No matter what I do I cannot get $target_num_array[$target_num] to assign it's contents to any variable. :?:
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Did you declare the $target5 var elsewhere?

I know you said that some $targetx's will be empty but, with just the code you posted, ALL the $targetx vars are empty.

You probably know print_r($target_num_array) will let you check the values in the array to help debug the script.

If I'm working on a new script it's always full of echo's and print_r's (comment them on and off) to check it's getting all the vars it expects and with correct values.
Matt Phelps
Forum Commoner
Posts: 82
Joined: Fri Jun 14, 2002 2:05 pm

Post by Matt Phelps »

Yep the $target5 does have some data in it. It gets the data from a webform.

I've been echoing out the variables all over the place but cannot make this work at all. I even tried :

Code: Select all

<?php
$anything = $target_num_array[$target_num]; 
echo "$anything";

?>
Suffice to say that $anything now contains nothing! I can use the $target_num_array[$target_num] as a variable in it's own right if I want to though.
?>
decoy1
Forum Commoner
Posts: 50
Joined: Fri Feb 21, 2003 1:33 pm
Location: St. Louis

Post by decoy1 »

Remove the '1=>' from the array too.
Matt Phelps
Forum Commoner
Posts: 82
Joined: Fri Jun 14, 2002 2:05 pm

Post by Matt Phelps »

Well I need the 1=> to assign the first variable in the array to key 1 because when I pull the variables out I am starting from 1 not zero.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Can you show me some more code?
Matt Phelps
Forum Commoner
Posts: 82
Joined: Fri Jun 14, 2002 2:05 pm

Post by Matt Phelps »

Not sure which bits you need but here is the whole thing. The conf file has all the database bits:

Code: Select all

<?php
include("conf.php");
$random_chance = '25';

	// seed with microseconds to make the random function work
function make_seed() {
    list($usec, $sec) = explode(' ', microtime());
    return (float) $sec + ((float) $usec * 100000);
	}
	srand(make_seed());


//get u-boat details
	$query = "SELECT unumber, crew_skill, fatigue, accuracy, mechanical, damage, experience, torpedos, gridletter, gridnumber, attack_scorecard, defense_scorecard  FROM myboat WHERE id = '1' ";
	$boatresults = mysql_query($query) or die ("Error in query: $query. " . mysql_error());

	// if a result is returned
	if (mysql_num_rows($boatresults) > 0)
	{
	// turn it into an object
	$boat = mysql_fetch_object($boatresults);
	}

		$convoy_array = explode('|', "$boat->attack_scorecard");
		$escort_array = explode('|', "$boat->defense_scorecard");

if ($attack)
	{
	$num_torpedos_fired = ($target1 + $target2 + $target3 + $target4 + $target5 + $target6 + $target7 + $target8 + $target9 + $target10 + $target11 + $target12 + $target13 + $target14 + $target15);
		if ($num_torpedos_fired > $boat->torpedos)
		{
		echo "Kapitän! We do not have enough torpedos left to make that many attacks! Click back and try again!";
		}
		else
		{
		$target_num_array = array(1 => $target1, $target2, $target3, $target4, $target5, $target6, $target7, $target8, $target9, $target10,	$target11, $target12, $target13, $target14, $target15);
		$target_name_array = array(1 => $targetname1, $targetname2, $targetname3, $targetname4, $targetname5, $targetname6, $targetname7, $targetname8, $targetname9, $targetname10, $targetname11, $targetname12, $targetname13, $targetname14, $targetname15);
		$target_tonnage_array = array(1 => $tonnage1, $tonnage2, $tonnage3, $tonnage4, $tonnage5, $tonnage6, $tonnage7, $tonnage8, $tonnage9, $tonnage10, $tonnage11, $tonnage12, $tonnage13, $tonnage14, $tonnage15);
		$target_type_array = array(1 => $type1, $type2, $type3, $type4, $type5, $type6, $type7, $type8, $type9, $type10, $type11, $type12, $type13, $type14, $type15);
		$target_nation_array = array(1 => $nation1, $nation2, $nation3, $nation4, $nation5, $nation6, $nation7, $nation8, $nation9, $nation10, $nation11, $nation12, $nation13, $nation14, $nation15);
		$target_num = '1';
		print_r($target_tonnage_array[$target_num]); 
		echo "<p><p>";
		foreach ($target_name_array As $target)// for each target or ship
			{
			$num_of_hits = 0; //resets the hit count for the next ship

			//for each torpedo fired at that ship
			$torpedos_fired = $target_num_array[$target_num];
			
					while ($torpedos_fired > '0')
					{
					$chance_percentage = ($boat->experience + $boat->accuracy + $boat->crew_skill + $boat->mechanical + $boat->fatigue + $boat->damage + $random_chance)/10;
					$random_hit_factor = rand(0, 100);
						if ($random_hit_factor <= $chance_percentage)
							{
							$num_of_hits = $num_of_hits + 1; //add one to the number of hits if successful
							}
					$torpedos_fired = $torpedos_fired - '1';  		
					}
			
					//now work out the amount of damage done to the ship//
					if ($num_of_hits > '0')
					{
					$tonnage = $target_tonnage_array[$target_num];
					//print_r($tonnage);
					
						switch ($target_tonnage_array[$target_num]) {
						case $target_tonnage_array[$target_num]>10000:
							if ($random_hit_factor > 70) //30% chance of sinking the ship
								{
								$status = 'sunk heavy';
								}
							break;
						case $target_tonnage_array[$target_num]<10000:
							if ($random_hit_factor > 5) //50% chance of sinking the ship
								{
								$status = 'sunk light';
								}
							break;
							default:
							$status = 'damaged';
							}
						}
						else
						{
						$status = 'undamaged';
						}
					
			$misses = $target_num_array[$target_num] - $num_of_hits;		
			echo "$target_num You fired <b>$target_num_array[$target_num]</b> torpedos at <i>$target</i> $tonnage. $misses torpedos missed. You got $num_of_hits hit(s). Ship is $status !<br>";
			
	//		$new_ship_data = array($target_name_array[$target_num], $target_nation_array[$target_num], $target_tonnage_array[$target_num], $target_type_array[$target_num], $status);
	//		$new_convoy_data[] = implode(',', $new_ship_data);
			$target_num++;

			}
			
			$updated_score = implode('|', $new_convoy_data);
			$query = "UPDATE myboat SET fatigue = '$fatigue', vunerability = '$vunerability', accuracy = '$accuracy', mechanical = '$mechanical', damage = '$damage', speed = '$speed', experience = '$experience', morale = '$morale', hours = '$hours_left' WHERE id = '1'";
			$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());

		}
		
		
		
	}
	else
	{		
		echo "<form method="post" action="$PHP_SELF">";
		echo "<table width=500><tr><td colspan=6>Targets</td><tr>"
			."<tr><td>Ship</td><td>Nation</td><td>Tonnage</td><td>Type</td><td>Condition</td></tr>";
			foreach ($convoy_array As $ship)
			{
			echo "<tr>";
			$ship_details = explode(',', $ship);
			$target_id++;
			$target_name = $ship_details[0];
			$target_nation = $ship_details[1];
			$target_tonnage = $ship_details[2];			
			$target_type = $ship_details[3];
			$target_condition = $ship_details[4];
			
				foreach ($ship_details As $detail)
					{
					echo "<td>$detail</td>";
					}
						if ($target_condition != "sunk")					
						{
						echo "<td><input name="target$target_id" type="text" value="0" size="2" maxlength="2"></td></tr>";
						}
						else
						{
						echo "<td></td></tr>";
						}
			
			echo "<input type="hidden" name="targetname$target_id" value="$target_name">";		
			echo "<input type="hidden" name="nation$target_id" value="$target_nation">";		
			echo "<input type="hidden" name="tonnage$target_id" value="$target_tonnage">";
			echo "<input type="hidden" name="type$target_id" value="$target_type">";		
			}
		echo "<tr><td colspan=6><center><input type="submit" name="attack" value="Fire Torpedos!"></center></td><tr></table></form>";	
	}
?>
?>
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

I was looking for the pathway from the form (where the $target vars are posted) to this script, where $target_num_array is declared.

Are all the $target vars present and correct in conf.php? If so, maybe it's a scope problem. If the vars are declared inside functions you'll need to return them and then get the value into this script with:

$targetX = functionName(arg's);
Matt Phelps
Forum Commoner
Posts: 82
Joined: Fri Jun 14, 2002 2:05 pm

Post by Matt Phelps »

The $target vars are sent from the form at the bottom of the script. This bit:

Code: Select all

echo "<form method="post" action="$PHP_SELF">"; 
      echo "<table width=500><tr><td colspan=6>Targets</td><tr>" 
         ."<tr><td>Ship</td><td>Nation</td><td>Tonnage</td><td>Type</td><td>Condition</td></tr>"; 
         foreach ($convoy_array As $ship) 
         { 
         echo "<tr>"; 
         $ship_details = explode(',', $ship); 
         $target_id++; 
         $target_name = $ship_details[0]; 
         $target_nation = $ship_details[1]; 
         $target_tonnage = $ship_details[2];          
         $target_type = $ship_details[3]; 
         $target_condition = $ship_details[4]; 
          
            foreach ($ship_details As $detail) 
               { 
               echo "<td>$detail</td>"; 
               } 
                  if ($target_condition != "sunk")                
                  { 
                  echo "<td><input name="target$target_id" type="text" value="0" size="2" maxlength="2"></td></tr>"; 
                  } 
                  else 
                  { 
                  echo "<td></td></tr>"; 
                  } 
          
         echo "<input type="hidden" name="targetname$target_id" value="$target_name">";       
         echo "<input type="hidden" name="nation$target_id" value="$target_nation">";       
         echo "<input type="hidden" name="tonnage$target_id" value="$target_tonnage">"; 
         echo "<input type="hidden" name="type$target_id" value="$target_type">";       
         } 
      echo "<tr><td colspan=6><center><input type="submit" name="attack" value="Fire Torpedos!"></center></td><tr></table></form>";
decoy1
Forum Commoner
Posts: 50
Joined: Fri Feb 21, 2003 1:33 pm
Location: St. Louis

Post by decoy1 »

I'm a bit confused about the form, what is the html source look like?

The user types in the value of the target?

Have you checked the value of $target_id itself?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

register_globals is enabled (somwhow, either by a php-version old enough or enabling the feature)?
$target_id++;
is this used somewhere else and not re-initialized?

I'd take a look at the form's code as seen by the client first and then echo all POSTed parameters via

Code: Select all

<?php echo '<pre>', print_r($HTTP_POST_VARS); echo '</pre>'; ?>
near the top of the script.

if $target_condition is equal to "sunk" there is no target$target_id at all, how do you handle this situation?
Matt Phelps
Forum Commoner
Posts: 82
Joined: Fri Jun 14, 2002 2:05 pm

Post by Matt Phelps »

register_globals is on. That's how it is on my host server and so that's how I set it up on my localhost for developing the code.

I echoed out all the POSTed parameters and had a look. Nothing unusual there. All data as expected and required.

As far as $target_condition being "sunk" - if that is the case then the form doesn't have a box to set the $target data.

Code: Select all

if ($target_condition != "sunk")					
						{
						echo "<td><input name="target$target_id" type="text" value="0" size="2" maxlength="2"></td></tr>";
						}
						else
						{
						echo "<td></td></tr>";
						}
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

OK - I didn't look at that carefully enough. Maybe I'm still not looking carefully but where do you declare vars for the $_POST values which you submitted back to this script?

Try something like this:

foreach ($_POST as $key=>$value) {
$$key = trim($value);
}

That should declare the lot in one go, with a bit of trimming to get rid of any empty spaces.

Also, when I was starting out (not so long ago) I used to get a lot of problems mixing up strings and vars like this: "...target$target_id..". They all went away when I started separating strings from vars like this: "...target" . $targetid . "..etc.
Matt Phelps
Forum Commoner
Posts: 82
Joined: Fri Jun 14, 2002 2:05 pm

Post by Matt Phelps »

Do I need to declare those vars?

I seem to be getting the variables I'm POSTing back to the script through okay. If I post $target1 to the script and then start using it it seems to contain what I sent from the form data.
Post Reply