这就是我想要的:
------------ ------------ ------------ ------------
| | | | | | | |
| 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个图像。第四个图像应该是一条新线,每个图像下面应该是名称
我做错了什么?
这是您想要做的。它支持任意数量的行,每次输出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>";
这有点粗糙,但应该可以
// 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>";