Page 1 of 1

Help with functions

Posted: Thu Sep 22, 2011 7:39 am
by nite4000
I need some help with a function.

This function credits accounts earnings based ont eh plan they have chosed but what i am doing is add ing the part to chk to see if the end_date is equal to the current date if it is then return the earnings plus the orginal amount invested.

Here is the orginal function, then i will show you what the function looks like now

Code: Select all

function credit_account($account, $investment, $rate)
{
		global $debug;


	$earnings = $investment * $rate / 100;
	$query = "UPDATE accounts
			  SET acct_balance = acct_balance + {$earnings},
			      earned_total = earned_total + {$earnings}
			  WHERE id = {$account}";
	if ($debug === TRUE)
	{
		echo nl2br($query) . "<br /><br />";
	}
	else
	{
		mysql_query($query) or die(mysql_error());
	}

	
		
		
		




this is what i have now shown below.



Code: Select all

function credit_account($account, $investment, $rate)
{
	  $q = mysql_query("SELECT * FROM package_settings WHERE id='1'") or die(mysql_error());
  $pack_settings = mysql_fetch_array($q, MYSQL_ASSOC);
  @mysql_free_result($q);
$today = date("Y-m-d");


	global $debug;
if($pack_settings['return_principal']=='1')
	{
	echo 'feature is on';
	
		if($row['end_date'] == $today)
		{
			echo 'today is the end';
			//add back invested amount
$earnings = $investment * $rate / 100 + $investment;
	$query = "UPDATE accounts
			 SET acct_balance = acct_balance + {$earnings},
			      earned_total = earned_total + {$earnings}
			  WHERE id = {$account}";
		} else {


	$earnings = $investment * $rate / 100;
	$query = "UPDATE accounts
			  SET acct_balance = acct_balance + {$earnings},
			      earned_total = earned_total + {$earnings}
			  WHERE id = {$account}";
	if ($debug === TRUE)
	{
		echo nl2br($query) . "<br /><br />";
	}
	else
	{
		mysql_query($query) or die(mysql_error());
	}

	
		
		
		
		}
	
		
	}	
	
}

the problem I think or have narrowed it down to is everything is fine BUT its not chking the end date and there is a function above this one that does the following.

Code: Select all

function get_account_plans($account)
{
	
	
	$query = "SELECT inv.id, inv.username,inv.user_id,inv.plan_id,inv.plan_name, inv.total_invest, inv.rate, inv.`date`,inv.end_date,
					 plans.parent, packages.days
			  FROM investments AS inv
			  INNER JOIN plans ON inv.plan_id = plans.id
			  INNER JOIN packages ON plans.parent = packages.id
			  WHERE inv.user_id = {$account}
			    AND inv.end_date > NOW()";
	$result = mysql_query($query);

	$plans = array();
	while ($row = mysql_fetch_assoc($result))
	{
		$plans[] = $row;
	}
	
	return $plans;
}
I am not sure what var to use i know its $var['end_date'] but what var i cant figure it out . This code was done by a good friend of mine to whom i no long talk to but if someone can help me find the right var to use that would solve it.


Thanks

Re: Help with functions

Posted: Thu Sep 22, 2011 2:08 pm
by Neilos
I think that $row[] is not global so your second function that you list cannot see $row['end_date'], try something like this...

Code: Select all

$plans = get_account_plans($account);
credit_account($account, $investment, $rate, $plans);

function get_account_plans($account)
{
        
        
        $query = "SELECT inv.id, inv.username,inv.user_id,inv.plan_id,inv.plan_name, inv.total_invest, inv.rate, inv.`date`,inv.end_date,
                                         plans.parent, packages.days
                          FROM investments AS inv
                          INNER JOIN plans ON inv.plan_id = plans.id
                          INNER JOIN packages ON plans.parent = packages.id
                          WHERE inv.user_id = {$account}
                            AND inv.end_date > NOW()";
        $result = mysql_query($query);

        $plans = array();
        while ($row = mysql_fetch_assoc($result))
        {
                $plans[] = $row;
        }
        
        return $plans;
}

function credit_account($account, $investment, $rate, $plans)
{
          $q = mysql_query("SELECT * FROM package_settings WHERE id='1'") or die(mysql_error());
  $pack_settings = mysql_fetch_array($q, MYSQL_ASSOC);
  @mysql_free_result($q);
$today = date("Y-m-d");


        global $debug;
if($pack_settings['return_principal']=='1')
        {
        echo 'feature is on';
        
                if($plans['end_date'] == $today)
                {
                        echo 'today is the end';
                        //add back invested amount
$earnings = $investment * $rate / 100 + $investment;
        $query = "UPDATE accounts
                         SET acct_balance = acct_balance + {$earnings},
                              earned_total = earned_total + {$earnings}
                          WHERE id = {$account}";
                } else {


        $earnings = $investment * $rate / 100;
        $query = "UPDATE accounts
                          SET acct_balance = acct_balance + {$earnings},
                              earned_total = earned_total + {$earnings}
                          WHERE id = {$account}";
        if ($debug === TRUE)
        {
                echo nl2br($query) . "<br /><br />";
        }
        else
        {
                mysql_query($query) or die(mysql_error());
        }

        
                
                
                
                }
        
                
        }       
        
}