dongyun8891 2014-07-19 17:16
浏览 46
已采纳

从MySQL查询生成垂直HTML表

I am trying to generate an HTML table from a MySQL query. Here is the format of the table I am trying to generate from a MySQL query:

 -------------------------- 
| Image1 | Image2 | Image3 |
|----------------- --------|
| Name1  | Name2  | Name3  |
|----------------- --------|
| Image4 | Image5 | Image6 |
|----------------- --------|
| Name4  | Name5  | Name6  |
 -------------------------- 

I am trying to create above table with my php code. The following is my code but it is not working properly can u help me out.

include_once("abc.php");

$query=mysql_query("select * from movix LIMIT 6");
echo'<table>';
$i=0;
while($sam=mysql_fetch_array($query))
{
$image = $sam['image'];
$name = $sam['name'];



if($i==0) 
   {
   echo '<tr>';
   }
echo '<td>'.'  <img src="'.$image.'"/>  '.'</td>';
  echo '</tr>';  echo '<tr>';
echo '<td>'.$name.'</td>';
echo '<tr>';
 if($i==2)

    $i=-1;
    }
  $i++;
}
echo '</table>';

Can you please help me to understand what I am doing wrong with code and point me in the right direction? or please correct my code according to my above table.

  • 写回答

4条回答 默认 最新

  • duanji5116 2014-07-19 17:54
    关注

    You need to create 2 rows with 3 cols and then echo them.

    [Not aware of php but i guess following code should work or atleast give an idea of how it needs to be achieved.]

    $row1="<tr>";
    $row2="<tr>";
    $i=3;
    echo'<table>';
    while($sam=mysql_fetch_array($query))
    { 
        $image = $sam['image'];
        $name = $sam['name'];
        $row1 = $row1.'<td><img src="'.$image.'"/></td>';
        $row2 = $row2.'<td>'.$name.'</td>';
        $i--;
    
        if($i==0){
            echo row1.'</tr>';
            echo row2.'</tr>';
            $row1="<tr>";
            $row2="<tr>";
            $i=3;
        }
    }
    echo '</table>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