Page 1 of 2
count and give a total of a specific output
Posted: Thu Oct 22, 2015 5:23 am
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
Re: count and give a total of a specific output
Posted: Thu Oct 22, 2015 6:26 am
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.
Re: count and give a total of a specific output
Posted: Thu Oct 22, 2015 6:42 am
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?
Re: count and give a total of a specific output
Posted: Thu Oct 22, 2015 6:47 am
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;
?
Re: count and give a total of a specific output
Posted: Thu Oct 22, 2015 7:26 am
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);
Re: count and give a total of a specific output
Posted: Thu Oct 22, 2015 7:50 am
by Celauran
Yes, it's an array.
Re: count and give a total of a specific output
Posted: Thu Oct 22, 2015 7:52 am
by jonnyfortis
Celauran wrote:Yes, it's an array.
sorry deleted the last question by mistake.
thanks
Re: count and give a total of a specific output
Posted: Thu Oct 22, 2015 7:54 am
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,
);
Re: count and give a total of a specific output
Posted: Thu Oct 22, 2015 7:58 am
by Celauran
You may need to use array() instead if you're using a really old version of PHP
Re: count and give a total of a specific output
Posted: Thu Oct 22, 2015 8:01 am
by jonnyfortis
Celauran wrote:You may need to use array() instead if you're using a really old version of PHP
ok thanks
Re: count and give a total of a specific output
Posted: Thu Oct 22, 2015 8:14 am
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
Re: count and give a total of a specific output
Posted: Thu Oct 22, 2015 8:17 am
by Celauran
That doesn't sound right. Please post your code.
Re: count and give a total of a specific output
Posted: Thu Oct 22, 2015 8:25 am
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);
Re: count and give a total of a specific output
Posted: Thu Oct 22, 2015 8:29 am
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);
Re: count and give a total of a specific output
Posted: Thu Oct 22, 2015 8:33 am
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.