5. asp script variables, functions, procedures and condition statements

Source: Internet
Author: User
Tags case statement
In the previous period, the author briefly introduced some basic knowledge about VBScript, one of ASP scripting languages. In this period, I will continue to explain how to compile VBScript, by presenting a series of examples of VBScript in ASP programming, you can better understand VBScript.
After learning the basic concepts of variables, constants, and processes in the script language VBScript, we will continue to introduce the functions and syntax of VBScript. Functions and procedures are named code blocks, but they differ greatly. When a process completes a program task, the function returns a value. We can understand that the process is like a complete sentence, and the function is like a word. For example, if you want to obtain the square root of a number, you only need to transmit it to the sqr () function of VBScript. This function returns the square root of the number immediately. For example:
A = sqr (9)

Then a = 3. Mastering the functions of the scripting language will bring you great convenience in writing ASP programs. For the exercises that the author arranged at the end of the above phase for everyone after class, if you do not have full understanding of the functions in the scripting language, it may take a lot of effort to solve such a small problem. Now let's review this exercise after class.
"The author is using ASP to create a web-based BBS system and hopes to add a special feature to it, that is, after logging on to the BBS, any user can view all newly released information in the last seven days."

If you are not familiar with vbscrip, you will not know that vbscrip itself provides a function dateserial to get the difference or sum between dates. Its syntax is as follows:

Dateserial (year, month, day)

