3 Replies - 404 Views - Last Post: 25 May 2009 - 05:11 AM Rate Topic: -----

#1 ghqwerty  Icon User is offline

  • if($spareTime > 0){ $this->writeCode(); }
  • member icon

Reputation: 43
  • View blog
  • Posts: 889
  • Joined: 08-August 08

executing wrong if statement

Posted 25 May 2009 - 04:58 AM

ok so i have an if statement inside a function

	function get_troops(){//trains troops from user input in show barracks
		$troop = $this->troop_types[$this->fort['tech_age']];
		$troops = $this->troops_plural[$this->fort['tech_age']];
		for($x=0; $x<6; $x++){			
			if(isset($_POST[$troop[$x]])){
				$quantity = $_POST[$this->db_troops[$x].'buy'];
				if(($this->row['money'] -= ($this->troop_cost[$x] * $quantity)) < 0){
					echo "Insufficient gold available.";
				}else{
					$this->fort[$this->db_troops[$x]] += $quantity;				
					$query = "update forts set ".$this->db_troops[$x]." = ".$this->fort[$this->db_troops[$x]]." where fortid = ".$this->fort['fortid']."";				
					$this->row['money'] -= $this->troop_cost[$x] * $quantity;
					$dbdata = mysql_query($query, $this->dblink) or die(mysql_error());		
					echo "You successfully bought $quantity ".$troops[$x].".";
					$this->update_table(0);
				}
			}
		}	
	}


however whenever i try and buy something that costs more than i have it sayd insufficient monety available and doesnt give me the troops however still deducts the money


this also happens here ->

	function upgrade_warehouses(){// increase resource limits
		for($x=0; $x<10; $x++){
			if(isset($_POST[$this->limit[$x]])){
				if(($this->row['money'] -= $this->warehouse_cost[$x]) < 0 ){
					echo "You have insufficient gold to upgrade your warehouse.";
				}else{
					$nl = $this->fort[$this->limit_level[$x]] +1;			
					$query = "update forts set ".$this->limit[$x]." = ".$this->limit_values[$nl].", ".$this->limit_level[$x]." = $nl where fortid = ".$this->fort['fortid']."";				
					$this->row['money'] -= $this->warehouse_cost[$x];
					$dbdata = mysql_query($query, $this->dblink) or die(mysql_error());		
					echo "You successfully upgraded ".$this->resources[$x]."'s capacity.";
					$this->update_table(0);
				}
			}
		}
	}


however this does not happen in this piece of code
	function convert_metal(){//converts metal oreand wood into metal	
		for($x=7; $x<10; $x++){			
			if(isset($_POST[$this->resources[$x]])){				
				$ore = $_POST[$this->resources[$x].'amount']*2;
				$wood = $_POST[$this->resources[$x].'amount']-0;
				$metal = $_POST[$this->resources[$x].'amount']-0;				
				if(($metal+$this->fort[$this->quantity[$x]]) > $this->fort[$this->limit[$x]]){
					echo "You have insufficient capacity available to convert that much metal.";
				}elseif(($this->fort[$this->quantity[$x]]-$ore) < 0){
					echo "You have insufficient ore left for that conversion";
				}elseif(($this->fort[$this->quantity[$x]]-$wood) < 0){
					echo "You have insufficient wood left for that conversion";
				}else{	
				$this->row['money'] -= $metal * 1500;			
				$this->fort[$this->quantity[$x-6]] -= $ore;
				$this->fort['quantity_e'] -= $wood;
				$this->fort[$this->quantity[$x]] += $metal;
				$query = "update forts set quantity_e = '".$this->fort['quantity_e']."', ".$this->quantity[$x-6]." = '".$this->fort[$this->quantity[$x-6]]."', ".$this->quantity[$x]." = '".$this->fort[$this->quantity[$x]]."' where fortid = '".$this->fort['fortid']."'";
				$dbdata = mysql_query($query, $this->dblink) or die(mysql_error()."   convert");
				echo "You successfully converted $ore ".$this->resources[$x-6]." and $wood ".$this->resources[4]." into $metal ".$this->resources[$x].".";
				$this->update_table(0);
				}
			}
		}
	}


which is why im so confused and can't relli see a differance in the cde.

Is This A Good Question/Topic? 0
  • +

Replies To: executing wrong if statement

#2 Wimpy  Icon User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: executing wrong if statement

Posted 25 May 2009 - 05:05 AM

You shouldn't use this "-=" operator since it will perform the deduction! Only use "-" and it should work just fine, I think! :)

View Postghqwerty, on 25 May, 2009 - 01:58 PM, said:

ok so i have an if statement inside a function

	function get_troops(){//trains troops from user input in show barracks
		$troop = $this->troop_types[$this->fort['tech_age']];
		$troops = $this->troops_plural[$this->fort['tech_age']];
		for($x=0; $x<6; $x++){			
			if(isset($_POST[$troop[$x]])){
				$quantity = $_POST[$this->db_troops[$x].'buy'];
				if(($this->row['money'] -= ($this->troop_cost[$x] * $quantity)) < 0){
					echo "Insufficient gold available.";
				}else{
					$this->fort[$this->db_troops[$x]] += $quantity;				
					$query = "update forts set ".$this->db_troops[$x]." = ".$this->fort[$this->db_troops[$x]]." where fortid = ".$this->fort['fortid']."";				
					$this->row['money'] -= $this->troop_cost[$x] * $quantity;
					$dbdata = mysql_query($query, $this->dblink) or die(mysql_error());		
					echo "You successfully bought $quantity ".$troops[$x].".";
					$this->update_table(0);
				}
			}
		}	
	}


however whenever i try and buy something that costs more than i have it sayd insufficient monety available and doesnt give me the troops however still deducts the money


this also happens here ->

	function upgrade_warehouses(){// increase resource limits
		for($x=0; $x<10; $x++){
			if(isset($_POST[$this->limit[$x]])){
				if(($this->row['money'] -= $this->warehouse_cost[$x]) < 0 ){
					echo "You have insufficient gold to upgrade your warehouse.";
				}else{
					$nl = $this->fort[$this->limit_level[$x]] +1;			
					$query = "update forts set ".$this->limit[$x]." = ".$this->limit_values[$nl].", ".$this->limit_level[$x]." = $nl where fortid = ".$this->fort['fortid']."";				
					$this->row['money'] -= $this->warehouse_cost[$x];
					$dbdata = mysql_query($query, $this->dblink) or die(mysql_error());		
					echo "You successfully upgraded ".$this->resources[$x]."'s capacity.";
					$this->update_table(0);
				}
			}
		}
	}


however this does not happen in this piece of code
	function convert_metal(){//converts metal oreand wood into metal	
		for($x=7; $x<10; $x++){			
			if(isset($_POST[$this->resources[$x]])){				
				$ore = $_POST[$this->resources[$x].'amount']*2;
				$wood = $_POST[$this->resources[$x].'amount']-0;
				$metal = $_POST[$this->resources[$x].'amount']-0;				
				if(($metal+$this->fort[$this->quantity[$x]]) > $this->fort[$this->limit[$x]]){
					echo "You have insufficient capacity available to convert that much metal.";
				}elseif(($this->fort[$this->quantity[$x]]-$ore) < 0){
					echo "You have insufficient ore left for that conversion";
				}elseif(($this->fort[$this->quantity[$x]]-$wood) < 0){
					echo "You have insufficient wood left for that conversion";
				}else{	
				$this->row['money'] -= $metal * 1500;			
				$this->fort[$this->quantity[$x-6]] -= $ore;
				$this->fort['quantity_e'] -= $wood;
				$this->fort[$this->quantity[$x]] += $metal;
				$query = "update forts set quantity_e = '".$this->fort['quantity_e']."', ".$this->quantity[$x-6]." = '".$this->fort[$this->quantity[$x-6]]."', ".$this->quantity[$x]." = '".$this->fort[$this->quantity[$x]]."' where fortid = '".$this->fort['fortid']."'";
				$dbdata = mysql_query($query, $this->dblink) or die(mysql_error()."   convert");
				echo "You successfully converted $ore ".$this->resources[$x-6]." and $wood ".$this->resources[4]." into $metal ".$this->resources[$x].".";
				$this->update_table(0);
				}
			}
		}
	}


which is why im so confused and can't relli see a differance in the cde.

Was This Post Helpful? 1
  • +
  • -

#3 ghqwerty  Icon User is offline

  • if($spareTime > 0){ $this->writeCode(); }
  • member icon

Reputation: 43
  • View blog
  • Posts: 889
  • Joined: 08-August 08

Re: executing wrong if statement

Posted 25 May 2009 - 05:06 AM

ok thanks will test it now


-----------------

edit :

thanks mate i didnt know that before, have changed all my -= to - and it works a treat thanks

This post has been edited by ghqwerty: 25 May 2009 - 05:10 AM

Was This Post Helpful? 0
  • +
  • -

#4 Wimpy  Icon User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: executing wrong if statement

Posted 25 May 2009 - 05:11 AM

No problem, I'm just happy to help! :)

View Postghqwerty, on 25 May, 2009 - 02:06 PM, said:

ok thanks will test it now


-----------------

edit :

thanks mate i didnt know that before, have changed all my -= to - and it works a treat thanks

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1