par
niuxe » 10 févr. 2010, 15:37
Hello,
Je repasse par là pour améliorer ce que j'ai écrit en amont. Là, le script est fonctionnel. Il manque un système de pagination. Tu peux utiliser
Pager et sa doc se trouve
ici. Pour retrouver les vues dans ta base, il suffit de changer le terme après LIKE de la requête :
$sqlVue = sprintf("SHOW TABLES LIKE %s ", "vue_%");
Bizarrement, je n'arrive pas à préparer les requêtes. Si une âme charitable peut m'expliquer le pourquoi du comment, ce serait sympa de sa part. ^^
*config_connect.php
<?php
define("HOTE","un_hote");
define("SGBDR","unebase");
define("DSN_MYSQL","mysql:dbname=".SGBDR.";host=".HOTE);
define("UTILISATEUR","un_utilisateur");
define("MOT_DE_PASSE","|_|n_p455_3cr!7_3n_|_337");
?>
*class.html.php
<?php
class HTML extends XMLWriter{
public function writeMyElement($element,$elementValue,$attributs){
$this->startElement($element);
foreach($attributs as $attribut => $value){
$this->writeAttribute($attribut,$value);
}
$this->text($elementValue);
$this->endElement();
}
}
?>
*visionneuse_de_vues_mysql.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="fr" xml:lang="fr">
<head>
<meta http-equiv="content-type" content="text/html;charset=iso-8859-1" />
<title>des vues</title>
<link href="style.css" type="text/css" rel="stylesheet" />
</head>
<body>
<?php
try{
require_once "config_connect.php";
require_once "class.html.php";
$vue = "vue";
$afficher = "afficher";
$mySql = new PDO(DSN_MYSQL,UTILISATEUR,MOT_DE_PASSE);
$mySql->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$html = new HTML();
$html->openMemory();
$html->setIndent(true);
$html->startElement("form");
$html->writeAttribute("method","post");
$html->writeAttribute("action",htmlspecialchars($_SERVER["SCRIPT_NAME"]));
$html->startElement("fieldset");
$html->writeElement("legend","choix des vues");
$html->startElement("p");
$attributsLabelChoisir = array("for" => $vue);
$html->writeMyElement("label","choisir : ",$attributsLabelChoisir);
$html->startElement("select");
$html->writeAttribute("name",$vue);
$html->writeAttribute("id",$vue);
$sqlVue = "SHOW TABLES "; // LIKE '%vue'
$listeVues = $mySql->query($sqlVue);
$lesVues = $listeVues->fetchAll(PDO::FETCH_NUM);
//option
for($vueActuelle = 0, $nbVues = count($lesVues); $vueActuelle < $nbVues ; $vueActuelle++){
$attributsOptionChoisir = array("value" => $lesVues[$vueActuelle][0]);
if(isset($_POST[$vue]) && $_POST[$vue] === $lesVues[$vueActuelle][0])
$attributsOptionChoisir["selected"] = "selected";
$html->writeMyElement("option",str_replace("_"," ",$lesVues[$vueActuelle][0]),$attributsOptionChoisir);
}
$html->endElement(); // select
$html->endElement(); // p
$attributsAfficher = array("type" => "submit", "name" => $afficher, "value" => $afficher);
$html->writeMyElement("button","afficher",$attributsAfficher);
$html->endElement(); // fieldset
$html->endElement(); // form
//soummission du formulaire
if(isset($_POST[$afficher]) && $_POST[$afficher] === $afficher){
$sqlDonnees = sprintf("SELECT * FROM %s",$_POST[$vue]);
$sqlEnTete = sprintf("SHOW COLUMNS FROM %s",$_POST[$vue]);
$listeDonnees = $mySql->query($sqlDonnees);
$donnees = $listeDonnees->fetchAll(PDO::FETCH_NUM);
$listeEnTete = $mySql->query($sqlEnTete);
$enTete = $listeEnTete->fetchAll(PDO::FETCH_ASSOC);
$html->startElement("table");
$html->writeAttribute("summary","Affichages de la vue ".$_POST[$vue]." (".SGBDR.")");
$html->writeElement("caption",str_replace("_"," ",$_POST[$vue])." : ");
//en tete
$html->startElement("tr");
$html->writeAttribute("class","bordure_basse");
for($i = 0 ; $i < count($enTete) ; $i++){
$html->writeElement("th",str_replace("_"," ",$enTete[$i]["Field"]));
}
$html->endElement();
//donnees
for($j = 0 ; $j < count($donnees) ; $j++){
$html->startElement("tr");
$modulo = $j % 2;
if($modulo === 1)
$html->writeAttribute("class","colorer");
for($k = 0 ; $k < count($donnees[$j]) ; $k++){
$html->writeElement("td",$donnees[$j][$k]);
}
$html->endElement();
}
$html->endElement();
}
echo $html->flush();
}catch(Exception $e){
echo $e->getMessage();
}
?>
</body>
</html>
*style.css (fait à l'arrache)
body{
color:#666; /* le chiffre de la bête lol */
font-family:arial,helvetica,sans-serif;
font-size:.8em;
margin:0;
padding:0;
}
form{
margin:10px;
}
fieldset{
border:1px solid #666;
}
legend,label{
color:#666;
}
legend{
font-variant:small-caps;
font-weight:bold;
}
caption{
text-align:left;
font-style:italic;
margin:5px 0px;
}
label{
font-variant:small-caps;
font-weight:bold;
}
select{
padding:3px;
border:none;
color:#47757e;
background-color:#e1ecef;
}
fieldset,button,select{
border-radius:5px;
-moz-border-radius:5px;
-webkit-border-radius:5px;
}
button{
border:none;
background-color:#47757e;
padding:5px;
color:white;
}
table{
margin:25px 10px;
border-bottom:1px solid #2b464c;
border-collapse:collapse;
min-width:98%;
}
th,td{
padding:5px 10px;
}
.colorer{
background-color:#e1ecef;
}
.bordure_basse{
border-bottom:1px solid #2b464c;
}
Hello,
Je repasse par là pour améliorer ce que j'ai écrit en amont. Là, le script est fonctionnel. Il manque un système de pagination. Tu peux utiliser [url=http://pear.php.net/package/Pager]Pager[/url] et sa doc se trouve [url=http://pear.php.net/manual/en/package.html.pager.intro.php]ici[/url]. Pour retrouver les vues dans ta base, il suffit de changer le terme après LIKE de la requête :
[php]$sqlVue = sprintf("SHOW TABLES LIKE %s ", "vue_%"); [/php]
Bizarrement, je n'arrive pas à préparer les requêtes. Si une âme charitable peut m'expliquer le pourquoi du comment, ce serait sympa de sa part. ^^
*config_connect.php
[php]
<?php
define("HOTE","un_hote");
define("SGBDR","unebase");
define("DSN_MYSQL","mysql:dbname=".SGBDR.";host=".HOTE);
define("UTILISATEUR","un_utilisateur");
define("MOT_DE_PASSE","|_|n_p455_3cr!7_3n_|_337");
?>
[/php]
*class.html.php
[php]
<?php
class HTML extends XMLWriter{
public function writeMyElement($element,$elementValue,$attributs){
$this->startElement($element);
foreach($attributs as $attribut => $value){
$this->writeAttribute($attribut,$value);
}
$this->text($elementValue);
$this->endElement();
}
}
?>
[/php]
*visionneuse_de_vues_mysql.php
[php]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="fr" xml:lang="fr">
<head>
<meta http-equiv="content-type" content="text/html;charset=iso-8859-1" />
<title>des vues</title>
<link href="style.css" type="text/css" rel="stylesheet" />
</head>
<body>
<?php
try{
require_once "config_connect.php";
require_once "class.html.php";
$vue = "vue";
$afficher = "afficher";
$mySql = new PDO(DSN_MYSQL,UTILISATEUR,MOT_DE_PASSE);
$mySql->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$html = new HTML();
$html->openMemory();
$html->setIndent(true);
$html->startElement("form");
$html->writeAttribute("method","post");
$html->writeAttribute("action",htmlspecialchars($_SERVER["SCRIPT_NAME"]));
$html->startElement("fieldset");
$html->writeElement("legend","choix des vues");
$html->startElement("p");
$attributsLabelChoisir = array("for" => $vue);
$html->writeMyElement("label","choisir : ",$attributsLabelChoisir);
$html->startElement("select");
$html->writeAttribute("name",$vue);
$html->writeAttribute("id",$vue);
$sqlVue = "SHOW TABLES "; // LIKE '%vue'
$listeVues = $mySql->query($sqlVue);
$lesVues = $listeVues->fetchAll(PDO::FETCH_NUM);
//option
for($vueActuelle = 0, $nbVues = count($lesVues); $vueActuelle < $nbVues ; $vueActuelle++){
$attributsOptionChoisir = array("value" => $lesVues[$vueActuelle][0]);
if(isset($_POST[$vue]) && $_POST[$vue] === $lesVues[$vueActuelle][0])
$attributsOptionChoisir["selected"] = "selected";
$html->writeMyElement("option",str_replace("_"," ",$lesVues[$vueActuelle][0]),$attributsOptionChoisir);
}
$html->endElement(); // select
$html->endElement(); // p
$attributsAfficher = array("type" => "submit", "name" => $afficher, "value" => $afficher);
$html->writeMyElement("button","afficher",$attributsAfficher);
$html->endElement(); // fieldset
$html->endElement(); // form
//soummission du formulaire
if(isset($_POST[$afficher]) && $_POST[$afficher] === $afficher){
$sqlDonnees = sprintf("SELECT * FROM %s",$_POST[$vue]);
$sqlEnTete = sprintf("SHOW COLUMNS FROM %s",$_POST[$vue]);
$listeDonnees = $mySql->query($sqlDonnees);
$donnees = $listeDonnees->fetchAll(PDO::FETCH_NUM);
$listeEnTete = $mySql->query($sqlEnTete);
$enTete = $listeEnTete->fetchAll(PDO::FETCH_ASSOC);
$html->startElement("table");
$html->writeAttribute("summary","Affichages de la vue ".$_POST[$vue]." (".SGBDR.")");
$html->writeElement("caption",str_replace("_"," ",$_POST[$vue])." : ");
//en tete
$html->startElement("tr");
$html->writeAttribute("class","bordure_basse");
for($i = 0 ; $i < count($enTete) ; $i++){
$html->writeElement("th",str_replace("_"," ",$enTete[$i]["Field"]));
}
$html->endElement();
//donnees
for($j = 0 ; $j < count($donnees) ; $j++){
$html->startElement("tr");
$modulo = $j % 2;
if($modulo === 1)
$html->writeAttribute("class","colorer");
for($k = 0 ; $k < count($donnees[$j]) ; $k++){
$html->writeElement("td",$donnees[$j][$k]);
}
$html->endElement();
}
$html->endElement();
}
echo $html->flush();
}catch(Exception $e){
echo $e->getMessage();
}
?>
</body>
</html>
[/php]
*style.css (fait à l'arrache)
[css]
body{
color:#666; /* le chiffre de la bête lol */
font-family:arial,helvetica,sans-serif;
font-size:.8em;
margin:0;
padding:0;
}
form{
margin:10px;
}
fieldset{
border:1px solid #666;
}
legend,label{
color:#666;
}
legend{
font-variant:small-caps;
font-weight:bold;
}
caption{
text-align:left;
font-style:italic;
margin:5px 0px;
}
label{
font-variant:small-caps;
font-weight:bold;
}
select{
padding:3px;
border:none;
color:#47757e;
background-color:#e1ecef;
}
fieldset,button,select{
border-radius:5px;
-moz-border-radius:5px;
-webkit-border-radius:5px;
}
button{
border:none;
background-color:#47757e;
padding:5px;
color:white;
}
table{
margin:25px 10px;
border-bottom:1px solid #2b464c;
border-collapse:collapse;
min-width:98%;
}
th,td{
padding:5px 10px;
}
.colorer{
background-color:#e1ecef;
}
.bordure_basse{
border-bottom:1px solid #2b464c;
}
[/css]