Tableau dynamique et panier en ligne

nicul Messages postés 2 Date d'inscription mardi 26 avril 2005 Statut Membre Dernière intervention 1 juin 2007 - 3 mai 2005 à 14:23
cs_Jackboy Messages postés 757 Date d'inscription vendredi 7 septembre 2001 Statut Membre Dernière intervention 19 juin 2008 - 5 mai 2005 à 16:12
Bon jour tout le monde . Je suis pas trop du genre a poser des questions et à embeter les gens mais la je suis à 2 doigts de craquer. J'aurais exprecemment besoin d'aide pour un probleme de Panier couplé entre session et Tableau.
Voila en parcourant le net j'ai trouvé un script qui devrait theoriquement marcher.
le Voici:
<% id = cstr(Request.Form("id")) %>
<% qte = cstr(Request.Form("nom")%>

<%
sub ajoutPanier( id , qte )
dim arPanier

arPanier = Session("panier")

if ( vartype( arPanier ) = 8204 ) then ' vrai si arPanier est un tableau de variant
arPanier = array( array( id , qte )) ' on 'initialise avec un nouveau tableau aui contient le tableau des valeur
'j'utilise la fonction cstr pour forcer le type de la valeur en String.
'Le tableau est de type variant. Un variant est un type spécial qui peut stocker tous les types ( string, date , time , int, etc ).
'Il peut y avoir un pb lors de comparaison entre deux variable variant de sous type différents ( voir retirePannier) .
'En forcant le variant en str, on évite ce pb.

else ' arPanier contient déjà des infos
dim nextIndex
nextIndex = ubound( arPanier )+1 'on determine le prochain index
redim preserve arPanier( nextIndex ) ' on redimensionne la tableau
arPanier( nextIndex ) = array( id , qte ) ' on affecte les valeurs
end if
Session("panier") = arPanier ' on stock le tableau

end sub

sub retirePannier( id )
dim arPanier
dim lastIndex
dim i
arPanier = Session("panier")

if ( vartype( arPanier ) = 8204 ) then
lastIndex = UBound( arPanier )
for i = 0 to lastIndex
article = arPanier( i )
if vartype( article ) = 8204 then ' vrai si article est un tableau de variant
if article(0) = id then
arPanier( i ) = 0 'on remplace le tableau article par 0
' le test vartype( article ) = 8204 ne sera plus vrai pour cette ligne
exit for
end if
end if
next
end if
Session("panier") = arPanier ' on stock le tableau
end sub

sub printPanier()
dim arPanier
dim article

arPanier = Session("panier")

if ( vartype( arPanier ) <> 0 ) then
for each article in arPanier
if vartype( article ) = 8204 then ' vrai si article est un tableau de variant
Response.Write "id : " & article(0) & " - qte : " & article(1) & "
"
end if
next
end if
end sub

%>

Mais le truc c'est que je n'arrive pas a appeller les fonctions. Ou sinon qd je les appelle elle me renvoi des erreurs . genre :
Microsoft VBScript runtime error '800a000d'


Type mismatch: 'ubound'


/zao/pages/catalogue.asp, line 26

sinon je ne saisis pas trop non plus le concept de vartype :) et les parametres que je dois passer dans l'appel de mes sub .

Voila merci d'avance pour votre aide :)

2 réponses

en premier lieu vartype permet de déterminer le type de la variable, s'il retourne 8204 (variant array). Je ne connais pas le reste du code et cela aiderai grandement, car comment que la variable session sesssion("panier") est elle remplis. Je t'ai fais qqchose qui fonctionne et une sub qui test le contenu de ta array selon la sub appeler, a toi maintenant a integrer le tout !

<%


sub ajoutPanier(arPanier,id,qte)
'dim arPanier(10)
'arPanier = Session("panier")

