Jump to content

Make an array global available


Marcel1993

Recommended Posts

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  ;D

 

 

Link to comment
Share on other sites

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]);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.