Jump to content

delay before updating data


zidsal

Recommended Posts

I am working on a php project in which players can equipt items from there inventory and it shows them there current stats. When players have decided to equipt an item they hit the submit button and the new stats should show. The issue I have is that the data is delaying in update such as:

 

we have 10 strength

we equipt a sword with +2 to strength and click submit

it displays we have 10 strength

we equipt a axe with +3 to strength and click submit

it displays 12 strength

we equipt a sword with +2 to strength and click submit

it displays 13 strength

we equipt a sword with +2 to strength and click submit

it displays 12 strength

....

 

my code is represented below

<?php
updateEquiptment();
require("playerInfo.php");
?>
<p title="This stat increases how hard you hit with weapons!">Strength:<?php echo $baseStrength +  getModStrength() ?> </p>

 

the functions updateEquiptment and getModStrength are in the playerInfo.php file and are shown like this:

 

$user = $_SESSION['username'];
$result = mysql_fetch_row(mysql_query("SELECT equiptment FROM warUsers WHERE name = '$user'"));
$equiptment	= explode(",",$result[0]);
function updateEquiptment()
	{
		global $user;
		$result  = mysql_fetch_row(mysql_query("SELECT equiptment FROM warUsers WHERE name = '$user'")) or die(mysql_error());
		global $equiptment;
		$equiptment	= explode(",",$result[0]);
	}

	//get there modified stats
	function getModStrength()
	{
		$total = 0;
		global $equiptment;
		foreach ($equiptment as $value) //loop through every item in the equiptment
		{
			if($value == 0 || $value == null) //if nothing is equipt go to the next loop
				continue;

			$result = mysql_query("SELECT * FROM items WHERE id = '$value'");
			$item = mysql_fetch_row($result);
			$total += $item[4]; //get the items strength and add it to the total
		}
		return $total;
	}

 

any help on the matter would be greatly appreciated

Link to comment
Share on other sites

Probably because you have duplication in your code.

 

$result = mysql_fetch_row(mysql_query("SELECT equiptment FROM warUsers WHERE name = '$user'"));

$equiptment = explode(",",$result[0]);

 

The $result and the $equipment should be inside your function.The $user is fine like this.

Is there a reason why you are not using mysql_fetch_array for the equipment?, it could save you a lot.

 

$equipment = array();
$query = "SELECT equiptment FROM warUsers WHERE name = '$user'";
$result = mysql_query($query);

while($row = mysql_fetch_array($result)) {
      $equipment[] = $row['item'];
}

 

Now you have an array with your items.

Link to comment
Share on other sites

I discourage the mysql_fetch_array function in favor of the mysql_fetch_row() or mysql_fetch_assoc(), depending on what you want returned.  Mysql_fetch_array() just returns both the row and the association thereby using unnecessary memory, when you plan on only using one of them anyway.

 

I also discourage Global keywords in function, pass them in the arguments, you will have less confusion and errors this way.

 

I also discourage storing items in a comma delimited manner inside a table, Mysql is a relational database, and has powerful functionality to tie information together.

You should have a 3rd table that has a many to many relationship, and ties the item to the user.

CREATE TABLE userItem (
   userID int(11),
   itemID int(11),
   itemQuantity int(3)
)

 

Then look into using the JOIN syntax.

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.