开发者

CSV Table Upload for Article

I have an articles website in PHP/MySQL. What I want to allow someone to do is to upload a CSV file a开发者_如何学编程nd have that appear as a table within the article itself. I want to avoid doing a CREATE table every time as I could easily end up with thousands of tables.

Thoughts on the best way to store table information like this efficiently and permanently?


Unless you're going to want to manipulate the data later on, it would probably be much easier just to convert the CSV file contents to a HTML table and store it as part of the article html. A simple search for csv to html table returns many, many prewritten functions, some in php; check them out.


Consider to store the csv-data simply as a file. Display the content of the csv-file as a table, when the page is rendered. Apply JavaScript/jQuery logic for better user experience (e.g. sorting, filtering).

If you do not want to prozess or join the information from the csv file, than there is no need to store it in a relational mysql table.


Creating a new database table for each CSV sounds like overkill. You should never have to modify your database schema in this way. Instead, design your schema in such a way as to allow for this kind of data to be inserted.

Here's one possible solution: You could have one table that contains:

  1. An ID which uniquely identifies the table
  2. An ID which uniquely identifies a row in that table
  3. The name of the column
  4. The value of the cell

SQL:

CREATE TABLE tables(
  table_id int,
  row_number int,
  column_name varchar(50),
  cell_value varchar(50) 
)

--row 1
INSERT INTO tables VALUES (1, 1, 'name', 'George Washington');
INSERT INTO tables VALUES (1, 1, 'dob', 'April 30, 1789');

--row 2
INSERT INTO tables VALUES (1, 2, 'name', 'John Adams');
INSERT INTO tables VALUES (1, 2, 'dob', 'March 4, 1797');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