Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="ISO-8859 " />
- <link rel="stylesheet" href="./ressources/style.css" />
- <title>Resultat SQL</title>
- </head>
- <body>
- <?php
- include 'PHPExcel.php';
- include 'PHPExcel/Writer/Excel2007.php';
- $pdo_option = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
- $i = 0;
- $test_pdo = 0;
- $k = 0;
- $j = 1;
- $l = 0;
- $strId = "Id";
- $strNom = "Nom";
- $strPrenom = "Prenom";
- $strAge = "Age";
- $strAbo = "Abo";
- $strEmail = "E-mail";
- $strVille = "Ville";
- $strAchats = "Achats";
- $strTotal = "Total";
- $id = FALSE;
- $nom = FALSE;
- $prenom = FALSE;
- $age = FALSE;
- $abo = FALSE;
- $email = FALSE;
- $ville = FALSE;
- $achats = FALSE;
- $total = FALSE;
- // On recupere les variables envoyees via le formulaire
- $requete = $_POST['requete'];
- $titre = $_POST['titre'];
- // Connection a la db et test d'erreur
- try
- {
- $bdd = new PDO('mysql:host=localhost;dbname=test;charset=utf8', 'root', '', $pdo_option);
- }
- catch (Exception $e)
- {
- die("Erreur : " . $e->getMessage());
- }
- // On recupere la requete entree et on lance le travail
- $reponse = $bdd->query($requete);
- if($pdo_option){
- $test_pdo = 0;
- }
- else{
- $test_pdo = 1;
- }
- if(preg_match('`id`', $requete) OR preg_match('`[*]`', $requete))
- $id = TRUE;
- if(preg_match('{`nom`}', $requete) OR preg_match('`[*]`', $requete))
- $nom = TRUE;
- if(preg_match('`prenom`', $requete) OR preg_match('`[*]`', $requete))
- $prenom = TRUE;
- if(preg_match('`age`', $requete) OR preg_match('`[*]`', $requete))
- $age = TRUE;
- if(preg_match('`abo`', $requete) OR preg_match('`[*]`', $requete))
- $abo = TRUE;
- if(preg_match('`email`', $requete) OR preg_match('`[*]`', $requete))
- $email = TRUE;
- if(preg_match('`ville`', $requete) OR preg_match('`[*]`', $requete))
- $ville = TRUE;
- if(preg_match('`achats`', $requete) OR preg_match('`[*]`', $requete))
- $achats = TRUE;
- if(preg_match('`total`', $requete) OR preg_match('`[*]`', $requete))
- $total = TRUE;
- ?>
- <table>
- <caption><b><?php echo $titre; ?></b></caption>
- <thead>
- <tr>
- <?php
- if($id == TRUE)
- echo '<th>' . $strId . '</th>';
- if($nom == TRUE)
- echo '<th>' . $strNom . '</th>';
- if($prenom == TRUE)
- echo '<th>' . $strPrenom . '</th>';
- if($age == TRUE)
- echo '<th>' . $strAge . '</th>';
- if($abo == TRUE)
- echo '<th>' . $strAbo . '</th>';
- if($email == TRUE)
- echo '<th>' . $strEmail . '</th>';
- if($ville == TRUE)
- echo '<th>' . $strVille . '</th>';
- if($achats == TRUE)
- echo '<th>' . $strAchats . '</th>';
- if($total == TRUE)
- echo '<th>' . $strTotal . '</th>';
- ?>
- </tr>
- </thead>
- <?php
- // On affiche le resultat de la requete avec une boucle
- while($donnees = $reponse->fetch())
- {
- ?>
- <tbody>
- <tr>
- <?php
- if($id == TRUE)
- echo '<td>' . $donnees['id'] . '</td>';
- if($nom == TRUE)
- echo '<td>' . $donnees['nom'] . '</td>';
- if($prenom == TRUE)
- echo '<td>' . $donnees['prenom'] . '</td>';
- if($age == TRUE)
- echo '<td>' . $donnees['age'] . '</td>';
- if($abo == TRUE)
- echo '<td>' . $donnees['abo'] . '</td>';
- if($email == TRUE)
- echo '<td>' . $donnees['email'] . '</td>';
- if($ville == TRUE)
- echo '<td>' . $donnees['ville'] . '</td>';
- if($achats == TRUE)
- echo '<td>' . $donnees['achats'] . '</td>';
- if($total == TRUE)
- echo '<td>' . $donnees['total'] . ' €</td>';
- ?>
- </tr>
- </tbody>
- <?php
- $i++;
- }
- $bddId = $donnees['id'];
- $bddNom = $donnees['nom'];
- $bddPrenom = $donnees['prenom'];
- $bddAge = $donnees['age'];
- $bddAbo = $donnees['abo'];
- $bddEmail = $donnees['email'];
- $bddVille = $donnees['ville'];
- $bddAchats = $donnees['achats'];
- $bddTotal = $donnees['total'];
- ?>
- </table>
- <?php
- // On termine enfin la requete
- $reponse->closeCursor();
- ?>
- <br /><br />
- <?php
- echo 'Il y a ' . $i . ' entrées correspondantes a vos critères. <br /><br />';
- echo 'Requéte SQL saisie : ' . $requete;
- ?>
- <br /><br />
- <?php
- $worksheet = new PHPExcel;
- $sheet = $worksheet->getActiveSheet();
- $worksheet->setActiveSheetIndex(0);
- $sheet=$worksheet->getActiveSheet();
- $sheet->setCellValueByColumnAndRow($k, $j, $titre);
- $j = 3;
- if($id == TRUE){
- $sheet->setCellValueByColumnAndRow($k, $j, $strId);
- $k++;
- }
- if($nom == TRUE){
- $sheet->setCellValueByColumnAndRow($k, $j, $strNom);
- $k++;
- }
- if($prenom == TRUE){
- $sheet->setCellValueByColumnAndRow($k, $j, $strPrenom);
- $k++;
- }
- if($age == TRUE){
- $sheet->setCellValueByColumnAndRow($k, $j, $strAge);
- $k++;
- }
- if($abo == TRUE){
- $sheet->setCellValueByColumnAndRow($k, $j, $strAbo);
- $k++;
- }
- if($email == TRUE){
- $sheet->setCellValueByColumnAndRow($k, $j, $strEmail);
- $k++;
- }
- if($ville == TRUE){
- $sheet->setCellValueByColumnAndRow($k, $j, $strVille);
- $k++;
- }
- if($achats == TRUE){
- $sheet->setCellValueByColumnAndRow($k, $j, $strAchats);
- $k++;
- }
- if($total == TRUE){
- $sheet->setCellValueByColumnAndRow($k, $j, $strTotal);
- $k++;
- }
- $j = 1;
- $k++;
- do{
- if($id == TRUE){
- $sheet->setCellValueByColumnAndRow($k, $j, $donnees['id']);
- $k++;
- }
- if($nom == TRUE){
- $sheet->setCellValueByColumnAndRow($k, $j, $donnees['nom']);
- $k++;
- }
- if($prenom == TRUE){
- $sheet->setCellValueByColumnAndRow($k, $j, $donnees['prenom']);
- $k++;
- }
- if($age == TRUE){
- $sheet->setCellValueByColumnAndRow($k, $j, $donnees['age']);
- $k++;
- }
- if($abo == TRUE){
- $sheet->setCellValueByColumnAndRow($k, $j, $donnees['abo']);
- $k++;
- }
- if($email == TRUE){
- $sheet->setCellValueByColumnAndRow($k, $j, $donnees['email']);
- $k++;
- }
- if($ville == TRUE){
- $sheet->setCellValueByColumnAndRow($k, $j, $donnees['ville']);
- $k++;
- }
- if($achats == TRUE){
- $sheet->setCellValueByColumnAndRow($k, $j, $donnees['achats']);
- $k++;
- }
- if($total == TRUE){
- $sheet->setCellValueByColumnAndRow($k, $j, $donnees['total']);
- $k++;
- }
- $l++;
- $k++;
- $j = 1;
- }while($l < $i);
- $writer = new PHPExcel_Writer_Excel2007($worksheet);
- $records = './' . $titre . '.xlsx';
- $writer->save($records);
- $fp = fopen('historique_requetes.txt', 'a+');
- $ligne = date('d/m/Y, H:i:s') . ' -> ' . $requete . ' | TRUE = ' . $test_pdo;
- fputs($fp, $ligne);
- fputs($fp, "\r\n");
- fclose($fp);
- ?>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement