count and give a total of a specific output

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

Re: count and give a total of a specific output

Post by jonnyfortis »

Celauran wrote:$options is an array and will always evaluate to true, so you've created an infinite loop. What I suspect you want is closer to this:

Code: Select all

mysql_select_db($database_hostprop, $hostprop);
$query_rsPayment = "SELECT * FROM host_payments2014, plus_signupComplete, host_editprop2015, hostStudentProperties WHERE plus_signupComplete.studentID = host_payments2014.id AND plus_signupComplete.studentID = host_payments2014.id AND host_editprop2015.prop_id = plus_signupComplete.prop_id AND host_payments2014.payment_paid_timestamp NOT LIKE '%2012%' AND host_payments2014.payment_paid_timestamp NOT LIKE '%2011%' AND hostStudentProperties.propID = plus_signupComplete.propFull ORDER BY host_payments2014.payment_id DESC";
$rsPayment = mysql_query($query_rsPayment, $hostprop) or die(mysql_error());
$totalRows_rsPayment = mysql_num_rows($rsPayment);

$options = array(
    'Option 1: Balance Before' => 0,
    'Option 2: Balance Before' => 0,
    'Option 3: Balance Before' => 0,
    'Option 4: Final Payment'  => 0,
);

while ($row_rsPayment = mysql_fetch_assoc($rsPayment)) {
   
    if (array_key_exists($row_rsPayment['payment_type'], $options)) {
        $options[$row_rsPayment['payment_type']]++;
    }
   
}

print_r($options);
i have tried to extract the array into usable seperate variables

Code: Select all

extract($options);
echo $balances = explode (" ", $options);
echo $balances[0];
echo $balances[1];
echo $balances[2];
echo $balances[3];
but this is not giving me an output
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: count and give a total of a specific output

Post by Celauran »

Not really sure what you're trying to do. You've already got an array of keys and values. What are you trying to accomplish?
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

Re: count and give a total of a specific output

Post by jonnyfortis »

Celauran wrote:Not really sure what you're trying to do. You've already got an array of keys and values. What are you trying to accomplish?
i want to be able to use the figures that are outputted in the array separately. i want to turn each of the results into a variable that i can use in different equations for example
these are my results
Array ( [Option 1: Balance Before] => 19 [Option 2: Balance Before] => 34 [Option 3: Balance Before] => 15 [Option 4: Final Payment] => 9 )

i want to be able to multiple them 19+34+15 =

o just have the numbers and seperate variables
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

Re: count and give a total of a specific output

Post by jonnyfortis »

jonnyfortis wrote:
Celauran wrote:Not really sure what you're trying to do. You've already got an array of keys and values. What are you trying to accomplish?
i want to be able to use the figures that are outputted in the array separately. i want to turn each of the results into a variable that i can use in different equations for example
these are my results
Array ( [Option 1: Balance Before] => 19 [Option 2: Balance Before] => 34 [Option 3: Balance Before] => 15 [Option 4: Final Payment] => 9 )

i want to be able to multiple them 19+34+15 =

o just have the numbers and seperate variables
sorted it thanks

$i = 0;

foreach ($options as $key => $value){

$variable[$i] = "$value";

$i++;
}

$balanceTotal = $variable[0] + $variable[1] + $variable[2] + $variable[3];
Post Reply