<?
$m=2;//设置每行新闻数量
$array=array("new1","new2","new3","new4","new5","new6","new7","new8","new9","new10","new11");
echo "<TABLE border style='border-collapse:collapse;'>";
for($n=0;$n<count($array);$n=$n+$m){
echo "<TR>";
for($k=0;$k<$m;$k++){
echo "<td>".$array[$n+$k]."</td>"; }
echo "<TR>";
}
echo "</TABLE>";
?>
$m=2;//设置每行新闻数量
$array=array("new1","new2","new3","new4","new5","new6","new7","new8","new9","new10","new11");
echo "<TABLE border style='border-collapse:collapse;'>";
for($n=0;$n<count($array);$n=$n+$m){
echo "<TR>";
for($k=0;$k<$m;$k++){
echo "<td>".$array[$n+$k]."</td>"; }
echo "<TR>";
}
echo "</TABLE>";
?>
<?
$i=3;//设置每行新闻数量
$array=array("new1","new2","new3","new4","new5","new6","new7","new8","new9","new10","new11");
$m=intval(count($array)/$i)+1;
$k=0;
for($n=0;$n<count($array);$n=$n+$m){
${"result".$k}=array_slice($array,$n,$n+$m);
$k++;
}
echo "<TABLE border style='border-collapse:collapse;'>";
for($kk=0;$kk<$m;$kk++){
echo "<TR>";
for($ii=0;$ii<$i;$ii++){
echo "<td>".${"result".$ii}[$kk]."</td>";
}
echo "</TR>";
}
echo "</TABLE>";
?>
for($ii=0;$ii<$i;$ii++){
echo "<td>".${"result".$ii}[$kk]."</td>";
}
这句循环改为:
for($ii=0;$ii<$i;$ii++){
if(${"result".$ii}[$kk])
echo "<td>".${"result".$ii}[$kk]."</td>";
}
更恰当
echo "<table width=100% border=0 cellspacing=0 cellpadding=4>\n";
$col=0;
$cols=2;//每行2列
$query="select * from table";
$db->query($query);
while($db->next_record())
{
$col=$col+1;
if ($col%$cols==1)echo "<tr>\n";
echo "<td width=25%>";
echo $db->f("test");
echo "</td>\n";
if ($col%$cols==0) echo "</tr>\n";
}
if($col%$cols!=0)
{
for($i=1;$i<=$cols-$col%$cols;$i++)
{
echo "<td width=25%> </td>\n";
}
}
if($i>1) echo "</tr>\n";
echo "</table>\n";
?>为什么不试试我这段代码呢??随便几列都可以。
仅做参考!
$array[]=$newsTitle;
}
然后用上面的方法处理$array
你程序中的$db是什么变量?
那位兄弟的排序不符合你的要求,他实现的结果和我第一个帖一样,就是:
新闻一 新闻二
新闻三 新闻四
新闻五 新闻六
新闻七 新闻八他程序里面$db是一个类的实例变量,他的类没有贴出来而已。不用类应该是:
echo "<table width=100% border=0 cellspacing=0 cellpadding=4>\n";
$col=0;
$cols=2;//每行2列
$query="select * from table";
$db_query=mysql_query($query);
while(list($newsTitle)=mysql_fetch_row($db_query))
{
$col=$col+1;
if ($col%$cols==1)echo "<tr>\n";
echo "<td width=25%>";
echo $newsTitle;
echo "</td>\n";
if ($col%$cols==0) echo "</tr>\n";
}
if($col%$cols!=0)
{
for($i=1;$i<=$cols-$col%$cols;$i++)
{
echo "<td width=25%> </td>\n";
}
}
if($i>1) echo "</tr>\n";
echo "</table>\n";
改为
$query="select id,newsTitle from table where...... order by .......";while(list($newsTitle)=mysql_fetch_row($db_query))
改为
while(list($id,$newsTitle)=mysql_fetch_row($db_query))
程序如下:
<?
$sql="select newstitle from table where …… order by …… ";
$result=mysql_query($sql);
while(list($newstitle)=mysql_fetch_row($title))
{$array[]=$name;}
$i=5;//设置每行新闻数量
$m=intval(count($array)/$i)+1;
$k=0;
for($n=0;$n<$i;$n=$n+$m){
${"result".$k}=array_slice($array,$n,$n+$m);
$k++;
}
echo "<TABLE border=1 style='border-collapse:collapse;'>";
for($kk=0;$kk<$m;$kk++){
echo "<TR>";
for($ii=0;$ii<$i;$ii++){
if(${"result".$ii}[$kk])
echo "<td width=20>";
echo "<div align=center></div>";
echo "</td>";
echo "<td width=245>";
echo "<img height=11 hspace=4 src='images/arrow_01.gif' tppabs='images/arrow_01.gif' width=11><a href='_daoli/index/Community/Medical.php'>".${"result".$ii}[$kk]."</a></td>";
echo "<td width=20> </td>"; }
echo "</TR>";
}
echo "</TABLE>";?>请问错在那里?
$result=mysql_query($sql);
while(list($newstitle)=mysql_fetch_row($title))
{$array[]=$name;}
改为:
$sql="select id,newstitle from table where …… order by …… ";
$result=mysql_query($sql);
while(list($id,$newstitle)=mysql_fetch_row($result))
{$array[]=$newstitle;}
所以应该
while(list($id,$newstitle)=mysql_fetch_row($result))
{$array[]=$newstitle;}
改为
while($tmpArr=mysql_fetch_array($result))
{$array[]=$tmpArr;}下面这部分:
for($ii=0;$ii<$i;$ii++){
if(${"result".$ii}[$kk])
echo "<td width=20>";
echo "<div align=center></div>";
echo "</td>";
echo "<td width=245>";
echo "<img height=11 hspace=4 src='images/arrow_01.gif' tppabs='images/arrow_01.gif' width=11><a href='_daoli/index/Community/Medical.php'>".${"result".$ii}[$kk]."</a></td>";
echo "<td width=20> </td>"; }
改为
for($ii=0;$ii<$i;$ii++){
if(is_array(${"result".$ii}[$kk])){
echo "<td width=20>";
echo "<div align=center></div>";
echo "</td>";
echo "<td width=245>";
echo "<img height=11 hspace=4 src='images/arrow_01.gif' tppabs='images/arrow_01.gif' width=11><a href='_daoli/index/Community/Medical.php?id=".${"result".$ii}[$kk]['id']."'>".${"result".$ii}[$kk]['newstitle']."</a></td>";
echo "<td width=20> </td>";
} }
for($n=0;$n<$i;$n=$n+$m){
${"result".$k}=array_slice($array,$n,$n+$m);
$k++;
}
应该为:
for($n=0;$n<count($array);$n=$n+$m){
${"result".$k}=array_slice($array,$n,$n+$m);
$k++;
}
CREATE TABLE news (
id int(11) NOT NULL auto_increment,
newstitle varchar(20) NOT NULL,
PRIMARY KEY (id)
);
INSERT INTO news VALUES ( '1', 'news1');
INSERT INTO news VALUES ( '2', 'news2');
INSERT INTO news VALUES ( '3', 'news3');
INSERT INTO news VALUES ( '4', 'news4');
INSERT INTO news VALUES ( '5', 'news5');
INSERT INTO news VALUES ( '6', 'news6');
INSERT INTO news VALUES ( '7', 'news7');
INSERT INTO news VALUES ( '8', 'news8');
INSERT INTO news VALUES ( '9', 'news9');
INSERT INTO news VALUES ( '10', 'news10');
INSERT INTO news VALUES ( '11', 'news11');
INSERT INTO news VALUES ( '12', 'news12');程序:
<?
$link=mysql_pconnect("localhost","root","");
mysql_select_db("test");
$sql="select id,newstitle from news order by id";
$result=mysql_query($sql);
while($tmpArr=mysql_fetch_array($result))
{$array[]=$tmpArr;}
$i=3;//设置每行新闻数量
$m=(is_int($tmN=count($array)/$i))?$tmN:(intval($tmN)+1);
$k=0;
for($n=0;$n<count($array);$n=$n+$m){
${"result".$k}=array_slice($array,$n,$n+$m);
echo "<pre>";
$k++;
}
echo "<pre>";
echo "</pre>";
echo "<TABLE border=1 style='border-collapse:collapse;'>";
for($kk=0;$kk<$m;$kk++){
echo "<TR>";
for($ii=0;$ii<$i;$ii++){
if(${"result".$ii}[$kk][newstitle]){
echo "<td width=245>";
echo "<a href='_daoli/index/Community/Medical.php?id=".${"result".$ii}[$kk]['id']."'>".${"result".$ii}[$kk]['newstitle']."</a></td>";
}
}
echo "</TR>";
}
echo "</TABLE>";
?>
$col=0;
$cols=2;//每行2列
$query="select id,title from table order by displayorder";
$result=mysql_query($query);
while($row=mysql_fetch_array($result))
{
$col=$col+1;
if ($col%$cols==1)echo "<tr>\n";
echo "<td>";
echo "<a href=list.php?id=$row[id]>$row[title]</a>";
echo "</td>\n";
if ($col%$cols==0) echo "</tr>\n";
}
// 下面处理当新闻总数是单数的时候输出空表单元
if($col%$cols!=0)
{
for($i=1;$i<=$cols-$col%$cols;$i++)
{
echo "<td width=25%> </td>\n";
}
}
if($i>1) echo "</tr>\n";
echo "</table>\n";以上代码可以处理多行N列,加个displayorder字段排序一下,即可实现楼主的意思