从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.

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>';

quick workaround

If you want "image and name under it" and sets of such things in rows , you can do this :

set i to number of sets you want in a row (i=3 for above sample)

then

<table>

     main loop:

     <tr>

          loop start :

          <td>
                <table>
                     <tr><td>Image</td></tr>
                     <tr><td>Name</td></tr>
                </table>
          </td>

          i--;

          loop back if i not 0 and fetch record;

      </tr>

      main loop back if more record; set i=3;

</table>

Something like :

echo'<table>';
while($sam=mysql_fetch_array($query))
{
echo '<tr>';
$i=3; 
do{
    echo '<td>';
    echo '<table>';
    $image = $sam['image'];
    $name = $sam['name'];
    echo '<tr><td>'.'  <img src="'.$image.'"/>  '.'</td></tr>';
    echo '<tr><td>'.$name.'</td></tr>';
    echo '</table>';
    echo '</td>';
    $i--;   
}while($i>0 && $sam=mysql_fetch_array($query));

echo '</tr>';
}
echo '</table>';

Tables are created row -> columns, left to right. You are trying to build your table by columns -> rows, above to below.

The easiest way is to put the name in the same cell, and separate it from the image by a line break or div

edit since you are not using the $image/$name variables anywhere else, you can just use them directly, and remove 2 more lines of code

echo '<table>';
$i=0;
while($sam=mysql_fetch_array($query)) {

      if($i%3==0){ // using modulus % to determine if 1st cell
         echo '<tr>';
      }

      echo '<td>'.'  <img src="'.$sam['image'].'"/> ';
      echo '<br />'.$sam['name'].'</td>'; // echo image name with a line break inside image cell

      if($i%3==2){  // using modulus % to determine if 3rd cell
         echo '</tr>';
      }

      $i++;
}
echo '</table>';

if you still want the image names in cells below the images, one way to do it would be to concat the names, and add the row after the 3rd cell -

echo '<table>';
$i=0;
while($sam=mysql_fetch_array($query)) {  

      if($i%3==0){ // using modulus % to determine if 1st cell
         echo '<tr>';
         $names = ''; // create image name variable 
      }

      echo '<td>'.'  <img src="'.$sam['image'].'"/> '.'</td>';
      $names .= '<td>'.$sam['name'].'</td>'; // concat each image name, within a cell

      if($i%3==2){  // using modulus % to determine if 3rd cell
         echo '</tr>';
         echo '<tr>'. $names . '</tr>';
      }

      $i++;
}
echo '</table>';

A full example with custom array similar to what you get from your mysql.

If you copy and paste the code below it will work and you can tweak it..

<?php 
$sams=array();

array_push($sams, array("image"=>"image1","name"=>"name1"));
array_push($sams, array("image"=>"image2","name"=>"name2"));
array_push($sams, array("image"=>"image3","name"=>"name3"));
array_push($sams, array("image"=>"image4","name"=>"name4"));
array_push($sams, array("image"=>"image5","name"=>"name5"));
array_push($sams, array("image"=>"image6","name"=>"name6"));
array_push($sams, array("image"=>"image7","name"=>"name7"));
array_push($sams, array("image"=>"image8","name"=>"name8"));
array_push($sams, array("image"=>"image9","name"=>"name9"));
array_push($sams, array("image"=>"image10","name"=>"name10"));
array_push($sams, array("image"=>"image11","name"=>"name11"));
array_push($sams, array("image"=>"image12","name"=>"name12"));
array_push($sams, array("image"=>"image13","name"=>"name13"));
echo "<html>";
echo "<body>";

echo '<table width="400" border="1" cellspacing="0" cellpadding="0"><tr>';

$i=0;
$limit=3;
foreach($sams as $sam)
{

    $image = $sam['image'];
    $name = $sam['name'];

    if($i==3){
        echo '</tr><tr>';
    }

    echo "<td>";
    echo '<table width="200" border="1" cellspacing="0" cellpadding="0"><tr><td>';

    echo $image;
    echo '</td></tr><tr><td>';

    echo $name;
    echo '</td></tr></table>';
    echo "</td>";


    if($i==3){
        $i=1;
    }else{
        $i++;
    }
}

echo '</tr></table>';
echo "</body>";
echo "</html>";
?>