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

PHP & Base de données Discussion :

Foreach et select


Sujet :

PHP & Base de données

  1. #1
    Membre confirmé
    Profil pro
    Inscrit en
    Mars 2011
    Messages
    131
    Détails du profil
    Informations personnelles :
    Localisation : Canada

    Informations forums :
    Inscription : Mars 2011
    Messages : 131
    Par défaut Foreach et select
    Bonjour,

    Depuis plusieurs jours je me fracasse la tête sur ce script ou je n'arrive pas à trouver de solution.
    Le problème est d'arriver à créer différents select html avec les bonnes valeurs.
    Actuellement les select sont crées et dupliqués à chaque fois avec des valeurs qui s'ajoutent !
    Pour créer une liste d'options de produit, cela ne fonctionne pas

    Si avez une idées pour arriver le le bon rendu, je suis preneur (cf 2/)


    Merci de votre aide.


    Si vous avez besoins compléments d'information notamment de données, n'hésitez pas.

    Comme vous pouvez le voir actuellement le rendu est comme ceci :

    1/ rendu actuel : cf https://jsfiddle.net/8zbqxyqo/2/

    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
    <div class="form-group">
    <div class="form-group">
    <label class="control-label" for="input-option">Couleur</label>
    <select name="option[]" id="input-option" class="form-control">
    <option value="">--- Selectioner ---</option>
    <option value=""> Red() + : 50</option>
    </select>
    </div>
    <div class="form-group">
    <label class="control-label" for="input-option">Couleur</label>
    <select name="option[]" id="input-option" class="form-control">
    <option value="">--- Selectioner ---</option>
    <option value=""> Red() + : 50</option>
    <option value=""> Vert() </option>
    </select>
    </div>
    <div class="form-group">
    <label class="control-label" for="input-option">Taille</label>
    <select name="option[]" id="input-option" class="form-control">
    <option value="">--- Selectioner ---</option>
    <option value=""> Red() + : 50</option>
    <option value=""> Vert() </option>
    <option value=""> S() </option>
    </select>
    </div>
    <div class="form-group">
    <label class="control-label" for="input-option">Taille</label>
    <select name="option[]" id="input-option" class="form-control">
    <option value="">--- Selectioner ---</option>
    <option value=""> Red() + : 50</option>
    <option value=""> Vert() </option>
    <option value=""> S() </option>
    <option value=""> M() + : 20</option>
    </select>
    </div>
    </div>
    2 / voici le bon rendu qui doit être généré :

    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
    <div class="form-group">
    <label class="control-label" for="input-option">Couleur</label>
    <select name="option[1]" id="input-option1" class="form-control">
    <option value="">--- Selectioner ---</option>
    <option value=""> Red() + : 50</option>
    <option value=""> Vert() </option>
    </select>
    </div>
     
    <div class="form-group">
    <label class="control-label" for="input-option">Taille</label>
    <select name="option[2]" id="input-option2" class="form-control">
    <option value="">--- Selectioner ---</option>
    <option value=""> S() </option>
    <option value=""> M() + : 20</option>
    </select>
    </div>

    Voici le code php en conséquence avec les informations complémentaires pour vous aider.

    la fonction

    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
     
    public function getProductOptions($product_id) {
          $QproductOption = $this->db->prepare('select * 
                                                from :table_test_products_option po left join :table_test_products_options_attributes o ON (po.option_id = o.option_id) 
                                                                                    left join :table_test_products_options_attributes_description od ON (o.option_id = od.option_id) 
                                                where po.products_id = :products_id
                                                and od.language_id = :language_id
                                                order by o.sort_order
                                                ');
     
          $QproductOption->bindInt(':products_id', $product_id );
          $QproductOption->bindInt(':language_id', $this->lang->getId());
          $QproductOption->execute();
     
          $products_option = $QproductOption->fetchAll();
     
          foreach ($products_option as $key => $product_option) {
     
            $QproductOptionValue = $this->db->prepare('select * 
                                                       from :table_test_products_option_value pov left join :table_test_products_options_attributes_value ov ON (pov.option_value_id = ov.option_value_id) 
                                                                                                  left join :table_test_products_options_attributes_value_description ovd ON (ov.option_value_id = ovd.option_value_id) 
                                                      where pov.products_id = :products_id
                                                      and pov.products_option_id = :products_option_id
                                                      and ovd.language_id = :language_id
                                                      order by ov.sort_order
                                                      ');
     
            $QproductOptionValue->bindInt(':products_id', $product_id );
            $QproductOptionValue->bindInt(':language_id', $this->lang->getId());
            $QproductOptionValue->bindInt(':products_option_id', $product_option['products_option_id']);
            $QproductOptionValue->execute();
     
            $product_option_value = $QproductOptionValue->fetchAll();
     
            foreach ($product_option_value as  $pov => $products_value) {
              $product_option_value_data[] = ['products_option_value_id' => $products_value['products_option_value_id'],
                                              'option_value_id'         => $products_value['option_value_id'],
                                              'name'                    => $products_value['name'],
                                              'image'                   => $products_value['image'],
                                              'quantity'                => $products_value['quantity'],
                                              'subtract'                => $products_value['subtract'],
                                              'price'                   => $products_value['price'],
                                              'price_prefix'            => $products_value['price_prefix'],
                                              'weight'                  => $products_value['weight'],
                                              'weight_prefix'           => $products_value['weight_prefix'],
                                              'customers_group_id'      => $products_value['customers_group_id'],
                                              'products_option_model'   => $products_value['products_option_model'],
                                              'option_tax_class_id'     => $products_value['option_tax_class_id']
                                            ];
            }
     
            $product_option_data[] = [
                                      'products_option_id'    => $product_option['products_option_id'],
                                      'option_id'            => $product_option['option_id'],
                                      'name'                 => $product_option['name'],
                                      'type'                 => $product_option['type'],
                                      'value'                => $product_option['value'],
                                      'required'             => $product_option['required'],
                                      'products_option_value' => $product_option_value_data
                                    ];
          }
     
          return $product_option_data;
        }

    résultat de $product_option_value_data


    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
     
    array (size=1)
      0 => 
        array (size=13)
          'products_option_value_id' => string '283' (length=3)
          'option_value_id' => string '187' (length=3)
          'name' => string 'Red' (length=3)
          'image' => null
          'quantity' => string '200' (length=3)
          'subtract' => string '0' (length=1)
          'price' => string '50.0000' (length=7)
          'price_prefix' => string '+' (length=1)
          'weight' => string '0.00' (length=4)
          'weight_prefix' => string '+' (length=1)
          'customers_group_id' => string '0' (length=1)
          'products_option_model' => string '' (length=0)
          'option_tax_class_id' => string '0' (length=1)
     
    array (size=2)
      0 => 
        array (size=13)
          'products_option_value_id' => string '283' (length=3)
          'option_value_id' => string '187' (length=3)
          'name' => string 'Red' (length=3)
          'image' => null
          'quantity' => string '200' (length=3)
          'subtract' => string '0' (length=1)
          'price' => string '50.0000' (length=7)
          'price_prefix' => string '+' (length=1)
          'weight' => string '0.00' (length=4)
          'weight_prefix' => string '+' (length=1)
          'customers_group_id' => string '0' (length=1)
          'products_option_model' => string '' (length=0)
          'option_tax_class_id' => string '0' (length=1)
      1 => 
        array (size=13)
          'products_option_value_id' => string '282' (length=3)
          'option_value_id' => string '186' (length=3)
          'name' => string 'Vert' (length=4)
          'image' => null
          'quantity' => string '0' (length=1)
          'subtract' => string '0' (length=1)
          'price' => string '0.0000' (length=6)
          'price_prefix' => string '+' (length=1)
          'weight' => string '0.00' (length=4)
          'weight_prefix' => string '+' (length=1)
          'customers_group_id' => string '0' (length=1)
          'products_option_model' => string '' (length=0)
          'option_tax_class_id' => string '0' (length=1)
     
    array (size=3)
      0 => 
        array (size=13)
          'products_option_value_id' => string '283' (length=3)
          'option_value_id' => string '187' (length=3)
          'name' => string 'Red' (length=3)
          'image' => null
          'quantity' => string '200' (length=3)
          'subtract' => string '0' (length=1)
          'price' => string '50.0000' (length=7)
          'price_prefix' => string '+' (length=1)
          'weight' => string '0.00' (length=4)
          'weight_prefix' => string '+' (length=1)
          'customers_group_id' => string '0' (length=1)
          'products_option_model' => string '' (length=0)
          'option_tax_class_id' => string '0' (length=1)
      1 => 
        array (size=13)
          'products_option_value_id' => string '282' (length=3)
          'option_value_id' => string '186' (length=3)
          'name' => string 'Vert' (length=4)
          'image' => null
          'quantity' => string '0' (length=1)
          'subtract' => string '0' (length=1)
          'price' => string '0.0000' (length=6)
          'price_prefix' => string '+' (length=1)
          'weight' => string '0.00' (length=4)
          'weight_prefix' => string '+' (length=1)
          'customers_group_id' => string '0' (length=1)
          'products_option_model' => string '' (length=0)
          'option_tax_class_id' => string '0' (length=1)
      2 => 
        array (size=13)
          'products_option_value_id' => string '281' (length=3)
          'option_value_id' => string '179' (length=3)
          'name' => string 'S' (length=1)
          'image' => null
          'quantity' => string '0' (length=1)
          'subtract' => string '0' (length=1)
          'price' => string '0.0000' (length=6)
          'price_prefix' => string '+' (length=1)
          'weight' => string '0.00' (length=4)
          'weight_prefix' => string '+' (length=1)
          'customers_group_id' => string '0' (length=1)
          'products_option_model' => string '' (length=0)
          'option_tax_class_id' => string '0' (length=1)
     
     
    array (size=4)
      0 => 
        array (size=13)
          'products_option_value_id' => string '283' (length=3)
          'option_value_id' => string '187' (length=3)
          'name' => string 'Red' (length=3)
          'image' => null
          'quantity' => string '200' (length=3)
          'subtract' => string '0' (length=1)
          'price' => string '50.0000' (length=7)
          'price_prefix' => string '+' (length=1)
          'weight' => string '0.00' (length=4)
          'weight_prefix' => string '+' (length=1)
          'customers_group_id' => string '0' (length=1)
          'products_option_model' => string '' (length=0)
          'option_tax_class_id' => string '0' (length=1)
      1 => 
        array (size=13)
          'products_option_value_id' => string '282' (length=3)
          'option_value_id' => string '186' (length=3)
          'name' => string 'Vert' (length=4)
          'image' => null
          'quantity' => string '0' (length=1)
          'subtract' => string '0' (length=1)
          'price' => string '0.0000' (length=6)
          'price_prefix' => string '+' (length=1)
          'weight' => string '0.00' (length=4)
          'weight_prefix' => string '+' (length=1)
          'customers_group_id' => string '0' (length=1)
          'products_option_model' => string '' (length=0)
          'option_tax_class_id' => string '0' (length=1)
      2 => 
        array (size=13)
          'products_option_value_id' => string '281' (length=3)
          'option_value_id' => string '179' (length=3)
          'name' => string 'S' (length=1)
          'image' => null
          'quantity' => string '0' (length=1)
          'subtract' => string '0' (length=1)
          'price' => string '0.0000' (length=6)
          'price_prefix' => string '+' (length=1)
          'weight' => string '0.00' (length=4)
          'weight_prefix' => string '+' (length=1)
          'customers_group_id' => string '0' (length=1)
          'products_option_model' => string '' (length=0)
          'option_tax_class_id' => string '0' (length=1)
      3 => 
        array (size=13)
          'products_option_value_id' => string '280' (length=3)
          'option_value_id' => string '180' (length=3)
          'name' => string 'M' (length=1)
          'image' => null
          'quantity' => string '100' (length=3)
          'subtract' => string '0' (length=1)
          'price' => string '20.0000' (length=7)
          'price_prefix' => string '+' (length=1)
          'weight' => string '0.00' (length=4)
          'weight_prefix' => string '+' (length=1)
          'customers_group_id' => string '0' (length=1)
          'products_option_model' => string '' (length=0)
          'option_tax_class_id' => string '0' (length=1)


    résultat de $product_option_data


    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
     
    array (size=4)
      0 => 
        array (size=7)
          'products_option_id' => string '410' (length=3)
          'option_id' => string '42' (length=2)
          'name' => string 'Couleur' (length=7)
          'type' => string 'select' (length=6)
          'value' => null
          'required' => null
          'products_option_value' => 
            array (size=1)
              0 => 
                array (size=13)
                  ...
      1 => 
        array (size=7)
          'products_option_id' => string '409' (length=3)
          'option_id' => string '42' (length=2)
          'name' => string 'Couleur' (length=7)
          'type' => string 'select' (length=6)
          'value' => null
          'required' => null
          'products_option_value' => 
            array (size=2)
              0 => 
                array (size=13)
                  ...
              1 => 
                array (size=13)
                  ...
      2 => 
        array (size=7)
          'products_option_id' => string '408' (length=3)
          'option_id' => string '40' (length=2)
          'name' => string 'Taille' (length=6)
          'type' => string 'select' (length=6)
          'value' => null
          'required' => null
          'products_option_value' => 
            array (size=3)
              0 => 
                array (size=13)
                  ...
              1 => 
                array (size=13)
                  ...
              2 => 
                array (size=13)
                  ...
      3 => 
        array (size=7)
          'products_option_id' => string '407' (length=3)
          'option_id' => string '40' (length=2)
          'name' => string 'Taille' (length=6)
          'type' => string 'select' (length=6)
          'value' => null
          'required' => null
          'products_option_value' => 
            array (size=4)
              0 => 
                array (size=13)
                  ...
              1 => 
                array (size=13)
                  ...
              2 => 
                array (size=13)
                  ...
              3 => 
                array (size=13)
                  ...


    génération du code côté front office :

    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
     
              $data['options'] = [];
     
              $options_array = $this->ProductsAttributesShop->getProductOptions($OSCOM_ProductsCommon->getID());
     
              foreach ($options_array as $option) {
                $product_option_value_data = [];
     
                foreach ($option['products_option_value'] as $option_value) {
                  if (isset($option_value['subtract']) || $option_value['quantity'] > 0) {
                      if ($option_value['price'] > 0) {
                        $price = (float)$option_value['price'];// inclure la taxe
                      } else {
                        $price = false;
                      }
     
                      $product_option_value_data[] =  ['products_option_value_id' => $option_value['products_option_value_id'],
                                                       'option_value_id'         => $option_value['option_value_id'],
                                                       'name'                    => $option_value['name'],
                                                       'image'                   => $option_value['image'],
                                                       'quantity'                => $option_value['quantity'],
                                                       'price'                   => $price,
                                                       'price_prefix'            => $option_value['price_prefix'],
                                                       'weight'                  => $option_value['weight'],
                                                       'weight_prefix'           => $option_value['weight_prefix'],
                                                       'customers_group_id'      => $option_value['customers_group_id'],
                                                       'products_option_model'   => $option_value['products_option_model'],
                                                      ];
                  }
                }
     
                $data['options'][] = [
                                      'products_option_id'    => $option['products_option_id'],
                                      'option_id'            => $option['option_id'],
                                      'name'                 => $option['name'],
                                      'type'                 => $option['type'],
                                      'value'                => $option['value'],
                                      'required'             => $option['required'],
                                      'products_option_value' => $product_option_value_data
                                      ];
              }
     
              $products_options_content_display .= ' <div>';
     
    //******************************************
    // select
    //******************************************
             $products_options_content_display .= '<div class="form-group">'; 
     
              if ($data['options']) {
                $test = '';
     
                foreach ($data['options'] as $key => $option) {
                  if ($option['type'] == 'select') {
                    if ($option['required'] == 1) $required = ' required';
     
                    $test .= '<div class="form-group' . $required . '">';
                    $test .= '<label class="control-label" for="input-option' . $option['product_option_id'] . '">' . $option['name'] . '</label>';
                    $test .= '<select name="option[' . $option['product_option_id'] . ']" id="input-option' . $option['product_option_id'] . ' class="form-control">';
                    $test .= '<option value="">' . OSCOM::getDef('text_select') . '</option>';
     
                    foreach ($option['products_option_value'] as $t => $option_value) {
                      $test .= '<option value="' .  $option_value['product_option_value_id'] . '"> ' . $option_value['name'];
     
                      if (!is_null($option_value['products_option_model'])) {
                        $model = $option_value['products_option_model'];
                        $test .= '(' . $model . ') ';
                      }
     
                      if ($option_value['price'] > 0) {
                        $test .= $option_value['price_prefix'] . ' : ' . $option_value['price'];
                      }
     
                      $test .='</option>';
                    }
     
                    $test .= '</select>';
                    $test .= '</div>';
                  }
                }
              }
     
              $products_options_content_display .= $test;
     
             echo $products_options_content_display;

  2. #2
    Expert confirmé
    Avatar de rawsrc
    Homme Profil pro
    Dev indep
    Inscrit en
    Mars 2004
    Messages
    6 142
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 49
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Dev indep

    Informations forums :
    Inscription : Mars 2004
    Messages : 6 142
    Billets dans le blog
    12
    Par défaut
    Salut,

    sans accès aux données, difficile de creuser.
    Je pense que le problème se situe au niveau des requêtes SQL.
    Il me semble que les données à afficher contiennent déjà le souci $options_array = $this->ProductsAttributesShop->getProductOptions($OSCOM_ProductsCommon->getID());.
    Exécute tes requêtes SQL en dehors du code est vérifie que les données retournées sont propres.

  3. #3
    Modératrice
    Avatar de Celira
    Femme Profil pro
    Développeuse PHP/Java
    Inscrit en
    Avril 2007
    Messages
    8 633
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 40
    Localisation : France

    Informations professionnelles :
    Activité : Développeuse PHP/Java
    Secteur : Industrie

    Informations forums :
    Inscription : Avril 2007
    Messages : 8 633
    Par défaut
    Hum.. Les <select> sont alimentés par $data['options'] si je vois bien. On peut voir le contenu de $data['options'] juste avant la boucle qui s'en sert ?
    Modératrice PHP
    Aucun navigateur ne propose d'extension boule-de-cristal : postez votre code et vos messages d'erreurs. (Rappel : "ça ne marche pas" n'est pas un message d'erreur)
    Cherchez un peu avant poser votre question : Cours et Tutoriels PHP - FAQ PHP - PDO une soupe et au lit !.

    Affichez votre code en couleurs : [CODE=php][/CODE] (bouton # de l'éditeur) et [C=php][/C]

  4. #4
    Membre confirmé
    Profil pro
    Inscrit en
    Mars 2011
    Messages
    131
    Détails du profil
    Informations personnelles :
    Localisation : Canada

    Informations forums :
    Inscription : Mars 2011
    Messages : 131
    Par défaut
    Merci,

    J'en ai profité pour regardé encore une fois. A priori, j'aurais un duplicate de donnéessur une requête. je vais creuser la dessus, peut être qu'au niveau de l'enregistrement cela ne se passe pas comme voulu.
    je te reviens plus tard.

  5. #5
    Membre confirmé
    Profil pro
    Inscrit en
    Mars 2011
    Messages
    131
    Détails du profil
    Informations personnelles :
    Localisation : Canada

    Informations forums :
    Inscription : Mars 2011
    Messages : 131
    Par défaut
    @rawsrc

    Oui j'avais un pb sur une des tables : après fait la modification, j'ai toujours le même problème côté front office.

    Pour le moment j'ai fait qu'un select sur lequel il doit recevoir 2 types de valeurs (taille M et une taille s).
    Comme vous pouvez le constater il me crée 2 select alors que je devrais en avoir qu'un

    Voici ce que cela donne


    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
    <div class="form-group">
    <label class="control-label" for="input-option">Taille</label>
    <select name="option[]" id="input-option" class="form-control">
    <option value="">--- Selectioner ---</option>
    <option value=""> M() + : 10</option>
    </select>
    </div>
    <div class="form-group">
    <label class="control-label" for="input-option">Taille</label>
    <select name="option[]" id="input-option" class="form-control">
    <option value="">--- Selectioner ---</option>
    <option value=""> M() + : 10</option>
    <option value=""> S() + : 20</option>
    </select>
    </div>
    @Celira

    Voici les éléments que tu me demandes.
    C'est mis au format json


    contenu de de la fonction :
    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
     
    [
       {
          "products_option_id":"1",
          "option_id":"40",
          "name":"Taille",
          "type":"select",
          "value":null,
          "required":"0",
          "products_option_value":[
             {
                "products_option_value_id":"284",
                "option_value_id":"180",
                "name":"M",
                "image":null,
                "quantity":"0",
                "subtract":"0",
                "price":"10.0000",
                "price_prefix":"+",
                "weight":"0.00",
                "weight_prefix":"+",
                "customers_group_id":"0",
                "products_option_model":"",
                "option_tax_class_id":"0"
             }
          ]
       },
       {
          "products_option_id":"2",
          "option_id":"40",
          "name":"Taille",
          "type":"select",
          "value":null,
          "required":"0",
          "products_option_value":[
             {
                "products_option_value_id":"284",
                "option_value_id":"180",
                "name":"M",
                "image":null,
                "quantity":"0",
                "subtract":"0",
                "price":"10.0000",
                "price_prefix":"+",
                "weight":"0.00",
                "weight_prefix":"+",
                "customers_group_id":"0",
                "products_option_model":"",
                "option_tax_class_id":"0"
             },
             {
                "products_option_value_id":"285",
                "option_value_id":"179",
                "name":"S",
                "image":null,
                "quantity":"0",
                "subtract":"0",
                "price":"20.0000",
                "price_prefix":"+",
                "weight":"0.00",
                "weight_prefix":"+",
                "customers_group_id":"0",
                "products_option_model":"",
                "option_tax_class_id":"0"
             }
          ]
       }
    ]

    Contenu de $data['options']

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
     
    Array ( [0] => Array ( [products_option_id] => 1 [option_id] => 40 [name] => Taille [type] => select [value] => [required] => 0 
     
    [products_option_value] => 
    Array ( 
    [0] => Array ( [products_option_value_id] => 284 [option_value_id] => 180 [name] => M [image] => [quantity] => 0 [price] => 10 [price_prefix] => + [weight] => 0.00 [weight_prefix] => + [customers_group_id] => 0 [products_option_model] => 
    ) ) ) 
     
    [1] => Array ( [products_option_id] => 2 [option_id] => 40 [name] => Taille [type] => select [value] => [required] => 0 
     
    [products_option_value] => 
    Array ( [0] => Array ( [products_option_value_id] => 284 [option_value_id] => 180 [name] => M [image] => [quantity] => 0 [price] => 10 [price_prefix] => + [weight] => 0.00 [weight_prefix] => + [customers_group_id] => 0 [products_option_model] => ) 
    [1] => Array ( [products_option_value_id] => 285 [option_value_id] => 179 [name] => S [image] => [quantity] => 0 [price] => 20 [price_prefix] => + [weight] => 0.00 [weight_prefix] => + [customers_group_id] => 0 [products_option_model] => 
    ) ) ) )

  6. #6
    Membre confirmé
    Profil pro
    Inscrit en
    Mars 2011
    Messages
    131
    Détails du profil
    Informations personnelles :
    Localisation : Canada

    Informations forums :
    Inscription : Mars 2011
    Messages : 131
    Par défaut
    [QUOTE=rawsrc;10003252]
    Pour info, cf ma réponse au 2 questions

  7. #7
    Modératrice
    Avatar de Celira
    Femme Profil pro
    Développeuse PHP/Java
    Inscrit en
    Avril 2007
    Messages
    8 633
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 40
    Localisation : France

    Informations professionnelles :
    Activité : Développeuse PHP/Java
    Secteur : Industrie

    Informations forums :
    Inscription : Avril 2007
    Messages : 8 633
    Par défaut
    Hum... le script créée 2 selects, parce que ton tableau $data['options'] contient deux blocs, parce que $product_option_data contient 2 blocs. Donc le problème est sur la récupération des données, dans la fonction getProductOptions.

    Tes requêtes contiennent des left join, ce qui implique que tu peux remonter plusieurs fois la même donnée.
    Pour explication si on a une table Utilisateur, une table Commande et une table Produit, avec 2 Commandes de 2 Produits chacune, en faisant Utilisateur left join Commande left joint Produit on va récupérer quelque chose comme
    User1 | Commande1 | ProduitA
    User1 | Commande1 | ProduitB
    User1 | Commande2 | ProduitC
    User1 | Commande2 | ProduitD
    Et si on ne fait pas attention en bouclant, on peut se retrouver les infos de Commande en double.

    Vérifie en exécutant tes requêtes directement en base que ce n'est pas le cas.

    Au passage : utiliser SELECT * est certes très pratique, mais ce n'est pas forcément une bonne idée, parce que tu ne maîtrises pas ce qui est récupéré, et que tu peux remonter 25 colonnes pour 2 informations réellement nécessaires.
    Modératrice PHP
    Aucun navigateur ne propose d'extension boule-de-cristal : postez votre code et vos messages d'erreurs. (Rappel : "ça ne marche pas" n'est pas un message d'erreur)
    Cherchez un peu avant poser votre question : Cours et Tutoriels PHP - FAQ PHP - PDO une soupe et au lit !.

    Affichez votre code en couleurs : [CODE=php][/CODE] (bouton # de l'éditeur) et [C=php][/C]

  8. #8
    Membre confirmé
    Profil pro
    Inscrit en
    Mars 2011
    Messages
    131
    Détails du profil
    Informations personnelles :
    Localisation : Canada

    Informations forums :
    Inscription : Mars 2011
    Messages : 131
    Par défaut
    Citation Envoyé par Celira Voir le message
    Hum... le script créée 2 selects......
    Ok, j'ai vu que j'avais un problème d'enregistrement de mes données coté admin, donc je vais regarder ca de plus prêt.
    merci pour l'orientation.
    Je vous reviens d'ici quelques jours.

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

    Citation Envoyé par Celira Voir le message
    ...utiliser SELECT * est certes très pratique, mais ce n'est pas forcément une bonne idée, parce que tu ne maîtrises pas ce qui est récupéré, et que tu peux remonter 25 colonnes pour 2 informations réellement nécessaires.
    Je rejoins Celira.

    Quand on voit le résultat que tu attends (des <select>, avec name, option value et option titre, soit 3 données à trier !), on se demande pourquoi tu enregistres autant de données dans tes array ??

    Tu aurais tout intérêt à simplifier les requêtes (et les array) pour ne sortir QUE les données nécessaires.

    D'autant qu'on devrait pouvoir faire UNE SEULE requête, avec la commande SQL GROUP BY.

  10. #10
    Membre confirmé
    Profil pro
    Inscrit en
    Mars 2011
    Messages
    131
    Détails du profil
    Informations personnelles :
    Localisation : Canada

    Informations forums :
    Inscription : Mars 2011
    Messages : 131
    Par défaut
    Oui, tu as raison, mais pour le moment jessaye de résoudre les points qui fonctionnent pas, après j'optimiserais les requêtes.

  11. #11
    Invité
    Invité(e)
    Par défaut
    C'est sûr !

    Pourquoi faire simple quand on peut faire compliqué ?....

  12. #12
    Membre confirmé
    Profil pro
    Inscrit en
    Mars 2011
    Messages
    131
    Détails du profil
    Informations personnelles :
    Localisation : Canada

    Informations forums :
    Inscription : Mars 2011
    Messages : 131
    Par défaut
    Bonjour,

    Bon,

    Mes bases de données sont saines maintenant.
    Cela n'a pas changé sur le résultat initial, donc voici les nouveaux éléments. J'en profite pour faire un récapitulatif, ce sera plus simple.

    je pense que le problème se situe au niveau de $product_option_value_data et la boucle for each. Vous en pensez quoi et si oui que peut on faire ?


    Au niveau du rendu HTML, J'ai ceci : Donc mon deuxième select contient les éléments du premier.

    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
     
    <div class="form-group">
    <div class="form-group">
    <label class="control-label" for="input-option">Couleur</label>
    <select name="option[]" id="input-option" class="form-control">
      <option value="">--- Sélectionner ---</option>
      <option value=""> vert+ : 10</option>
      <option value=""> rouge</option>
    </select>
    </div>
    <div class="form-group">
    <label class="control-label" for="input-option">Taille</label>
    <select name="option[]" id="input-option" class="form-control">
      <option value="">--- Sélectionner ---</option>
      <option value=""> vert+ : 10</option>
      <option value=""> rouge</option
      ><option value=""> Petit</option>
    </select>
    </div>
    </div>
    La fonction détaillé avec les éléments de la requête
    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
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
     
         public function getProductOptions($product_id) {
          $QproductOption = $this->db->prepare('select po.products_option_id,
                                                        po.products_id,
                                                        po.option_id,
                                                        po.value,
                                                        po.required,
                                                        o.type,
                                                        o.sort_order,
                                                        od.language_id,
                                                        od.name
                                                from :table_test_products_option po 
                                                left join :table_test_products_options_attributes o ON (po.option_id = o.option_id) 
                                                left join :table_test_products_options_attributes_description od ON (o.option_id = od.option_id) 
                                                where po.products_id = :products_id
                                                and od.language_id = :language_id
                                                order by o.sort_order
                                                ');
     
          $QproductOption->bindInt(':products_id', $product_id );
          $QproductOption->bindInt(':language_id', $this->lang->getId());
          $QproductOption->execute();
     
          $products_option = $QproductOption->fetchAll();
     
    //*********************************
    //Résultat de $products_option
    // le résultat semble être correct
    //*********************************
    array (size=2)
      0 => 
        array (size=9)
          'products_option_id' => string '141' (length=3)
          'products_id' => string '239' (length=3)
          'option_id' => string '43' (length=2)
          'value' => string 'null' (length=4)
          'required' => string '0' (length=1)
          'type' => string 'select' (length=6)
          'sort_order' => string '0' (length=1)
          'language_id' => string '2' (length=1)
          'name' => string 'Couleur' (length=7)
      1 => 
        array (size=9)
          'products_option_id' => string '142' (length=3)
          'products_id' => string '239' (length=3)
          'option_id' => string '44' (length=2)
          'value' => string 'null' (length=4)
          'required' => string '0' (length=1)
          'type' => string 'select' (length=6)
          'sort_order' => string '0' (length=1)
          'language_id' => string '2' (length=1)
          'name' => string 'Taille' (length=6)
     
     
    //*********************************
    //*********************************
          foreach ($products_option as $key => $product_option) {
     
           $QproductOptionValue = $this->db->prepare('select pov.products_option_value_id,
                                                              pov.products_option_id,
                                                              pov.products_id,
                                                              pov.option_value_id,
                                                              pov.quantity,
                                                              pov.subtract,
                                                              pov.price,
                                                              pov.price_prefix,
                                                              pov.weight,
                                                              pov.weight_prefix,
                                                              ov.image,
                                                              ov.sort_order,
                                                              ovd.language_id,
                                                              ovd.name
                                                       from :table_test_products_option_value pov left join :table_test_products_options_attributes_value ov ON (pov.option_value_id = ov.option_value_id) 
                                                                                                  left join :table_test_products_options_attributes_value_description ovd ON (ov.option_value_id = ovd.option_value_id) 
                                                      where pov.products_id = :products_id
                                                      and pov.products_option_id = :products_option_id
                                                      and ovd.language_id = :language_id
                                                      order by ov.sort_order
                                                      ');
     
            $product_option_value = $QproductOptionValue->fetchAll();
     
    //*********************************
    //Résultat de $product_option_value
    // le résultat semble est être correct
    //*********************************
     
    array (size=2) =====> Couleur
      0 => 
        array (size=20)
          'products_option_value_id' => string '350' (length=3)
          'products_option_id' => string '141' (length=3)
          'products_id' => string '239' (length=3)
          'option_id' => string '43' (length=2)
          'option_value_id' => string '189' (length=3)
          'quantity' => string '0' (length=1)
          'subtract' => string '1' (length=1)
          'price' => string '10.0000' (length=7)
          'price_prefix' => string '+' (length=1)
          'points' => string '0' (length=1)
          'points_prefix' => string '+' (length=1)
          'weight' => string '0.00' (length=4)
          'weight_prefix' => string '+' (length=1)
          'products_option_model' => null
          'customers_group_id' => string '0' (length=1)
          'option_tax_class_id' => string '1' (length=1)
          'image' => null
          'sort_order' => string '0' (length=1)
          'language_id' => string '2' (length=1)
          'name' => string 'vert' (length=4)
      1 => 
        array (size=20)
          'products_option_value_id' => string '349' (length=3)
          'products_option_id' => string '141' (length=3)
          'products_id' => string '239' (length=3)
          'option_id' => string '43' (length=2)
          'option_value_id' => string '188' (length=3)
          'quantity' => string '0' (length=1)
          'subtract' => string '1' (length=1)
          'price' => string '0.0000' (length=6)
          'price_prefix' => string '+' (length=1)
          'points' => string '0' (length=1)
          'points_prefix' => string '+' (length=1)
          'weight' => string '0.00' (length=4)
          'weight_prefix' => string '+' (length=1)
          'products_option_model' => null
          'customers_group_id' => string '0' (length=1)
          'option_tax_class_id' => string '1' (length=1)
          'image' => null
          'sort_order' => string '0' (length=1)
          'language_id' => string '2' (length=1)
          'name' => string 'rouge' (length=5)
     
    array (size=1) =====> Taille
      0 => 
        array (size=20)
          'products_option_value_id' => string '351' (length=3)
          'products_option_id' => string '142' (length=3)
          'products_id' => string '239' (length=3)
          'option_id' => string '44' (length=2)
          'option_value_id' => string '190' (length=3)
          'quantity' => string '0' (length=1)
          'subtract' => string '0' (length=1)
          'price' => string '0.0000' (length=6)
          'price_prefix' => string '+' (length=1)
          'points' => string '0' (length=1)
          'points_prefix' => string '+' (length=1)
          'weight' => string '0.00' (length=4)
          'weight_prefix' => string '+' (length=1)
          'products_option_model' => null
          'customers_group_id' => string '0' (length=1)
          'option_tax_class_id' => string '1' (length=1)
          'image' => null
          'sort_order' => string '0' (length=1)
          'language_id' => string '2' (length=1)
          'name' => string 'Petit' (length=5)
     
    //*********************************
    //*********************************
     
            foreach ($product_option_value as  $pov => $products_value) {
              $product_option_value_data[] = ['products_option_value_id' => $products_value['products_option_value_id'],
                                              'option_value_id'         => $products_value['option_value_id'],
                                              'name'                    => $products_value['name'],
                                              'image'                   => $products_value['image'],
                                              'quantity'                => $products_value['quantity'],
                                              'subtract'                => $products_value['subtract'],
                                              'price'                   => $products_value['price'],
                                              'price_prefix'            => $products_value['price_prefix'],
                                              'weight'                  => $products_value['weight'],
                                              'weight_prefix'           => $products_value['weight_prefix'],
                                              'customers_group_id'      => $products_value['customers_group_id'],
                                              'products_option_model'   => $products_value['products_option_model'],
                                              'option_tax_class_id'     => $products_value['option_tax_class_id']
                                            ];
            }
     
     
     
    //*********************************
    //Résultat de $product_option_value_data
    // le résultat est incorrect
    //*********************************
     
    array (size=2) ======> résultat correct
      0 => 
        array (size=13)
          'products_option_value_id' => string '350' (length=3)
          'option_value_id' => string '189' (length=3)
          'name' => string 'vert' (length=4)
          'image' => null
          'quantity' => string '0' (length=1)
          'subtract' => string '1' (length=1)
          'price' => string '10.0000' (length=7)
          'price_prefix' => string '+' (length=1)
          'weight' => string '0.00' (length=4)
          'weight_prefix' => string '+' (length=1)
          'customers_group_id' => null
          'products_option_model' => null
          'option_tax_class_id' => null
      1 => 
        array (size=13)
          'products_option_value_id' => string '349' (length=3)
          'option_value_id' => string '188' (length=3)
          'name' => string 'rouge' (length=5)
          'image' => null
          'quantity' => string '0' (length=1)
          'subtract' => string '1' (length=1)
          'price' => string '0.0000' (length=6)
          'price_prefix' => string '+' (length=1)
          'weight' => string '0.00' (length=4)
          'weight_prefix' => string '+' (length=1)
          'customers_group_id' => null
          'products_option_model' => null
          'option_tax_class_id' => null
     
    array (size=3)  ======> résultat incorrect, 1 valeur doit être affichée et non 3; Ce qui correspond au résultat du 2ème select
      0 => 
        array (size=13)
          'products_option_value_id' => string '350' (length=3)
          'option_value_id' => string '189' (length=3)
          'name' => string 'vert' (length=4)
          'image' => null
          'quantity' => string '0' (length=1)
          'subtract' => string '1' (length=1)
          'price' => string '10.0000' (length=7)
          'price_prefix' => string '+' (length=1)
          'weight' => string '0.00' (length=4)
          'weight_prefix' => string '+' (length=1)
          'customers_group_id' => null
          'products_option_model' => null
          'option_tax_class_id' => null
      1 => 
        array (size=13)
          'products_option_value_id' => string '349' (length=3)
          'option_value_id' => string '188' (length=3)
          'name' => string 'rouge' (length=5)
          'image' => null
          'quantity' => string '0' (length=1)
          'subtract' => string '1' (length=1)
          'price' => string '0.0000' (length=6)
          'price_prefix' => string '+' (length=1)
          'weight' => string '0.00' (length=4)
          'weight_prefix' => string '+' (length=1)
          'customers_group_id' => null
          'products_option_model' => null
          'option_tax_class_id' => null
      2 => 
        array (size=13)
          'products_option_value_id' => string '351' (length=3)
          'option_value_id' => string '190' (length=3)
          'name' => string 'Petit' (length=5)
          'image' => null
          'quantity' => string '0' (length=1)
          'subtract' => string '0' (length=1)
          'price' => string '0.0000' (length=6)
          'price_prefix' => string '+' (length=1)
          'weight' => string '0.00' (length=4)
          'weight_prefix' => string '+' (length=1)
          'customers_group_id' => null
          'products_option_model' => null
          'option_tax_class_id' => null
     
    //*********************************
    //*********************************
     
     
            $product_option_data[] = [
                                      'products_option_id'    => $product_option['products_option_id'],
                                      'option_id'            => $product_option['option_id'],
                                      'name'                 => $product_option['name'],
                                      'type'                 => $product_option['type'],
                                      'value'                => $product_option['value'],
                                      'required'             => $product_option['required'],
                                      'products_option_value' => $product_option_value_data
                                    ];
     
    //*********************************
    // je n'affiche pas les résultats dépendant de l'élément ci dessus (products_option_value), 
    le problème est ci dessus
    //*********************************
          }
     
     
     
    //      $product_option_data = json_encode($product_option_data);
     
          return $product_option_data;
        }

  13. #13
    Modératrice
    Avatar de Celira
    Femme Profil pro
    Développeuse PHP/Java
    Inscrit en
    Avril 2007
    Messages
    8 633
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 40
    Localisation : France

    Informations professionnelles :
    Activité : Développeuse PHP/Java
    Secteur : Industrie

    Informations forums :
    Inscription : Avril 2007
    Messages : 8 633
    Par défaut
    Ah j'ai trouvé ! Tu ne réinitialises pas tes tableaux au début des boucles ! Donc, à chaque boucle, il complète le tableau précédemment créé, au lieu d'en faire un nouveau, et tu te retrouves avec les données de la boucle précédente en trop.

    Ajoute une initialisation du tableau avant le foreach qui le remplit :
    Ici :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    $product_option_data = [];
    foreach($products_option as $key => $product_option) {
    et ici :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    $product_option_value_data = [];
    foreach($product_option_value as $pov => $products_value) {
            $product_option_value_data[] = //...
    Modératrice PHP
    Aucun navigateur ne propose d'extension boule-de-cristal : postez votre code et vos messages d'erreurs. (Rappel : "ça ne marche pas" n'est pas un message d'erreur)
    Cherchez un peu avant poser votre question : Cours et Tutoriels PHP - FAQ PHP - PDO une soupe et au lit !.

    Affichez votre code en couleurs : [CODE=php][/CODE] (bouton # de l'éditeur) et [C=php][/C]

  14. #14
    Membre confirmé
    Profil pro
    Inscrit en
    Mars 2011
    Messages
    131
    Détails du profil
    Informations personnelles :
    Localisation : Canada

    Informations forums :
    Inscription : Mars 2011
    Messages : 131
    Par défaut
    Merci @Celira, c'était la solution. Maintenant je vais pouvoir continuer

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. pour les pro de développement (joomla,php,ajax) xD
    Par Arsofts dans le forum Général Conception Web
    Réponses: 0
    Dernier message: 21/07/2010, 14h33
  2. petit conseil pour les index
    Par fpouget dans le forum Langage SQL
    Réponses: 11
    Dernier message: 10/12/2005, 04h39
  3. Petit prog pour éxécuter un script PHP
    Par Sub0 dans le forum Contribuez / Téléchargez Sources et Outils
    Réponses: 3
    Dernier message: 26/04/2005, 15h53
  4. Réponses: 6
    Dernier message: 28/09/2004, 16h47
  5. Une petite aide pour les API ?
    Par Yop dans le forum Windows
    Réponses: 2
    Dernier message: 04/04/2002, 21h45

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