Join 300,363 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,422 people online right now. Registration is fast and FREE... Join Now!
Fatal error: Maximum execution time of 30 seconds exceeded in /home/bwars/public_html/food.php on line 43 ok so i have this piece of code which i just made anyway i could because im quite tired and last time i tried to do something similiar it failed however now it exceeds the time limit.
now i need to improve execution time but im not relli sure WHERE i can improve it. please hep lol
CODE
<?php require("general2.php");
class food extends general{ var $foodeaten; var $foodproduced; var $differance; var $endfood; var $db_troops = array("troop_a", "troop_b", "troop_c", "troop_d", "troop_e", "troop_f", "pt1", "pt2", "pt3"); var $crop_usage = array(2, 2, 2, 4, 4, 8, 3, 3, 3); var $trooparray = array(); var $foodmin; var $foodday;
function update_o_fort(){// this is where i update the other players fort $query = "update forts set "; foreach($fort as $key => $value){ if(!is_numeric($key) && $key != "fortid") { $query .= $key." = '".$value."', "; } } $query = substr($query,0,-2)." where fortid ='".$this->ofort['fortid']."'"; mysql_query($query, $this->dblink) or die("Error updating forts error: ".mysql_error()); }
function sort_food(){ $query = "select * from forts"; $dbdata = mysql_query($query, $this->dblink); $fortsa = mysql_num_rows($dbdata); $forts = mysql_fetch_array($dbdata); for($x=0;$x<$fortsa;$x++){// $x is the player $fort = $forts[$x]; for($y=0;$y<9;$y++){ $this->trooparray[] = $fort[$this->db_troops[$y]]*$fort['tech_age'];// number of troops $this->foodmin[] = ($this->trooparray[$y]*$this->crop_usage[$y])/60;// food eaten per minute } $this->foodeaten = ceil(array_sum($this->foodmin)); $this->foodproduced = $fort['production_a']/60;//per mim $this->differance = $this->foodproduced - $this->foodeaten;//per min if($fort['quantity_a'] + $this->differance < 0){ // if food is now below 0 for($a=$fort['quantity_a'];$a<5;$a+$this->foodday){ $y = rand(0, 5); if($this->trooparray[$y] > 0){ $this->trooparray[$y]--; }else{ $y = rand(6,8); $this->trooparray[$y]--; } $this->foodday = $this->crop_usage[$y]*$fort['tech_age']*24;//hour $fort['quantity_a'] = $a; } }else{ $fort['quantity_a'] = $fort['quantity_a'] + $this->differance; } $this->update_o_fort(); } } }
session_start(); $food = new food; $food->sort_food();
Your update_o_fort() function looks funny. You have:
CODE
foreach($fort as $key => $value){
but $fort isn't defined, so I think you're causing:
CODE
$query = substr($query,0,-2)." where fortid ='".$this->ofort['fortid']."'";
to end up with: $query = " where fortid ... because $substr($query,0,-2) = ""
thanks for pointing that out (i only half changed the function) so thi is my current code
CODE
<?php require("general2.php");
class food extends general{ var $foodeaten; var $foodproduced; var $differance; var $endfood; var $db_troops = array("troop_a", "troop_b", "troop_c", "troop_d", "troop_e", "troop_f", "pt1", "pt2", "pt3"); var $crop_usage = array(2, 2, 2, 4, 4, 8, 3, 3, 3); var $trooparray = array(); var $foodmin; var $foodday;
function update_o_fort($fort){// this is where i update the other players fort $query = "update forts set "; foreach($fort as $key => $value){ if(!is_numeric($key) && $key != "fortid") { $query .= $key." = '".$value."', "; } } $query = substr($query,0,-2)." where fortid ='".$fort['fortid']."'"; mysql_query($query, $this->dblink) or die("Error updating forts error: ".mysql_error()); }
function sort_food(){ $query = "select * from forts"; $dbdata = mysql_query($query, $this->dblink); $fortsa = mysql_num_rows($dbdata); $forts = mysql_fetch_array($dbdata); for($x=0;$x<$fortsa;$x++){// $x is the player $fort = $forts[$x]; for($y=0;$y<9;$y++){ $this->trooparray[] = $fort[$this->db_troops[$y]]*$fort['tech_age'];// number of troops $this->foodmin[] = ($this->trooparray[$y]*$this->crop_usage[$y])/60;// food eaten per minute } $this->foodeaten = ceil(array_sum($this->foodmin)); $this->foodproduced = $fort['production_a']/60;//per mim $this->differance = $this->foodproduced - $this->foodeaten;//per min if($fort['quantity_a'] + $this->differance < 0){ // if food is now below 0 for($a=$fort['quantity_a'];$a<5;$a+$this->foodday){ $y = rand(0, 5); if($this->trooparray[$y] > 0){ $this->trooparray[$y]--; }else{ $y = rand(6,8); $this->trooparray[$y]--; } $this->foodday = $this->crop_usage[$y]*$fort['tech_age']*24;//hour $fort['quantity_a'] = $a; } }else{ $fort['quantity_a'] = $fort['quantity_a'] + $this->differance; } $this->update_o_fort($fort); } } }
session_start(); $food = new food; set_time_limit(60); $food->sort_food();
well what i need to do is loop through each row in my forts table of my db, then work out how much food they are left with after crop usage is calculated and then if it is negative kill troops untill it is positive and add on the amount of food for each killed soldier for 1 day.
any help with this subject ?? thanks for your help so far but i need to get this sorted to be honest.
the problem with just looping through the queris where fortid = $x is that if someone seletes a fort and makes a new one then there is a gap in the table, wehats the best way to do this
Did you actually try what I said?? If you set a time limit of 5 minutes and it still times out then it's a problem with your script, if not then there's just a hell of alot of data to work with...
Did you actually try what I said?? If you set a time limit of 5 minutes and it still times out then it's a problem with your script, if not then there's just a hell of alot of data to work with...
well i tried putting it at 1 minute however i intended it to be continuos and had to stick with every one minute so having to let it run for 5 mins would not be ideal
--------
just set timeout to 300 seconds like you recommended and i get this error
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, webmaster@blisswars.com and inform them of the time the error occurred, and anything you might have done that may have caused the error.
More information about this error may be available in the server error log.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request. Apache/2.2.11 (Unix) mod_ssl/2.2.11 OpenSSL/0.9.8i DAV/2 mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635 Server at www.blisswars.com Port 80
This post has been edited by ghqwerty: 27 Jun, 2009 - 07:47 AM