zidsal Posted December 24, 2011 Share Posted December 24, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/253807-delay-before-updating-data/ Share on other sites More sharing options...
shaiang Posted December 25, 2011 Share Posted December 25, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/253807-delay-before-updating-data/#findComment-1301254 Share on other sites More sharing options...
jcbones Posted December 25, 2011 Share Posted December 25, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/253807-delay-before-updating-data/#findComment-1301259 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.