PrestaShop Forums: [RESOLU] Problème avec la fonction array() - PrestaShop Forums

Jump to content


Welcome to the PrestaShop Forum! We hope you'll share your comments and suggestions with us. We ask that you please post in English to the main sections of the PrestaShop Forum. If you want to write in another language, please post in the corresponding PrestaShop Community section below.

Please note that PrestaShop Community sections are largely self-moderated. PrestaShop team members may or may not participate in non-English sections. To improve the chances of receiving feedback to your question or comment, please post it in English to the main sections of our Forum.

NYC

Vous parlez français ? par ici !


[RESOLU] Problème avec la fonction array()


[RESOLU] Problème avec la fonction array()

#1 domdas

    PrestaShop Newbie

  • 05 Aug 2009
  • Members
  • Pip
  • 16 posts

Posted 22 December 2009 - 02:17 PM

Bonjour,

Je n'arrive pas à récupérer correctement les données d'un champ de configuration afin de les traiter dans un tableau avec la fonction array. Il s'agit d'un module d'upload.

Voici les valeurs du champ UPLOADER_AUTH_EXT à traiter : ".pdf", ".jpg", ".jpeg", ".eps", ".ai", ".psd", ".tif"

Et si je fais un echo Configuration::get('UPLOADER_AUTH_EXT'), j'obtiens bien : ".pdf", ".jpg", ".jpeg", ".eps", ".ai", ".psd", ".tif"

Maintenant si j'utilise ma variable Configuration::get('UPLOADER_AUTH_EXT') dans une fonction array, j'obtiens FALSE alors que si je la remplace par la chaine qu'elle est censée afficher ".pdf", ".jpg", ".jpeg", ".eps", ".ai", ".psd", ".tif", j'obtiens TRUE.

Voici un exemple de mon code :

$b = array(Configuration::get('UPLOADER_AUTH_EXT'));
if (in_array(".jpg", $b)){
echo "TRUE";
}else{
echo "FALSE";
}


J'ai essayé de filtrer Configuration::get('UPLOADER_AUTH_EXT') avec addslashes() ou htmlentities() et rien n'y fait !!!

Pouvez-vous m'aider ?
Par avance merci.
Dominique

Quote



#2 SotEW

    PrestaShop Addict

  • 17 Jul 2008
  • Members
  • PipPipPip
  • 990 posts

Posted 22 December 2009 - 04:11 PM

salut,

En copiant ton code, j'ai effectivement un false, mais j'ai surtout $b vide. Dans la table configuration, je n'ai rien qui ressemble à UPLOADER_AUTH_EXT. où as tu eu vu cette variable de configuration ?


Vu qu'il s'agit d'un module d'upload, je suppose que c'est toi qui as créé cette variable, donc je vais plutôt te conseiller de regarder (et de copier-coller ici pendant que tu y es) le résultat de ceci :


$b = array(Configuration::get('UPLOADER_AUTH_EXT'));
print_r($b);
echo "

RESULT: ".((in_array(".jpg", $b))?"TRUE":"FALSE");

Posted Image
Add content on your PrestaShop website (pictures, flash, html and SLIDESHOW) with AddBlocks V1.4 Posted Image
Documentation for AddBlocksV1.3b (french and english)
Demo website: http://demo.sotew.com/ - http://demo.sotew.com/back/

#3 domdas

    PrestaShop Newbie

  • 05 Aug 2009
  • Members
  • Pip
  • 16 posts

Posted 22 December 2009 - 05:29 PM

Merci pour ton aide.
J'ai tester ton code et j'ai toujours un retour FALSE !

#4 domdas

    PrestaShop Newbie

  • 05 Aug 2009
  • Members
  • Pip
  • 16 posts

Posted 22 December 2009 - 05:51 PM

Pour être complet, voici l'affichage du résultat :

Array ( [0] => ".pdf", ".jpg", ".jpeg", ".eps", ".ai", ".psd", ".tif" )
RESULT: FALSE

En clair, la fonction n'arrive pas à séparer les valeurs de la variable UPLOADER_AUTH_EXT pourtant l'affichage semble correct.

#5 SotEW

    PrestaShop Addict

  • 17 Jul 2008
  • Members
  • PipPipPip
  • 990 posts

Posted 22 December 2009 - 06:23 PM

oki, donc c'est normal.

la fonction in_array($a, $b) test si un élément de $b est exactement $a.

Toi, tu as un seul élement dans ta liste, et c'est la chaine de caractère : ' “.pdf”, “.jpg”, “.jpeg”, “.eps”, “.ai”, “.psd”, “.tif” ' qui est différente de ".jpg ", donc ça renverra false.

Tu as plusieurs solutions, en voici une parmi d'autres :

Dans ta variable que tu as sans doute faite toi même arrange toi pour que le retour du print_r soit :

Array ( [0] => .pdf|.jpg|.jpeg|.eps|.ai|.psd|.tif )

ce qui chez moi pour le test correspond à faire :

$b = array('.pdf|.jpg|.jpeg|.eps|.ai|.psd|.tif');



puis test avec ceci :


$b = array(Configuration::get('UPLOADER_AUTH_EXT'));
print_r($b);
echo "

RESULT: ".((in_array(".jpg", explode('|', $b[0])))?"TRUE":"FALSE");


On pourrais aussi passer par la fonction strpos qui décomposera le premier élément de $b pour en trouver la chaine de caractère sans faire la décomposition de explode et le in_array, il y a toujours plusieurs façon de faire la même chose.

Fait attention, tu avais fait plusieurs erreurs dans ton code à la base :

in_array(".jpg", $b)
ne test pas la chaine de caractère ".jpg" mais .jpg. il aurait fallu mettre :
in_array('".jpg"', $b)


Aussi, il faut se méfier des extensions : .JPG est aussi une image jpeg, et un fichier peux très bien être avec une extensions ne correspondant pas à son type. Il conviendrais de ne pas tester les extensions mais le type du fichier obtenu dans la variable $_FILES de ton formulaire d'upload.
Posted Image
Add content on your PrestaShop website (pictures, flash, html and SLIDESHOW) with AddBlocks V1.4 Posted Image
Documentation for AddBlocksV1.3b (french and english)
Demo website: http://demo.sotew.com/ - http://demo.sotew.com/back/

#6 domdas

    PrestaShop Newbie

  • 05 Aug 2009
  • Members
  • Pip
  • 16 posts

Posted 22 December 2009 - 06:41 PM

[Resolu] Super tout fonctionne à merveille !!! Un grand Merci SotEW

#7 SotEW

    PrestaShop Addict

  • 17 Jul 2008
  • Members
  • PipPipPip
  • 990 posts

Posted 22 December 2009 - 07:26 PM

Et bah de rien, et pense à mettre [Resolu] dans le sujet ^^
Posted Image
Add content on your PrestaShop website (pictures, flash, html and SLIDESHOW) with AddBlocks V1.4 Posted Image
Documentation for AddBlocksV1.3b (french and english)
Demo website: http://demo.sotew.com/ - http://demo.sotew.com/back/

#8 a_sun

    PrestaShop Newbie

  • 13 Jan 2010
  • Members
  • Pip
  • 21 posts

Posted 02 February 2010 - 03:55 AM

From 1261503664:

[Resolu] Super tout fonctionne à merveille !!! Un grand Merci SotEW


Hi Dominica:
I need this solution. Can you show me how to do it, am not very sure to amend it. Where to start and where to amend, can you please guide me? If you can help me, I'd be enormously grateful.

Regards,
a_sun





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users