使用php在每个4图像的表中放一行?

时间:2013-12-04 02:47:41

标签: php

这就是我想要发生的事情:

------------   ------------    ------------     ------------  
|          |   |          |    |          |     |          |
|  image   |   |   image  |    |  image   |     |  image   |
|          |   |          |    |          |     |          |
------------   ------------    ------------     ------------  
Name           Name            Name             Name

------------   ------------    ------------     ------------ 
|          |   |          |    |          |     |          |
|  image   |   |   image  |    |  image   |     |   image  |
|          |   |          |    |          |     |          |
------------   ------------    ------------     ------------ 
Name           Name            Name             Name

但这就是发生的事情:

------------ ------  ------------ ------  ------------     ------------  
|          | |name|  |          | |name|  |          |     |          |
|  image   | |    |  |   image  | |    |  |  image   |     |  image   |
|          | |    |  |          | |    |  |          |     |          |
------------ ------  ------------ ------  ------------     ------------  

所有这些都只是一行

以下是我现在使用的游戏代码

echo"<table border=1>";
while ($row = mysql_fetch_array($content))
{


        echo "<td><img src='".$row['image']."' width='100'></td>"; 

        echo"<td>" . $row['name'] . "</td>";

}
echo"</table>";

每个第四张图像可能有8张图像应该是一个新行,每张图像下面应该是名称

我做错了什么?

2 个答案:

答案 0 :(得分:0)

这有点粗糙,但应该有效

// store the cells in arrays instead of echoing them
$images=$names=array();
while ($row = mysql_fetch_array($content)){
       $images[]="<td><img src='".$row['image']."' width='100'></td>"; 
       $names[]="<td>" . $row['name'] . "</td>";
}

// split them into 4 each. This will give $image_cells[0] as an array with the first 4 images
// and $image_cells[1] containing the latter 4
$image_cells=array_chunk($images, 4); 
// same with $names
$name_cells=array_chunk($names, 4);

// echo the table 
echo"<table border=1>";

// echo a table row, concat the cells stored as an array to a string using implode
echo '<tr>'. implode('', $image_cells[0]).'</tr>';

// and again but using $name_cells[0] which is the first 4 names
echo '<tr>'. implode('', $name_cells[0]).'</tr>';

// second set of images
echo '<tr>'. implode('', $image_cells[1]).'</tr>';

// second set of names
echo '<tr>'. implode('', $name_cells[1]).'</tr>';

echo"</table>";

答案 1 :(得分:0)

这是你想要做的。它一次支持任意数量的行和输出4。例如,如果有15个项目,则前三行输出4,最后一行输出3。

echo"<table border=1>";
$images = array();
$names = array();
while ($row = mysql_fetch_array($content))
{
    $images[] = $row['image'];
    $names[] = $row['name'];
}
while(!empty($images))
{
    echo "<tr>";
    foreach($images as $count=>$image)
    {
        echo "<td><img src='".$image."' width='100'></td>";
        if($count==3)
        {
            $images = array_slice($images, 4);
            break;
        }
    }
    echo "</tr><tr>";
    foreach($names as $count=>$name)
    {
        echo"<td>" . $name . "</td>";
        if($count==3)
        {
            $names = array_slice($names, 4);
            break;
        }
    }
    echo "</tr>";
}
echo"</table>";

这是一个较短的版本,它也是如此:

echo"<table border=1>";

//get images and names in two arrays
$images = array();
$names = array();
while ($row = mysql_fetch_array($content))
{
    $images[] = "<img src='".$row['image']."' width='100'>";
    $names[] = $row['name'];
}


while(!empty($images))
{       
    //output images     
    foreach(array($images, $names) as $items)
    {
        echo "<tr>";
        foreach($items as $key=>$item)
        {
            echo "<td>$item</td>";              
            //output only four of them
            if($key==3)
            {
                break;
            }
        }
        echo "</tr>";
    }
    //remove the first four images from $images because they're already printed
    $images = array_slice($images, 4);
    $names = array_slice($names, 4);
}
echo"</table>";