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

count and give a total of a specific output

Post by jonnyfortis »

I have a column in the database that shows either

Option 1: Balance Before
Option 2: Balance Before
Option 3: Balance Before
Option 4: Final Payment

the variables are

Code: Select all

$row_rsPayment['payment_type'] == 'Option 1: Balance Before');
$row_rsPayment['payment_type'] == 'Option 2: Balance Before');
$row_rsPayment['payment_type'] == 'Option 3: Balance Before');
$row_rsPayment['payment_type'] == 'Option 4: Final Payment');
?>
i need to count and give a total of each time each one of these shows in the column
i have tried
<?php $option_one_Balance_before = count($row_rsPayment['payment_type'] == 'Option 2: Balance Before');
echo $option_one_Balance_before;

but am getting the output

Code: Select all

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
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 »

Code: Select all

$row_rsPayment['payment_type'] == 'Option 2: Balance Before'
is a boolean expression.

Code: Select all

echo count(true);
// 1
echo count(false);
// 1
You need to either tally them as you iterate over the results and display the tallies at the end, or you need to use SUM in your query.
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:

Code: Select all

$row_rsPayment['payment_type'] == 'Option 2: Balance Before'
is a boolean expression.

Code: Select all

echo count(true);
// 1
echo count(false);
// 1
You need to either tally them as you iterate over the results and display the tallies at the end, or you need to use SUM in your query.
so basically its echoing out all the individual results?

so i need to count all the 1's?
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:

Code: Select all

$row_rsPayment['payment_type'] == 'Option 2: Balance Before'
is a boolean expression.

Code: Select all

echo count(true);
// 1
echo count(false);
// 1
You need to either tally them as you iterate over the results and display the tallies at the end, or you need to use SUM in your query.
so basically its echoing out all the individual results?

so i need to count all the 1's?

Code: Select all

	$option_one_Balance_before = count($row_rsPayment['payment_type'] == 'Option 2: Balance Before');
	$totalOptionOneBalanceBefore += $option_one_Balance_before;
	echo $totalOptionOneBalanceBefore;
?
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 »

There's no sense using count. It's meant for counting the elements in an array. That's not what you're doing.

Code: Select all

<?php

$options = [
    'Option 1: Balance Before' => 0,
    'Option 2: Balance Before' => 0,
    'Option 3: Balance Before' => 0,
    'Option 4: Final Payment'  => 0,
];

while (stuff) {
    ...
    if (array_key_exists($row_rsPayment['payment_type'], $options)) {
        $options[$row_rsPayment['payment_type']]++;
    }
    ...
}

print_r($options);
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 »

Yes, it's an array.
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:Yes, it's an array.
sorry deleted the last question by mistake.

thanks
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:Yes, it's an array.
im getting syntax errors on the brackets

[

$options = [
'Option 1: Balance Before' => 0,
'Option 2: Balance Before' => 0,
'Option 3: Balance Before' => 0,
'Option 4: Final Payment' => 0,
];

should it be like this
$options = array(
'Option 1: Balance Before' => 0,
'Option 2: Balance Before' => 0,
'Option 3: Balance Before' => 0,
'Option 4: Final Payment' => 0,
);
Last edited by jonnyfortis on Thu Oct 22, 2015 7:59 am, edited 1 time in total.
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 »

You may need to use array() instead if you're using a really old version of PHP
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:You may need to use array() instead if you're using a really old version of PHP
ok thanks
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:You may need to use array() instead if you're using a really old version of PHP
tried that and it crashed the site giving me a Resource Limit Is Reached
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 »

That doesn't sound right. Please post your code.
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:That doesn't sound right. Please post your code.

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());
$row_rsPayment = mysql_fetch_assoc($rsPayment);
$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 ($options) {
    
    if (array_key_exists($row_rsPayment['payment_type'], $options)) {
        $options[$row_rsPayment['payment_type']]++;
    }
   
}

print_r($options);
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 »

$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);
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);
thats it brilliant thanks.
Post Reply