When the solution matters

Tips...

Registration Documentation Knowledgebase Seminars / Training Technical Support Partner Central

4DLOOP

Now we need to create a method that will populate the LoopData table. With the 4D Design environment open, create a new Project Method by going to the Design menu and choosing New Method... Name the method WSC_LoopDataCreate. Enter the following into the Project Method:

If (Records in table([LoopData])=0)

C_LONGINT($iMaxRecords;$iRecIdx;$iCharIdx;$iFieldLength;$iType)

$iMaxRecords:=25

GET FIELD PROPERTIES(->[LoopData]AlphaField;$iType;$iFieldLength)

For ($iIdx;1;$iMaxRecords)
CREATE RECORD([LoopData])
For ($iCharIdx;1;$iFieldLength)
[LoopData]AlphaField:=[LoopData]AlphaField+Char(Random%256)
End for
[LoopData]NumberField:=Random*Tickcount
SAVE RECORD([LoopData])
End for

End if

ALL RECORDS([LoopData])

screenshot

When you are done, your WSC_LoopDataCreate method should look like the one in the picture above. The objective of this method is to fill in the two fields in the [LoopData] table with some random values. Go to the Method menu and choose Method Properties, then select the Available through 4DACTION option in the properties dialog. Press the OK button.

The next thing to do is to return to your text editor and open up the index.shtml page we've been working with. You should replace the entire contents of this method with the following:

<html>
<head>
<title>4D Web Loops</title>
</head>
<body>
<!--4DSCRIPT/WSC_LoopDataCreate-->
<table width="100%" border="1" cellspacing="2" cellpadding="0">
<tr>
<td width="50%"><b>AlphaField</b></td>
<td width="50%"><b>NumberField</b></td>
</tr>
<!--4DLOOP [LoopData]-->
<tr>
<td width="50%"><!--4DVAR [LoopData]AlphaField--></td>
<td width="50%"><!--4DVAR [LoopData]NumberField--></td>
</tr>
<!--4DENDLOOP-->
</table>
</body>
</html>

Before proceeding, be sure to save your index.shtml file and all open 4D Project Methods.

Return to your web browser and again type "127.0.0.1" into the address field. If all goes well, your web browser should display something along these lines:

screenshot

So how did all this work? The first thing that happened when the index.shtml page was built was the WSC_LoopDataCreate method was run via a call to 4DSCRIPT: <!--4DSCRIPT/WSC_LoopDataCreate-->. The WSC_LoopDataCreate method checked to see if there were any records in the [LoopData] table. If none existed, it created 25 of them, and assigned random values to the AlphaField and NumberField fields. The WSC_LoopDataCreate method also called ALL RECORDS([LoopData]) in the last line. This is so that all records in the table would be looped through.

The 4DLOOP tag is embedded into the HTML <TABLE> structure. A standard HTML table is created, and after the first row (used for the column titles), the 4DLOOP tag is inserted:

<!--4DLOOP [LoopData]-->

Inside the "cells" of the second row of the HTML table, we use the 4DVAR tag to dispaly the contents of the AlphaField and NumberField values:

<!--4DVAR [LoopData]AlphaField-->
<!--4DVAR [LoopData]NumberField-->

After the second table row and before the end table tag (</TABLE>), we add:

<!--4DENDLOOP-->

When the index.shtml page is sent out via 4D, everything between the <!--4DLOOP [LoopData]--> and <!--4DENDLOOP--> tags will be repeated for as many records of [LoopData] as exist in the selection of records at the time the page is sent. At the "top" of every loop, the next [LoopData] record is automatically loaded.

Now that you have completed this tutorial, you should be familiar with using the 4DLOOP tag. What's more, you have now started using 4D's powerful built-in database and integrating that data dynamically into a web page. You can apply this information for use with other tutorials in this series .

 Previous Step  1 2 3 4  


International | Company | Contact 4D | Site Map | Privacy Policy | © 4D UK 1995-2008 | Change font size: [A] [A] [A] | Print this page