Visualizzazione dei risultati da 1 a 2 su 2
  1. #1
    Utente di HTML.it
    Registrato dal
    Oct 2011
    Messaggi
    63

    Creare tabella in sql da .csv

    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 ExampleCSV 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(filePathtargetDiv){
        
    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"filePathtrue);
            
    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 0field numFieldsfield++) {
            
    res += "<th>" ths[field] + "</th>";
        }
        
    res += "</tr>";
        
        
        
    // for every other line...
        
    for (line 1line lines.length 1line++) {
            var 
    tds lines[line].split(DEFAULT_SEPARATOR); // split the line to get cell values
            
            
    res += "<tr>";
            for (
    field 0field numFieldsfield++) {
                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).

  2. #2
    Moderatore di Programmazione L'avatar di alka
    Registrato dal
    Oct 2001
    residenza
    Reggio Emilia
    Messaggi
    24,465

    Moderazione

    Trattandosi di JavaScript, sposto nel forum dedicato a questo linguaggio.
    MARCO BREVEGLIERI
    Software and Web Developer, Teacher and Consultant

    Home | Blog | Delphi Podcast | Twitch | Altro...

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.