1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
| <?php
//connexion à ma BDD perso
$connect = mysqli_connect("..", "..", "..", "..");
// On crée une function qui va s'occuper de créer le tableau
// De cette façon, on pourra facilement l'intégrer à plusieurs endroits
function export($echo = true) {
$connect = $GLOBALS['connect'];
$query = "SELECT identifiant,nom,prenom,adresse,complementAdresse,ville,codePostal,mail,fixe,portable,kit,tube,aiguille,adaptateur,cartonSmall,cartonBig,etiquetteChronopost,enveloppeBulle,commentaire,dateCommande FROM exemple";
$res = mysqli_query($connect, $query);
// ob_start() permet de "temporiser la sortie"
// en gros, on va empêcher tout affichage jusqu'à utiliser ob_get_clean() pour récupérer ce qui a été intercepté
ob_start();
if (mysqli_num_rows($res) > 0) :
?>
<table>
<tr>
<th>identifiant</th>
<th>nom</th>
<th>prenom</th>
<th>adresse</th>
<th>complement adresse</th>
<th>ville</th>
<th>code postal</th>
<th>mail</th>
<th>fixe</th>
<th>portable</th>
<th>nbre de kits</th>
<th>nbre de tubes</th>
<th>nbre aiguilles</th>
<th>nbre adaptateur</th>
<th>nbre petit carton</th>
<th>nbre grand carton</th>
<th>nbre etiquette chronopost</th>
<th>nbre enveloppe bulle</th>
<th>commentaire</th>
<th>date commande</th>
</tr>
<?php while ($row = mysqli_fetch_array($res)) : ?>
<tr>
<td><?= $row["identifiant"]; ?></td>
<td><?= $row["nom"]; ?></td>
<td><?= $row["prenom"]; ?></td>
<td><?= $row["adresse"]; ?></td>
<td><?= $row["complementAdresse"]; ?></td>
<td><?= $row["ville"]; ?></td>
<td><?= $row["codePostal"]; ?></td>
<td><?= $row["mail"]; ?></td>
<td><?= $row["fixe"]; ?></td>
<td><?= $row["portable"]; ?></td>
<td><?= $row["kit"]; ?></td>
<td><?= $row["tube"]; ?></td>
<td><?= $row["aiguille"]; ?></td>
<td><?= $row["adaptateur"]; ?></td>
<td><?= $row["cartonSmall"]; ?></td>
<td><?= $row["cartonBig"]; ?></td>
<td><?= $row["etiquetteChronopost"]; ?></td>
<td><?= $row["enveloppeBulle"]; ?></td>
<td><?= $row["commentaire"]; ?></td>
<td><?= $row["dateCommande"]; ?></td>
</tr>
<?php endwhile; ?>
<?php endif; ?>
</table>
<?php
// On va intercepter tous les contenus affichés depuis ob_start() et les mettre dans une variable
$export_content = ob_get_clean();
// Par défaut on peut echo la variable mais on peut faire en sorte de la retourner
if ($echo) {
echo $export_content;
}
return $export_content;
}
if (!empty($_POST['action'])) {
switch ($_POST['action']) {
case 'export':
header('Content-Type: application/xls');
header('Content-Disposition: attachment; filename=commande-ifce.xls');
export();
break;
case 'mail':
$to = "email@domain.tld";
$subject = "Export de données";
$message = "<p>Résultat de l'export : </p>" . export(false);
$headers = [
'Content-Type: text/html; charset=UTF-8',
];
mail($to, $subject, $message, $headers);
break;
}
} |
Partager