php里一个关于循环的问题

chsing123 2010-05-14 02:59:22
比如我想在首页显示12个产品,分为3行显示,每行显示4个。可是效果显示的12个产品多到一行去了,请帮忙看看啊

<?php
$sql="select id,pic,title from product where pic<>'' order by id desc limit 0,12";
$result=mysql_query($sql);
if(!$num=mysql_num_rows($result))
{
echo( "暂无产品数据!");
}
else
?>
<Br>
<table width="140" height="135" cellspacing="0" cellpadding="15">
<tr>
<?php
{
while($rs=mysql_fetch_array($result))
{
?>
<td><div align="center"><a href="product.php?id=<?=$rs["id"]?>" title="<?=$rs["title"]?>" ><img src="<?=$rs["pic"]?>" alt="<?=$rs["title"]?>" width="140" height="135" border="0" /></a><Br><Br>
<a href="product.php?id=<?=$rs["id"]?>" title="<?=$rs["title"]?>">
<?=msubstr($rs["title"],0,16)?>
</a> </div></td>
<?php
}
}
?>
...全文
87 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhuhuquan29 2010-05-14
  • 打赏
  • 举报
回复
table 看着头晕
fyxw 2010-05-14
  • 打赏
  • 举报
回复
<?php
$sql="select id,pic,title from product where pic<>'' order by id desc limit 0,12";
$result=mysql_query($sql);
if(!$num=mysql_num_rows($result))
{
echo( "暂无产品数据!");
}
else
?>
<Br>
<table width="140" height="135" cellspacing="0" cellpadding="15">
<tr>
<?php
{
$i=0;
while($rs=mysql_fetch_array($result))
{
if ($i%4==0)
echo "<tr>";
$i++;
?>
<td><div align="center"><a href="product.php?id=<?=$rs["id"]?>" title="<?=$rs["title"]?>" ><img src="<?=$rs["pic"]?>" alt="<?=$rs["title"]?>" width="140" height="135" border="0" /></a><Br><Br>
<a href="product.php?id=<?=$rs["id"]?>" title="<?=$rs["title"]?>">
<?=msubstr($rs["title"],0,16)?>
</a> </div></td>
<?php
}
}
?>
by_baby 2010-05-14
  • 打赏
  • 举报
回复
<?php
$sql="select id,pic,title from product where pic<>'' order by id desc limit 0,12";
$result=mysql_query($sql);
if(!$num=mysql_num_rows($result))
{
echo( "暂无产品数据!");
}
else
?>
<Br>
<table width="140" height="135" cellspacing="0" cellpadding="15">
<tr>
<?php
{
while($rs=mysql_fetch_array($result))
{
?>
<tr><td><div align="center"><a href="product.php?id=<?=$rs["id"]?>" title="<?=$rs["title"]?>" ><img src="<?=$rs["pic"]?>" alt="<?=$rs["title"]?>" width="140" height="135" border="0" /></a><Br><Br>
<a href="product.php?id=<?=$rs["id"]?>" title="<?=$rs["title"]?>">
<?=msubstr($rs["title"],0,16)?>
</a> </div></td></tr>
<?php
}
}
?>

<tr>才是列,<td>只是行
土地公公 2010-05-14
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 chsing123 的回复:]
引用 1 楼 bing19870624 的回复:
把tr放在循环中 加个计数器 每四个增加一对tr

能帮忙在上面代码修改一下,然后贴出来吗
[/Quote]

都给你思路了,你不动手去做,下次还一样白。
chsing123 2010-05-14
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 bing19870624 的回复:]
把tr放在循环中 加个计数器 每四个增加一对tr
[/Quote]
能帮忙在上面代码修改一下,然后贴出来吗
餅餅 2010-05-14
  • 打赏
  • 举报
回复
把tr放在循环中 加个计数器 每四个增加一对tr

21,887

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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