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 :

Optimisation et Boucle For


Sujet :

Langage PHP

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre à l'essai
    Homme Profil pro
    Étudiant
    Inscrit en
    Juillet 2016
    Messages
    5
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 31
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Associations - ONG

    Informations forums :
    Inscription : Juillet 2016
    Messages : 5
    Par défaut Optimisation et Boucle For
    Bonjour à tous,

    Je travail actuellement sur mon site et on ma conseillé d'optimiser mes pages en y ajoutant une boucle For.
    J'ai pas mal fais de tentative sans résultat.

    Le code initiale :

    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
    <?php session_start();
    date_default_timezone_set('Europe/Paris');
    include('./lib/class-db.php');
    include('./lib/objects/class-tournament.php');
     
    $ez_tournament = new ezAdmin_Tournament();
     
         if(!isset($_SESSION['ez_admin'])) {
            header("Location: login.php");
         } else {
            $username = $_SESSION['ez_admin'];
         }
    if( isset( $_POST['form'] ) && isset( $_POST['tournament_id'] ) ) {
        $form = trim( $_POST['form'] );
        $tournament_id = trim( $_POST['tournament_id'] );
     
        if( $form == 'generate-matches' ) {
            // clear any previously generated matchups first
            $ez_tournament->clear_tournament_matchups( $tournament_id );
     
            // randomize tournament matchups and store them
            $tournament_max_teams = trim( $_POST['max_teams'] );
            $teams = $ez_tournament->get_tournament_teams( $tournament_id );
            shuffle( $teams );
        ?>
     
            <li class="spacer">&nbsp;</li>
            <!-- wrap score inside span element -->
            <li class="game game-top"><?php echo $teams[0]['guild']; ?></li>
            <li class="game game-spacer">&nbsp;</li>
            <li class="game game-bottom "><?php echo $teams[1]['guild']; ?></li>
            <?php $ez_tournament->set_tournament_matchups($tournament_id, $teams[0]['guild'], $teams[0]['id'], $teams[1]['guild'], $teams[1]['id'], '1'); ?>
            <li class="spacer">&nbsp;</li>
     
            <li class="game game-top"><?php echo $teams[2]['guild']; ?></li>
            <li class="game game-spacer">&nbsp;</li>
            <li class="game game-bottom "><?php echo $teams[3]['guild']; ?></li>
            <?php $ez_tournament->set_tournament_matchups($tournament_id, $teams[2]['guild'], $teams[2]['id'], $teams[3]['guild'], $teams[3]['id'], '1'); ?>
            <li class="spacer">&nbsp;</li>
            <?php if( $tournament_max_teams >= 8 ) { ?>
            <li class="game game-top "><?php echo $teams[4]['guild']; ?></li>
            <li class="game game-spacer">&nbsp;</li>
            <li class="game game-bottom"><?php echo $teams[5]['guild']; ?></li>
            <?php $ez_tournament->set_tournament_matchups($tournament_id, $teams[4]['guild'], $teams[4]['id'], $teams[5]['guild'], $teams[5]['id'], '1'); ?>
            <li class="spacer">&nbsp;</li>
     
            <li class="game game-top"><?php echo $teams[6]['guild']; ?></li>
            <li class="game game-spacer">&nbsp;</li>
            <li class="game game-bottom "><?php echo $teams[7]['guild']; ?></li>
            <?php $ez_tournament->set_tournament_matchups($tournament_id, $teams[6]['guild'], $teams[6]['id'], $teams[7]['guild'], $teams[7]['id'], '1'); ?>
            <li class="spacer">&nbsp;</li>
            <?php }
     
            if( $tournament_max_teams >= 16 ) { ?>
            <li class="game game-top "><?php echo $teams[8]['guild']; ?></li>
            <li class="game game-spacer">&nbsp;</li>
            <li class="game game-bottom"><?php echo $teams[9]['guild']; ?></li>
            <?php $ez_tournament->set_tournament_matchups($tournament_id, $teams[8]['guild'], $teams[8]['id'], $teams[9]['guild'], $teams[9]['id'], '1'); ?>
            <li class="spacer">&nbsp;</li>
     
            <li class="game game-top"><?php echo $teams[10]['guild']; ?></li>
            <li class="game game-spacer">&nbsp;</li>
            <li class="game game-bottom "><?php echo $teams[11]['guild']; ?></li>
            <?php $ez_tournament->set_tournament_matchups($tournament_id, $teams[10]['guild'], $teams[10]['id'], $teams[11]['guild'], $teams[11]['id'], '1'); ?>
            <li class="spacer">&nbsp;</li>
     
            <li class="game game-top "><?php echo $teams[12]['guild']; ?></li>
            <li class="game game-spacer">&nbsp;</li>
            <li class="game game-bottom"><?php echo $teams[13]['guild']; ?></li>
            <?php $ez_tournament->set_tournament_matchups($tournament_id, $teams[12]['guild'], $teams[12]['id'], $teams[13]['guild'], $teams[13]['id'], '1'); ?>
            <li class="spacer">&nbsp;</li>
     
            <li class="game game-top"><?php echo $teams[14]['guild']; ?></li>
            <li class="game game-spacer">&nbsp;</li>
            <li class="game game-bottom "><?php echo $teams[15]['guild']; ?></li>
            <?php $ez_tournament->set_tournament_matchups($tournament_id, $teams[14]['guild'], $teams[14]['id'], $teams[15]['guild'], $teams[15]['id'], '1'); ?>
            <li class="spacer">&nbsp;</li>
     
            <?php }
     
            if( $tournament_max_teams >= 32 ) { ?>
            <li class="game game-top "><?php echo $teams[16]['guild']; ?></li>
            <li class="game game-spacer">&nbsp;</li>
            <li class="game game-bottom"><?php echo $teams[17]['guild']; ?></li>
            <?php $ez_tournament->set_tournament_matchups($tournament_id, $teams[16]['guild'], $teams[16]['id'], $teams[17]['guild'], $teams[17]['id'], '1'); ?>
            <li class="spacer">&nbsp;</li>
     
            <li class="game game-top"><?php echo $teams[18]['guild']; ?></li>
            <li class="game game-spacer">&nbsp;</li>
            <li class="game game-bottom "><?php echo $teams[19]['guild']; ?></li>
            <?php $ez_tournament->set_tournament_matchups($tournament_id, $teams[18]['guild'], $teams[18]['id'], $teams[19]['guild'], $teams[19]['id'], '1'); ?>
            <li class="spacer">&nbsp;</li>
     
            <li class="game game-top "><?php echo $teams[20]['guild']; ?></li>
            <li class="game game-spacer">&nbsp;</li>
            <li class="game game-bottom"><?php echo $teams[21]['guild']; ?></li>
            <?php $ez_tournament->set_tournament_matchups($tournament_id, $teams[20]['guild'], $teams[20]['id'], $teams[21]['guild'], $teams[21]['id'], '1'); ?>
            <li class="spacer">&nbsp;</li>
     
            <li class="game game-top"><?php echo $teams[22]['guild']; ?></li>
            <li class="game game-spacer">&nbsp;</li>
            <li class="game game-bottom "><?php echo $teams[23]['guild']; ?></li>
            <?php $ez_tournament->set_tournament_matchups($tournament_id, $teams[22]['guild'], $teams[22]['id'], $teams[23]['guild'], $teams[23]['id'], '1'); ?>
            <li class="spacer">&nbsp;</li>
     
            <li class="game game-top "><?php echo $teams[24]['guild']; ?></li>
            <li class="game game-spacer">&nbsp;</li>
            <li class="game game-bottom"><?php echo $teams[25]['guild']; ?></li>
            <?php $ez_tournament->set_tournament_matchups($tournament_id, $teams[24]['guild'], $teams[24]['id'], $teams[25]['guild'], $teams[25]['id'], '1'); ?>
            <li class="spacer">&nbsp;</li>
     
            <li class="game game-top"><?php echo $teams[26]['guild']; ?></li>
            <li class="game game-spacer">&nbsp;</li>
            <li class="game game-bottom "><?php echo $teams[27]['guild']; ?></li>
            <?php $ez_tournament->set_tournament_matchups($tournament_id, $teams[26]['guild'], $teams[26]['id'], $teams[27]['guild'], $teams[27]['id'], '1'); ?>
            <li class="spacer">&nbsp;</li>
     
            <li class="game game-top "><?php echo $teams[28]['guild']; ?></li>
            <li class="game game-spacer">&nbsp;</li>
            <li class="game game-bottom"><?php echo $teams[29]['guild']; ?></li>
            <?php $ez_tournament->set_tournament_matchups($tournament_id, $teams[28]['guild'], $teams[28]['id'], $teams[29]['guild'], $teams[29]['id'], '1'); ?>
            <li class="spacer">&nbsp;</li>
     
            <li class="game game-top"><?php echo $teams[30]['guild']; ?></li>
            <li class="game game-spacer">&nbsp;</li>
            <li class="game game-bottom "><?php echo $teams[31]['guild']; ?></li>
            <?php $ez_tournament->set_tournament_matchups($tournament_id, $teams[30]['guild'], $teams[30]['id'], $teams[31]['guild'], $teams[31]['id'], '1'); ?>
            <li class="spacer">&nbsp;</li>
     
            <?php } ?>
    <?php
        }
     
        if( $form == 'clear-matches' ) {
            $ez_tournament->clear_tournament_matchups( $tournament_id );
        }
    }
    ?>
    J'ai essayer plusieurs choses mais je pense que celle qui s'approche le plus est celle ci :

    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
    <?php session_start();
    date_default_timezone_set('Europe/Paris');
    include('./lib/class-db.php');
    include('./lib/objects/class-tournament.php');
     
    $ez_tournament = new ezAdmin_Tournament();
     
         if(!isset($_SESSION['ez_admin'])) {
            header("Location: login.php");
         } else {
            $username = $_SESSION['ez_admin'];
         }
    if( isset( $_POST['form'] ) && isset( $_POST['tournament_id'] ) ) {
        $form = trim( $_POST['form'] );
        $tournament_id = trim( $_POST['tournament_id'] );
     
        if( $form == 'generate-matches' ) {
            // clear any previously generated matchups first
            $ez_tournament->clear_tournament_matchups( $tournament_id );
     
            // randomize tournament matchups and store them
            $tournament_max_teams = trim( $_POST['max_teams'] );
            $teams = $ez_tournament->get_tournament_teams( $tournament_id );
            shuffle( $teams ); }
        ?>
     
            <li class="spacer">&nbsp;</li>
     
        <?php
     
        for($i=0;$i<=$tournament_max_teams;$i+2;) { ?>
     
            <li class="game game-top"><?php echo $teams[$i]['guild']; ?></li>
            <li class="game game-spacer">&nbsp;
            <li class="game game-bottom "><?php echo $teams[$i++]['guild']; ?></li>
            <?php $ez_tournament->set_tournament_matchups($tournament_id, $teams[$i]['guild'], $teams[$i]['id'], $teams[$i++]['guild'], $teams[$i++]['id'], '1'); ?>
            <li class="spacer">&nbsp;</li>
     
                <?php } ?>
    <?php
        }
     
        if( $form == 'clear-matches' ) {
            $ez_tournament->clear_tournament_matchups( $tournament_id );
        }
    }
    ?>
    Un coup de main ne serait pas de refut.
    Il s'agit d'un site de tournoi (pour information)

  2. #2
    Modérateur
    Avatar de sabotage
    Homme Profil pro
    Inscrit en
    Juillet 2005
    Messages
    29 208
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations forums :
    Inscription : Juillet 2005
    Messages : 29 208
    Par défaut
    Tu fais des erreurs de syntaxe :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    <?php
    $i = 0;
     
    echo $i + 2; // 2;
    echo $i; // 0 : il n'y a pas eu de modification de $i
     
    echo $i += 2; // 2
     
    echo $i = $i + 2; // 4 // même chose qu'au dessus en version longue
     
    echo $i++; // 4 : $i est retourné PUIS incrementé 
    echo $i; // 5 : $i a bien été incrementé
    echo ++$i; //6 : $i est incrementé puis retourné
    Donc
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
     
    <?php
    for($i=0; $i < $tournament_max_teams; $i += 2;) { ?>
     
            <li class="game game-top"><?php echo $teams[$i]['guild']; ?></li>
            <li class="game game-spacer">&nbsp;
            <li class="game game-bottom "><?php echo $teams[$i+1]['guild']; ?></li>
            <?php $ez_tournament->set_tournament_matchups($tournament_id, $teams[$i]['guild'], $teams[$i]['id'], $teams[$i+1]['guild'], $teams[$i+1]['id'], '1'); ?>
            <li class="spacer">&nbsp;</li>
     
    <?php } ?>
    N'oubliez pas de consulter les FAQ PHP et les cours et tutoriels PHP

  3. #3
    Membre à l'essai
    Homme Profil pro
    Étudiant
    Inscrit en
    Juillet 2016
    Messages
    5
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 31
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Associations - ONG

    Informations forums :
    Inscription : Juillet 2016
    Messages : 5
    Par défaut
    Merci pour votre aide, je vais essayer de comprendre mes erreurs puis je le referrais de mon côté.
    Bonne soirée !

  4. #4
    Membre à l'essai
    Homme Profil pro
    Étudiant
    Inscrit en
    Juillet 2016
    Messages
    5
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 31
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Associations - ONG

    Informations forums :
    Inscription : Juillet 2016
    Messages : 5
    Par défaut
    Bonjour !

    Je reviens vers vous car malheureusement mon arbre ne se lance pas. J'ai essayé toutes la journée une solution mais la je ne comprend pas.

    J'ai fais les modifications sur mon fichier est à présent j'ai donc :

    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
    <?php session_start();
    date_default_timezone_set('Europe/Paris');
    include('./lib/class-db.php');
    include('./lib/objects/class-tournament.php');
     
    $ez_tournament = new ezAdmin_Tournament();
     
         if(!isset($_SESSION['ez_admin'])) {
         	header("Location: login.php");
         } else {
         	$username = $_SESSION['ez_admin'];
         }
    if( isset( $_POST['form'] ) && isset( $_POST['tournament_id'] ) ) {
        $form = trim( $_POST['form'] );
        $tournament_id = trim( $_POST['tournament_id'] );
     
        if( $form == 'generate-matches' ) {
            // clear any previously generated matchups first
            $ez_tournament->clear_tournament_matchups( $tournament_id );
     
            // randomize tournament matchups and store them
            $tournament_max_teams = trim( $_POST['max_teams'] );
            $teams = $ez_tournament->get_tournament_teams( $tournament_id );
            shuffle( $teams ); }
        ?>
     
            <li class="spacer">&nbsp;</li>
     
    <?php for($i=0; $i =< $tournament_max_teams; $i+2;) { ?>
     
            <li class="game game-top"><?php echo $teams[$i]['guild']; ?></li>
            <li class="game game-spacer">&nbsp;</li>
            <li class="game game-bottom "><?php echo $teams[$i+1]['guild']; ?></li>
            <?php $ez_tournament->set_tournament_matchups($tournament_id, $teams[$i]['guild'], $teams[$i]['id'], $teams[$i+1]['guild'], $teams[$i+1]['id'], '1'); ?>
            <li class="spacer">&nbsp;</li>
     
    <?php } ?>
    <?php 
     
        if( $form == 'clear-matches' ) {
            $ez_tournament->clear_tournament_matchups( $tournament_id );
        }
    }
    ?>
    Quand je clique sur le bouton pour générer mon arbre je n'ai rien et sur ma console j'ai : "POST http://addict-esport.fr/tournoi/admi...01-bracket.php 500 Internal Server Error 102ms"

  5. #5
    Modérateur
    Avatar de sabotage
    Homme Profil pro
    Inscrit en
    Juillet 2005
    Messages
    29 208
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations forums :
    Inscription : Juillet 2005
    Messages : 29 208
    N'oubliez pas de consulter les FAQ PHP et les cours et tutoriels PHP

  6. #6
    Invité
    Invité(e)
    Par défaut
    Bonjour,

    j'ai une ou deux remarques...

    si j'ai bien compris :
    • $tournament_max_teams est le nombre TOTAL d'équipes sur CE tournoi.
    • la boucle for parcourt ces équipes DEUX par DEUX : $teams[$i] et $teams[$i+1].


    Donc, la boucle devrait être :
    Code php : Sélectionner tout - Visualiser dans une fenêtre à part
    <?php for($i=0; $i <= floor($tournament_max_teams/2); $i+=2;) { ?>

    N.B. Si $tournament_max_teams est impair, la dernière équipe n'entre pas dans la boucle.

    Astuce pour tester un nombre pair/impair : le modulo "%" (voir : Les opérateurs arithmétiques)
    Code php : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    if( $nombre%2 == 0){
      echo $nombre.' est pair';
    }
    if( $nombre%2 == 1){
      echo $nombre.' est impair';
    }

Discussions similaires

  1. Optimiser une boucle For Each
    Par zaghi dans le forum VB.NET
    Réponses: 14
    Dernier message: 21/06/2012, 12h19
  2. Optimisation des boucles for
    Par Kikouyou1080 dans le forum Général Python
    Réponses: 5
    Dernier message: 04/06/2010, 18h16
  3. optimiser une boucle for
    Par bakaratoun dans le forum MATLAB
    Réponses: 2
    Dernier message: 28/01/2010, 15h22
  4. Optimiser 2 boucles FOR ?
    Par B&B dans le forum SQL
    Réponses: 6
    Dernier message: 18/04/2008, 16h43
  5. Comment optimiser plusieurs boucles FOR-END imbriquées
    Par totoc1001 dans le forum MATLAB
    Réponses: 26
    Dernier message: 13/05/2007, 18h59

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