Mini-Scripts |
demandés par les visiteurs du site |
|
Quelques scripts divers demandés par les visiteurs en attendant que je leur trouve une place ailleurs...
Devis dynamique (ASP ou PHP)
Répertoire automatique (ASP)
Lien conditionné à l'existence d'une page (ASP ou PHP)
Tri d'un tableau à plusieurs colonnes (ASP)
Application au listing d'une base Access ou d'un tableau (ASP)
Exemple de pagination d'un listing (ASP)
Afficher une ligne particulière d'un fichier (ASP)
Afficher une ligne particulière d'un fichier en ASP
| s2.asp |  |  | |
| <% function ligne(fichier,num) dim k,inF set inF=FSO.openTextFile(fichier,1,false) k = 1 while k < num and not inF.atEndOfStream inF.skipLine : k = k + 1 wend if not inF.atEndOfStream then ligne = inF.readLine inF.close end function %> <% Set FSO = Server.CreateObject("Scripting.FileSystemObject") Path = Server.MapPath("/mon_rep") & "\" %> <%=ligne(Path & "mon_fichier.txt",3)%><br>
|
Index des mini-scripts
Exemple de pagination d'un listing en ASP
| s3.asp |  |  | |
| <%function navig(deb,nb,total) dim URL,QUERY,cherche,temp,prec ' récupération de l'URL et de la Query URL = Request.ServerVariables("SCRIPT_NAME") QUERY = Request.ServerVariables("QUERY_STRING") ' un début est indiqué ? si oui, on vire cherche = instr(QUERY,"deb=") if cherche = 1 then QUERY = "" elseif cherche > 1 then QUERY = left(QUERY,cherche-2) end if ' préparation de la future query if QUERY = "" then QUERY = "?deb=" else QUERY = "?" & QUERY & "&deb=" ' chaîne de navigation temp = "" : prec = deb - nb if prec < 1 then prec = 1 if deb > 1 then ' s'il y a des fiches précédentes temp = "<A href='" & URL & QUERY & "1'>Début</A> " _ & "<A href='" & URL & QUERY & prec & "'>Précédentes</A> " end if if (deb+nb) <= total then ' s'il y a des fiches suivantes temp = temp & "<A href='" & URL & QUERY & (deb+nb) & "'>Suivantes</A> " _ & "<A href='" & URL & QUERY & (total-nb+1) & "'>Fin</A>" end if ' retourne le résultat navig = temp end function%>
|
Index des mini-scripts
Application au listing d'une base Access
| s4.asp |  |  | |
| <!-- #include file="_connexion.asp"-->
<% SQL="SELECT * FROM [TABLE]" Set RS = server.createobject("ADODB.Recordset") RS.Open SQL,Conn , 3, 3
nmb = 4 ' nombre de fiches par page deb = 1 ' première fiche if request.queryString("deb")<>"" then ' si queryString deb = cInt(request.queryString("deb")) end if
if RS.recordcount >= deb then ' des fiches à afficher ? for A=1 to RS.recordcount ' pour chaque fiche if A >= deb then ' si ok, affiche %><%=RS("champ")%><br> <%end if if A = (deb + nmb - 1) then ' c'est fini ! exit for else RS.MOVENEXT ' fiche suivante end if next end if %> <br> <%=navig(deb,nmb,RS.recordcount)%>
<%conn.close : Set conn=nothing %>
|
|
Application au listing d'un tableau
| s4b.asp |  |  | |
| <%dim fruits fruits = Array("" _ ,"pommes","bananes","oranges" _ ,"poires","kiwis","fraises" _ ,"ananas","citrons","abricots" _ ,"pêches","goyaves","papayes")
nmb = 5 ' nombre de fiches par page deb = 1 ' première fiche if request.queryString("deb")<>"" then deb = cInt(request.queryString("deb")) end if
for A=deb to uBound(fruits) %> <%=fruits(A)%><br> <% if A = (deb + nmb - 1) then exit for end if next %> <br> <%=navig(deb,nmb,uBound(fruits))%>
|
|
|---|
Index des mini-scripts
Tri en ASP d'un tableau à plusieurs colonnes
| s6.asp |  |  | |
| <% dim fruits(5,2) fruits(0,0) = "banane" : fruits(0,1) = 100 fruits(1,0) = "pomme" : fruits(1,1) = 50 fruits(2,0) = "abricot" : fruits(2,1) = 75 fruits(3,0) = "poire" : fruits(3,1) = 10 fruits(4,0) = "ananas" : fruits(4,1) = 25
sens = request.querystring("sens") col = request.querystring("col")
dim tri : redim tri(uBound(fruits))
for i = 0 to uBound(fruits)-1 for k = 0 to i test = false if tri(k) = "" then test = true elseif sens = "up" then test = (fruits(i,col) < fruits(tri(k),col)) elseif sens = "dn" then test =(fruits(i,col) > fruits(tri(k),col)) end if if test then temp = i for m = k to i temp2 = tri(m) : tri(m) = temp : temp = temp2 next exit for end if next next %>
<HTML><HEAD><title>Exemple</title> </HEAD><BODY><center>
<table border cellspacing=0 cellpadding=5><tr> <th> <A href="<%=url%>?sens=up&col=0">up</A> FRUITS <A href="<%=url%>?sens=dn&col=0">dn</A> </th><th> <A href="<%=url%>?sens=up&col=1">up</A> QUANTITE <A href="<%=url%>?sens=dn&col=1">dn</A> </th> </tr> <%for i = 0 to uBound(fruits)-1 %> <tr><th><%=fruits(tri(i),0)%></th><th><%=fruits(tri(i),1)%></th></tr> <%next%> </table>
</BODY></HTML>
|
Index des mini-scripts
Lien conditionné à l'existence d'une page

