Jump to content

Problem with an array sum


gurpreet94

Recommended Posts

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.

Link to comment
Share on other sites

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']?

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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']?

 

 

 

 

 

Link to comment
Share on other sites

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!

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.