if ( vartype( arPanier ) = 8204 ) then ' vrai si arPanier est un tableau de variant
arPanier = Array(id, qte) ' on 'initialise avec un nouveau tableau aui contient le tableau des valeur
dim txt
txt = " sub ajoutPanier"
AfficherArray txt, arPanier
'j'utilise la fonction cstr pour forcer le type de la valeur en String.
'Le tableau est de type variant. Un variant est un type spécial qui peut stocker tous les types ( string, date , time , int, etc ).
'Il peut y avoir un pb lors de comparaison entre deux variable variant de sous type différents ( voir retirePannier) .
'En forcant le variant en str, on évite ce pb.

else ' arPanier contient déjà des infos
dim nextIndex
nextIndex = UBound( arPanier )+1 'on determine le prochain index
redim preserve arPanier( nextIndex ) ' on redimensionne la tableau
arPanier( nextIndex ) = array(id,qte) ' on affecte les valeurs
end if
Session("panier") = arPanier ' on stock le tableau


end sub


sub retirePannier(arPanier,id)


dim lastIndex
dim i


if ( vartype( arPanier ) = 8204 ) then

lastIndex = UBound( arPanier )
for i = 0 to lastIndex

session("article") = arPanier( i )
if vartype( arPanier ) = 8204 then ' vrai si article est un tableau de variant
if session("article")= id then
arPanier( i ) = 0 'on remplace le tableau article par 0
' le test vartype( article ) = 8204 ne sera plus vrai pour cette ligne
exit for
end if
end if
next
end if
Session("panier") = arPanier ' on stock le tableau
dim txt
txt = " sub retirePannier"
AfficherArray txt, arPanier
end sub


sub printPanier(arPanier)


dim article


if ( vartype( arPanier ) <> 0 ) then
article = session("article")
Response.Write "

Pour la sub printPanier :
"
for i = 0 to article
if vartype( arPanier ) = 8204 then ' vrai si article est un tableau de variant
Response.Write "id : " & i
Response.Write " - qte : " & arPanier(i) & "
"
end if
next
end if
end sub


Sub AfficherArray(txt, ArrayToShow)
Dim I
Dim iArraySize


iArraySize = (UBound(ArrayToShow) - LBound(ArrayToShow)) + 1


Response.Write "Il y a " & iArraySize _
& " pour :" & txt &"

" & vbCrLf
Response.Write "\" & vbCrLf
Response.Write \"\" & vbCrLf
Response.Write \"----
\" & vbCrLf
Response.Write \" Index |\" & vbCrLf
Response.Write \"Value |\" & vbCrLf
Response.Write \"\" & vbCrLf
Response.Write \"\" & vbCrLf
Response.Write \"\" & vbCrLf

For I = LBound(ArrayToShow) To UBound(ArrayToShow)
Response.Write \"----
\" & vbCrLf
Response.Write \"" & I & ", \" & vbCrLf
Response.Write \"" & ArrayToShow(I) & ", \" & vbCrLf
Response.Write \"\" & vbCrLf
Next 'I
Response.Write \"\" & vbCrLf
Response.Write "
" & vbCrLf
End Sub



r_id = cstr(Request.Form("id"))
r_qte = cstr(Request.Form("qte"))


dim arPanier(10)
arPanier(0) = "1"
arPanier(1) = "2"
arPanier(2) = "3"
arPanier(3) = "4"


Session("panier") = arPanier


'panier total ajouter au debut
dim txt
txt = " Array du debut"
AfficherArray txt, Session("panier")


'ajouter les element du form
ajoutPanier Session("panier"), r_id, r_qte


'retirer les element du tableau deja entrer
retirePannier Session("panier"), r_id


'afficher resultat
printPanier(Session("panier"))


response.Write ("
Nombre d'element dans le panier: " & session("article"))


%>
0
cs_Jackboy Messages postés 757 Date d'inscription vendredi 7 septembre 2001 Statut Membre Dernière intervention 19 juin 2008
5 mai 2005 à 16:12
jespere que sa va t'aider, reviens moi la dessus !
0
Rejoignez-nous