*** 怎样将显示的结果分成 4 列显示在页面中? ***

tom00007 2006-02-09 04:54:31
比如记录集一列的效果是:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
......


现在想在页面中按 4 列显示:
1 8 15 22
2 9 16 23
3 10 17 24
4 11 18 25
5 12 19
6 13 20
7 14 21
...全文
223 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
Net8Java 2006-02-17
  • 打赏
  • 举报
回复
这个我做过,思路是在<tr>行起头标签外面做个判断,如果是4的倍数就有<tr>如果不是就没有
不要写</tr>行结束标签,你试试!
crazy2die 2006-02-16
  • 打赏
  • 举报
回复
<%
rs.last();
int count = rs.getRow();
int row = count/4;
rs.beforeFirst();
int value = 0;
while(rs.next()){
for(int i=1;i<=row;i++){
if((i+row*3)<=count){
out.println("<td>" + rs.getxxx(i) + "</td>" +
"<td>" + rs.getxxx(i+row) + "</td>" +
"<td>" + rs.getxxx(i+row*2) + "</td>" +
"<td>" + rs.getxxx(i+row*3) + "</td>");
}else if((i+row*3)>count&&(i+row*2<=count){
out.println("<td>" + rs.getxxx(i) + "</td>" +
"<td>" + rs.getxxx(i+row) + "</td>" +
"<td>" + rs.getxxx(i+row*2) + "</td>" +
"<td>" + " " + "</td>");
}else if((i+row*2>count)&&(i+row<=count)){
out.println("<td>" + rs.getxxx(i) + "</td>" +
"<td>" + rs.getxxx(i+row) + "</td>" +
"<td>" + " " + "</td>" +
"<td>" + " " + "</td>");
}else if(i+row>count&&i<=count){
out.println("<td>" + rs.getxxx(i) + "</td>" +
"<td>" + " " + "</td>" +
"<td>" + " " + "</td>" +
"<td>" + " " + "</td>");
}

}
%>
crazy2die 2006-02-16
  • 打赏
  • 举报
回复
<%
rs.last();
int count = rs.getRow();
int row = count/4;
for(int i=1;i<row;i++){
out.println("<td>" + rs.getxxx(i) + "</td>" +
"<td>" + rs.getxxx(i+row) + "</td>" +
"<td>" + rs.getxxx(i+row*2) + "</td>" +
"<td>" + rs.getxxx(i+row*3) + "</td>");
}
%>
fog628 2006-02-15
  • 打赏
  • 举报
回复
<%@ page contentType="text/html; charset=UTF-8" %>

<%@ page import="java.sql.*"%>
<%@ page import="java.util.*"%>


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<%!
Connection conn;
Statement stmt;
%>
<%
String strSql = "select 1 id from dual ";
strSql += "union all select 2 from dual ";
strSql += "union all select 3 from dual ";
strSql += "union all select 4 from dual ";
strSql += "union all select 5 from dual ";
strSql += "union all select 6 from dual ";
strSql += "union all select 7 from dual ";
strSql += "union all select 8 from dual ";
strSql += "union all select 9 from dual ";
strSql += "union all select 10 from dual ";
strSql += "union all select 11 from dual ";
strSql += "union all select 12 from dual ";
strSql += "union all select 13 from dual ";
strSql += "union all select 14 from dual ";
strSql += "union all select 15 from dual ";
strSql += "union all select 16 from dual ";
strSql += "union all select 17 from dual ";
strSql += "union all select 18 from dual ";
strSql += "union all select 19 from dual ";
strSql += "union all select 20 from dual ";
strSql += "union all select 21 from dual ";
strSql += "union all select 22 from dual ";
strSql += "union all select 23 from dual ";
strSql += "union all select 24 from dual ";
strSql += "union all select 25 from dual ";


try{
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url = "jdbc:oracle:thin:@localhost:1521:ORA";
//ORA为 SID
String user = "scott";
String password = "tiger";
conn = DriverManager.getConnection(url, user, password);
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery(strSql);

int recordCount = 0; //record count
if(rs.last()){
recordCount = rs.getRow();
}
int[] arrData = new int[recordCount];
rs.beforeFirst();
int index = 0;
while(rs.next()){
arrData[index++] = rs.getInt(1);
}

//for(int i = 0; i < arrData.length; i++){
// out.println(arrData[i] + "<br>");
//}
int rowCount = recordCount / 4; //row count
if(recordCount % 4 != 0){
rowCount += 1;
}
//out.println(rowCount);
%>
<body>
<table border="1">
<%
for (int i = 0; i < rowCount; i++){
%>
<tr>
<td width="20"><% if(i < arrData.length) out.println(arrData[i]); %></td>
<td width="20"><% if(i + rowCount * 1 < arrData.length)
out.println(arrData[i + rowCount * 1]);
else
out.println(" "); %></td>
<td width="20"><% if(i + rowCount * 2 < arrData.length) out.println(arrData[i + rowCount * 2]);
else
out.println(" "); %></td>
<td width="20"><% if(i + rowCount * 3 < arrData.length) out.println(arrData[i + rowCount * 3]);
else
out.println(" "); %></td>
</tr>
<%
}
%>
</table>
</body>
<%
}catch(Exception e){
out.println(e);
}finally{
stmt.close();
conn.close();
}

%>
</html>
hanhongmin 2006-02-11
  • 打赏
  • 举报
回复
死脑子,此题无解!!!!!!!这是数学问题!!
请问搂主15怎么排阿??????
在记录总数a>5并有允许出现3列的情况下可解

只是思路:
如果是a(a>5)
case1: a整除4-------自己捉摸去
case2: !a整除4
a除以4取整得b
b再加1,得c-----这是行数

最后一列的行数就是:c-(4*c-a)=d(d=0则出现3列的情况)
——————————

不知道对不对,如果对的话完善一下,代码自己捉摸去

小弟妄言,多多包含
wangx1949 2006-02-10
  • 打赏
  • 举报
回复
两层循环,判断有几列则产生几个table,然后每个table里面再显示相应记录.
xunxm 2006-02-10
  • 打赏
  • 举报
回复
没怎么弄过,不过我会考虑排好序后放在BEAN里面,最后在页面上显示。
tom00007 2006-02-10
  • 打赏
  • 举报
回复
大家看看有什么优化的方法吗?
fog628 2006-02-10
  • 打赏
  • 举报
回复
注意:从第2列起,就有可能会数组越界,要用if来判断一下
fog628 2006-02-10
  • 打赏
  • 举报
回复
用数组来做应该会好点:

1.得到总记录数及行数:
if (reccount不是4的倍数) count=reccount增加到4的倍数
row = count / 4;

2.遍历ResultSet,把1,2,3.....25(假如是25)放到一个数组里arrData

3.生成表格:
for(int i = 0; i < row; i++){
%>
<tr>
<td>arrData[i]</td>
<td>arrData[i + 7 * 1]</td>
<td>arrData[i + 7 * 2]</td>
<td>arrData[i + 7 * 3]</td>
</tr>
<%
}

注意:从第行起,就有可能会数组越界,要用if来判断一下
tom00007 2006-02-10
  • 打赏
  • 举报
回复
我快疯了......
tom00007 2006-02-10
  • 打赏
  • 举报
回复
非常明确的 4 列!

tom00007 2006-02-09
  • 打赏
  • 举报
回复
To: Samland(samland)
我现在得到的记录是 30 ,现在的现象是 我的最后一列显示为 9 个了,那只有把 前三列显示成 8 第四列显示成 6 ,这样才是罪理想的!

请指点!

最好能说的详细一点儿...

谢谢!!!
Samland 2006-02-09
  • 打赏
  • 举报
回复
给出思路:
reccount=记录总数
if (reccount不是4的倍数) count=reccount增加到4的倍数
row = count / 4;
<tr>
for (1 to 4) {
<td>
<div>
for (1 to row) 输出 rs.next()就内容
</div>
</td>
}
</tr>
tom00007 2006-02-09
  • 打赏
  • 举报
回复
看看我的代码吧:(现在还在调试...)大家帮我看看吧!!!
<%
......

int recordNum = 0;

while( rs.next() )

recordNum++;
{
%>



<table border="1" width="100%">
<tr>

<td>
<table border="1" width="100%">
<tr>
<td>
<table border="1" width="100%">
<tr>
<td>
<table border="1" width="100%">
<%
rs.beforeFirst();

int n = (recordNum + 1)/4;
int i = 0;

while( i < n && rs.next() )
{
%>
<tr>
<td><% out.println(i+1);%></td>
<td><%=rs.getString(2)%></td>
<td><%=rs.getString(3)%></td>
<td><u><font color="#FF0000">    </font></u></td>
</tr>
<%
i = i + 1;
}
%>
</table>
</td>


<td>
<table border="1" width="100%">
<%
while( i < ( ((recordNum + 1)/4)*2 ) && rs.next() )
{
%>
<tr>
<td><% out.println( (i++) + 1 ); %></td>
<td><%=rs.getString(2)%></td>
<td><%=rs.getString(3)%></td>
<td><u><font color="#FF0000">    </font></u></td>
</tr>
<%
i = i + 1;
}
%>
</table>
</td>
</tr>
</table>
</td>

<td>
<table border="1" width="100%">
<tr>
<td>
<table border="1" width="100%">
<%

while( i < ( ((recordNum + 1)/4)*3 ) && rs.next() )
{
%>
<tr>
<td><% out.println(i+1);%></td>
<td><%=rs.getString(2)%></td>
<td><%=rs.getString(3)%></td>
<td><u><font color="#FF0000">    </font></u></td>
</tr>
<%
i = i + 1;
}
%>
</table>
</td>

<td>
<table border="1" width="100%">
<%
while(rs.next())
{
%>
<tr>
<td><% out.println( (i++) + 1 ); %></td>
<td><%=rs.getString(2)%></td>
<td><%=rs.getString(3)%></td>
<td><u><font color="#FF0000">    </font></u></td>
</tr>
<%
}
%>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
<%
}
rs.close();
%>
</tr>
</table>
tomuno 2006-02-09
  • 打赏
  • 举报
回复
you want what it is,and it is
tom00007 2006-02-09
  • 打赏
  • 举报
回复
To: tomuno(tomuno)
那个7是根据总的记录集分成4列得出来的呀
tomuno 2006-02-09
  • 打赏
  • 举报
回复
<%int i=1;%>
<%
for(int c=1;c<=7;c++){
out.println(i+" "+(i+7)+" "+(i+14)+" "+(i+21));
i++;
}
%>
tom00007 2006-02-09
  • 打赏
  • 举报
回复
我的方法不是太好,不知道大家有做过的吗,贴出来看看
Little_qd 2006-02-09
  • 打赏
  • 举报
回复
感觉多了一层循环而已

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