代码如下:
<?
$ClassSql=mysql_query("select * from $productclass where id2='0' order by taxis");
while($classrow=mysql_fetch_object($ClassSql))
{
echo htmlspecialchars($classrow->classname);
echo"<br>";//换行
//第二个SQL语句
$break=1;
$ClassSql2=mysql_query("select * from $productclass where id2='$classrow->id' order by taxis");
echo"<font color=#ffffff>|</font> ";while($classrow2=mysql_fetch_object($ClassSql2))
{
if($break%3==1)
{
$filebit .="<tr>";
}
$filebit .= "<td>$classrow2->classname</td>"; //$classrow2->classname是产品名
if($break%3==0)
{
$filebit .= "</tr>";
}
$break++;
$showbit = $filebit;
echo"<table width=100% border=0 cellspacing=0 cellpadding=0>$showbit</table>";
}
echo"<br><br>";//这两个BR是用于第一个SQL语句换行
}
?>
<?
$ClassSql=mysql_query("select * from $productclass where id2='0' order by taxis");
while($classrow=mysql_fetch_object($ClassSql))
{
echo htmlspecialchars($classrow->classname);
echo"<br>";//换行
//第二个SQL语句
$break=1;
$ClassSql2=mysql_query("select * from $productclass where id2='$classrow->id' order by taxis");
echo"<font color=#ffffff>|</font> ";while($classrow2=mysql_fetch_object($ClassSql2))
{
if($break%3==1)
{
$filebit .="<tr>";
}
$filebit .= "<td>$classrow2->classname</td>"; //$classrow2->classname是产品名
if($break%3==0)
{
$filebit .= "</tr>";
}
$break++;
$showbit = $filebit;
echo"<table width=100% border=0 cellspacing=0 cellpadding=0>$showbit</table>";
}
echo"<br><br>";//这两个BR是用于第一个SQL语句换行
}
?>
解决方案 »
- 乱码问题,但在MyAdmin中能显示汉字
- 关于字符截取
- 这个递归函数错在哪里了
- 用mssql_fetch_object显示表内记录只显示一条
- 空间、域名申请
- 市面上比较流行的在线编辑器比如,htmlarea等编辑器都具有html输入功能,这会不会有漏洞?
- 这段代码是哪出错了?菜菜的问一下,小弟刚学,有分奖励!
- 如何删除数组中重复的元素
- 求救:含中文的网址:http://10.28.30.17/总结.doc 不能在ie直接使用,在php中如何进行转换,类似asp中 <%server.urlencode(url)%>的功
- 0和空的问题,我对一个为0的变量进行empty判断,php老是认为是空怎么办?
- 找一个留言板源代码!
- 我消除所有的前后全半角空格以后有些情况会出乱码·请帮忙、谢谢
$break++;
$showbit = $filebit;
echo"<table width=100% border=0 cellspacing=0 cellpadding=0>$showbit</table>";
}
echo"<br><br>";//这两个BR是用于第一个SQL语句换行改为
$break++;
$showbit = $filebit;
}
echo"<table width=100% border=0 cellspacing=0 cellpadding=0>$showbit</table>";
echo"<br><br>";//这两个BR是用于第一个SQL语句换行
http://www.isharp.com.cn/a2.php
<?
for($j=1;$j<4;$j++)
{
echo"<font color=red>$j</font> <br>";
$break=1;
for($i=1;$i<11;$i++)
{
if($break%3==1)
{
$filebit .="<tr>";
}
$filebit .= "<td>$i</td>";
if($break%3==0)
{
$filebit .= "</tr>";
}
$break++;
$showbit = $filebit;
}
echo"<table width=100% border=1 cellspacing=0 cellpadding=0>$showbit</table>";
}
?>
表1 (type字段)= A,B,C,D
表2 (type字段)= A{1,2,3,4,5},B{q,e,t,c,x,w},...
# 1,2,3,4,5属于A类型的,q,e,t...属于B类型的
$res1 <-- select * from 1 group by type;{
输出父类型($res1[type]);
$res2 <-- select * from 2 where type=$res1[type];{
输出子类型($res2[type]);
}
}最终结果很明显了:
==============
A ==> 1,2,3,4,5
B ==> q,w,e...
==============
(PHP 3, PHP 4 )implode -- Join array elements with a string
Description
string implode ( string glue, array pieces)
Returns a string containing a string representation of all the array elements in the same order, with the glue string between each element. Example 1. implode() example<?php$array = array('lastname', 'email', 'phone');
$comma_separated = implode(",", $array);print $comma_separated; // lastname,email,phone?>
Note: implode() can, for historical reasons, accept its parameters in either order. For consistency with explode(), however, it may be less confusing to use the documented order of arguments. Note: As of PHP 4.3.0, the glue parameter of implode() is optional and defaults to the empty string(''). This is not the preferred usage of implode(). We recommend to always use two parameters for compatibility with older versions. See also explode(), and split().
implode() 怎样实现我上面的代码?NetSniffer(扑克):
不明白你的呢!我笨的我现要主要是想第二个循环能每行只显示三个。
让你贴出代码,你给出了while循环的
给了你答复,却又给个for循环的说不行。你究竟要“干什么”
抱歉,我自作聪明,我是想以for代替while来试下你的方法行不行。:)请别介意,我现在里暂时不能读数据库所以用for代替一下。谢谢你的解答,我试一下,:)
<tr><td></td><td></td><td></td></tr><tr><td></td><td></td><td></td></tr>...
我试过你的方法了,的确是不行,这样做第二循环会将记录加起来的。
:)
$arrayX=new array(1,2,3,4,5,6,7,8)
$output="";if ($length=count($arrayX)%$cols)
for ($i=$length;$i<$cols;++$i)
$arrayX[]=" ";
$length=count($arrayX);
for ($i=0;$i<$length;$i+=$cols)
$output.="<tr><td>".implode("</td><td>", array_slice($arrayX,$i,$cols))."</td></tr>";核心代码就是这些,将就着看,因为我现在不在开发电脑旁边,无法调试
只用到了implode,array_slice
$cols=5;
$arrayX=array(1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,j,i,k,l,m);
$output="";if ($length=count($arrayX)%$cols)
for ($i=$length;$i<$cols;++$i)
$arrayX[]=" ";
$length=count($arrayX);
for ($i=0;$i<$length;$i+=$cols)
$output.="<tr><td>".implode("</td><td>", array_slice($arrayX,$i,$cols))."</td></tr>";
echo "<table border=1>".$output."</table>";
?>
:(
这是完整的程序,由于不知道你的taxis字段是干什么的,所以略去排序
<?php
$conn = mysql_connect();
mysql_select_db("test");$productclass = "productclass";
$ClassSql=mysql_query("select * from $productclass where id2='0'");
while($classrow=mysql_fetch_object($ClassSql)) {
echo htmlspecialchars($classrow->classname);
echo"<br>";//换行
//第二个SQL语句
$break=1;
$ClassSql2=mysql_query("select * from $productclass where id2='$classrow->id'");
echo"<font color=#ffffff>|</font> ";
$filebit = "";
while($classrow2=mysql_fetch_object($ClassSql2)) {
if($break%3==1) {
$filebit .="<tr>";
}
$filebit .= "<td>$classrow2->classname</td>"; //$classrow2->classname是产品名
if($break%3==0) {
$filebit .= "</tr>";
}
$break++;
}
$showbit = $filebit;
echo "<table width=100% border=0 cellspacing=0 cellpadding=0>$showbit</table>";
echo"<br><br>";//这两个BR是用于第一个SQL语句换行
}
?>
测试数据:
#
# 数据表的结构 `productclass`
#CREATE TABLE productclass (
id tinyint(4) NOT NULL default '0',
id2 tinyint(4) NOT NULL default '0',
classname varchar(100) NOT NULL default ''
) TYPE=MyISAM;#
# 导出下面的数据库内容 `productclass`
#INSERT INTO productclass VALUES (1, 0, '电脑配件');
INSERT INTO productclass VALUES (2, 1, 'CPU');
INSERT INTO productclass VALUES (3, 1, '硬盘');
INSERT INTO productclass VALUES (4, 1, '电源');
INSERT INTO productclass VALUES (5, 1, '机箱');
INSERT INTO productclass VALUES (6, 1, '内存');
INSERT INTO productclass VALUES (7, 1, 'U盘');
INSERT INTO productclass VALUES (8, 1, '手写板');
INSERT INTO productclass VALUES (9, 1, 'COMPAQ');
INSERT INTO productclass VALUES (10, 1, '显卡');
INSERT INTO productclass VALUES (11, 1, '全心');
INSERT INTO productclass VALUES (12, 1, '数码相机');
INSERT INTO productclass VALUES (13, 1, '主板');
INSERT INTO productclass VALUES (14, 1, '光驱');
INSERT INTO productclass VALUES (15, 1, '声卡');
INSERT INTO productclass VALUES (16, 1, '网卡');
INSERT INTO productclass VALUES (17, 1, '键盘');
INSERT INTO productclass VALUES (18, 1, '鼠标');
INSERT INTO productclass VALUES (19, 0, '显示器');
INSERT INTO productclass VALUES (20, 19, '二手原装');
INSERT INTO productclass VALUES (21, 19, '原版翻新');
INSERT INTO productclass VALUES (22, 0, '数码产品');
INSERT INTO productclass VALUES (23, 22, 'MP3');
INSERT INTO productclass VALUES (24, 22, 'U盘');
INSERT INTO productclass VALUES (25, 22, '数码相机摄相头');输出效果:
电脑配件
| CPU 硬盘 电源
机箱 内存 U盘
手写板 COMPAQ 显卡
全心 数码相机 主板
光驱 声卡 网卡
键盘 鼠标
显示器
| 二手原装 原版翻新
数码产品
| MP3 U盘 数码相机摄相头 html代码:
电脑配件<br><font color=#ffffff>|</font> <table width=100% border=0 cellspacing=0 cellpadding=0><tr><td>CPU</td><td>硬盘</td><td>电源</td></tr><tr><td>机箱</td><td>内存</td><td>U盘</td></tr><tr><td>手写板</td><td>COMPAQ</td><td>显卡</td></tr><tr><td>全心</td><td>数码相机</td><td>主板</td></tr><tr><td>光驱</td><td>声卡</td><td>网卡</td></tr><tr><td>键盘</td><td>鼠标</td></table><br><br>显示器<br><font color=#ffffff>|</font> <table width=100% border=0 cellspacing=0 cellpadding=0><tr><td>二手原装</td><td>原版翻新</td></table><br><br>数码产品<br><font color=#ffffff>|</font> <table width=100% border=0 cellspacing=0 cellpadding=0><tr><td>MP3</td><td>U盘</td><td>数码相机摄相头</td></tr></table><br><br>
也谢谢各位的热心帮助,来到这里另我这个老的所谓网络工作者学习了不少新知识。