Person.CSP — MVBasic Code
Now we will insert an MVBasic routine, inside <script></script> tags, into Person.CSP. This routine will read the data from MVFILE.PERSON and add it to Person.CSP. Place the following code in between the closing </caption> and the closing </table> tags in Person.CSP.
<script language="mvbasic" runat="server">
OPEN "DICT", "PERSON" ELSE PRINT "DICT NOT OPEN";GOTO 100
READV NAME.AMC FROM "NAME",2
READV HAIR.AMC FROM "HAIR",2
READV AGE.AMC FROM "AGE",2
LIST = 2
OPEN "PERSON" TO PERSFILE
ELSE PRINT "FILE NOT OPEN";GOTO 100
SELECTN PERSFILE TO LIST
I=1
LOOP
END.OF.LIST=0
READNEXT ID FROM LIST ELSE END.OF.LIST=1
UNTIL END.OF.LIST DO
IF MOD(I,2) <>0 THEN PRINT " <tr class=odd>"
ELSE PRINT " <tr class=even>"
I=I+1
GOSUB PERSONS
PRINT "</tr>"
REPEAT
GOTO 100
PERSONS:
READ PERSON.ITEM FROM PERSFILE, ID
ELSE PRINT "Person not found";GOTO 100
PRINT "<td>":PERSON.ITEM<NAME.AMC>:"</td>"
PRINT "<td>":PERSON.ITEM<AGE.AMC>:"</td>"
PRINT "<td>":PERSON.ITEM<HAIR.AMC>:"</td>"
PRINT " <td><a href='Phones.CSP?ID=":ID:"'>View Phones</a></td>"
Return
100
</script>
The MVBasic routine opens MVFILE.PERSON and reads through all of the items in the file. For each item that it finds, the routine outputs the values of the NAME, AGE, and HAIR attributes. The only unusual feature of this code is that the PRINT statements output HTML tags in addition to the data from MVFILE.PERSON. This is standard CSP practice.
Note the following about the PRINT statements:
-
The first two PRINT statements insert HTML <tr></tr> tags into the output. These tags define the rows of the HTML table. Odd numbered rows contain the attribute class="odd" while even number rows contain the attribute class="even". These attributes support the "zebra" styling of the table — alternating rows are slightly different colors.
-
The first three PRINT statements in the PERSONS subroutine insert MVFILE.PERSON data surrounded by HTML <td></td> tags. These tags place the data into “table cells”.
-
The fourth PRINT statement in the PERSONS subroutine inserts the text “View Phones” into the table. In addition to wrapping this text in <td></td> tags, it also creates an HTML hyperlink with the text. The url of the hyperlink, href attribute of the anchor (<a>) tag, includes Phones.CSP along with a query string specifying the item-ID of the current MVFILE.PERSON item. The query string, marked with the ?, passes the item-ID value to Phones.CSP.
In order to use MVBasic object syntax in your CSP scripts, you must first run PROTOCLASS on your DICT files. For instructions read the pages on PROTOCLASS in Using SQL and ODBC. Start reading on the PROTOCLASS page.