我要实现这样的样子
<table>
<tr>
<td width="42">标题1</td>
<td width="42">标题2</td>
</tr>
<tr>
  <td>图片1</td>
  <td>图片2</td>
</tr>
<tr>
  <td>链接1td>
  <td>链接2</td>
</tr>
</table> while(list($id,$title,$image)=mysql_fetch_row($result))
{
这怎么写?
}

解决方案 »

  1.   

    .....你为啥不改下HTML的写法呢
      

  2.   


    $i=0; $str="";
     while(list($id,$title,$image)=mysql_fetch_row($result))
    {
    $i++;
      if($i==1){
    //思路是这样 ,当循环第1次则给宽度。
      $str=" width="42";
      }else{$str=" ";}  $str=<<<HTML
    <tr>
      <td $t >标题1</td>
      <td $t >标题2</td>
      </tr>
    HTML;
    }
      

  3.   

    多谢楼上的,可能误会我的意思了,不需要那宽度
    <table>
    <tr>
    <td >标题1</td>
    <td >标题2</td>
    </tr>
    <tr>
      <td>图片1</td>
      <td>图片2</td>
    </tr>
    <tr>
      <td>链接1td>
      <td>链接2</td>
    </tr>
    </table>这样就可以
      

  4.   

    //因为从数据库查询出来的是2维数组,
    //所以你可以
     while($row=mysql_fetch_array($result))
    {
       $array[]=$row;
    }
    echo "<table>";
    foreach($array as $value){
        echo "<tr>";
        foreach($value as $v){
           echo "<td>".$v."</td>"
        }
        echo "</tr>";
    }
    echo "</table>";
      

  5.   

    我是个PHP的新手,各位帮帮忙,给个代码吧
      

  6.   


    //因为从数据库查询出来的是2维数组,
    //所以你可以
     while($row=mysql_fetch_array($result))
    {
       $array[]=$row;
    }
    echo "<table>";
    foreach($array as $value){
        echo "<tr>";
        foreach($value as $v){
           echo "<td>".$v."</td>"
        }
        echo "</tr>";
    }
    echo "</table>";
    这个不对吗?
      

  7.   


    $resultArray = array();
    while ($row = mysql_fetch_array($result)) 
    {
    $row=array_unique($row);//删除重复
    array_push($resultArray,$row);
    }
    echo "<pre/>";
    echo "<table>";
    foreach($resultArray as $value){
        echo "<tr>";
        foreach($value as $v){
           echo "<td>".$v."</td>";
        }
        echo "</tr>";
    }
    echo "</table>";
      

  8.   

    $resultArray = array();
    //也可以这样枚举一行就不用删除重复
    while ($row = mysql_fetch_row($result)) 
    {
        array_push($resultArray,$row);
    }
    echo "<pre/>";
    echo "<table>";
    foreach($resultArray as $value){
        echo "<tr>";
        foreach($value as $v){
           echo "<td>".$v."</td>";
        }
        echo "</tr>";
    }
    echo "</table>";
      

  9.   


    $dbhost = '127.0.0.1'; 
    $dbuser = 'temp'; 
    $dbpass = 'temp'; 
    $dbname = 'mydb'; 
    $connect = mysql_connect($dbhost,$dbuser,$dbpass); 
    if ($connect)
    {
    mysql_select_db($dbname);
    $sql="select * from test ;"; 
    $resultArray = array();
    //也可以这样枚举一行就不用删除重复 while ($row = mysql_fetch_row($result)) 
    {
    array_push($resultArray,$row);
    }
    echo "<pre/>";
    echo "<table>";
    foreach($resultArray as $value){     
    echo "<tr>";
    foreach($value as $v){        
    echo "<td>".$v."</td>";
    }     
    echo "</tr>";
    }
    echo "</table>";
    }
      

  10.   

    上面这写错了
    //也可以这样枚举一行就不用删除重复                           
     while ($row = mysql_fetch_row($result))  不知道哪的问题
      

  11.   

    还有个问题,现在虽然可以输出,比如是这样
    主题1    主题2 
    图片1    图片2
    链接1    链接2图片存的是一个地址 比如 /pic/xxx.gif
    我在刚才那循环里,怎么把这图显示出来?
     echo "<td>".$v."</td>"; 这是图的时候,怎么做?
      

  12.   

     echo "<td><a href='".$v."'/></td>"; 这是图的时候,怎么做?
      

  13.   

    这样做,那主题和图链接什么的,不全都变了吗?
    其实我就是要实现这种格式
    主题1 主题2
    图片1 图片2
    链接1 链接2图片是把图显示出来,链接是直接显示一个比如AAA,然后AAA上带着链接
      

  14.   

    //上面错误 $resultArray=array();
    while ($row = mysql_fetch_row($result))                     
    {
    array_push($resultArray,$row);
    }
    echo "<pre/>";                            
    echo "<table>";                            
    foreach($resultArray as $value){                                
    echo "<tr>";                                
    foreach($value as $v){      
    $img=explode(".",basename($path));
    if($img["1"]){
       $val="<img href='.$v.' />";
    }else{$val=$v;}                         
    echo "<td>".$val."</td>";                                
    }                                    
    echo "</tr>";                                    
    }
    echo "</table>";
    }
      

  15.   

    echo "<pre/>";
    echo "<table>";foreach($resultArray as $value){
        echo "<tr>";
        foreach($value as $v){
         $img=explode(".",basename($v));
        if($img[1]){
       $val="<img href='{$v}' />";
    }else{
    $val=$v;
    }
           echo "<td>".$val."</td>";
        }
        echo "</tr>";
    }
    echo "</table>";
      

  16.   

    突然发现一个问题,这个格式不对,应该是这样,比如:
    IBM笔记本          杯子
    IBM笔记本图        杯子图片
    笔记本链接         杯子链接而不是
    IBM笔记本   IBM笔记本图   笔记本链接 杯子        杯子图片       杯子链接
      

  17.   

    foreach($arr as $key=>$value)
    foreach($arr as $value);.,,,
      

  18.   


    <?php
    while($row=mysql_fetch_row($result))
    {
    $data[] = $row;
    }
    $len = count($data);
    ?><table>
    <tr>
    <td width="42">标题1</td>
    <td width="42">标题2</td>
    </tr>
    <?php
    for($i=0;$i<$len;$i+=2):
    ?>
    <tr>
      <td><?php echo $data[$i]['img'];?></td>
      <td><?php echo $data[$i+1]['img'];?></td>
    </tr>
    <tr>
      <td><?php echo $data[$i]['title'];?></td>
      <td><?php echo $data[$i+1]['title'];?></td>
    </tr>
    <?php
    endfor;
    ?>
    </table>
      

  19.   


    楼主的这问题我最了解,也曾经想了1天。<table>
    <?php
     while($row=mysql_fetch_array($result))
    {
       $str_name.="<td>".$row->name.</td>;
       $str_pic.="<td><img src=".$row->pic."/></td>;
       $str_url.="<td>".$row->url.</td>;
    }
    echo "<tr>".$str_name."</tr>";
    echo "<tr>".$str_pic."</tr>";
    echo "<tr>".$str_url."</tr>";
    ?>
    </table>img标签怎么用太基础,自个查。我只想解决你的布局要求。
    请散分,谢谢!
      

  20.   

    lz你何必非要在一个table里呢,table是可以嵌套的啊echo "<table><tr>"
    while(list($id,$title,$image)=mysql_fetch_row($result))
    {
    echo "<td><table><tr><td>".$id."</td></tr><tr><td>".$title."</td><tr><td>".$image."</td></tr></table></td>"
    }
    echo "</tr></table>"
      

  21.   

    你先把查询出来的结果这样输出下<?php
    ...
    while($row=mysql_fetch_row($result)){        $title.=$row["title"];//假设字段名是title
        }
        echo $title;
    ...
    ?>
    看下$title的输出结果,你就知道这个问题多简单了!
      

  22.   

    <?php
    $data = array(
    array('id'=>1,'img'=>'1.jpg','title'=>'1111'),
    array('id'=>2,'img'=>'2.jpg','title'=>'222'),
    array('id'=>3,'img'=>'3.jpg','title'=>'333'),
    array('id'=>4,'img'=>'4.jpg','title'=>'444'),
    );
    $len = count($data);
    ?>
    <table width="200" border="1" cellspacing="0" cellpadding="0">
    <?php
    for($i=0;$i<$len;$i+=2):
    ?>
      <tr>
      <td>&nbsp;<?php echo $data[$i]['title'];?></td>
      <td>&nbsp;<?php echo $data[$i+1]['title'];?></td>
      </tr>
      <tr>
      <td>&nbsp;<?php echo $data[$i]['img'];?></td>
      <td>&nbsp;<?php echo $data[$i+1]['img'];?></td>
      </tr>
      <tr>
      <td><a href="?id=<?php echo $data[$i]['id'];?>">链接1</a></td>
      <td><a href="?id=<?php echo $data[$i+1]['id'];?>">链接2</a></td>
      </tr>
    <?php
    endfor;
    ?>
    </table>
      

  23.   

    38楼数据,如果数组是奇数情况就会出错,所以改良了一下HTML,具体代码如下:<?php
    $data = array(
    array('id'=>1,'img'=>'1.jpg','title'=>'1111'),
    array('id'=>2,'img'=>'2.jpg','title'=>'222'),
    array('id'=>3,'img'=>'3.jpg','title'=>'333'),
    array('id'=>4,'img'=>'4.jpg','title'=>'444'),
    );
    $len = count($data);
    ?>
    <div style="width:210px;border:1px solid #ccc;text-align:left;"><?php
    for($i=0;$i<$len;$i++):
    ?>
    <div style="float:left; width:100px;">
    <ul style="list-style:none;">
        <li><?php echo $data[$i]['title'];?></li>
        <li><?php echo $data[$i]['img'];?></li>
        <li><a href="?id=<?php echo $data[$i]['id'];?>">链接1</a></li>
    </ul>
    </div>
    <?php
    endfor;
    ?>
    </div>
      

  24.   


    $i=0;
    foreach($resultArray as $value){
    $i++;
    if($i==1)
    {
    $str="<tr>";$str1="<tr>";$str2="<tr>";
    }
    $str.="<td>".$value[0]."</td>";
    $str1.="<td>".$value[1]."</td>";
    $str2.="<td>".$value[2]."</td>";
    }
    $str.="</tr>";$str1.="</tr>";$str2.="</tr>";
    echo $str.$str1.$str2;
    //或者
    foreach($resultArray as $value){
    $str.=<<<HTML
    <div style="float:left; width:100px;">
    <ul style="list-style:none;">
        <li>{$value[0]}</li>
        <li>{$value[1]}</li>
        <li><img src="{$value[2]}"/></li>
    </ul>
    </div>
    HTML;
    }
    echo $str;