Baba Tech Blog

TR EN

Using PDO Sum Function (Includes morris.js)

This article is older than 2 years. Some information might be outdated.

Hello! We will discuss the use of a simple and effective function I discovered to get the sum of specific columns in a table on MySQL (phpMyAdmin). In addition, we will visualize this data using morris.js, one of my favorite chart libraries. 🎨📊

Data Collection#

The following code will get the sum of hesap_toplam and hesap_odenen columns and create a chart.

Sample Visualization#

Chart with PDO and Morris.js

Code#

Morris.Donut({
    element: 'morris-donut-chart',
    data: [
        <?php
        $kac_tane = $db->query("SELECT SUM(hesap_toplam) AS toplagel FROM hesaplar")->fetch();
        $kac_bane = $db->query("SELECT SUM(hesap_odenen) AS bulgel FROM hesaplar")->fetch();

        $toplam = $kac_tane['toplagel'];
        $cik    = $kac_bane['bulgel'];

        echo '{label: "Total Debts", value: '.$toplam.'},';
        echo '{label: "Total Payments", value: '.$cik.'}';
        ?>
    ]
});

Explanations#

  • SUM() function sums all values in a specific column.
  • fetch() method gets the result of the query and assigns it to variables.
  • Morris.js Donut chart is used to visualize the acquired data as a graph.

Result#

After running the code, you will get a chart like the following:

Morris.js Chart Example

If you have any questions, you can leave a comment! 💬😊

Webmentions

Likes

  • Loading likes...

Comments & Replies

  • Loading comments...

Comments
Reply via Bluesky

With an account on the Fediverse or Mastodon, you can respond to this post.

Copy and paste this URL into the search field of your favourite Fediverse app or the web interface of your Mastodon server.

Loading comments...