Marcel1993 Posted December 27, 2010 Share Posted December 27, 2010 Hey, I've been developing a browsergame. In this you are able to produce something ("weed"). Now I've got some kind of trouble with the function that is responsible for updating the "weed" on the user accounts. Im going to explain, how my script is working First step: Loading the datas from "worlds". In this case id, weed_factor, weed_basis. What this is required for you will see in the next steps <?php include 'includes/settings/mysql.php'; mysql_connect($dbhost, $dbuser, $dbpass); mysql_select_db($dbbase); $time = time(); $update_weed_world_data_sql = "SELECT id, weed_factor, weed_basis FROM worlds"; $update_weed_world_data_res = mysql_query($update_weed_world_data_sql) OR DIE (mysql_error()); while($update_weed_world_data_while = mysql_fetch_assoc($update_weed_world_data_res)){ $update_weed_world_id = $update_weed_world_data_while['id']; $update_weed_world_basis[$update_weed_world_id] = $update_weed_world_data_while['weed_basis']; $update_weed_world_factor[$update_weed_world_id] = $update_weed_world_data_while['weed_factor']; global $update_weed_world_basis; } Second step: This is the function which is editing each account by the data given as parameters. function do_update_weed($profile_id,$world,$update,$level,$time){ $update_weed_period = $time - $update; $update_weed_add = ($update_weed_period / 3600 * $update_weed_world_basis[$world] * pow($update_weed_world_factor[$world],$level)) +1; echo $update_weed_world_basis[$world]; echo "<br>"; mysql_query("UPDATE profiles SET weed = weed +$update_weed_add, weed_update = $time WHERE id = $profile_id LIMIT 1"); } Note: echo $update_weed_world_basis[$world]; doesnt result an output. In step three, the accounts are loaded: function todo_update_weed($time){ $todo_update_weed_sql = "SELECT * FROM profiles WHERE weed_update < $time"; $todo_update_weed_res = mysql_query($todo_update_weed_sql) or die (mysql_error()); while($todo_update_weed_while = mysql_fetch_assoc($todo_update_weed_res)){ do_update_weed( $todo_update_weed_while['id'], $todo_update_weed_while['world_id'], $todo_update_weed_while['weed_update'], $todo_update_weed_while['level_farm'], $time ); } } And the last step is calling function "todo_update_weed" with the time. todo_update_weed($time); I script calculate the time between the last update and the current time. This make a difference in time. With this value I calculate something else. And then the update time has to be saved. The script has just working fine, when I set the variable "$update_weed_add" with a fix value. But by now the script doesn't work, because I don't know how to make the array available inside functions. I considered whether I put the database-function for the world_data inside the function that updates the profiles. But if I do this, the database will be called thousands of times... Hope anybody has a nice idea to fix the problem Quote Link to comment https://forums.phpfreaks.com/topic/222726-make-an-array-global-available/ Share on other sites More sharing options...
BlueSkyIS Posted December 27, 2010 Share Posted December 27, 2010 include the array in the function call, update function to take $update_weed_add from the parameters. todo_update_weed($time, $update_weed_add); function todo_update_weed($time, $update_weed_add) { // etc. Quote Link to comment https://forums.phpfreaks.com/topic/222726-make-an-array-global-available/#findComment-1151788 Share on other sites More sharing options...
Marcel1993 Posted December 27, 2010 Author Share Posted December 27, 2010 Thanks, but $update_weed_add has to be set inside the function. Quote Link to comment https://forums.phpfreaks.com/topic/222726-make-an-array-global-available/#findComment-1151792 Share on other sites More sharing options...
kenrbnsn Posted December 27, 2010 Share Posted December 27, 2010 Return the computed value from the function and set the value in the main code. That's how functions are supposed to be used. If you need to set more than on value, return the values in an array. Ken Quote Link to comment https://forums.phpfreaks.com/topic/222726-make-an-array-global-available/#findComment-1151795 Share on other sites More sharing options...
Marcel1993 Posted December 27, 2010 Author Share Posted December 27, 2010 Okay, I tried, but that doesn't work: <?php include 'includes/settings/mysql.php'; mysql_connect($dbhost, $dbuser, $dbpass); mysql_select_db($dbbase); $time = time(); $update_weed_world_data_sql = "SELECT id, weed_factor, weed_basis FROM worlds"; $update_weed_world_data_res = mysql_query($update_weed_world_data_sql) OR DIE (mysql_error()); while($update_weed_world_data_while = mysql_fetch_assoc($update_weed_world_data_res)){ $update_weed_world_id = $update_weed_world_data_while['id']; $update_weed_world_basis[$update_weed_world_id] = $update_weed_world_data_while['weed_basis']; $update_weed_world_factor[$update_weed_world_id] = $update_weed_world_data_while['weed_factor']; } function do_update_weed($profile_id,$world,$update,$level,$time,$ba,$fa){ $update_weed_period = $time - $update; $update_weed_add = ($update_weed_period / 3600 * $ba[$world] * pow($fa[$world],$level)) +1; echo $update_weed_world_basis[$world]*5; echo "<br>"; mysql_query("UPDATE profiles SET weed = weed +$update_weed_add, weed_update = $time WHERE id = $profile_id LIMIT 1"); } function todo_update_weed($time,$ba,$fa){ $todo_update_weed_sql = "SELECT * FROM profiles WHERE weed_update < $time"; $todo_update_weed_res = mysql_query($todo_update_weed_sql) or die (mysql_error()); while($todo_update_weed_while = mysql_fetch_assoc($todo_update_weed_res)){ do_update_weed( $todo_update_weed_while['id'], $todo_update_weed_while['world_id'], $todo_update_weed_while['weed_update'], $todo_update_weed_while['level_farm'], $time, $ba, $fa ); } } todo_update_weed($time, $update_weed_world_basis[$update_weed_world_id], $update_weed_world_factor[$update_weed_world_id]); Quote Link to comment https://forums.phpfreaks.com/topic/222726-make-an-array-global-available/#findComment-1151798 Share on other sites More sharing options...
Marcel1993 Posted December 27, 2010 Author Share Posted December 27, 2010 Sorry, there were little mistakes in. I corrected these and now I think it is working :-) Thanks for helping! I try this script seriously and then I post again Quote Link to comment https://forums.phpfreaks.com/topic/222726-make-an-array-global-available/#findComment-1151799 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.