The KoolReport Blog

Announcements, discussions, and more for KoolReport and its extended packages.

How to create charts using PHP and MySQL database

As a budding developer, you may find yourself grappling with the intricacies of visualizing data from databases like MySQL in captivating charts and graphs. Fear not, for I am here to guide you through the process with the help of KoolReport—a powerful PHP framework that will revolutionize your data visualization journey and simplify your development workflow.

Traditionally, achieving data visualization involved a series of complex steps. You would need to harness the power of PHP to query your database, transform the retrieved data into the specific format required by the chosen charting library, export it as JSON, and then write client-side JavaScript code to render the charts. This process not only consumed a significant amount of time but also presented challenges when switching between different charting libraries.

References:

  1. AnyChart: Connecting MySQL Database to JS Chart Using PHP
  2. FusionChart: Create Interactive Charts using PHP and MySQL Database
  3. Learning Programming: Google Chart with PHP and MySQL

But behold! KoolReport is about to unveil a new world of data visualization simplicity. Developed with a laser focus on data reporting, this framework will enable you to effortlessly create visually stunning charts directly from your database, eliminating the need for tedious and error-prone transformation processes. Allow me to guide you through an example that showcases the elegance and power of KoolReport.

Imagine you wish to create a column chart to visualize the spending amount of each customer in your MySQL database. With KoolReport, accomplishing this becomes a breeze. Let's dive into the code:

<?php
use \koolreport\datasources\PdoDataSource;
use \koolreport\widgets\google\ColumnChart;

// Database connection information
$connection = array(
    "connectionString" => "mysql:host=localhost;dbname=sales",
    "username" => "root",
    "password" => "",
    "charset" => "utf8"
);

// Create a column chart to visualize the spending amount of each customer.
ColumnChart::create([
    "dataSource" => (new PdoDataSource($connection))->query("
        SELECT customerName, SUM(amount) as total
        FROM orders
        GROUP BY customerName
    ")
]);

Impressive, isn't it? With just a few lines of code, you establish a connection to your MySQL database, retrieve the desired data, and generate a beautiful column chart displaying the spending amount for each customer. The simplicity and efficiency of KoolReport allow you to focus on the bigger picture rather than being tangled in cumbersome technicalities.

Moreover, KoolReport offers a diverse range of chart types to cater to your specific needs. From fundamental line charts and bar charts to intricate pie charts, area charts, geo charts, gauge charts, histograms, and combo charts, you have the freedom to unleash the full potential of your data.

What truly sets KoolReport apart is its seamless integration with multiple charting libraries. Whether you prefer Google Charts, ChartJS, Morris Chart, D3, or new player ApexCharts, KoolReport has you covered. Thanks to its common API, transitioning between charting libraries is effortless. By merely changing the chart class, you can explore different visualization options without wasting precious time rewriting your entire transformation process.

In summary, as an aspiring developer, you now possess a powerful tool—KoolReport—to simplify your data visualization endeavors. This framework empowers you to streamline your development process, save invaluable time, and focus on creating impactful data visualizations. Say goodbye to convoluted data transformations and embrace a world where charting becomes an intuitive and seamless experience. With KoolReport by your side, you can elevate your data visualization skills and unlock a realm of possibilities.

10 PHP Libraries That You Should Not Miss

It has been 25 years since the first version of PHP in 1995. Despite of its age, PHP is still one of the most programming languages in the world and be the top choice of many back-end developers. Let look at 10 PHP libraries that you need to know.

Most Used PHP Libraries

1. PHP-ML

https://github.com/php-ai/php-ml

A new way to approach Machine Learning (ML) in PHP, PHP-ML is an interesting library to work with. It provides you an easy way to use API training your bot to do prediction based on your inputs. It contains a series of algorithms to recognize patterns and performs complicated statistics.

PHP Machine Learning

2. Opauth

https://github.com/opauth/opauth

This library allows users to authenticate via their personal account on social networks or other services. Of course, it includes all the big names: Google, Facebook, Twitter, Github, Instagram, LinkedIn.

PHP Authentication

Opauth is supported by many PHP Frameworks, it is easily to be integrated in almost all current PHP applications such as: vanilla (plain) PHP applications, CakePHP, CodeIgniter, FuelPHP, Laravel, PrestaShop, and many more.

3. PHPMailer

https://github.com/PHPMailer/PHPMailer

No PHP library collections is perfect without PHPMailer - an complete email library for PHP. This project is supported by large community and being used in very popular applications such as Wordpress and Drupal. That makes it become first choice for sending email in PHP. It supports SMTP, HTML email and more..

PHPMailer

4. ImageWorkshop

http://phpimageworkshop.com/

ImageWorkshop is open-source PHP library allowing you to work with image layers. With it, you can easily change image size, crop, create thumbnail, adding watermark and more. Here is some example:

PHP Image Library

ImageWorkshop is developed to make your most common work on image in the easiest way. If you need more powerful library, you can look for Imagine library.

5. Carbon

https://github.com/briannesbitt/Carbon

Carbon is a great open source library to deal with datetime with various class and methods. For example:

PHP Datetime library

6. Ubench

https://github.com/devster/ubench

Ubench is a small library to check performance of your PHP code. It measures your code execution time as well as the needed memory. Using Ubench during your application development is a good idea as it can help to analyze, detect and enhance your code quality, ultimately improve the performance of your application.

Ubench

7. Faker

Faker is a PHP library to create fake data. It is very convenient and easy to use when you need an experiment database or sample data for your web application. Let look at some sample code:

PHP Fake Data

Every time you call properties of the object, a new random data will be generated.

8. Omnipay

https://github.com/adrianmacneil/omnipay

Omnipay is a payment library for PHP. It has a clear and coherent API and support various payment gateway. With this library, we only need to work with one API and work with different payment options. Because of using single unified API, we can easily use many gateways at same time or switch between them when needed.

PHP Omni payment

9. PHPGeo

https://github.com/mjaschen/phpgeo

PHPGeo is an simple library that allows us to calculate geographical distance between coordinates with high precision. This library support well those applications working based on geo locations. In order to get the coordinates, you can use location api of HTML5 or Yahoo api.

PHP Geo Library

10. KoolReport's Google Chart

https://github.com/koolreport/core

KoolReport is an good open source PHP library to provide reporting capability for your application. Google Charts is part of KoolReport can bring you good and free php chart solution. It provides more than 20 most common & advanced charts. The code is very simple to use:

ColumnChart::create([
    "dataSource"=>[
        ["category","amount"],
        ["Computer",100],
        ["Laptop",50],
        ["Tablet",80],
        ["Phones",150],    
    ]
]);

You may view more examples

Conclusion

Above are 10 most popular libraries that you can use to power your php application. Hope that our share is useful.

<3 koolreport team


KoolReport helps to analyze your data and ultimately turn them into visual reports or dynamic dashboards.

"KoolReport helps me very much in creating data report for my corporate! Keep up your good work!"

Alain Melsens

"The first use of your product. I was impressed by its easiness and powerfulness. This product is a great product and amazing."

Dr. Lew Choy Onn

"Fantastic framework for reporting!"

Greg Schneider
Get KoolReport Now, It's FREE!