| s8.asp |  |  | |
| <% set FSO = _ Server.CreateObject("Scripting.FileSystemObject") fn=lcase(Request.ServerVariables("SCRIPT_NAME")) ' si c'est la version française if instr(fn,"_fr.asp") then fn2=replace(fn,"_fr","_us") ' et que la version anglaise existe if FSO.fileExists(server.mapPath(fn2)) then %> <A HREF="<%=fn2%>">English version</A> <% end if ' sinon si c'est la version anglaise elseif instr(fn,"_us.asp") then fn2=replace(fn,"_us","_fr") ' et que la version française existe if FSO.fileExists(server.mapPath(fn2)) then %> <A HREF="<%=fn2%>">Version française</A> <% end if end if %>
|
|

| s8.php |  |  | |
| <? $fn = strtolower(basename($PHP_SELF)); // si c'est la version française if(strpos($fn,"_fr.php")) { $fn2 = str_replace("_fr","_us",$fn); // et que la version anglaise existe if(file_exists($fn2)) { ?> <A HREF="<? echo $fn2; ?>">English version</A> <? } // sinon si c'est la version anglaise elseif(strpos($fn,"_us.php")) { $fn2 = str_replace("_us","_fr",$fn); // et que la version française existe if(file_exists($fn2)) { ?> <A HREF="<? echo $fn2; ?>">Version française</A> <? } } ?>
|
|
|---|
Index des mini-scripts
Répertoire automatique
demandé par Amadou pour la Présidence de la Côte d'Ivoire !
| s10.asp |  |  | |
| <% ' Liste les articles AAAAMMxxxx.ext du répertoire courant ' AAAA et MM étant l'année et le mois de publication dim nommois nommois = Array("","Janvier","Février","Mars","Avril","Mai","Juin" _ ,"Juillet","Août","Septembre","Octobre","Novembre","Décembre") ' Quel choix ? ou alors mois courant mois = request.form("lemois") if mois="" then mois=month(date) else mois=cInt(mois) an = request.form("annee") if an="" then an=year(date) else an=cInt(an) %> <HTML><BODY><title>Exemple</title> <table border=0 cellpadding=2 cellspacing=0> <tr><td colspan=4> <FONT color=#2c4793 size=5><b>Conseil des ministres</b></font><BR><BR> </td></tr><tr bgcolor=#eeeeff><form name="monform" method="post"> <td nowrap> <b>Communiqués de :</b> </td><td> <select name="lemois"> <% for i = 1 to 12 %> <option value="<%=i%>" <%if i=mois then%>SELECTED<%end if%>><%=nommois(i)%> <% next%> </select> </th><td> <select name="annee"> <% for i = 2000 to year(date) %> <option value="<%=i%>" <%if i=an then%>SELECTED<%end if%>><%=i%> <% next%> </select> </th><td> <input type="Submit" value="Lister"> </td> </form></tr> </table> <ol> <% ' Lecture de la liste des communiqués contenus dans le dossier courant Set FSO = Server.CreateObject("Scripting.FileSystemObject") set fc = FSO.getFolder(server.MapPath(".")).files nombre = 0 for each f in fc if left(f.name,6) = (an & right("0"&mois,2)) then%> <li><A href = "<%=f.name%>" target="_blank"><%=f.name%></A><br> <%nombre = nombre + 1 end if next%> </ol> <% ' combien de communiqués ? if nombre=0 then%>Aucun<%else%><%=nombre%><%end if%> communiqué<%if nombre>1 then%>s<%end if%> en <%=nommois(mois) & " " & an%> </BODY></HTML> |
Index des mini-scripts
Devis dynamique - Tester l'exemple ?

| s11b.asp |  |  | |
| <HTML><HEAD><TITLE>Devis</TITLE> </HEAD><BODY> <table border cellspacing=1 cellpadding=5> <tr bgcolor=#f0f0f0> <th>Référence</th><th>Description</th> <th>Prix unitaire</th><th>Quantité</th> </tr><form name="devis" onSubmit="return total()"> <% ' Affichage du catalogue dim mat(2,3) mat(1,0)="ref1" : mat(1,1)="Blablabla" : mat(1,2)="12.5" mat(2,0)="ref2" : mat(2,1)="Reblarebla" : mat(2,2)="8.75" ' etc... for i = 1 to ubound(mat) %> <tr> <td><% =mat(i,0) %></td> <td><% =mat(i,1) %></td> <td><center><% =mat(i,2) %> <input type="hidden" name="pu<%=i%>" value="<% =mat(i,2) %>"> </td><td><center> <input size=6 type="text" name="qte<%=i%>"> </td> </tr> <% next %> <script language="JavaScript"><!-- function total() { tot = 0 with(document.devis) { for(i=1; i<<%=i%>; i++) tot += eval("pu"+i+".value * qte"+i+".value") montant.value = tot } return false } //--></script> <tr bgcolor=#f0f0f0> <th><input type="Reset"></th> <th><input type="Submit" value="Calcul du devis"> </th><td><div align=right><b>TOTAL</b></div></td> <th><input size=6 type="text" name="montant"> </th></tr></form> </table> </BODY></HTML> |
Le même avec une base Access ?
| s11.asp |  |  | |
| <HTML><HEAD><TITLE>Devis</TITLE> </HEAD><BODY> <table border cellspacing=1 cellpadding=5> <tr bgcolor=#f0f0f0> <th>Référence</th><th>Description</th> <th>Prix unitaire</th><th>Quantité</th> </tr><form name="devis" onSubmit="return total()"> <!-- #include file="_connexion.asp"--> <% ' requete sur la table materiel SQL = "SELECT * FROM materiel" Set RS = server.createobject("ADODB.Recordset") RS.Open SQL,Conn , 3, 3 : i=0 While not RS.eof : i=i+1 %> <tr> <td><%=RS("ref")%></td><td><%=RS("des")%> </td><td><center><%=RS("p_u")%> <input type="hidden" name="pu<%=i%>" value="<%=RS("p_u")%>"> </td><td><center> <input size=6 type="text" name="qte<%=i%>"> </td> </tr> <% RS.movenext wend : conn.close : Set conn=nothing %> <script language="JavaScript"><!-- function total() { tot = 0 with(document.devis) { for(i=1; i<=<%=i%>; i++) tot += eval("pu"+i+".value * qte"+i+".value") montant.value = tot } return false } //--></script> <tr bgcolor=#f0f0f0> <th><input type="Reset"></th> <th><input type="Submit" value="Calculer le devis"> </th><td><div align=right><b>TOTAL</b></div></td> <th><input size=6 type="text" name="montant"> </th></tr></form> </table> </BODY></HTML> |
|

