Advertisement
Guest User

Script PHP/MySQL

a guest
May 2nd, 2015
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.04 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3.     <head>
  4.         <meta charset="ISO-8859 " />
  5.         <link rel="stylesheet" href="./ressources/style.css" />
  6.         <title>Resultat SQL</title>
  7.     </head>
  8.    
  9.     <body>
  10.        
  11.         <?php
  12.             include 'PHPExcel.php';
  13.             include 'PHPExcel/Writer/Excel2007.php';
  14.        
  15.             $pdo_option = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
  16.             $i = 0;
  17.             $test_pdo = 0;
  18.             $k = 0;
  19.             $j = 1;
  20.             $l = 0;
  21.            
  22.             $strId = "Id";
  23.             $strNom = "Nom";
  24.             $strPrenom = "Prenom";
  25.             $strAge = "Age";
  26.             $strAbo = "Abo";
  27.             $strEmail = "E-mail";
  28.             $strVille = "Ville";
  29.             $strAchats = "Achats";
  30.             $strTotal = "Total";
  31.                
  32.             $id = FALSE;
  33.             $nom = FALSE;
  34.             $prenom = FALSE;
  35.             $age = FALSE;
  36.             $abo = FALSE;
  37.             $email = FALSE;
  38.             $ville = FALSE;
  39.             $achats = FALSE;
  40.             $total = FALSE;
  41.            
  42.             // On recupere les variables envoyees via le formulaire
  43.             $requete = $_POST['requete'];
  44.             $titre = $_POST['titre'];
  45.            
  46.             // Connection a la db et test d'erreur
  47.             try
  48.             {
  49.                 $bdd = new PDO('mysql:host=localhost;dbname=test;charset=utf8', 'root', '', $pdo_option);
  50.             }
  51.             catch (Exception $e)
  52.             {
  53.                 die("Erreur : " . $e->getMessage());
  54.             }
  55.            
  56.             // On recupere la requete entree et on lance le travail
  57.             $reponse = $bdd->query($requete);
  58.                
  59.             if($pdo_option){
  60.                 $test_pdo = 0;
  61.             }
  62.             else{
  63.                 $test_pdo = 1;
  64.             }
  65.            
  66.             if(preg_match('`id`', $requete) OR preg_match('`[*]`', $requete))
  67.                 $id = TRUE;
  68.             if(preg_match('{`nom`}', $requete) OR preg_match('`[*]`', $requete))
  69.                 $nom = TRUE;
  70.             if(preg_match('`prenom`', $requete) OR preg_match('`[*]`', $requete))
  71.                 $prenom = TRUE;
  72.             if(preg_match('`age`', $requete) OR preg_match('`[*]`', $requete))
  73.                 $age = TRUE;
  74.             if(preg_match('`abo`', $requete) OR preg_match('`[*]`', $requete))
  75.                 $abo = TRUE;
  76.             if(preg_match('`email`', $requete) OR preg_match('`[*]`', $requete))
  77.                 $email = TRUE;
  78.             if(preg_match('`ville`', $requete) OR preg_match('`[*]`', $requete))
  79.                 $ville = TRUE;
  80.             if(preg_match('`achats`', $requete) OR preg_match('`[*]`', $requete))
  81.                 $achats = TRUE;
  82.             if(preg_match('`total`', $requete) OR preg_match('`[*]`', $requete))
  83.                 $total = TRUE;
  84.            
  85.         ?>
  86.            
  87.         <table>
  88.             <caption><b><?php echo $titre; ?></b></caption>
  89.            
  90.             <thead>
  91.                 <tr>
  92.                     <?php
  93.                         if($id == TRUE)
  94.                             echo '<th>' . $strId . '</th>';
  95.                         if($nom == TRUE)
  96.                             echo '<th>' . $strNom . '</th>';
  97.                         if($prenom == TRUE)
  98.                             echo '<th>' . $strPrenom . '</th>';
  99.                         if($age == TRUE)
  100.                             echo '<th>' . $strAge . '</th>';
  101.                         if($abo == TRUE)
  102.                             echo '<th>' . $strAbo . '</th>';
  103.                         if($email == TRUE)
  104.                             echo '<th>' . $strEmail . '</th>';
  105.                         if($ville == TRUE)
  106.                             echo '<th>' . $strVille . '</th>';
  107.                         if($achats == TRUE)
  108.                             echo '<th>' . $strAchats . '</th>';
  109.                         if($total == TRUE)
  110.                             echo '<th>' . $strTotal . '</th>';
  111.                     ?>
  112.                 </tr>
  113.             </thead>
  114.            
  115.             <?php
  116.                 // On affiche le resultat de la requete avec une boucle
  117.                 while($donnees = $reponse->fetch())
  118.                 {
  119.             ?>
  120.        
  121.             <tbody>
  122.                 <tr>
  123.                     <?php
  124.                         if($id == TRUE)
  125.                             echo '<td>' . $donnees['id'] . '</td>';
  126.                         if($nom == TRUE)
  127.                             echo '<td>' . $donnees['nom'] . '</td>';
  128.                         if($prenom == TRUE)
  129.                             echo '<td>' . $donnees['prenom'] . '</td>';
  130.                         if($age == TRUE)
  131.                             echo '<td>' . $donnees['age'] . '</td>';
  132.                         if($abo == TRUE)
  133.                             echo '<td>' . $donnees['abo'] . '</td>';
  134.                         if($email == TRUE)
  135.                             echo '<td>' . $donnees['email'] . '</td>';
  136.                         if($ville == TRUE)
  137.                             echo '<td>' . $donnees['ville'] . '</td>';
  138.                         if($achats == TRUE)
  139.                             echo '<td>' . $donnees['achats'] . '</td>';
  140.                         if($total == TRUE)
  141.                             echo '<td>' . $donnees['total'] . ' &euro;</td>';
  142.                     ?>
  143.                 </tr>
  144.             </tbody>
  145.        
  146.         <?php
  147.                 $i++;
  148.             }
  149.            
  150.             $bddId = $donnees['id'];
  151.             $bddNom = $donnees['nom'];
  152.             $bddPrenom = $donnees['prenom'];
  153.             $bddAge = $donnees['age'];
  154.             $bddAbo = $donnees['abo'];
  155.             $bddEmail = $donnees['email'];
  156.             $bddVille = $donnees['ville'];
  157.             $bddAchats = $donnees['achats'];
  158.             $bddTotal = $donnees['total'];
  159.         ?>
  160.         </table>
  161.         <?php
  162.             // On termine enfin la requete
  163.             $reponse->closeCursor();
  164.         ?>
  165.        
  166.         <br /><br />
  167.        
  168.         <?php
  169.             echo 'Il y a ' . $i . ' entr&eacute;es correspondantes a vos crit&egrave;res. <br /><br />';
  170.             echo 'Requ&eacute;te SQL saisie : ' . $requete;
  171.         ?>
  172.        
  173.         <br /><br />
  174.    
  175.         <?php
  176.            
  177.                 $worksheet = new PHPExcel;
  178.                 $sheet = $worksheet->getActiveSheet();
  179.                 $worksheet->setActiveSheetIndex(0);
  180.                 $sheet=$worksheet->getActiveSheet();
  181.                
  182.                 $sheet->setCellValueByColumnAndRow($k, $j, $titre);
  183.                 $j = 3;
  184.                
  185.                 if($id == TRUE){
  186.                     $sheet->setCellValueByColumnAndRow($k, $j, $strId);
  187.                     $k++;
  188.                 }              
  189.                 if($nom == TRUE){
  190.                     $sheet->setCellValueByColumnAndRow($k, $j, $strNom);
  191.                     $k++;
  192.                 }
  193.                 if($prenom == TRUE){
  194.                     $sheet->setCellValueByColumnAndRow($k, $j, $strPrenom);
  195.                     $k++;
  196.                 }  
  197.                 if($age == TRUE){
  198.                     $sheet->setCellValueByColumnAndRow($k, $j, $strAge);
  199.                     $k++;
  200.                 }
  201.                 if($abo == TRUE){
  202.                     $sheet->setCellValueByColumnAndRow($k, $j, $strAbo);
  203.                     $k++;
  204.                 }
  205.                 if($email == TRUE){
  206.                     $sheet->setCellValueByColumnAndRow($k, $j, $strEmail);
  207.                     $k++;
  208.                 }
  209.                 if($ville == TRUE){
  210.                     $sheet->setCellValueByColumnAndRow($k, $j, $strVille);
  211.                     $k++;
  212.                 }
  213.                 if($achats == TRUE){
  214.                     $sheet->setCellValueByColumnAndRow($k, $j, $strAchats);
  215.                     $k++;
  216.                 }
  217.                 if($total == TRUE){
  218.                     $sheet->setCellValueByColumnAndRow($k, $j, $strTotal);
  219.                     $k++;
  220.                 }
  221.  
  222.                 $j = 1;
  223.                 $k++;
  224.                
  225.                 do{
  226.                     if($id == TRUE){
  227.                         $sheet->setCellValueByColumnAndRow($k, $j, $donnees['id']);
  228.                         $k++;
  229.                     }              
  230.                     if($nom == TRUE){
  231.                         $sheet->setCellValueByColumnAndRow($k, $j, $donnees['nom']);
  232.                         $k++;
  233.                     }
  234.                     if($prenom == TRUE){
  235.                         $sheet->setCellValueByColumnAndRow($k, $j, $donnees['prenom']);
  236.                         $k++;
  237.                     }  
  238.                     if($age == TRUE){
  239.                         $sheet->setCellValueByColumnAndRow($k, $j, $donnees['age']);
  240.                         $k++;
  241.                     }
  242.                     if($abo == TRUE){
  243.                         $sheet->setCellValueByColumnAndRow($k, $j, $donnees['abo']);
  244.                         $k++;
  245.                     }
  246.                     if($email == TRUE){
  247.                         $sheet->setCellValueByColumnAndRow($k, $j, $donnees['email']);
  248.                         $k++;
  249.                     }
  250.                     if($ville == TRUE){
  251.                         $sheet->setCellValueByColumnAndRow($k, $j, $donnees['ville']);
  252.                         $k++;
  253.                     }
  254.                     if($achats == TRUE){
  255.                         $sheet->setCellValueByColumnAndRow($k, $j, $donnees['achats']);
  256.                         $k++;
  257.                     }
  258.                     if($total == TRUE){
  259.                         $sheet->setCellValueByColumnAndRow($k, $j, $donnees['total']);
  260.                         $k++;
  261.                     }
  262.                     $l++;
  263.                     $k++;
  264.                     $j = 1;
  265.                    
  266.                 }while($l < $i);
  267.                
  268.                 $writer = new PHPExcel_Writer_Excel2007($worksheet);
  269.                
  270.                 $records = './' . $titre . '.xlsx';
  271.                 $writer->save($records);
  272.            
  273.    
  274.             $fp = fopen('historique_requetes.txt', 'a+');
  275.             $ligne = date('d/m/Y, H:i:s') . ' -> ' . $requete . ' | TRUE = ' . $test_pdo;
  276.             fputs($fp, $ligne);
  277.             fputs($fp, "\r\n");
  278.             fclose($fp);
  279.         ?>
  280.        
  281.     </body>
  282. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement