Page 1 of 1

Adding every number of a specific together by user.

Posted: Thu Oct 08, 2009 6:12 am
by Stuffs
Hello everyone,

I'm sorry about the title. I would also like to apologize in advance for my newbieness and the rest of this badly-written post. This has been driving me crazy for about twenty hours straight and my mind is too scattered and sleep-deprived to make this post readable. I'll try, though. Here's the situation:

I have a referral system on my website. There's also a few different numerical values I assign to each member in a table called sup_numbers.

I want to find the total value of each of the specific numbers assigned to each user on the table (let's call this value sys1_num) that was referred by the person viewing the page.

Sorry, that didn't make any sense, did it?

Here's an example:

Jim refers Jen, Sally, Susie, and Kenny. Jen's value A=5, Sally's A=9, Susie's A=8, and Kenny's A=23.

Value B is supposed to be the total value of the referral's A values, so in Jim's case it would be Jen's A+Sally's A+Susie's A+Kenny's A, or 45 (I hope I added that correctly...)

What I need is a way to display the value B of the member viewing that page. This is what I have so far, and it worked out horrible.

Code: Select all

 
function value_b() {
global $user;
   $r__valb = db_query("SELECT w.value_a
    FROM {referral} r INNER JOIN {users} u USING(uid) INNER JOIN
    {sup_numbers} w USING(uid)
    WHERE referral_uid = %d", $user->uid);
  while ($om_valb = db_fetch_object($r__valb)) {    
    $output .= t("Value B") .": ". $om_valb;
}
}
 
I know, I know, it's horrible. I'm a total newbie at this, but I managed to get everything else working (thank Soup), except for this one thing. So does anyone know how I can add that string together? I need help; I don't even know if I used the word 'string' correctly!

Thanks in advance,
Stuffs

P.S. If this makes no ense at all, wait until tomorrow. I'll clean it all up after a good night's sleep. I'm just hoping someone could understand it all and help me out while I'm asleep! D:

Re: Adding every number of a specific together by user.

Posted: Thu Oct 08, 2009 8:03 am
by Eric!
You need to change your string value into an integer. Here is an example:

Code: Select all

$ID_user="15"; //string
$ID_age="45"; //string
$total=(int)$ID_user + (int)$ID_age; //converted to integers and added
echo $total;
Since PHP isn't strict about type casting you can do this other ways as well, if you wish.
http://www.php.net/manual/en/language.t ... ggling.php

But that is the basic idea.

Re: Adding every number of a specific together by user.

Posted: Thu Oct 08, 2009 5:55 pm
by Stuffs
I got it! Here's the end result:

Code: Select all

function wow_system2() {
  global $user;
    $r__system2 = db_query("SELECT system2_spent FROM
 
    {wow_user_gold} WHERE uid = %d", $user->uid);
    $sn__system2 = db_result(db_query("SELECT w.system1_earned FROM {referral} r INNER JOIN {users} u USING(uid) INNER JOIN {wow_user_gold} w USING(uid) WHERE referral_uid = %d AND u.status = 1", $user->uid));
      while ($om__system2 = db_fetch_object($r__system2)) {
$sys2_earned = ($sn__system2/4);        
        $gold_avail = $sys2_earned - ($om__system2->system2_spent);
I lied about the names of the tables and stuff before. This works for what I need it to do. Thanks, Eric!