Wasn't quite sure what to do for a title on this one, anyway...

number_format(getNetDonations(date('%')) + 23.97, 2)

I'm using the above to try and pull total donations ever received on my site but here's where it gets hinky. There's safety checks in the code "or so I'm told by the author" to prevent wild cards from being used. Well, the dates in the row it's trying to pull from are in a format of date('F Y').

ex. November 2010 , September 2009 etc. etc. etc.

Well, I can't seem to pull anything but current month totals by either using

number_format(getNetDonations(date('F Y')) + 23.97, 2)

or

number_format(getNetDonations('November 2010') + 23.97, 2)

So I decided to try and go with something like below...

$date = whatever ;

number_format(getNetDonations($date) + 23.97, 2)

But I don't know what to use for the whatever... so, here's my question...

Regardless of how it is done I basically need to ignore the values in the row where the dates are stored in the named format (November 2010 etc.). So, anyone got any ideas on this. The code author has given one suggestion which I will quote below but I wouldn't know how to write said loop.

--- Begin Quote ---
It's probably not going to work with a wildcard without taking out the safety checks in the getNetDonations function, but the wildcard is % instead of * in SQL. You might need to write a loop to call the getNet. for each month from the start date until the current month.
--- End Quote ---

Dani AI

Generated

Short summary and a safe plan. is right that looping month-by-month will work, but it is brittle and slow if the table grows. The real issue is the dates are stored as human text like "November 2010", which prevents easy SQL aggregation and range queries. Two reliable approaches:

  1. Convert the column to a proper DATE (recommended)
  • Add a DATE (or YEAR+MONTH) column and populate it by converting the text with MySQL. Once stored as dates the database can SUM and index efficiently.
UPDATE donations
SET donation_month = STR_TO_DATE(date_text_column, '%M %Y')
WHERE STR_TO_DATE(date_text_column, '%M %Y') IS NOT NULL;

Then aggregate with a simple SQL SUM on the new column. See STR_TO_DATE docs for formats: STR_TO_DATE (MySQL).

  1. If schema changes are not possible, parse and sum in PHP
  • Pull the rows, parse the month name strings into DateTime objects, discard unparsable rows, and sum amounts in PHP. This keeps safety checks intact and avoids trying to feed wildcards into an existing function.
$stmt = $pdo->query("SELECT amount, date_text FROM donations");
$total = 0.0;
while ($row = $stmt->fetch()) {
    $d = DateTime::createFromFormat('F Y', trim($row['date_text']));
    if ($d !== false) {
        $total += (float)$row['amount'];
    }
}

Date parsing reference: DateTime::createFromFormat (PHP).

Practical notes: do not remove the function’s safety checks; they likely prevent SQL injection. Watch locale/month-name matching (English month names by default). If performance matters, migrate to a proper DATE column and add an index. If keeping the loop approach, iterate from the earliest parsed month to the latest using DateTime->modify('+1 month') and sum each month’s total.

I'm guessing he meant something like writing a loop that uses the below with a set start date but no real end date.

getNetDonations(date('F Y'))

And then taking all of the figures gathered and making one big total which as of right now should be like $61.90

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.