在格式化表中显示mysql数据 [英] Display mysql data in formatted table
问题描述
我正在从数据库中检索数据并显示在表格中.我希望桌子采用 4 x 3 布局.数据检索得很好,这是布局不起作用.这是我的代码:
I am retrieving data from a database and displaying in a table. I would like the table to be in a 4 x 3 layout. The data retrieves just fine, it's the layout that isn't working. Here is my code:
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
$db = new mysqli('localhost', 'root', '', 'ezwayautos');
if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}
$sql = "SELECT * FROM vehicles ORDER BY RAND() LIMIT 12";
if(!$result = $db->query($sql)){
die('There was an error running the query [' . $db->error . ']');
}
echo "<table>";
while($row = $result->fetch_assoc())
{
echo "<tr>";
echo "<td height='160' valign='top' class='featured'>";
echo "<div class='Image-Thumbnail'>";
echo "<a href=''>";
echo "<img src='".$row['image']."' width='160' height='54'>";
echo "</a>";
echo "</div> <a href=''>" .$row['vehicle_name']. "</a>";
echo "</td>";
echo "</tr>";
}
echo "</table>";
?>
每个单元格中应该显示的是车辆图片,图片下方带有名称.
What is supposed to go in each cell is a picture of the vehicle with the name underneath the picture.
以下是另一个网站的示例,说明我希望它的外观:
Here is an example from another website as to how I would like it to look:
我没有从他们的网站窃取任何信息,我只是想让我的数据以他们网站上的格式显示.
I am not stealing any information from their site I am just trying to get my data to display in the format that they have on their site.
我不关心空的 href 标签,因为它们稍后会被填充.
I am not concerned about the empty href tag as they will be filled in later.
在做了一些研究之后,我认为我必须使用 2 个 for 循环来创建表,说明我想要多少列和行.
推荐答案
是否像将关闭外部表的最后 3 行移动到 while 循环之外一样简单?
Is it as simple as moving the last 3 lines that close the outer table outside the while loop?
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
$db = new mysqli('localhost', 'root', '', 'ezwayautos');
if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}
$sql = "SELECT * FROM vehicles ORDER BY RAND() LIMIT 12";
if(!$result = $db->query($sql)){
die('There was an error running the query [' . $db->error . ']');
}
echo "<table>";
echo "<tr>";
echo "<td>";
while($row = $result->fetch_assoc())
{
echo "<table border='0' cellpadding='0' cellspacing='0'>";
echo "<tr>";
echo "<td height='160' valign='top' class='featured'>";
echo "<div class='Image-Thumbnail'>";
echo "<a href='inventory/view/7995179/2005-Volvo-XC90-4dr-2.html'>";
echo "<img src='".$row['image']."' width='160' height='54' alt='".$row['vehicle_name']."'>";
echo "</a>";
echo "</div> <a href='inventory/view/7995179/2005-Volvo-XC90-4dr-2.html'>" .$row['vehicle_name']. "</a>";
echo "</td>";
echo "</tr>";
echo "</table>";
}
echo "</td>";
echo "</tr>";
echo "</table>";
?>
这篇关于在格式化表中显示mysql数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!