宜用表格控制对齐
基本框架
<?php
$mode = 3; // 每行列数
echo "<table>";
$n = -1;
while(...) { // 读取数据
switch($n) {
case 0:
echo "</tr>";
case -1:
echo "<tr>\n";
$n = 0;
}
echo "<td>"."相关内容"."</td>\n"; // 输出内容
$n = ($n+1)%$mode;
}
for(;$n<$mode;$n++) // 补齐尾部
echo "<td></td>";
echo "</tr></table>";?>
基本框架
<?php
$mode = 3; // 每行列数
echo "<table>";
$n = -1;
while(...) { // 读取数据
switch($n) {
case 0:
echo "</tr>";
case -1:
echo "<tr>\n";
$n = 0;
}
echo "<td>"."相关内容"."</td>\n"; // 输出内容
$n = ($n+1)%$mode;
}
for(;$n<$mode;$n++) // 补齐尾部
echo "<td></td>";
echo "</tr></table>";?>
$pagesize = 9;
/********设置一行显示几个图片*******/
$Tmpcount=3;
?>
<table width="535" border="0" cellspacing="0" cellpadding="0" height="34">
<tr>
<td colspan="3">
<?echo $img_link;?>
</td>
</tr>
<tr>
<?
$sql = "SELECT * FROM product_info where class_id='$row[id]' ORDER BY id ";
$pro_res = $mysql_server->send_result_sql($sql,0);
$tempnum=1;
while($pro_row = @mysql_fetch_array($pro_res))
{
$tempnum1=($dp->rowscount)%$Tmpcount;
$tmpimg="../../upload/".$pro_row[image_s];
if(@file_exists($tmpimg) && !(@is_dir($tmpimg)))
$img_link="<a href=\"javascript:MM_openBrWindow('../pop/product.php?id=$pro_row[id]','product','scrollbars=yes,top=0,left=0,width=555,height=450')\"><img src=\"$tmpimg\" border=\"0\"></a>";
else
$img_link="<a href=\"javascript:MM_openBrWindow('../pop/product.php?id=$pro_row[id]','product','scrollbars=yes,top=0,left=0,width=555,height=450')\"><img src=\"../images/haiao_1.gif\" border=\"0\"></a>";
?>
<td width="186" align="center" height="14">
<div align="center"></div>
<div align="center">
<table width="186" border="0">
<tr>
<td>
<div align="center"><?echo $img_link;?></div>
</td>
</tr>
<tr>
<td height="16">
<div align="center"><a href="javascript:MM_openBrWindow('../pop/product.php?id=<?echo $pro_row[id];?>','product','scrollbars=yes,top=0,left=0,width=555,height=450')"><?echo $pro_row[hwlx];?></a></div>
</td>
</tr>
</table>
</div>
</td>
<?
if(($tempnum%$Tmpcount)==0)
{
?>
</tr>
<?
}
if($dp->rowscount==1)
{
echo"<td height=\"120\" width=\"148\"></td><td height=\"120\" width=\"148\"></td>";
}
$tempnum++;
}
if($dp->rowscount==2)
{
echo"<td height=\"120\" width=\"148\"></td>";
}
?>
</table>
<?
$sqlstr2=$sqlstr_tt."limit $offset,$pagesize";
echo "<table width=580 border=0 cellspacing=0 cellpadding=2 align=center>";
$myrs_qq=mysql_query($sqlstr2,$myconn);
$ii=$offset+1;
$col=4;//每行列数
while ($rec=mysql_fetch_array($myrs_qq))
{
echo "<tr>";
$n=1;
while($n<=$col) {//输出内容
echo "<td>";
echo "<table width=100% border=0 cellspacing=0 cellpadding=2>
<tr>
<td><a href=productlist.php?id=".$rec["id"]." target=_blank><img src=../../../image/onhand/".$rec["s_image"]." width=100 height=100 border=1></a></td>
</tr>
<tr>
<td>编号:".$rec["s_name"]."</td></tr>
</table>";//显示完毕
echo "</td>";
$n+=1;
$ii=$ii+1;
}
echo "</tr>";
echo "<tr>
<td colspan=4 height=4 background=../../../image/left_line.gif></td>
</tr>";
}
echo "</table>";
mysql_free_result($myrs_qq);
?>
那么你可以这样做$sqlstr2 = $sqlstr_tt."limit $offset,$pagesize";
$myrs_qq = mysql_query($sqlstr2,$myconn);
$ii = $offset + 1;
$col = 4;//设置列数
echo "<table width=580 border=0 cellspacing=0 cellpadding=2 align=center>
<tr>";
$s = 1; //设置一计数器
while(得到数据的那一条语句){
echo"<td>显示一条</td>";
if ($s/$col == ceil($s/$col)){//加上一个tr换行,也可以用round函数,不过不能设$col为0
echo"</tr><tr>";
}
$s++;
}
echo"</tr></table>"}
那么你可以这样做$sqlstr2 = $sqlstr_tt."limit $offset,$pagesize";
$myrs_qq = mysql_query($sqlstr2,$myconn);
$ii = $offset + 1;
$col = 4;//设置列数
echo "<table width=580 border=0 cellspacing=0 cellpadding=2 align=center>
<tr>";
$s = 1; //设置一计数器
while(得到数据的那一条语句){
echo"<td>显示一条</td>";
if ($s/$col == ceil($s/$col)){//加上一个tr换行,也可以用round函数,不过不能设$col为0
echo"</tr><tr>";
}
$s++;
}
echo"</tr></table>"}
<?
$sqlstr2=$sqlstr_tt."limit $offset,$pagesize";
echo "<table width=580 border=0 cellspacing=0 cellpadding=2 align=center>";
$myrs_qq=mysql_query($sqlstr2,$myconn);
$ii=$offset+1;
$col=4;//每行列数
while ($rec=mysql_fetch_array($myrs_qq))
{
echo "<tr>";
$n=1;
while($n<=$col) {//输出内容
echo "<td>";
echo "<table width=100% border=0 cellspacing=0 cellpadding=2>
<tr>
<td><a href=productlist.php?id=".$rec["id"]." target=_blank><img src=../../../image/onhand/".$rec["s_image"]." width=100 height=100 border=1></a></td>
</tr>
<tr>
<td>编号:".$rec["s_name"]."</td></tr>
</table>";//显示完毕
echo "</td>";$rec=mysql_fetch_array($myrs_qq); //按你的在这里加了一句$n+=1;
$ii=$ii+1;
}
echo "</tr>";
echo "<tr>
<td colspan=4 height=4 background=../../../image/left_line.gif></td>
</tr>";
}
echo "</table>";
mysql_free_result($myrs_qq);
?>
如果没有的话,就不用了,如果有的话,就
在显示这个之前,加上
if (!isset($rec["s_name"]) || trim($rec["s_name"]) == ''){
continue;
}
但是你得确定一条就是你的这个字段每一个记录都得有内容,不能为空,不然可不显示呵.
所以按我上面给出的框架,则其中:
"相关内容" 用
$s = "<table width=100% border=0 cellspacing=0 cellpadding=2>
<tr>
<td><a href=productlist.php?id=".$rec["id"]." target=_blank><img src=../../../image/onhand/".$rec["s_image"]." width=100 height=100 border=1></a></td>
</tr>
<tr>
<td>编号:".$rec["s_name"]."</td></tr>
</table>";
替换while(...) { // 读取数据
用
while ($rec=mysql_fetch_array($myrs_qq)) {
替换就行了
做个提示你具体写一下吧,
可以在哪一个
$n+=1;
$ii=$ii+1;
再加一句判断,如果超出条件就不执行$rec=mysql_fetch_array($myrs_qq);,没有就执行,大体是这样子.
$n+=1;
$ii=$ii+1;
if ($n<=$col){
$rec=mysql_fetch_array($myrs_qq); //按你的在这里加了一句
}
while($n<=$col) {//输出内容也就是说你是要输出$col个相同内容
请把思路理清楚
while($n<=$col) {//输出内容也就是说你是要输出$col个相同内容
请把思路理清楚
$myrs_qq=mysql_query($sqlstr2,$myconn);
$ii=$offset+1;
$col=4;//每行列数
while ($rec=mysql_fetch_array($myrs_qq))
{
echo "<tr>";
$n=1;
while($n<=$col) {//输出内容
echo "<td>";
echo "<table width=100% border=0 cellspacing=0 cellpadding=2>
<tr>
<td><a href=productlist.php?id=".$rec["id"]." target=_blank><img src=../../../image/onhand/".$rec["s_image"]." width=100 height=100 border=1></a></td>
</tr>
<tr>
<td>编号:".$rec["s_name"]."</td></tr>
<tr>
</table>";//显示完毕
echo "</td>";
$rec=mysql_fetch_array($myrs_qq); //按你的在这里加了一句$n=$n+1;
$ii=$ii+1;
}
echo "</tr>";
echo "<tr>
<td colspan=4 height=4 background=../../../image/left_line.gif></td>
</tr>";
}
echo "</table>";
mysql_free_result($myrs_qq);
?>
error_reporting (E_ERROR | E_WARNING | E_PARSE);
set_magic_quotes_runtime(0);
$hostname_conn = "localhost";
$database_conn = "mydb";
$username_conn = "root";
$password_conn = "";
$conn = mysql_connect($hostname_conn, $username_conn, $password_conn) or die("连接MySQL失败!");
$query_sort_page = "select * from product";
mysql_select_db($database_conn);
$result = mysql_query($query_sort_page, $conn);
$this_Rst = mysql_fetch_assoc($result);
$total_row = mysql_num_rows($result);
$loop_time = ceil($total_row/2);
echo $loop_time;
mysql_free_result($result);
?>
<table>
<?
$i = 0;
do
{
?>
<tr bgcolor="#CCCCCC">
<?
$offset = $i * 2;
$sql = "select * from product limit ".$offset.",2";
$result = mysql_query($sql);
$this_Rst = mysql_fetch_assoc($result);
do
{
?>
<td><?echo $this_Rst['sort_id'];?></td>
<td><?echo $this_Rst['item_number'];?></td>
<?
}while($this_Rst = mysql_fetch_assoc($result));
mysql_free_result($result);
?>
</tr>
<?
$i++;
}while($i < $loop_time);
?>
</table>
怎么php实现这种功能要那么的麻烦,还是我们不会用,以上我自己写的。烦啊。要不断查数据库,但没有你说的现像出现
$product_Rst = mysql_fetch_assoc($result_product);
$total_rows = mysql_num_rows($result_product);
$col = 2;//设置列数
$loop = ceil($total_rows/2);//计算行数
$j = 0;
$l = 0;
do
{
if (($i%2) != 0)//取余
{
$leftResult[$j] = $product_Rst;//将当前记录集保存到数组里(单数)
$j++;
}
else
{
$rightResult[$l] = $product_Rst;//将当前记录集保存到数组里(双数)
$l++;
}
} while($product_Rst = mysql_fetch_assoc($result_product));
//两列多行
$k = 0;
while ($k < $loop)//实现行的显示
{
echo "左:".$leftResult[$k]['数据库里的列名'] . "右:". $rightReuselt[$k]['数据库里的列名']."<br>\n";//读取数组,实现列的显示
//例如: 表里有id,name,addtime列,那样下面可以
//echo "左:".$leftResult[$k]['id'] . "右:". $rightReuselt[$k]['name']."<br>\n";
$k++;
}
//上面是我在多行两列的实现方法,自己可以看懂~~
<?
echo "<table width=580 border=0 cellspacing=0 cellpadding=2 align=center>";
// $myrs_qq=mysql_query($sqlstr2,$myconn);
for($i=0;$i<19;$i++) // 调试用,构造模拟数据
$ar[] = array(s_name=>$i); $ii=$offset+1;
$col=4;//每行列数
$n = 1;//列计数器放在这里
// while ($rec=mysql_fetch_array($myrs_qq))
foreach($ar as $rec) // 调试用,读取模拟数据
{
if($n==1) // 一行开始
echo "<tr>";
// $n=1; // 这两句删去
// while($n<=$col) {//输出内容
echo "<td>";
echo "<table width=100% border=0 cellspacing=0 cellpadding=2>
<tr>
<td><a href=productlist.php?id=".$rec["id"]." target=_blank><img src=../../../image/onhand/".$rec["s_image"]." width=100 height=100 border=1></a></td>
</tr>
<tr>
<td>编号:".$rec["s_name"]."</td></tr>
<tr>
</table>";//显示完毕
echo "</td>";
//$rec=mysql_fetch_array($myrs_qq); //这句不该用,他将多读一个记录 $n=$n+1;
$ii=$ii+1;
//}
if($n > $col) { // 列数判断
echo "</tr>";
$n = 1;
echo "<tr>
<td colspan=4 height=4 background=../../../image/left_line.gif></td>
</tr>";
}
}
if($n>1){ // 补齐空缺的位置
while($n++<=$col) {
echo "<td><table width=100% border=0 cellspacing=0 cellpadding=2>
<tr>
<td><img src=../../../image/onhand/blank.gif width=100 height=100 border=1></td>
</tr>
<tr>
<td>编号:无</td></tr>
<tr>
</table></td>";
}
echo "<tr>
<td colspan=4 height=4 background=../../../image/left_line.gif></td>
</tr>";
}echo "</table>";
// mysql_free_result($myrs_qq); // 调试结束后,取消注释
?>