Home
 Articles
 Book Store
 DIT
 Login
 Register
 News
 Online Training Courses
 Programming
 Research Papers
 Software Development
 Students Notes
  Web Hosting


 
 

 

Learning PHP from admin of this Websie

 PHP About  PHP $_GET  PHP Array Refrence  PHP MYSQL Database
 PHP  Intro  PHP $_POST  PHP Date/Time Ref  PHP Sql Connection
 PHP Download  PHP Date()  PHP File System Ref  PHP Create
 PHP Syntax  PHP include  PHP FTP  PHP Insert
 PHP Variables   PHP File system  PHP HTTP  PHP Select
 PHP Operators  PHP Upload  PHP Mail Refrence  PHP Where
 PHP if..else  PHP Cookies  PHP Math  PHP Order By
 PHP switch  PHP Sessions  PHP MYSQL  PHP Update
 PHP array  PHP Mail  PHP String  PHP Delete
 PHP loops  PHP E-mail injunction  PHP Miscellaneous  PHP ODBC
 PHP Functions  PHP XML  PHP XML Parser  
 PHP Forms  PHP SimpleXML  PHP Zip  

 
The Dictionary object
 

In order to learn how Dictionary object works we will create a small script which will translate number 1 to 10 from English to Spanish.
 
 

translate.asp  
<%
SET MyDictionary=CreateObject("Scripting.Dictionary")
MyDictionary.Add "one","uno"
MyDictionary.Add "two","dos"
MyDictionary.Add "three","tres"
MyDictionary.Add "four","cuatro"
MyDictionary.Add "five","cinco"
MyDictionary.Add "six","seis"
MyDictionary.Add "seven","siete"
MyDictionary.Add "eight","ocho"
MyDictionary.Add "nine","nueve"
MyDictionary.Add "ten","diez"
EnglishNumber="four"
SpanishNumber=MyDictionary.Item (EnglishNumber)
Response.Write(SpanishNumber)
%>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

How the script works

  • Fist we have define a Dictionary named "Mydictionary" (line 2)
  • We have add to the dictionary the data corresponding to the different number in English and Spanish (lines 4 to 13).
    When adding pairs of English and Spanish numbers to the Dictionary object, the number writen in English is a Key, and the number writen in Spanish a Item.
  • In line 15 we have defined a variable named EnglishNumber and we have provided a value for this variable (in red).
  • In line 16 we have defined a new variable (SpanishNumber) and we have get its value from the dictionary by indicating we want to get the Item corresponding to a specific Key (EnglishNumber).
  • In line 17 the translated number is send to our visitor. The response will be "cuatro".
We may change the values in our dictionary by using this kind of code:
  • MyDictionary.Key ("one")="1"
    In our original script the key "one" will be substitute by a new key value ("1"). The item "uno" will not be changed.
  • MyDictionary.Item ("two")="2"
    In our original script the item corresponding to key "two" will be substitute by a new item value ("2"). The key "two" will not be changed.
We may display the number of element pairs in the dictyonary by using this code:
  • MyDictionary.Count
If we want to check whether a key exists in our dictionary before responding to our visitor we will use this kind of comparisoncode
 
if MyDictionary.Exists ("ten")=True  then
           Response.Write("this key is included in the dictionary")
lse
           Response.Write("Error: no such a key in the dictionary")
end if

Example: Translation of a number from English to Spanish

This example uses most of the elements explained above.

  • If there is no information posted to the script (line 6), the script will send to the visitor the form in the  Sendform() subrouting (lines 34-40).
  • When a request to translate a number is get the script will check whether the number corresponds to a key in the dictionary (line 25). If the response is afirmative the corresponding item is send to the visitor. In case the key does not exists, a "No translation available" response is send to the visitor (line 29).
translation.asp  
<html>
<title>Page under construction</title>
<body>

<% 
if request.form="" then
      Sendform()
else
      SET MyDictionary=CreateObject("Scripting.Dictionary")

MyDictionary.Add "one","uno"
MyDictionary.Add "two","dos"
MyDictionary.Add "three","tres"
MyDictionary.Add "four","cuatro"
MyDictionary.Add "five","cinco"
MyDictionary.Add "six","seis"
MyDictionary.Add "seven","siete"
MyDictionary.Add "eight","ocho"
MyDictionary.Add "nine","nueve"
MyDictionary.Add "ten","diez"
      EnglishNumber=request.form("EnglishNumber")
      Response.Write("English number: " & EnglishNumber)

      if MyDictionary.Exists (EnglishNumber)=True then
           SpanishNumber=MyDictionary.Item (EnglishNumber)
           Response.Write("<BR>Spanish number: " & SpanishNumber)
     else
           Response.Write("<BR>Spanish number: " & "No translation available")
     end if
end if
%>

<% Sub Sendform() %>
<form action=translation.asp method=post>
Write a number in English<BR>
<input type=text size=30 name=EnglishNumber><BR>
<input type=submit Value="Enter to my Secret Page">
</form>
<% End Sub %>

</body>
</html>

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

 

Example: Password protected information

In this example keys and items are used as usernames and passwords. It is very similar to the one above.
 
 

secretpage.asptry !  
<% 
if request.form="" then
      Sendform()
else
      SET MyDictionary=CreateObject("Scripting.Dictionary")
      MyDictionary.Add "John","123"
      MyDictionary.Add "Peter","456"
      MyDictionary.Add "Anna","789"
      Username=request.form("Username")
      Password=request.form("password")

      if MyDictionary.Exists (Username)=True AND Password=MyDictionary.Item (Username) then
            SecretInfo() 
     else
           Response.Write("Error: incorrect userame or password")
     end if
end if
%>

<% Sub Sendform() %>
<form action=secretpage.asp method=post>
Username: <input type=text size=30 name=Username><BR>
Password: <input type=password size=30 name=Password><BR>
<input type=submit Value="Submit">
</form>
<% End Sub %>
 

<% Sub SecretInfo() %>
<html>
<head>
<title>My Secret Page</title>
</head>
<body bgcolor=FFFFFF>
<center>
<h1>This is my secret info</h1>
Hello !<BR>
Do you want to be my friend?
</center>
</body>
</html>
<% End Sub %>

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

 


  

 

 
 

Copyright © http://www.itbaba.com. 2007 All Rights