If you want to specify a date, for example, January 1, November 10, 1998, the value range of each parameter in the dateserial function should be acceptable. The value of today should be between 1 and 31, the value of the month should be between 1 and 12. However, you can also use a numeric expression indicating the year, month, and number of days before or after a specific day to specify a relative date for each parameter. The following example uses a numeric expression instead of an absolute date. Here, the dateserial function returns the date of the second day (10-1) before January 1, 1990, and two months (11-2. The procedure is as follows:
Datep = dateserial (1998-20, 11-2, 10-1)

For the year parameter, if the value range is from 0 to 99, it is interpreted as 1900 to 1999. For the year parameter beyond this range, four digits are used to represent the year (for example, January 1, 1800 ). When the value of any parameter exceeds the acceptable range, it is carried to the next large time unit as appropriate. For example, if 35 days are specified, the number of days is interpreted as a month plus the number of days. The number of days is determined by the year and month. However, if the value of the parameter exceeds the range of-32,768 to 32,767, or the date specified by the three parameters (either directly or through the expression) exceeds the acceptable date range, an error occurs. After learning how to use the dateserial function, let's take a look at the topic that the author has set. Everything will be solved. Below I will publish this part of the code in the program as follows: itemp = dateserial (Year (date), month (date), Day (date)-7)
Itemp = datevalue (itemp)
SQL = "select * From message where message. creatime between #" & Date & "# And #" & itemp &"#"

Here we come into contact with a group of functions year, month, and day, which are used to obtain the year, month, and day of a date. Date is a constant, indicating the date of today, and the function datevalue is a variable that converts string variables into date formats. In the third line of this program, we first came into contact with the standard SQL query statement. What does this statement mean?

"Select" is a standard SQL database query command. Using the SELECT statement, we can retrieve data in the database and provide the query results to users, "*" indicates to query all records in the database named "message", and "where" is used to set a query condition, to retrieve the qualified records in the database, "message. "creatime" is a variable that stores records of the creation date in the database. The entire statement is used to query all records in the database named message, and store all records created within the seven days before today and today in the variable SQL. This may be due to the first contact with SQL statements, so I cannot fully understand the functions of SQL statements at a time. However, I don't have to worry about using SQL statements in the next chapter.
Through the above study, we should be able to understand the role of functions in the program. Of course, we do not have to stick to the function, but there is only one shortcut to be skillful in using IT-more practices. Next, let's take a look at the basic syntax of VBScript.

If you know the programming language, you must know that the statements that control the program flow in the program can be divided into conditional statements and cyclic statements. In VBScript, you can use the following conditional statements: if... then... else statement
Select case statement

If... then... else is used to calculate whether the condition is true or false, and specify the statement to be run based on the calculation result. Generally, the condition is an expression that uses a comparison operator to compare values or variables. If... then... else statements can be nested as needed.

Let's create two sample files: if1.asp and if2.asp.

Cut the following statement into the workbook and save it as if1.asp (Note: remove the spaces after "<" in the program) <HTML>
<Head>
<Title> if1.asp </title>
</Head> <body bgcolor = "# ffffff">
<Form action = "if2.asp" method = get>
Your first name <input name = "firstname" maxlength = 20> <p>
Your last name <input name = "lastname" maxlength = 20> <p>
<Input type = submit> <input type = reset>
</Form>
</Body>
</Html>
Cut the following statement into the notebook and save it as if2.asp <HTML>
<Head>
<Title> ifrespond. asp </title>
</Head>
<% Fname = request. querystring ("firstname ")
Lname = request. querystring ("lastname ")
If fname = "George" and lname = "Washington" then %>
Hi. You must be the first president!
<% Else %>
Hi! Nice to meet you
<% End if %>
</Body>
</Html>

Asp1.asp generates a text input box, requiring the user to enter the last name and name, such:

 

Asp2.asp uses the if statement to determine whether the name entered by the user is "George Washington" and give feedback accordingly. Here we have encountered an ASP built-in object request, which can be used to access any information transmitted using HTTP requests, including parameters, cookies, and user authentication transmitted using the post or get method in the HTML table. The querystring set retrieves the value of the variable in the HTTP query string. The HTTP query string is composed of question marks (?) Specify the value after. For example:
Http: // localhost/if2.asp? Firstname = George & lastname = Washington

Generate a variable name string with the value "firstname = George & lastname = Washington. The author of ASP objects will focus on it in several articles in the future.

If... then... an else statement deformation allows you to select from multiple conditions, that is, add the elseif clause to expand if... then... else statements allow you to control a variety of possible program processes.

Expand the asp2.asp program as follows: <%
Fname = lcase (request. querystring ("firstname "))
Lname = lcase (request. querystring ("lastname "))
If fname = "George" and lname = "Washington" then %>
Hi. You must be the first president! <P>
<% Elseif fname = "Ronald" and lname = "Reagan" then %>
Hi. You must be the actor president! <P>
<% Elseif fname = "Jimmy" and lname = "Carter" then %>
Hi. You must be the peanut farmer president! <P>
<% Elseif fname = "Naoko" or fname = "Charles" then %>
Hi. Your name reminds me of someone, but I am not sure who! <P>
<% Else %>
Hi! Nice to meet you
<% End if %>

You can add any number of elseif clauses to provide multiple options. However, using multiple elseif clauses often makes the program cumbersome. A better way to select multiple conditions is to use the select case statement.

The Select case structure provides a flexible form of the IF... then... elseif structure. You can select multiple statement blocks to execute one of them. The Select case statement provides similar functions as the IF... then... else statement, but can make the code more concise and easy to read. The Select case structure uses a simple test expression that is only calculated once at the beginning. The result of the expression is compared with the value of each case in the structure. If yes, execute the statement block associated with the case. We can also use the select case statement to write the asp2.asp file:
<%
Fname = lcase (request. querystring ("firstname "))
Lname = lcase (request. querystring ("lastname "))
Name = fname + lname
Select case name
Case "georgewashington"
Response. Write "Hi. You must be the first president! <P>"
Case "ronaldreagan"
Response. Write "Hi. You must be the actor president! <P>"
Case "jimmycarter"
Response. Write "Hi. You must be the peanut farmer president! <P>"
Case "naokocharles"
Response. Write "Hi. Your name reminds me of someone, but I am not sure who! <P>"
Case else
Response. Write "Hi! Nice to meet you"
End select %>

Note that the select case structure only calculates one expression at the beginning and only calculates once, while the if... then... the elseif structure calculates the expressions of each elseif statement. These expressions can be different. Therefore, the Select case structure can be used to replace the IF... then... elseif structure only when the expressions calculated by each elseif statement are the same. The Select case statement can also be nested. Each layer of nested select case statement must have an end SELECT statement that matches it.

The usage of VBScript functions and conditional statements in the script language described above cannot be detailed due to the length. I hope you will be interested in learning ASP, you can perform self-study and exercises to a certain extent after class. In the process of daily ASP application development, the author gradually realized the importance of the script language. the flexible use of the script language will not only greatly improve the ASP application development process, it saves a lot of time for website developers and enhances the execution efficiency and functions of ASP applications. To do this, you must first sharpen the tool. Therefore, the author strongly recommends that you master the script language, which will be of great help to your ASP program development. As this document is not a VBScript tutorial, you can only use a small space to briefly introduce some basic knowledge about VBScript. After introducing the cyclic statements of VBScript in the next phase, we will officially start learning ASP built-in objects. To go deep into VBScript, we suggest you find some teaching materials for your own study. If you have any questions after reading this article, please mail me in time. If you have any good suggestions, please send me a letter. Thank you.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.