Jump to content

Select total additions in this month


Mutley

Recommended Posts

I'm trying to do a donate script that selects the total donation amount made in the current month.

 

<?php

$result = mysql_query("SELECT SUM(amount), dateline FROM forum_paymenttransaction");
while($row = mysql_fetch_array( $result ))
{
	$amount = $row['amount'];
}

?>

 

 

The above gives the total amount of money and selects the date field of when the donation was made (which is a unix timestamp) the part I'm stuck at, is how to only show the total donation for THIS present month.

 

Any ideas?

 

Thanks in advance.

 

Link to comment
Share on other sites

Use this (note - not tested so there may be some typos):

 

$current_date = time();
$current_month = date("m", $current_date);
$current_year = date("Y", $current_date);
switch($current_month)
{
    case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    case 12:
       $number_of_days = 31;
       break;
    case 4:
    case 6:
    case 9:
    case 11:
        $number_of_days = 30;
    case 2:
        $number_of_days = (date("L", $current_date) == 0) ? 28 : 29;
        break;
}
$start_of_month = mktime(0, 0, 0, $current_month, 1, $current_year);
$end_of_month = mktime(23, 59, 59, $current_month, $number_of_days, $current_year);
$query = "SELECT SUM(amount), dateline FROM forum_paymenttransaction WHERE date >= {$start_of_month} AND date <= {$end_of_month}";

Link to comment
Share on other sites

Thanks a lot Haku, I tweaked it to echo the result but get an error (I think your code is correct though, just bad SQL?):

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in [path]/modules/donations.php on line 30

 

<?php
$current_date = time();
$current_month = date("m", $current_date);
$current_year = date("Y", $current_date);
switch($current_month)
{
	case 1:
	case 3:
	case 5:
	case 7:
	case 8:
	case 10:
	case 12:
	   $number_of_days = 31;
	   break;
	case 4:
	case 6:
	case 9:
	case 11:
		$number_of_days = 30;
	case 2:
		$number_of_days = (date("L", $current_date) == 0) ? 28 : 29;
		break;
}
$start_of_month = mktime(0, 0, 0, $current_month, 1, $current_year);
$end_of_month = mktime(23, 59, 59, $current_month, $number_of_days, $current_year);
	$result = mysql_query("SELECT SUM(amount), dateline FROM forum_paymenttransaction WHERE date >= {$start_of_month} AND date <= {$end_of_month}");
	while($row = mysql_fetch_array( $result ))
	{
		$display = $row['amount'];
		echo $display;
	}
?>

 

Link to comment
Share on other sites

I kind of thought that may be illegal, but I'd never tried it. You can't do what you want in 1 query. Use this:

 

$result 1= mysql_query("SELECT SUM(amount) FROM forum_paymenttransaction WHERE date >= {$start_of_month} AND date <= {$end_of_month}") or die(mysql_error());
$result2 = mysql_query("SELECT dateline FROM forum_paymenttransaction WHERE dateline >= {$start_of_month} AND dateline <= {$end_of_month}") or die(mysql_error());

Link to comment
Share on other sites

Try this query (and forget that horrendous php code to come up with a start and end unix timestamp) -

 

$result = mysql_query("SELECT SUM(amount), dateline, FROM_UNIXTIME(dateline) as stddate
FROM forum_paymenttransaction
WHERE MONTH(stddate) = MONTH(CURDATE()) AND YEAR(stddate) = YEAR(CURDATE()) GROUP BY stddate");

Link to comment
Share on other sites

Don't understand what you mean, should I do 2 queries?

 

Yes, using the two queries I gave you.

 

Although what the guy above me (who is still obviously pissed off that I was right last night) may serve you better. Try it out first.

Link to comment
Share on other sites

Try this query (and forget that horrendous php code to come up with a start and end unix timestamp) -

 

$result = mysql_query("SELECT SUM(amount), dateline, FROM_UNIXTIME(dateline) as stddate
FROM forum_paymenttransaction
WHERE MONTH(stddate) = MONTH(CURDATE()) AND YEAR(stddate) = YEAR(CURDATE()) GROUP BY stddate");

 

Thanks, nice and compact, although:

 

Unknown column 'stddate' in 'where clause'

Link to comment
Share on other sites

Like the error states, it does not like the alias name in the WHERE clause.  ;D

 

Do this instead (tested this time to make sure the syntax works) -

 

$query = "SELECT SUM(amount), dateline FROM forum_paymenttransaction
WHERE MONTH(FROM_UNIXTIME(dateline)) = MONTH(CURDATE()) AND
YEAR(FROM_UNIXTIME(dateline)) = YEAR(CURDATE()) GROUP BY MONTH(FROM_UNIXTIME(dateline))";

Link to comment
Share on other sites

Hmm, I can't get the die(mysql_error()) to work but I have an error still:

 

 

$result = "SELECT SUM(amount), dateline FROM forum_paymenttransaction
WHERE MONTH(FROM_UNIXTIME(dateline)) = MONTH(CURDATE()) AND
YEAR(FROM_UNIXTIME(dateline)) = YEAR(CURDATE()) GROUP BY MONTH(FROM_UNIXTIME(dateline))" or die(mysql_error());
while($row = mysql_fetch_array( $result ))
	{
		$display = $row['amount'];
		echo $display;
	}

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in [path]/modules/donations.php on line 7

 

Thanks so far! :)

Link to comment
Share on other sites

Okay, the SQL works and displays the correct amount in the database but the script displays "Resource id #29", no errors, so it must be working.

 

Can't understand why it isn't displaying the correct amount, no idea what "Resource id #29" means?

 

 

$query = "SELECT SUM(amount), dateline FROM forum_paymenttransaction
WHERE MONTH(FROM_UNIXTIME(dateline)) = MONTH(CURDATE()) AND
YEAR(FROM_UNIXTIME(dateline)) = YEAR(CURDATE()) GROUP BY MONTH(FROM_UNIXTIME(dateline))" or die(mysql_error());

$result = mysql_query($query) or die("Query failed: " . mysql_error());
echo $result;

Link to comment
Share on other sites

Thanks Andy, although the Resource message has gone it is just blank now, no errors or any text.

 

 

<?php
$query = "SELECT SUM(amount), dateline FROM forum_paymenttransaction
WHERE MONTH(FROM_UNIXTIME(dateline)) = MONTH(CURDATE()) AND
YEAR(FROM_UNIXTIME(dateline)) = YEAR(CURDATE()) GROUP BY MONTH(FROM_UNIXTIME(dateline))" or die(mysql_error());

$result = mysql_query($query) or die("Query failed: " . mysql_error());
$row = mysql_fetch_array($result);
echo $row['amount'];
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.