| s11b.php |  |  | |
| <HTML><HEAD><TITLE>Devis</TITLE> </HEAD><BODY> <table border cellspacing=1 cellpadding=5> <tr bgcolor=#f0f0f0> <th>Référence</th><th>Description</th> <th>Prix unitaire</th><th>Quantité</th> </tr><form name="devis" onSubmit="return total()"> <? // Affichage du catalogue $mat = array(); $mat[1] = array("ref1","Blablabla","12.5"); $mat[2] = array("ref2","Reblarebla","8.75"); // etc... for($i=1;$i<=sizeof($mat);$i++) { ?> <tr> <td><? echo $mat[$i][0]; ?></td> <td><? echo $mat[$i][1]; ?></td> <td><center><? echo $mat[$i][2]; ?> <input type="hidden" name="pu<? echo $i; ?>" value="<? echo $mat[$i][2]; ?>"> </td><td><center> <input size=6 type="text" name="qte<? echo $i; ?>"> </td> </tr> <? } ?> <script language="JavaScript"><!-- function total() { tot = 0 with(document.devis) { for(i=1; i<<? echo $i; ?>; i++) tot += eval("pu"+i+".value * qte"+i+".value") montant.value = tot } return false } //--></script> <tr bgcolor=#f0f0f0> <th><input type="Reset"></th> <th><input type="Submit" value="Calculer le devis"> </th><td><div align=right><b>TOTAL</b></div></td> <th><input size=6 type="text" name="montant"> </th></tr></form> </table> </BODY></HTML> |
Le même avec une base MySQL ?
| s11.php |  |  | |
| <HTML><HEAD><TITLE>Devis</TITLE> </HEAD><BODY> <table border cellspacing=1 cellpadding=5> <tr bgcolor=#f0f0f0> <th>Référence</th><th>Description</th> <th>Prix unitaire</th><th>Quantité</th> </tr><form name="devis" onSubmit="return total()"> <? // requete sur la table materiel include("_connexion.php"); $requete = "SELECT * FROM materiel"; $result=mysql_query($requete); $i=0; While($val = mysql_fetch_array($result)) { $i++; ?> <tr> <td><? echo $val["ref"]; ?></td> <td><? echo $val["des"]; ?></td> <td><center><? echo $val["p_u"]; ?> <input type="hidden" name="pu<? echo $i; ?>" value="<? echo $val["p_u"]; ?>"> </td><td><center> <input size=6 type="text" name="qte<? echo $i; ?>"> </td> </tr> <? } mysql_close(); ?> <script language="JavaScript"><!-- function total() { tot = 0 with(document.devis) { for(i=1; i<=<? echo $i; ?>; i++) tot += eval("pu"+i+".value * qte"+i+".value") montant.value = tot } return false } //--></script> <tr bgcolor=#f0f0f0> <th><input type="Reset"></th> <th><input type="Submit" value="Calculer le devis"> </th><td><div align=right><b>TOTAL</b></div></td> <th><input size=6 type="text" name="montant"> </th></tr></form> </table> </BODY></HTML> |
|
|---|
Index des mini-scripts
Didier le
22/03/2004 (57 160 hits) |
|
|