Problème tableau PHP
Posté : 29 déc. 2011, 11:13
Bonjour a tous, je sus novice dans la programmation et je voudrais avoir de l'aide pour résoudre un petit problème de tableau.
Le tableau actuelle est :

Avec le code : (

Le tableau actuelle est :

Avec le code : (
function affiche_attributs_form_part(&$product_object, $return_mode = false, $save_cart_id = null, $saved_id_attribut = null, $save_suffix_id = '', $form_id)
{
// On récupère éventuellement les attributs sauvegardés
$tableau_id_array = !empty($saved_id_attribut)? explode('-', $saved_id_attribut):array();
$output = '';
// On gère les attributs normaux.
// Les attributs d'un produit (ex : parfum) sont énumérés dans la table peel_nom_attributs
// Pour chacun des attributs, il y a diverses valeurs possibles qui sont stockées dans peel_attributs
// Pour un produit donné, on peut associer les attributs que l'on veut, et également spécifier les valeurs acceptables de ces attributs pour ce produit en particulier
// Il ne faut donc pas faire de jointure entre peel_attributs et peel_nom_attributs, mais passer par peel_produits_attributs pour faire les deux jointures indépendemment
$sql = "SELECT pa.attribut_id, a.descriptif_" . $_SESSION['session_langue'] . " AS descriptif, a.prix, a.prix_revendeur, pa.nom_attribut_id, na.nom_" . $_SESSION['session_langue'] . " AS nom, texte_libre
FROM peel_produits_attributs pa
INNER JOIN peel_attributs a ON a.id = pa.attribut_id
INNER JOIN peel_nom_attributs na ON na.id = pa.nom_attribut_id AND na.etat = '1'
WHERE pa.produit_id = '" . intval($product_object->id) . "'
ORDER BY a.position, a.descriptif_" . $_SESSION['session_langue'] . " ASC, na.nom_" . $_SESSION['session_langue'] . " ASC";
$query = query($sql);
while ($result = fetch_assoc($query)) {
$attributs_array[$result['nom_attribut_id']][$result['attribut_id']] = $result;
}
if (!empty($attributs_array)) {
// Va servir à paramétrer la liste des données des attribut
$i = 0;
$output .='
<p style="font-weight:bold; margin:2px ">' . OPTIONS_ATTRIBUTS . BEFORE_TWO_POINTS . ':</p>
<table cellpadding="3" cellspacing="3" border="1" style="width: 100%;">';
foreach ($attributs_array as $this_nom_attribut_id => $this_attribut_values_array ) {
$j = 0;
foreach ($this_attribut_values_array as $this_attribut_id => $this_attribut_infos) {
if (empty($j)) {
$output .='
<tr>
<td class="normal attribut-size">' . String::html_entity_decode_if_needed($this_attribut_infos['nom']) . BEFORE_TWO_POINTS . ':<br>';
if (empty($GLOBALS['site_parameters']['type_affichage_attribut'])) {
$output .= '
<select id="' . $form_id . '_custom_attribut' . $i . '" name="attribut' . $i . '" style="width:230px" onchange="update_product_price_from_attribute' . $save_suffix_id . '(this.value, \'' . vn($product_object->id) . $save_suffix_id . '\')">';
}
$checked = 'checked="checked"';
} else {
$checked = '';
}
$optionValue = $this_nom_attribut_id . '|' . $this_attribut_id;
if (empty($GLOBALS['site_parameters']['type_affichage_attribut'])) {
$output .= '
<option value="' . String::str_form_value($optionValue) . '" ' . frmvalide(!empty($tableau_id_array) && in_array($optionValue, $tableau_id_array), ' selected="selected"') . '>' . String::html_entity_decode_if_needed($this_attribut_infos['descriptif']);
} else {
$output .= '
</br><input type="radio" value="' . String::str_form_value($optionValue) . '" id="attribut' . $i . '-' . $j . '" name="attribut' . $i . '" ' . frmvalide(!empty($tableau_id_array) && in_array($optionValue, $tableau_id_array), ' checked="checked" ', $checked) . ' onclick="update_product_price_from_attribute' . ($save_suffix_id) . '(this.value, \'' . vn($product_object->id) . $save_suffix_id . '\')" /><label for="attribut' . $i . '-' . $j . '">' . String::html_entity_decode_if_needed($this_attribut_infos['descriptif']);
}
if ($this_attribut_infos['prix'] > 0) {
if (is_reseller_module_active() && is_reseller() && $this_attribut_infos['prix_revendeur'] > 0) {
$additional_price_ttc = $this_attribut_infos['prix_revendeur'];
} else {
$additional_price_ttc = $this_attribut_infos['prix'];
}
$final_additional_price_ht = $additional_price_ttc / (1 + $product_object->tva / 100) * (1 - $product_object->get_all_promotions_percentage(is_reseller_module_active() && is_reseller(), get_current_user_promotion_percentage(), false) / 100);
$output .= BEFORE_TWO_POINTS . ': +' . $product_object->format_prices($final_additional_price_ht, display_prices_with_taxes_active(), false, true, true);
}
if (empty($GLOBALS['site_parameters']['type_affichage_attribut'])) {
$output .= '</option>';
} else {
$output .= '</label>';
}
$j++;
}
if (empty($GLOBALS['site_parameters']['type_affichage_attribut'])) {
$output .= "
</select>";
}
$output .= '
</td>';
$output .= '
</tr>';
$i++;
}
$output .= '
</table>';
}
// On gère les textes libres.
$sql = "SELECT pa.nom_attribut_id, na.nom_" . $_SESSION['session_langue'] . " AS nom, texte_libre,upload
FROM peel_produits_attributs pa
INNER JOIN peel_nom_attributs na ON na.id = pa.nom_attribut_id AND na.etat = '1'
WHERE pa.produit_id = '" . intval($product_object->id) . "' AND texte_libre = 1
ORDER BY na.nom_" . $_SESSION['session_langue'] . " ASC";
$query = query($sql);
if (num_rows($query) > 0) {
$k = 0;
$upload_index = 0; // compteurs pour les attributs d'upload d'images
if (empty($attributs_array)) {
$output .= '
<p style="font-weight:bold; margin:2px">' . OPTIONS_ATTRIBUTS . BEFORE_TWO_POINTS . ':</p>';
}
$output .= '
<table cellpadding="3" cellspacing="3" border="0">';
while ($result = fetch_assoc($query)) {
$output .= '
<tr>
<td class="normal attribut-cell">
' . String::html_entity_decode_if_needed($result['nom']) . BEFORE_TWO_POINTS . ':
';
// On récupère éventuellement le texte libre
$free_text_or_uploaded_image = "";
foreach($tableau_id_array as $attribut_id) {
$tableau_attribut_id = explode("|0|", $attribut_id);
if (count($tableau_attribut_id) === 2 && intval($tableau_attribut_id[0]) == intval($result['nom_attribut_id'])) {
$free_text_or_uploaded_image = $tableau_attribut_id[1];
}
}
if (!empty($result['upload'])) { // cas des attibut d'upload d'image
$upload_index++;
$attribute_name = 'attribut' . $upload_index . '_upload';
if ((!empty($_SESSION["session_display_popup_quantity_error"][$attribute_name])) && ($_SESSION["session_display_popup_quantity_error"][$attribute_name] !== false)) {
// si l'image a été déjà téléchargé alors, on l'affiche tout simplement (avec la possibilité de la supprimer)
$output .= display_option_image($_SESSION["session_display_popup_quantity_error"][$attribute_name]);
} elseif (preg_match('`' . $GLOBALS['uploaded_images_name_pattern'] . '`' , $free_text_or_uploaded_image)) {
// si l'image a été déjà téléchargé alors, on l'affiche tout simplement (avec la possibilité de la supprimer)
$output .= display_option_image($free_text_or_uploaded_image)
. '<input type="hidden" name="' . $attribute_name . '" value="' . $free_text_or_uploaded_image . '" />';
} else {
$output .= '<input type="file" name="' . $attribute_name . '" />';
}
$output .= '<input type="hidden" name="attribut' . $upload_index . '_nom_attribut_upload" value="' . String::html_entity_decode_if_needed($result['nom_attribut_id']) . '" />';
} else {
$output .= '<input type="text" name="attribut' . $k . '_texte_libre" value="' . $free_text_or_uploaded_image . '" /><input type="hidden" name="attribut' . $k . '_nom_attribut_texte_libre" value="' . String::html_entity_decode_if_needed($result['nom_attribut_id']) . '" />';
$k++;
}
$output .= '<input type="hidden" value="' . $upload_index . '" name="attribut_upload_count" />
</td>
</tr>';
}
$output .= '
</table>';
}
if ($return_mode) {
return $output;
} else {
echo $output;
}
}
/**
* Formate l'attribut (de type upload) du produit
*
* @param string $str_image : est une chaine de caractère qui peut être au format image directement ou alors peut être un texte qui contient des extraits d'images
* @param boolean $set : definit si l'on a passé un format d'image (false) ou alors si on a passé du text contenant des images (true)
* @return
*/
function display_option_image($str_image, $set = false)
{
$output = '';
if ($set) {
// si $str_image est un texte contenant des images
$inital_text = $str_image;
$option_tab = explode("{{", $str_image);
if (count($option_tab) > 1) {
// s'il ya au moins une image
foreach ($option_tab as $str_img) {
if (($end_str = String::strpos($str_img, "}}")) !== false) {
$str_img = String::substr($str_img, 0, $end_str);
$small_option_image = thumbs($str_img, 25, 25, 'fit');
$str_img_new = '<a onclick="return(window.open(this.href)?false:true);" id="zoom1" rel="lightbox" href="' . $GLOBALS['repertoire_upload'] . '/' . $str_img . '">
<img src="' . $GLOBALS['repertoire_upload'] . '/thumbs/' . $small_option_image . '" alt="" /> </a>';
$str_image = str_replace('{{' . $str_img . '}}', $str_img_new, $str_image);
}
}
}
$output .= $str_image;
} else {
$small_option_image = thumbs($str_image, 25, 25, 'fit');
$output .= '<a rel="lightbox" href="' . $GLOBALS['repertoire_upload'] . '/' . $str_image . '">
<img src="' . $GLOBALS['repertoire_upload'] . '/thumbs/' . $small_option_image . '" alt="" />
</a>';
}
return $output;
}
/**
* Affiche le code javascript necessaire pour changer le prix en fonction de l'attribut
*
* @param string $product_object
* @param string $save_suffix_id
* @return
*/
function display_javascript_for_attribut($product_object, $save_suffix_id, $form_id)
{
$output = '';
$attributs_infos_array = $product_object->get_possible_attributs('infos', get_current_user_promotion_percentage(), display_prices_with_taxes_active(), is_reseller_module_active() && is_reseller());
$sizes_infos_array = $product_object->get_possible_sizes('infos', get_current_user_promotion_percentage(), display_prices_with_taxes_active(), is_reseller_module_active() && is_reseller());
$attributs_infos_array_count = count($attributs_infos_array);
$output .= '<script type="text/javascript"><!--//--><![CDATA[//><!--';
if (!empty($attributs_infos_array) && !empty($sizes_infos_array)) {
//if (true) {
// Affichage sous forme de select de la taille
$get_size_js='select_size = document.getElementById("taille' . $save_suffix_id . '");
size_id = select_size.options[select_size.selectedIndex].value;
';
/*}else{
// Affichage sous forme de boutons radio de la taille
// Jamais actif
$get_size_js='select_size = document.getElementById("taille' . $save_suffix_id . '");
for (var i=0; i<select_size.length;i++) {
if (radio[i].checked) {
size_id = radio[i].value;
break;
}
}
';
}*/
$output .= '
function update_product_price_from_attribute' . ($save_suffix_id) . '(attribut, product_id){
'.$get_size_js.'
' . build_attr_var_js('attribut', $attributs_infos_array_count, $form_id) . '
document.getElementById("prix_"+product_id).innerHTML=attribut_size_price' . ($save_suffix_id) . '[size_id+"_"+attribut];
}';
$output .= '
( function($) {
$(document).ready(function(){
' . build_attr_var_js('attr_val', $attributs_infos_array_count, $form_id, true) . '
update_product_price_from_attribute' . ($save_suffix_id) . '(attr_val , \'' . vn($product_object->id) . $save_suffix_id . '\');
});
} ) ( jQuery );';
} elseif (!empty($attributs_infos_array)) {
$output .= '
var attribut_price' . $save_suffix_id . ' = new Array();';
$attributs_infos_array_keys = array_keys($attributs_infos_array);
$option_value_array = build_option_value_array($attributs_infos_array, array(), $attributs_infos_array_keys, 0);
foreach ($option_value_array as $optionValue) {
$product_object->set_configuration(null, null, $optionValue, is_reseller_module_active() && is_reseller());
$output .= '
attribut_price' . ($save_suffix_id) . '["' . $optionValue . '"] = \'' . filtre_javascript($product_object->get_final_price(get_current_user_promotion_percentage(), display_prices_with_taxes_active(), is_reseller_module_active() && is_reseller(), true, true, 1, true, true, true), true, true, false) . '\';';
}
$product_object->set_configuration(null, null, null, is_reseller_module_active() && is_reseller());
$output .= '
function update_product_price_from_attribute' . ($save_suffix_id) . '(attribut, product_id) {
' . build_attr_var_js('attribut', $attributs_infos_array_count, $form_id) . '
document.getElementById("prix_"+product_id).innerHTML=attribut_price' . ($save_suffix_id) . '[attribut];
}';
$output .= '
( function($) {
$(document).ready(function(){
' . build_attr_var_js('attr_val', $attributs_infos_array_count, $form_id, true) . '
update_product_price_from_attribute' . ($save_suffix_id) . '(attr_val , \'' . vn($product_object->id) . $save_suffix_id . '\');
});
} ) ( jQuery );';
}
$output .= '
//--><!]]></script>';
return $output;
}
/**
* build_option_value_array()
*
* @param mixed $attributs_infos_array
* @param mixed $option_value_array
* @param mixed $attributs_infos_array_keys
* @param int $depth
* @return
*/
function build_option_value_array($attributs_infos_array, $option_value_array, $attributs_infos_array_keys, $depth, $all_combinaisons = false)
{
$option_value_array_tmp = $all_combinaisons? $option_value_array : array();
if ($depth >= count($attributs_infos_array)) {
return array_unique($option_value_array);
}
$this_nom_attribut_id = $attributs_infos_array_keys[$depth];
if (empty($option_value_array)) {
$option_value_array[] = '';
}
if ($all_combinaisons) {
foreach ($attributs_infos_array[$this_nom_attribut_id] as $this_attribut_id => $this_attribut_infos) {
$option_value_array_tmp[] = $this_nom_attribut_id . '|' . $this_attribut_id;
}
}
foreach ($option_value_array as $option_value) {
foreach ($attributs_infos_array[$this_nom_attribut_id] as $this_attribut_id => $this_attribut_infos) {
$option_value_array_tmp[] = !empty($option_value)? $option_value . '-' . $this_nom_attribut_id . '|' . $this_attribut_id : $this_nom_attribut_id . '|' . $this_attribut_id;
}
}
return build_option_value_array($attributs_infos_array, $option_value_array_tmp, $attributs_infos_array_keys, ++$depth, $all_combinaisons);
}
/**
* build_combinaison_option_name()
*
* @param mixed $attributs_infos_array
* @param mixed $combinaison_option_value
* @param int $depth
* @return
*/
function build_combinaison_option_name($attributs_infos_array, $combinaison_option_value)
{
$combinaison_option_name = '';
$option_value_array_tmp = explode('-', $combinaison_option_value);
foreach ($option_value_array_tmp as $option_value) {
$value_array = explode('|', $option_value);
$combinaison_option_name = !empty($combinaison_option_name)?
$combinaison_option_name . '-' . $attributs_infos_array[$value_array[0]][$value_array[1]]['descriptif']
: $attributs_infos_array[$value_array[0]][$value_array[1]]['descriptif'];
}
return $combinaison_option_name;
}
/**
* build_attr_var_js()
*
* @param string $attr_var_name
* @param int $attributs_infos_array_count
* @param string $form_id
* @param boolean $without_attr_action
* @return
*/
function build_attr_var_js($attr_var_name, $attributs_infos_array_count, $form_id, $without_attr_action = false)
{
$output = '';
if ($attributs_infos_array_count <= 1 && !$without_attr_action) {
return $output;
}
if ($GLOBALS['site_parameters']['type_affichage_attribut'] == 0) {
// Affichage sous forme de select
for($i = 0; $i < $attributs_infos_array_count; $i++) {
$select_id = $form_id . '_custom_attribut' . $i;
$js_values_array[] = 'document.getElementById("' . $select_id . '").options[document.getElementById("' . $select_id . '").selectedIndex].value;
';
}
// Initialise la variable en javascript pour permettre la concatenation.
$output .= '' . $attr_var_name . '='. implode($attr_var_name . '+= "-"+', $js_values_array);
} else {
// Affichage sous forme de boutons radio
$output .= '
radio = document.getElementById("' . $form_id . '").attribut0;
for (var i=0; i<radio.length;i++) {
if (radio[i].checked) {
' . $attr_var_name . ' = radio[i].value;
break;
}
}';
for($i = 1; $i < $attributs_infos_array_count; $i++) {
$output .= '
radio = document.getElementById("' . $form_id . '").attribut' . $i . ';
for (var i=0; i<radio.length;i++) {
if (radio[i].checked) {
' . $attr_var_name . ' += "-"+radio[i].value;
break;
}
}';
}
}
return $output;
}
?>
J'essaie depuis une semaine obtenir ceci quelqu'un pourrait jouer le rôle du papa Noël SVP :