Computer Programming web Web programming Tips



How to Easily Generate Charts from a MySQL Database Using PHP

By Sergey Skudaev


August Royalties Earned Per Day

TitleAuthorPriceDateQuantityEarning
Learn SQL By ExamplesSergey Skudaev2.9908/01/201925.98
Learn SQL By ExamplesSergey Skudaev2.9908/02/201925.98
Learn SQL By ExamplesSergey Skudaev2.9908/03/2019411.96
Learn SQL By ExamplesSergey Skudaev2.9908/04/201925.98
Learn SQL By ExamplesSergey Skudaev2.9908/05/201925.98
Learn SQL By ExamplesSergey Skudaev2.9908/06/201912.99
Learn SQL By ExamplesSergey Skudaev2.9908/07/201938.97
Learn SQL By ExamplesSergey Skudaev2.9908/08/201938.97
Learn SQL By ExamplesSergey Skudaev2.9908/09/201912.99
Learn SQL By ExamplesSergey Skudaev2.9908/10/2019411.96

August Royalties Earned Per Day Chart



When I start a new project, first, I create a database and tables. In this demo project I use MySQL. The database name is mydb. The first table name is books. It contains data about a book: title, author, cost. The primary key that identifies each record is bookid. To create a table, use the following statement:

CREATE TABLE books (
bookid INT NOT NULL AUTO_INCREMENT,
title varchar (100),
author varchar (20),
cost DECIMAL (4,2),
PRIMARY KEY (bookid)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=latin1

The bookid data type is an integer. Auto increment means that when you insert a new record, the bookid will automatically be increased by one. (AUTO_INCREMENT=1). The title column has a string data type. The varchar (100) means that the string length is limited to 100 characters. The author column has a string data type limited to 20 characters. For currency, I use a decimal data type. It is a number with 2 decimal places. Insert data in the books table with the following SQL statements:

insert into books (bookid, title, author, cost) values (1, 'Learn SQL By Examples', 'Sergey Skudaev', 2.99);
insert into books (bookid, title, author, cost) values (2, 'PHP Programming for Beginners', 'Sergey Skudaev', 2.99);
insert into books (bookid, title, author, cost) values (3, 'PHP Programming for Beginners C++ Programming By Example', 'Sergey Skudaev', 2.99);

The second table stores data about books sales. This table includes bookid from the first table to link the book data with the book sales data. The saleid is the primary key for the books_sales table. The sold_date is the date when the book was sold and the book_num field stores number of books sold on that date. The bookid is a foreign key from the books table. It allows one to determine which book was sold.

CREATE TABLE books_sales (
saleid int (11) NOT NULL AUTO_INCREMENT,
sold_date date,
book_num TINYINT,
bookid TINYINT NOT NULL,
PRIMARY KEY (saleid)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=latin1

Insert data in the books_sales table:

insert into books_sales (saleid, sold_date, book_num, bookid) values (0, '2019-08-01', 2,1);
insert into books_sales (saleid, sold_date, book_num, bookid) values (0, '2019-08-02', 2,1);
insert into books_sales (saleid, sold_date, book_num, bookid) values (0, '2019-08-03', 4,1);
insert into books_sales (saleid, sold_date, book_num, bookid) values (0, '2019-08-04', 2,1);
insert into books_sales (saleid, sold_date, book_num, bookid) values (0, '2019-08-05', 2,1);
insert into books_sales (saleid, sold_date, book_num, bookid) values (0, '2019-08-06', 1,1);
insert into books_sales (saleid, sold_date, book_num, bookid) values (0, '2019-08-07', 3,1);
insert into books_sales (saleid, sold_date, book_num, bookid) values (0, '2019-08-08', 3,1);
insert into books_sales (saleid, sold_date, book_num, bookid) values (0, '2019-08-09', 1,1);
insert into books_sales (saleid, sold_date, book_num, bookid) values (0, '2019-08-10', 4,1);

To pull data from books and books_sales tables we need to create a join query:

select books.title, books.author, books_sales.sold_date, books.cost * books_sales.book_num as earning
from books inner join books_sales
where books.bookid= books_sales.bookid
and books.bookid=1 order by books_sales.sold_date;

The join query includes desired columns from the first table, the from keyword, the name of the first table, the inner join keyword, the name of the second table and where clause that includes criteria on how the record from the first table is related to the record of the second table and criteria which record to select. Then comes order by clause that tells the database how to sort records. In our case the records are sorted by sold date. The books.cost * books_sales.book_num as earning means that we want to know how much we earned in one day. For that we multiply the cost of a book by the number of books sold. The "as easrning" means that we want this result column titled "earning". If we did not write "as earning" then the column name would be left as "books.cost * books_sales.book_num"

We may make this query shorter by using a table alias. As an alias we can use any letter. For the books table, we use alias b and for the books_sales table we can use alias s:

select b.title, b.author, s.sold_date, b.cost * s.book_num as earning
from books b inner join books_sales s
where b.bookid=s.bookid and b.bookid=1
order by s.sold_date;

When we multiply cost 2.99 by the number of books sold, we will get a number with decimal places. To round the result to a whole number, we can use the CEIL SQL function: CEIL(b.cost * s.book_num).

The chart.php file PHP code pulls sales data from the MySQL database and draws a bar chart.
chart.php source code:

The display_chart.php file PHP code displays sales data in the grid (table) and displays the chart image created by the chart.php file.

display_chart.php code: