Il mio problema è molto ampio e non sapendo dove aprire la discussione ho deciso di aprirla in "programmazione", spero di aver fatto la scelta giusta. Detto questo, vi presento il mio problema.
Su internet ho trovato questo codice
index.html
Codice PHP:
<html>
<head>
<meta http-equiv="Content-Language" content="it">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Simple Ajax Example: CSV file -> HTML table</title>
<script language="javascript" type="text/javascript" src="ops.js">
</script>
<link href="styles.css" rel="stylesheet" type="text/css" />
</head>
<body onload="loadCSVFile('Mio_File.csv?', 'ResTable');">
<center>
<div id="ResTable"></div>
</center>
</body>
</html>
ops.js
Codice PHP:
// global variables
var xmlhttp;
var destDiv;
var DEFAULT_SEPARATOR = ";";
function loadCSVFile(filePath, targetDiv){
xmlHttp = null;
// code for Firefox, Opera, ...
if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
// code for IE...
else
if (window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
if (xmlHttp != null) {
destDiv = targetDiv;
xmlHttp.onreadystatechange = stateChange;
xmlHttp.open("GET", filePath, true);
xmlHttp.send(null);
}
else {
alert("Your browser doesn't support xmlHttp: cannot read CSV file!");
}
}
function stateChange(){
// if xmlHttp is loaded...
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
var content = xmlHttp.responseText;
var res = makeHTMLTable(content);
document.getElementById(destDiv).innerHTML = res;
}
else {
alert("Problem retrieving CSV file");
}
}
}
function makeHTMLTable(content){
var res = "";
var lines = content.split("\n"); // split the content to get an array of lines
var numFields = lines[0].split(DEFAULT_SEPARATOR).length; // get number of fields
// open HTML table
res += "<table width='90%' id='ResTable'>";
// for simplicity, the first line of the CSV file
// contains the header cells captions
res += "<tr>";
var ths = lines[0].split(DEFAULT_SEPARATOR); // split the line to get header cell values
for (field = 0; field < numFields; field++) {
res += "<th>" + ths[field] + "</th>";
}
res += "</tr>";
// for every other line...
for (line = 1; line < lines.length - 1; line++) {
var tds = lines[line].split(DEFAULT_SEPARATOR); // split the line to get cell values
res += "<tr>";
for (field = 0; field < numFields; field++) {
if (tds[field] == undefined || tds[field] == "") {
tds[field] = "-";
}
res += "<td>" + tds[field] + "</td>";
}
res += "</tr>";
}
// close HTML table
res += "</table>";
return res;
}
in pratica non fa altro che prendere il file Mio_File.csv e convertirlo in una tabella html.
Il primo problema è il seguente, questo codice mi prende il file che si trova nella cartella e lo trasforma in una tabella, io vorrei che tramite una form del tipo
Codice PHP:
<form> <input type=file name=documento> </form>
mi permetta di selezionare il documento che voglio io, per poi trasformarlo in tabella html, ma come lo posso passare al file index.html?
Codice PHP:
<body onload="loadCSVFile('Mio_File.csv?', 'ResTable');">
Secondo problema: ho la necessità di creare una tabella con i campi <th> nel mio database, è possibile farlo direttamente dal file ops.js?
Se si, come?
Se non è possibile, avete qualche idea di come risolvere il problema?
Da notare che i campi <th> che mi andrebbero a creare la tabella, non li so, so solo che sono stringhe (come tutta la tabella).