gurpreet94 Posted October 30, 2011 Share Posted October 30, 2011 Hey guys, I've got a problem with an array sum. I tried doign array_sum(), and a few other stuff but none worked. Here's my code: $upkeepcheck = $db->query("SELECT i.itmid, i.upkeep, inv . * , u.userid, u.will, u.maxwill, u.upkeepowed, u.money, h.hPRICE, hWILL FROM items i LEFT JOIN inventory inv ON inv.inv_itemid = i.itmid LEFT JOIN users u ON inv.inv_userid = u.userid LEFT JOIN houses h ON h.hWILL = u.maxwill WHERE i.upkeep >0"); $q5 = $db->query("SELECT * FROM settings WHERE conf_name = 'upkeep'"); while($upkeep=mysql_fetch_array($upkeepcheck)) { while($upkeep1=mysql_fetch_array($q5)) { if($upkeep['money'] > $upkeep['upkeep']) { $db->query("UPDATE users SET money = money - {$upkeep['upkeep']} WHERE userid = {$upkeep['userid']}"); } else { $db->query("UPDATE users SET upkeepowed = upkeepowed + {$upkeep['upkeep']} WHERE userid = {$upkeep['userid']}"); } if($upkeep1['conf_value'] == 'Stackup') { if($upkeep['upkeepowed'] > $upkeep['hPRICE']) { $db->query("UPDATE users SET will=100,maxwill=100 WHERE userid = {$upkeep['userid']}"); $db->query("UPDATE users SET upkeepowed = 0 WHERE userid = {$upkeep['userid']}"); event_add($upkeep['userid'], "You haven't paid your upkeep, so your house has been taken away. Next time sell some weapons or earn some more money to pay your upkeep!", $c); } } else if($upkeep1['conf_value'] == 'RemoveItems') { $rem = $db->query("SELECT i.itmid, i.upkeep, inv.*, u.userid, u.upkeepowed FROM items i LEFT JOIN inventory inv ON inv.inv_itemid = i.itmid LEFT JOIN users u ON inv.inv_userid = u.userid WHERE userid = {$upkeep['userid']}"); while($remove = mysql_fetch_array($rem)) { if(isset($remove['upkeep'])) { var_dump($remove['upkeep']); $upkeep = $upkeep + $remove['upkeep']; } } } } } The part that is messed up is $upkeep = $upkeep + $remove['upkeep']; var_dump($remove['upkeep']); produces: null string '100' (length=3) which is correct, I have 4 null values and 1 value of 100. And the error I get is: Fatal error: Unsupported operand types in C:\wamp\www\pagetest.php Any ideas how to fix this? I want the $upkeep to be updated with $remove['upkeep'] if the value isn't null. Quote Link to comment https://forums.phpfreaks.com/topic/250121-problem-with-an-array-sum/ Share on other sites More sharing options...
creata.physics Posted October 30, 2011 Share Posted October 30, 2011 Looking through your code, $upkeep is an array, not an int value. So doing, $upkeep + $another_array will not work. Here you are reassigning upkeep which is an array, to an integer. if(isset($remove['upkeep'])) { var_dump($remove['upkeep']); $upkeep = $upkeep + $remove['upkeep']; } Now, if you want to count the keys in upkeep, to added to $remove['upkeep'], yo can do: $_upkeep = count($upkeep) + $remove['upkeep']; So I understand what you're wanting, but not exactly what to do with $upkeep, is there a certain key that has a value you want to add to $remove['upkeep']? Or are you wanting all keys from $upkeep to be added to $remove['upkeep']? Quote Link to comment https://forums.phpfreaks.com/topic/250121-problem-with-an-array-sum/#findComment-1283503 Share on other sites More sharing options...
gurpreet94 Posted October 30, 2011 Author Share Posted October 30, 2011 Looking through your code, $upkeep is an array, not an int value. So doing, $upkeep + $another_array will not work. Here you are reassigning upkeep which is an array, to an integer. if(isset($remove['upkeep'])) { var_dump($remove['upkeep']); $upkeep = $upkeep + $remove['upkeep']; } Now, if you want to count the keys in upkeep, to added to $remove['upkeep'], yo can do: $_upkeep = count($upkeep) + $remove['upkeep']; So I understand what you're wanting, but not exactly what to do with $upkeep, is there a certain key that has a value you want to add to $remove['upkeep']? Or are you wanting all keys from $upkeep to be added to $remove['upkeep']? What do you mean by keys? I am trying to get every row of upkeep that a certain user has, and then store them in $upkeep. For example if my userid is 1, and I have 5 upkeep records of 1000 each. My $upkeep is 5000, which I'll be using later in the script. This is to let users know their total upkeep cost. Edit: I tried using count($upkeep) and echoing $upkeep, and it returns the value 126. However, I ran the query $rem in PHPmyadmin, and it should equal to 100. Here is what the query returns: http://i40.tinypic.com/28ko5zo.png Quote Link to comment https://forums.phpfreaks.com/topic/250121-problem-with-an-array-sum/#findComment-1283505 Share on other sites More sharing options...
gurpreet94 Posted October 30, 2011 Author Share Posted October 30, 2011 Sorry for the double post but I though I'd clarify a bit more. What I'm trying to achieve is basically: $upkeep += $remove['upkeep']; in a while loop. But that produces errors. Quote Link to comment https://forums.phpfreaks.com/topic/250121-problem-with-an-array-sum/#findComment-1283509 Share on other sites More sharing options...
creata.physics Posted October 30, 2011 Share Posted October 30, 2011 Is it just the error you've mentioned in the first post? Fatal error: Unsupported operand types in C:\wamp\www\pagetest.php What other types of errors do you get? Like I said, $upkeep is currently an array that has information from your database query. So doing, Array+= $numerical_value; will most definitely give you an error. I get what you're saying. 5 rows of 1,000 = 5,000. Can you try doing, $new_upkeep += $remove['upkeep'] That seems more like what you're trying to do. I don't understand why you have the first $upkeep in there. What value from $upkeep are you trying to add to the users $remove['upkeep']? Quote Link to comment https://forums.phpfreaks.com/topic/250121-problem-with-an-array-sum/#findComment-1283518 Share on other sites More sharing options...
gurpreet94 Posted October 30, 2011 Author Share Posted October 30, 2011 Yes it was just that error, but it works perfectly now. The $upkeep was just used to store the value of the total. However, would you suggest storing this new $upkeep in the DB, or just running this code on every page load? Quote Link to comment https://forums.phpfreaks.com/topic/250121-problem-with-an-array-sum/#findComment-1283526 Share on other sites More sharing options...
creata.physics Posted October 30, 2011 Share Posted October 30, 2011 For what you're trying to do it seems perfectly fine to store the upkeep inside of a variable instead of storing it within a database. I'm glad you've gotten it working properly. If you need any further explanation or have any other questions feel free to ask. For the time being will you please mark this topic as solved if not already, thanks! Quote Link to comment https://forums.phpfreaks.com/topic/250121-problem-with-an-array-sum/#findComment-1283533 Share on other sites More sharing options...
gurpreet94 Posted October 31, 2011 Author Share Posted October 31, 2011 Once again thanks a lot for this, it has been bugging me for a while! Quote Link to comment https://forums.phpfreaks.com/topic/250121-problem-with-an-array-sum/#findComment-1283640 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.