Bonsoir,
Après 2 jours perdus sur des forums, des tutos... je fais appel à ce forum.
J'ai un formulaire de recherche qui récupère les membres qui appartiennent à un "corps" donné et affiche les résultats sous forme d'une liste.
Jusqu'ici tout va bien.
Mais je souhaite en plus, donner la possibilité à l'utilisateur de faire un export csv de ces résultats s'il le souhaite, pour une utilisation avec Excel.
Pour ce soir, j'ai testé une fonction enregistrée dans un fichier "fonctions.inc.php" mais je ne sais pas très bien où l'appeler ? Il m'est difficile de faire cohabiter les 2 possibilités (affichage sous forme de liste et/ou export csv) avec une seule requête ?
Je vous remercie par avance pour votre aide.
Evelyne 31
code de traitement de mon formulaire de recherche :
code de la fonction enregistrée dans le fichier "fonctions.inc.php" :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143 <?php if( empty(session_id()) ){ session_start(); } //error_reporting(E_ALL); $base = include './include/connect.inc.php'; include('./include/fonctions.inc.php'); // on teste la déclaration de l'envoi du formulaire de recherche if (isset($_POST['r_corps']) ) { ?> <!DOCTYPE html> <html lang="fr"> <head> <link rel="shortcut icon" type="image/x-icon" href="./favicon.ico"> <title>Recherche par corps</title> <?php include("./include/head.php"); ?> <link href="./include/chosen.css" rel="stylesheet" type="text/css"> <link href="./include/styles.css" rel="stylesheet" type="text/css"> <script type="text/javascript" src="./scripts/jquery-latest.js"></script> <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> <link rel="stylesheet" href="/resources/demos/style.css"> <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta charset="utf-8" /> </head> <body> <div id="page"> <?php include ("./include/menu.php"); ?> <div id="main" class="container text-left corps-de-page"> <div class="row content"> <!-- début fil d'ariane --> <div class="container"> <div class='row'> <div class="col-lg-12 col-md-12 col-sm-12"> <div class="ariane text-left"> <span><font color="#993333"><span class="glyphicon glyphicon-home"></font></span><a href="index.php"> Accueil </a><font color="#993333"><span class="glyphicon glyphicon-chevron-right"></font></span><a href="bdd.php"> menu base de données </a><font color="#993333"><span class="glyphicon glyphicon-chevron-right"></font></span><a href="formulaire_recherche.php"> formulaire de recherche </a></span> </div> </div> </div> </div> <!-- fin fil d'ariane --> <?php include("./include/menu_intranet.php"); ?> <div class='row'> <div class="col-lg-12 col-md-12 col-sm-12"> <center> <h4>Résultat de votre recherche par corps pour : </h4><h3><?php echo $_POST['r_corps']; ?></h3> </center> </div> </div> <!--barre horizontale--> <br><hr width='75%' color='maroon'><br> <?php $resultat = $base->query(' SELECT m.`nom`, m.`prenom`, s.`corps`, i.`nom_institut` FROM MEMBRE m INNER JOIN STATUT s ON s.`ID_statut` = m.`ID_statut` INNER JOIN INSTITUT i ON i.`ID_institut` = s.`ID_institut` WHERE s.`corps` = "'.$_POST['r_corps'].'" ORDER BY m.`nom` ') or die($mysqli->error.__LINE__); while($donnees = $resultat->fetch()) { $nom = $donnees['nom']; $prenom = $donnees['prenom']; $corps = $donnees['corps']; $institut = $donnees['nom_institut']; //echo 'var_dump variable donnees : <br>'; //var_dump($donnees); //function export_data_to_csv(); ?> <div class="container"> <div class="row"> <div class="col-lg-12 col-md-12 col-sm-12"> <div class="table-responsive"> <table class="table"> <tr class="tab1"> <td class="tab1">Données enregistrées : <br>nom, prénom : <font color="#993333"><?php echo $nom; ?> <?php echo $prenom; ?></font> <br>corps : <font color="#993333"><?php echo $corps; ?></font> <br>institut : <font color="#993333"><?php echo $institut; ?></font> </td> <td class="tab2"> <br> <a href="page_membre.php? nom=<?php echo $nom; ?>&prenom=<?php echo $prenom; ?>&corps=<?php echo $corps; ?>&institut=<?php echo $institut; ?> "> <span class ="bouton_voir">Voir la page <span class="glyphicon glyphicon-eye-open"></span></a> </td> </tr> </table> </div> <hr width='75%' color='maroon'> <br> </div></div></div></div></div></div></div> <?php } } $resultat->closeCursor(); // Termine le traitement de la requête include('./include/footer.php'); ?> </body> </html>
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 <?php /** * * Exports an associative array into a CSV file using PHP. * * @see https://stackoverflow.com/questions/21988581/write-utf-8-characters-to-file-with-fputcsv-in-php * * @param array $data The table you want to export in CSV * @param string $filename The name of the file you want to export * @param string $delimiter The CSV delimiter you wish to use. The default ";" is used for a compatibility with microsoft excel * @param string $enclosure The type of enclosure used in the CSV file, by default it will be a quote " */ function export_data_to_csv($donnees,$filename='export_csv',$delimiter = ';',$enclosure = '"') { // Tells to the browser that a file is returned, with its name : $filename.csv header("Content-disposition: attachment; filename=$filename.csv"); // Tells to the browser that the content is a csv file header("Content-Type: text/csv"); // I open PHP memory as a file $fp = fopen("php://output", 'w'); // Insert the UTF-8 BOM in the file fputs($fp, $bom =( chr(0xEF) . chr(0xBB) . chr(0xBF) )); // I add the array keys as CSV headers fputcsv($fp,array_keys($donnees[0]),$delimiter,$enclosure); // Add all the data in the file foreach ($donnees as $fields) { fputcsv($fp, $fields,$delimiter,$enclosure); } // Close the file fclose($fp); // Stop the script die(); } ?>
Partager