变数据控制为结构控制
echo '<table>';
for($row=0; $row<行数; $row++) {
  echo '<tr>';
  for($col=0; $col<列数; $col++) {
    echo '<td>';
    list($sortid3, $forum3) = each($forums);
    echo $forum3;
    echo '</td>';
  }
  echo '</tr>';
}
echo '</table>';

解决方案 »

  1.   

    楼上这个大哥的思路是很清楚,但是我感觉好像不是十分是我想要表达的形式,我再描述一下我的问题实际我是使用的模板,在程序a.php(其中使用a.htm模板文件)中调用getsortlist($query)函数,这个函数如下:
    function getsortlist($query='') 

            global $db,$table_sorts; 
            $forumlist = array(); 
            $forums = array(); 
            if (empty($sqlstr)) $query = $db->query("SELECT sortid, class, sortname, supsortid, largesortid FROM $table_sorts where is_display_sort='yes' ORDER BY displayorder"); 
            while($forum = $db->fetch_array($query)) { 
                            $forum['sortname'] = strip_tags($forum['sortname']); 
                            $forums[$forum['sortid']] = $forum; 
            }         foreach($forums as $sortid1 => $forum1) { 
                    if($forum1['class'] == 'group') { 
                            $forumlist[] =$forum1; 
                            foreach($forums as $sortid2 => $forum2) { 
                                    if($forum2['supsortid'] == $sortid1 && $forum2['class'] == 'sort') { 
    //                                        $forumlist[] =$forum2; 
                                            $sub_line=""; 
    $i=0;
                                            foreach($forums as $sortid3 => $forum3) { 
    if($forum3['supsortid'] == $sortid2 && $forum3['class'] == 'sub' && (!$forum3['largesortid']|| ($forum3['largesortid']))) { 
    ++$i;if($i%1==0){$crlf="<br>";}else{$crlf="";}
                                                            $sub_line .="<font color=ff0000>·</font><a class=sort href='productdisplay.php?sortid=$forum3[sortid]' target=_blank>".$forum3['sortname']."</a>&nbsp;".$crlf; 
                                                    } 
                                            } 
                                            $forum2['subline']=$sub_line; 
                                            $forumlist[]=$forum2; 
                                    } 
                            } 
                    } 
            }         return $forumlist; 
    }a.htm中相关的模板代码如下:
    <table border="0" cellspacing="{BORDERWIDTH}" cellpadding="{TABLESPACE}" width="100%">
    <tr><td class=header height=1></td></tr>
    <!--{loop $tablelist $table}-->
      <!--{if ($table['class'] == 'group')}-->
    <tr>
    <td  colspan="6">
    <table border="0" cellspacing="0" cellpadding="0" width="100%" align=center height="31">
    <tr class=header>
    <td align=center>
    <p align="center"><font face="幼圆" size="4">$table['sortname']</font></td>
    </tr>
    </table>
    </td></tr>
       <!--{else}-->
    <tr>
    <td bgcolor="{ALTBG1}" height="24"  colspan="7"><p align="center"><b><font face="幼圆"><span class="style1"><a href="productdisplay.php?sortid=$table['sortid']">$table['sortname']</a></span></font></b>
     </td>
    </tr>
    <tr>
    $table['subline']
    </tr>
       <!--{/if}-->
    <!--{/loop}-->
    <tr><td class=header height=1></td></tr>
    </table></td></table></td>
    </tr>
    </table>
    </td>
    </tr>
    </table>我现在的问题就是当某一类的数据不到一行时我怎么去打印没有数据的这些表格,使每行都是整齐的表格,当然最后一行可能是有部分的表格有数据,部分的没有数据
      

  2.   

    在  <!--{/loop}--> 后插入一个变量{a} 通过php文件判断当记录数不能填满表格时,把a当成tr输出,少几行输出几个tr
      

  3.   

    使用模板就更简单了
    你只需补齐数组$tablelist 就可以了
    设$col为需要的列数,则需要补上的的数组元素为 $col-(count($tablelist)%$col)你这是什么模板引擎?模板怎么那么难看
      

  4.   

    呵呵,用discuz的模板改的,
    老大可以说的详细一点吗?小弟在此谢过了!!
      

  5.   

    我在这个函数的
     $forumlist[]=$forum2; 语句
    前面加上了这样的输出循环,但是好像不对,每次都是只加了一个,也就是说只在每一类后面加了一个空表格的输出,请问我的语句有什么错误吗?我单独输出$left,$sub_line1,$sub_line.$sub_line1;这些变量都是对的,那为什么我到页面的显示还是不对呢
    $left=6-($i%$col);
    for($aa=0;$aa<$left;$aa++){
    $sub_line1="<div><td  background=\"./images/default/pbg.gif\" width=\"16%\"><table width=\"100%\"><tr><td><center>aaaa</center></td></tr></table></td></div>"; 
    }
    $forum2['subline']=$sub_line.$sub_line1;