Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multiple rows...

Status
Not open for further replies.

wouter

Technical User
Jul 3, 2000
45
NL
Hello,

i want to read a number of records from a database. It's just names (John Johnson etc.) But i never know what the result of the query will be, so i don't know how many records the query will return.
I want Cold Fusion to divide the number of results by three, so i can make three kolumns. But that's not all..
I want the records to be put on the screen like this:
1 5 9
2 6 10
3 7 11
4 8 12

Not this way:
1 2 3
4 5 6
7 8 9
10 11 12

How can i do this?

wouter [sig][/sig]
 
You'll have to read all the records in before you can generate any HTML. There are at least 2 ways of doing this:

1) Read values into a ColdFusion array, instead of generating HTML directly. When you have read all records, you will know how many there are, and can start generating the table. If there were 14, then your first row would contain 1, 6 & 11. I'll leave the algorithm for this to you.

2) Use client-side JavaScript to generate the HTML for the table on the client.

a) Put a javascript function called MakeTable in the HEAD section of your HTML. This will be run when the form is loaded:
Code:
<SCRIPT Language=&quot;JavaScript&quot;>
// The following line is generated by your ColdFusion query.
// Will need to use JSStringFormat function if it's possible
// for any name to contain a quote char.
nameList=['John Johnson', 'Sue Smith', 'Tim Tyler',...];

function MakeTable()
{
    var numNames = nameList.length;
    var numRows = (numNames+2) / 3;
    var rowNum;
    var colNum;
    var html = '<TABLE>';

    for (rowNum=0; rowNum<numRows; rowNum++)
    {
        html += '<TR>';
        for (colNum=0; colNum<3; colNum++)
        {
            html += '<TD>';

            if (numNames > rowNum*3 + colNum)
                html += nameList[rowNum*3 + colNum];
            else
                html += '&nb sp;';

            html += '</TD>';
        }
        html += '</TR>';
    }
    html += '</TABLE>';
    nameTable.innerHTML = html;
}
</SCRIPT>

b) Add a clause to the BODY tag, so it looks like the following.
<BODY OnLoad=&quot;MakeTable()&quot;>

c) Add a DIV tag at the point where you want your table. The MakeTable function will 'fill' this with your table details when the page is loaded on the client.
<DIV ID=nameTable></DIV>

Hope this helps,
Russell Amner [sig][/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top