IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage PHP Discussion :

code php light


Sujet :

Langage PHP

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Profil pro
    Inscrit en
    Décembre 2003
    Messages
    107
    Détails du profil
    Informations personnelles :
    Âge : 54
    Localisation : France, Bas Rhin (Alsace)

    Informations forums :
    Inscription : Décembre 2003
    Messages : 107
    Par défaut code php light
    bonjour, voilà j'aimerais savoir si c'était possible d'alléger les codes php.

    J'ai 4 pages PHP comprenant 1 page de recherche, 1 page d'ajout, 1 page de modification et 1 page de suppression....

    mais bon ça c'est juste pour les pétroliers, j'ai la même chose pour les marques, les motoristes, les pilotes... ce qui fait des pages PHP en masse.....

    voici le code d'ajout dans la base et modification.... pour exemple.. si quelqu'un veux bien me donner un coup de main... cela m'arrangerait bien la vie....

    VOICI LA PAGE PETROLIER ADD
    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
     
    <? require("commun/connexion.php") ?>
    <?php
    function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
    {
      $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;
     
      switch ($theType) {
        case "text":
          $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
          break;    
        case "long":
        case "int":
          $theValue = ($theValue != "") ? intval($theValue) : "NULL";
          break;
        case "double":
          $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
          break;
        case "date":
          $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
          break;
        case "defined":
          $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
          break;
      }
      return $theValue;
    }
     
    $editFormAction = $HTTP_SERVER_VARS['PHP_SELF'];
    if (isset($HTTP_SERVER_VARS['QUERY_STRING'])) {
      $editFormAction .= "?" . $HTTP_SERVER_VARS['QUERY_STRING'];
    }
     
    if ((isset($HTTP_POST_VARS["MM_insert"])) && ($HTTP_POST_VARS["MM_insert"] == "inscription")) {
    $insertSQL = sprintf("INSERT INTO petrolier (idpetrolier, petrolier, nomcomplet, copyrightlogo, natPE, datecreation, arretactivite, causearret, adresse, telephone, fax, email, histoire, siteweb, loginmaj, datemaj) VALUES ('', %s, %s, '-', %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, '1', now())",
    						GetSQLValueString($HTTP_POST_VARS['petrolier'], "text"),
    						GetSQLValueString($HTTP_POST_VARS['nomcomplet'], "text"),
    					   GetSQLValueString($HTTP_POST_VARS['natPE'], "text"),
    						GetSQLValueString($HTTP_POST_VARS['datecreation'], "text"),
    					   GetSQLValueString($HTTP_POST_VARS['arretactivite'], "text"),
    					   GetSQLValueString($HTTP_POST_VARS['causearret'], "text"),
    					   GetSQLValueString($HTTP_POST_VARS['adresse'], "text"),
    					   GetSQLValueString($HTTP_POST_VARS['telephone'], "text"),
    					   GetSQLValueString($HTTP_POST_VARS['fax'], "text"),
    					   GetSQLValueString($HTTP_POST_VARS['email'], "text"),
    					   GetSQLValueString($HTTP_POST_VARS['histoire'], "text"),
    					   GetSQLValueString($HTTP_POST_VARS['siteweb'], "text"));
     
      $Result1 = mysql_query($insertSQL) or die(mysql_error());
    	$idpetrolier=mysql_insert_id();
      $insertGoTo = "petrolier_update.php?idpetrolier=$idpetrolier";
      if (isset($HTTP_SERVER_VARS['QUERY_STRING'])) {
        $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
        $insertGoTo .= $HTTP_SERVER_VARS['QUERY_STRING'];
      }
      header(sprintf("Location: %s", $insertGoTo));
    }
     
    ?>
    <!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>
    <style type="text/css">
    @import url(commun/style.css);
    </style>
    <? require("commun/head.php");
    $dateyear=date("Y"); ?>
    </head>
     
    <body>
     
    <div id="page">
     
    <div id="titre"><a href="page.php"><img src="images/icones/btn_setup_bg.gif" title="retour à l'accueil"></a> petroliers</div>
    <div id="filariane">ajouter un petrolier</div>
     
    <?
    $requetehomme="SELECT * FROM pilote WHERE fonctionhomme='1' ORDER BY pilote ASC";
    $resulthomme=mysql_query($requetehomme);
    $requetepays="SELECT DISTINCT nation.nation, nation_detail.idnationdetail FROM nation, nation_detail WHERE nation_detail.actifnationdetail Like '1%' AND nation.idnation = nation_detail.nationID ORDER BY nation ASC";
    $resultpays=mysql_query($requetepays);
    ?>
     
    <a href="petrolier_search.php?idvar=P" target="page">rechercher un pétrolier</a>
     
    <div id="typeAjouter"></div>
    <div id="typeB">
    <form method="post" name="inscription" action="<?php echo $editFormAction; ?>">
    <label>Nom</label><input type="text" class="form3" name="petrolier" ONBLUR="if (value == '') {value = '-'}" ONFOCUS="if (value == '-') {value =''}" VALUE="-" size="80" /><br />
    <label>Nom complet</label><input type="text" class="form3" name="nomcomplet" ONBLUR="if (value == '') {value = '-'}" ONFOCUS="if (value == '-') {value =''}" VALUE="-" size="80" /><br />
    <label>Nationalité</label><select class="form3" name="natPE"><option value="-">-</option><? while($resultatpays=mysql_fetch_array($resultpays)) { ?><option value="<? echo $resultatpays["idnationdetail"]; ?>"><? echo $resultatpays["nation"]; ?></option><? }; ?></select><br />
    <label>Date de création</label><select class="form3" name="datecreation"><option value="?">?</option><? for($i=1800;$i<=$dateyear;$i+=1) {echo "<option>".$i."</option>\n";} ?></select><br />
    <label>Arrêt activité</label><select class="form3" name="arretactivite"><option value="-">-</option><? for($i=$dateyear;$i>=1850;$i-=1) {echo "<option>".$i."</option>\n";} ?></select><br />
    <label>cause arrêt activité</label><input type="text" class="form3" name="causearret" ONBLUR="if (value == '') {value = '-'}" ONFOCUS="if (value == '-') {value =''}" VALUE="-" size="80" /><br />
    <label>Adresse</label><textarea class="form3" name="adresse" cols="60" rows="6" value="-">-</textarea><br />
    <label>Téléphone</label><input class="form3" type="text" name="telephone" ONBLUR="if (value == '') {value = '-'}" ONFOCUS="if (value == '-') {value =''}" VALUE="-" size="80"><br />
    <label>Fax</label><input class="form3" type="text" name="fax" ONBLUR="if (value == '') {value = '-'}" ONFOCUS="if (value == '-') {value =''}" VALUE="-" size="80"><br />
    <label>Email</label><input class="form3" type="text" name="email" ONBLUR="if (value == '') {value = '-'}" ONFOCUS="if (value == '-') {value =''}" VALUE="-" size="80"><br />
    <label>Site internet</label><input class="form3" type="text" name="siteweb" ONBLUR="if (value == '') {value = '-'}" ONFOCUS="if (value == '-') {value =''}" VALUE="-" size="80"><br />
    <label>Commentaire</label><textarea class="form3" name="histoire" cols="60" rows="6" value="-">-</textarea><br />
    <input type="image" name="submit" src="images/icones/ok.gif" alt="valider" />
    <input type="hidden" name="MM_insert" value="inscription">
    </form>
    </div>
     
    </body>
    </html>
    et la page PETROLIER UPDATE

    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
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
     
    <? require("commun/connexion.php") ?>
    <?php
    function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
    {
      $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;
     
      switch ($theType) {
        case "text":
          $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
          break;    
        case "long":
        case "int":
          $theValue = ($theValue != "") ? intval($theValue) : "NULL";
          break;
        case "double":
          $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
          break;
        case "date":
          $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
          break;
        case "defined":
          $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
          break;
      }
      return $theValue;
    }
     
    $editFormAction = $HTTP_SERVER_VARS['PHP_SELF'];
    if (isset($HTTP_SERVER_VARS['QUERY_STRING'])) {
      $editFormAction .= "?" . $HTTP_SERVER_VARS['QUERY_STRING'];
    }
     
    if ((isset($HTTP_POST_VARS["MM_insert"])) && ($HTTP_POST_VARS["MM_insert"] == "inscription")) {
    $insertSQL = sprintf("UPDATE petrolier SET petrolier=%s, nomcomplet=%s, copyrightlogo=%s, natPE=%s, datecreation=%s, arretactivite=%s, causearret=%s, adresse=%s, telephone=%s, fax=%s, email=%s, histoire=%s, siteweb=%s, loginmaj='1', datemaj=now() WHERE idpetrolier=%s",
    						GetSQLValueString($HTTP_POST_VARS['petrolier'], "text"),
    						GetSQLValueString($HTTP_POST_VARS['nomcomplet'], "text"),
    						GetSQLValueString($HTTP_POST_VARS['copyrightlogo'], "text"),
    					   GetSQLValueString($HTTP_POST_VARS['natPE'], "text"),
    						GetSQLValueString($HTTP_POST_VARS['datecreation'], "text"),
    					   GetSQLValueString($HTTP_POST_VARS['arretactivite'], "text"),
    					   GetSQLValueString($HTTP_POST_VARS['causearret'], "text"),
    					   GetSQLValueString($HTTP_POST_VARS['adresse'], "text"),
    					   GetSQLValueString($HTTP_POST_VARS['telephone'], "text"),
    					   GetSQLValueString($HTTP_POST_VARS['fax'], "text"),
    					   GetSQLValueString($HTTP_POST_VARS['email'], "text"),
    					   GetSQLValueString($HTTP_POST_VARS['histoire'], "text"),
    					   GetSQLValueString($HTTP_POST_VARS['siteweb'], "text"),
    					   GetSQLValueString($HTTP_POST_VARS['idpetrolier'], "INT"));
     
      $Result1 = mysql_query($insertSQL) or die(mysql_error());
    	$idpetrolier=$_GET['idpetrolier'];
      $insertGoTo = "petrolier_update.php?searchnom=$searchnom&searchpays=$searchpays";
      if (isset($HTTP_SERVER_VARS['QUERY_STRING'])) {
        $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
        $insertGoTo .= $HTTP_SERVER_VARS['QUERY_STRING'];
      }
      header(sprintf("Location: %s", $insertGoTo));
    }
    $idpetrolier=$_GET['idpetrolier'];
    $query_rsliste = "SELECT * FROM petrolier WHERE idpetrolier='$idpetrolier'";
    $rsliste = mysql_query($query_rsliste) or die(mysql_error());
    $totalRows = mysql_num_rows($rsliste);
    $row = mysql_fetch_array($rsliste);
    $sqlfondateur = "SELECT * FROM petrolier_fondateur, homme WHERE homme.idhomme=petrolier_fondateur.hommeID AND petrolier_fondateur.petrolierID='$idpetrolier' ORDER by homme ASC";
    $resultfondateur = mysql_query($sqlfondateur) or die(mysql_error());
    ?>
    <!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>
    <style type="text/css">
    @import url(commun/style.css);
    </style>
    <? require("commun/head.php");
    $dateyear=date("Y"); ?>
    </head>
     
    <body>
     
    <?
    $requetehomme="SELECT * FROM pilote WHERE fonctionhomme='1' ORDER BY pilote ASC";
    $resulthomme=mysql_query($requetehomme);
    $idmaj=$row["loginmaj"];
    $requetemaj="SELECT * FROM x_membres WHERE idmembres='$idmaj'";
    $resultmaj=mysql_query($requetemaj);
    $resultatmaj=mysql_fetch_array($resultmaj);
    $requetepays="SELECT DISTINCT nation.nation, nation_detail.idnationdetail FROM nation, nation_detail WHERE nation_detail.actifnationdetail Like '1%' AND nation.idnation = nation_detail.nationID ORDER BY nation ASC";
    $resultpays=mysql_query($requetepays);
    ?>
     
    <div id="page">
     
    <div id="titre">
    Modifier un petrolier
    </div>
     
    <div id="filariane">
    <a href="page.php" target="page">accueil</a> > <a href="petrolier_search.php" target="page">rechercher un petrolier</a> > modifier un petrolier
    </div>
    <a href="petrolier_search.php?idvar=P" target="page">rechercher un pétrolier</a>
    <a href="petrolier_add.php" target="page">ajouter un pétrolier</a>
     
    <? echo $row["petrolier"]; ?>
     
    <div id="typeAjouter"></div>
    <div id="typeB">
    <form method="post" name="inscription" action="<?php echo $editFormAction; ?>">
    <label>Code petrolier</label><input class="form3" type="text" disabled VALUE="<? echo $row["idpetrolier"]; ?>" size="80"><br />
    <label>Nom</label><input class="form3" type="text" name="petrolier" VALUE="<? echo $row["petrolier"]; ?>" size="80"><br />
    <label>Nom complet</label><input class="form3" type="text" name="nomcomplet" VALUE="<? echo $row["nomcomplet"]; ?>" size="80"><br />
    <label>logo petrolier</label><input class="form3" type="image" disabled src="images/petrolier/logo/<? echo $row["idpetrolier"]; ?>.gif" align="absmiddle"><br />
    <label>copyright logo</label><input type="text" class="form3" name="copyrightlogo" VALUE="<? echo $row["copyrightlogo"]; ?>" size="80" /><br />
    <label>Nationalité</label><select class="form3" name="natPE"><option value="-">-</option><? while($resultatpays=mysql_fetch_array($resultpays)) { ?><option value="<? echo $resultatpays["idnationdetail"]; ?>" <? if ($resultatpays["idnationdetail"]==$row["natPE"]) {echo " selected";} else {}; ?>><? echo $resultatpays["nation"]; ?></option><? }; ?></select>&nbsp;<input class="form3" type="image" disabled src="images/flag/flag13/<? echo $row["natPE"]; ?>.jpg" align="absmiddle"><br />
    <label>date de création</label><select class="form3" name="datecreation"><? $built2=$row["datecreation"]; ?><option value="?">?</option><? for($i=1800;$i<=$dateyear;$i+=1) {echo "<option "; if ($built2==$i) echo "SELECTED"; echo ">".$i."</option>\n";} ?></select><br />
    <label>arret activite</label><select class="form3" name="arretactivite"><? $closed2=$row["arretactivite"]; ?><option value="-">-</option><? for($i=$dateyear;$i>=1850;$i-=1) {echo "<option "; if ($closed2==$i) echo "SELECTED"; echo ">".$i."</option>\n";} ?></select><br />
    <label>cause arret</label><input class="form3" type="text" name="causearret" VALUE="<? echo $row["causearret"]; ?>" size="80"><br />
    <label>Adresse</label><textarea class="form3" name="adresse" cols="60" rows="6" value="-"><? echo $row["adresse"]; ?></textarea><br />
    <label>Téléphone</label><input class="form3" type="text" name="telephone" VALUE="<? echo $row["telephone"]; ?>" size="80"><br />
    <label>Fax</label><input class="form3" type="text" name="fax" VALUE="<? echo $row["fax"]; ?>" size="80"><br />
    <label>Email</label><input class="form3" type="text" name="email" VALUE="<? echo $row["email"]; ?>" size="80"><br />
    <label>Site internet</label><input class="form3" type="text" name="siteweb" VALUE="<? echo $row["siteweb"]; ?>" size="80"><br />
    <label>histoire</label><textarea class="form3" name="histoire" cols="60" rows="6" value="-"><? echo $row["histoire"]; ?></textarea><br />
    <label>Mise à jour</label><input class="form3" type="text" disabled VALUE="<? echo $resultatmaj["login"]; ?>" size="80"><br />
    <label>Date de mise à jour</label><input class="form3" type="text" disabled VALUE="<? echo $row["datemaj"]; ?>" size="80"><br />
    <input type="image" name="submit" src="images/icones/ok.gif" alt="valider" />
    <input type="hidden" name="idpetrolier" value="<? echo $idpetrolier; ?>">
    <input type="hidden" name="MM_insert" value="inscription">
    </form>
    </div>
    detail petroliers
    <div id="add"><a href="petrolier_fondateur_add.php?idpetrolier=<? echo $idpetrolier; ?>">ajouter un fondateur</a></div>
     
    <div id="typeResultats"></div>
    <div id="typeC">
    <table>
    <tr>
    <th></th>
    <th></th>
    <th>CODE</th>
    <th>PAYS</th>
    <th>NOM</th>
    <th></th>
    </tr>
    <?
    $color="#D6DFF7";
    $color1="white";
    $color2="#D6DFF7";
    while ($resultatfondateur = mysql_fetch_array($resultfondateur))
    {?>
    <tr onMouseOut=this.style.backgroundColor="<? echo $color; ?>" onMouseOver=this.style.backgroundColor="yellow" style="background-color: <? echo $color; ?>">
    <td width="16"><a href="petrolier_fondateur_update.php?idpetrolier=<? echo $idpetrolier; ?>&idpetrolierfondateur=<? echo $resultatfondateur['idpetrolierfondateur']; ?>&idhommefondateur=<? echo $resultatfondateur['hommeID']; ?>"><img src="images/icones/b_edit.png" alt="modifier"></a></td>
    <td width="16"><a href="etablissement_update.php?drop=ok&iddetailseries=<? echo $resultattypeseries['id_detailsaison']; ?>&idtypecategorie=<? echo $idtypecategorie; ?>"><img src="images/icones/b_browse.png" alt="afficher"></a></td>
    <td><? echo $resultatfondateur['idpetrolierfondateur']; ?></td>
    <td><img src="images/flag/flag13/<? echo $resultatfondateur["natHO"]; ?>.jpg" alt="<? echo $resultat["nation"]; ?>"></td>
    <td><? echo $resultatfondateur['homme']; ?></td>
    <td width="16"><a href="petrolier_fondateur_delete.php?idpetrolier=<? echo $idpetrolier; ?>&idpetrolierfondateur=<? echo $resultatfondateur['idpetrolierfondateur']; ?>"><img src="images/icones/b_drop.png" alt="supprimer"></a></td>
    </tr>
    <?
    $color=$color1;
    $color1=$color2;
    $color2=$color;
    };
    ?>
    </table>
    </div>
     
    </div>
     
    </body>
    </html>

  2. #2
    Rédacteur

    Avatar de Yogui
    Homme Profil pro
    Directeur technique
    Inscrit en
    Février 2004
    Messages
    13 721
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Yonne (Bourgogne)

    Informations professionnelles :
    Activité : Directeur technique

    Informations forums :
    Inscription : Février 2004
    Messages : 13 721
    Par défaut
    Salut

    Une première étape serait de séparer logique et affichage en utilisant un moteur de templates (il me semble qu'il y a une discussion en post-it).
    Ainsi, tu pourrais certainement réutiliser ton HTML et donc en avoir moins.

    Ensuite, peut-être est-il possible de passer à un niveau d'abstraction supérieur. Tes pages de recherche, ajout, modification et suppression ont probablement de nombreux points commun selon le produit traité. Si c'est le cas, alors il sera possible de les regrouper, ce qui te permettra encore d'alléger ton code.
    Tu pourras alors appeler tes scripts avec une syntaxe de ce type :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    recherche.php?what=motoriste
    suppression.php?what=pilote&id=3
    Désolé si je ne donne que des pistes : je préfère tenter de t'orienter dans la bonne direction que te donner la solution toute prête (ce qui ne te pousserait pas à apprendre).

    Bonne chance !

Discussions similaires

  1. Transcrire code PHP dans une DLL
    Par LEK dans le forum C++
    Réponses: 16
    Dernier message: 26/09/2005, 14h17
  2. Mettre un code php dans une variable...
    Par kedare dans le forum Langage
    Réponses: 6
    Dernier message: 19/09/2005, 12h55
  3. [Conception] Code php dans une base de donnée
    Par krfa1 dans le forum PHP & Base de données
    Réponses: 8
    Dernier message: 13/09/2005, 10h58
  4. pb eval pour le code php dans une feuille xslt
    Par nipepsi dans le forum XSL/XSLT/XPATH
    Réponses: 8
    Dernier message: 10/09/2004, 11h23

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo