Frog frog consultation: how to solve the problem of dynamic table joining

Source: Internet
Author: User

Frog frog consultation: how to solve the problem of dynamic table joining
The problem is that I want to extract records from the database and output the following form of tables, that is, a table is generated for every four record sets, and each recordset is displayed in two rows and two columns in each table, how to dynamically generate it? Note that the ID attribute in the table needs to increase progressively from 0.
<Table border = 1 id = 0> <tr> <TD> RS (0) </TD>
<TD> RS (0) </TD> </tr> <tr>
<TD> RS (0) </TD>
<TD> RS (0) </TD> </tr> </table>

<Table border = 1 id = 1> <tr> <TD> RS (0) </TD>
<TD> RS (0) </TD> </tr> <tr>
<TD> RS (0) </TD>
<TD> RS (0) </TD> </tr> </table>

The record set we usually output is a row of a record, and occasionally it is displayed in a column. One row shows N records. This time, we have to close one table for every 4 records, you must ensure that the generated table cannot only contain tags that are not closed.

Convert the problem
Generate a table using numbers between 1 and a certain number, and use the document. Write method of JS as well.
Actually, this is an algorithm problem.

<Table border = 1 id = 0> <tr> <TD> 1 </TD>
<TD> 2 </TD> </tr> <tr>
<TD> 3 </TD>
<TD> 4 </TD> </tr> </table>

<Table border = 1 id = 1> <tr> <TD> 5 </TD>
<TD> 6 </TD> </tr> <tr>
<TD> 7 </TD>
<TD> 8 </TD> </tr> </table>

...... The following is omitted. I think we can see that the rule has come up. We have to consider that if the number of termination cannot be fully divided by 4, we have to ensure that the generated HTML Tag is correct and should not be left blank, it cannot be displayed badly. If necessary, you have to consider displaying 2 numbers in the row above a table, and 1 number in the row below. In this way, the cell below must span two cells, for example, at 7

Solved the problem. The following algorithm was written one morning.
<Script language = "JavaScript">
<! --
VaR J, Max, STR;
Max = 100;
STR = "";
J = 0;
For (VAR I = 1; I <= max; I ++ ){
If (I % 4 = 1 ){
STR + = ("<Table border = 1 id =" + (J ++) + "> <tr> <TD>" + I + "</TD> ");
If (I = max) STR + = ("</tr> </table> ");
}
If (I % 4 = 2 ){
STR + = ("<TD>" + I + "</TD> ");
If (I = max ){
STR + = ("</tr> </table> ");
} Else {
STR + = ("</tr> <tr> ");
}
}
If (I % 4 = 3 ){
STR + = ("<TD ")
If (I = max) STR + = ("colspan = 2 ")
STR + = (">" + I + "</TD> ");
}
If (I % 4 = 0) STR + = ("<TD>" + I + "</TD> </tr> </table> ");
}
// Document. Write (STR );
// Alert (STR );
// -->
</SCRIPT>
<Script language = "VBScript">
<! --
Dim J, Max, STR
Max = 8
J = 0
STR = ""
Dim I
For I = 0 to Max
If (I mod 4 = 1) then
STR = STR & "<Table border = 1 id =" & J & "> <tr> <TD>" & I & "</TD>"
If (I = max) Then STR = STR & "</tr> </table>"
J = J + 1
End if
If (I mod 4 = 2) then
STR = STR & "<TD>" & I & "</TD>"
If (I = max) then
STR = STR & "</tr> </table>"
Else
STR = STR & "</tr> <tr>"
End if
End if
If (I mod 4 = 3) then
STR = STR & "<TD"
If (I = max) Then STR = STR & "colspan = 2"
STR = STR & ">" & I & "</TD>"
End if
If (I mod 4 = 0 and I <> 0) Then STR = STR & "<TD>" & I & "</TD> </tr> </table>"
Next
Document. Write (STR)
'Msgbox (STR)
// -->
</SCRIPT>

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.