How best to store this table in MySQL and print it with PHP?

I’ve searched the web trying to find examples of how to store tabular data in MySQL and loop through the rows to assemble a table using PHP, but no luck.

Here is a sample of my Excel file that I want to insert into MySQL. I then want to display it with PHP/HTML in its tabular form.

Keep in mind I have a lot of tables exactly like this for different countries so I am looking for a good MySQL schema to efficiently store this data, then have PHP make use of it by looping through the rows and id’s to pull the correct data.

  1. How would you store this in MySQL?
  2. How would you loop through the database with PHP to build columns?

1 way is store the data in a table with columns called the headings in your image, plus add a column for the data’s country code.

this tutorial might help

Table: surgeries
Columns: type, year, numberofsurgeries

Get the data from the table ordered by type and year, and create your table. Growth rate and total row will be calculated in PHP.

I concur with Kalon also you may want to get a great book by Kevin Yank of Sitepoint Database driven website. It was a great start for me Christmas 4 years ago now.

HEre Get the book

yep, data that can be calculated from stored data should not be stored in the database itself.

another option to calculating it in the application (php or whatever) is to calculate the “column” as part of the sql query retrieving the records from the database

Yet another way to do the summaries might be to do it on the client, I’ve got column (vertical) summaries working like that in the past, but not row (horizontal) totals at the same time I must admit (though I cannot easily see what “growth rate” is deduced from)

Seeing the totals would then be dependent upon JS being turned on of course.