0

so to better describe my problem :

1- i aquire data from Mysql database

2- I organise it into my Html Table with an id of : id="#Elements"

while ($row = $result->fetch_assoc()) {
echo('<tr>');
echo '<td>'.$row["Rang"].'</td>';
echo '<td>'.$row["Pays"].'</td>';
echo '<td>'.$row["Region"].'</td>';
echo '<td>'.$row["Documents"].'</td>';
echo '<td>'.$row["Documents_citables"].'</td>';
echo '<td>'.$row["Citations"].'</td>';
echo '<td>'.$row["Autocitations"].'</td>';
echo'<td>'.$row["Citations_par_document"].'</td>';
echo '<td>'.$row["Indiceh"].'</td>';
echo '<td>'.$row["Discipline"].'</td>';
echo '<td>'.$row["Sous_discipline"].'</td>';
echo '<td>'.$row["Annee"].'</td>';
echo( '</tr>');
}

3- In my js file , i tried to convert this table to Csv using these two functions :


function exportTableToCSV(filename) {
    var csv = [];
    var rows = document.querySelectorAll("table tr");
    
    for (var i = 0; i < rows.length; i++) {
        var row = [], cols = rows[i].querySelectorAll("td, th");
        
        for (var j = 0; j < cols.length; j++){
            row.push(cols[j].innerText);
            console.log(cols[j].innerText);
        }
        
        csv.push(row.join(","));        
    }

    // Download CSV file
    downloadCSV(csv.join("\n"), filename);
}

function downloadCSV(csv, filename) {
    var csvFile;
    var downloadLink;

    // CSV file
    csvFile = new Blob([csv], {type: "text/csv"});

    // Download link
    downloadLink = document.createElement("a");

    // File name
    downloadLink.download = filename;

    // Create a link to the file
    downloadLink.href = window.URL.createObjectURL(csvFile);

    // Hide download link
    downloadLink.style.display = "none";

    // Add the link to DOM
    document.body.appendChild(downloadLink);

    // Click download link
    downloadLink.click();
}

For the button that calls the function :

<button id="download-csv" class="downloadcsv" onclick="exportTableToCSV('data.csv');">Télécharger le tableau</button>

4- i get a CSV file but with unreadable text, for example : Région is shown as Région

My html file is set to Charset UTF-8 and my database is also using default characters in it's settings

Can anyone help me figure out how to fix this issue, or even how to Convert the table to csv in a "cleaner" way. thank you

Note : im only using php and js for this project .

Hephaestus
  • 11
  • 1
  • 2
  • Are you sure you're specifying the UTF-8 charset [when initiating the MySQL connection](https://stackoverflow.com/questions/4361459/php-pdo-charset-set-names)? – kmoser Jun 19 '22 at 03:46
  • Also, what you use to inspect your csv file could be a problem. It may be the data is fine, but if you are importing the csv into Excel, there will be trouble. – Jerry Jun 19 '22 at 07:54
  • i actually view the csv in excel yes ... would that also create problem? cuz im used to view csvs there – Hephaestus Jun 19 '22 at 09:40
  • sadly it didnt work @kmoser – Hephaestus Jun 19 '22 at 09:42
  • guys ! i open directly the csv file into excel which causes this issue, but if i went to excel and went to Data > Import data > from text/csv and select the csv, the data appeared fine, i think that was the issue as @Jerry said . thank you so much – Hephaestus Jun 19 '22 at 09:54

0 Answers0