格式化并输出数据库表中的所有数据

时间:2014-05-04 08:01:56

标签: php mysql

我正在尝试创建一个页面,其中数据库中表格中的所有内容都显示在页面上。但是在一个很好的格式,比如ebay,它在左边显示图片,在中间显示标题。虽然我只有一个清单。

我似乎只能找到一些教程,这些教程只是回显了我认为我无法改变的所有信息,使它看起来像我想要的。我也尝试使用变量来存储它,但我只是在我的数据库中显示最后一件事。

以下是我的一些代码:

$query = mysql_query("SELECT * FROM BoatsForRent") or die("Error Searching");
$count = mysql_num_rows($query);
if ($count == 0) {
    $output = 'No results found';    
} else {
    while ($row = mysql_fetch_array($query))
    {                
        $id = $row['boatId'];
        $title = $row['title'];
        $type = $row['type'];    
        $address = $row['address'];
        $city = $row['city'];
        $postcode = $row['postcode'];
        $rooms = $row['rooms'];
        $decks = $row['decks'];
        $price = $row['price'];
        $title = $row['title'];

        echo "$title<br>";
    }
}

4 个答案:

答案 0 :(得分:0)

要设置表格的输出样式,你可以像这样做一些CSS:

strong {background: #0ff; text-align: right; display: inline-block; width: 50px;}
span {display: inline-block;}

您需要将HTML设为:

echo "<strong>Title</strong> <span>$title</span><br>";

预览

小提琴:http://jsfiddle.net/praveenscience/WBh52/

答案 1 :(得分:0)

使用div,span或table以您希望的任何方式显示数据。这是一个例如。

<table boder="0" cellspacing="5" cellpadding="5">
<tr>
   <td>Boat ID</td>
   <td>Title</td>
   <td>Type</td>
   <td>Address</td>
   <td>City</td>
   <td>Post Code</td>
   <td>Rooms</td>
   <td>Decks</td>
   <td>Price</td>
</tr>
<?php

                $id = $row['boatId'];
                $title = $row['title'];
                $type = $row['type'];   
                $address = $row['address'];
                $city = $row['city'];
                $postcode = $row['postcode'];
                $rooms = $row['rooms'];
                $decks = $row['decks'];
                $price = $row['price'];


    $count =mysql_num_rows($query);
        if($count == 0){
            $output = 'No results found';   
        } else {
            while ($row = mysql_fetch_array($query))
            {

?>

<tr>
   <td><?php echo $row['id']; ?></td>
   <td><?php echo $row['title']; ?></td>
   <td><?php echo $row['type']; ?></td>
   <td><?php echo $row['address']; ?></td>
   <td><?php echo $row['city']; ?></td>
   <td><?php echo $row['postcode']; ?></td>
   <td><?php echo $row['rooms']; ?></td>
   <td><?php echo $row['decks']; ?></td>
   <td><?php echo $row['price']; ?></td>
</tr>

<?php
   }
?>
</table>

或者,您可以将完整的表结构放在echo ''中。您现在可以根据需要使用外部或内联css。

答案 2 :(得分:0)

您需要将数据放入html标记中。这里只是一些非常基本的例子。然后你需要使用CSS来添加样式。

$query = mysql_query("SELECT * FROM BoatsForRent") or die("Error Searching");
$count = mysql_num_rows($query);
if ($count == 0) {
    $output = 'No results found';    
} else {
    while ($row = mysql_fetch_array($query))
    {                
        $id = $row['boatId'];
        $title = $row['title'];
        $type = $row['type'];    
        $address = $row['address'];
        $city = $row['city'];
        $postcode = $row['postcode'];
        $rooms = $row['rooms'];
        $decks = $row['decks'];
        $price = $row['price'];
        $title = $row['title'];

        echo "<h1>{$title}</h1>";
        echo "<h2>{$type}</h2>";
        echo "<h3>{$address}, {$city} {$postcode}</h3>";
        echo "<p>This boat has {$rooms} rooms and {$decks} decks!</p>";
        echo "<p>The selling price is ${$price}</p>";

    }
}

答案 3 :(得分:0)

您所考虑的代码应该在您的头部中,然后while的输出应该更改为数组:

...
while ($row = mysql_fetch_array($query))
    {                
        $id[] = $row['boatId'];
        $title[] = $row['title'];
        $type[] = $row['type'];    
        $address[] = $row['address'];
        $city[] = $row['city'];
        $postcode[] = $row['postcode'];
        $rooms[] = $row['rooms'];
        $decks[] = $row['decks'];
        $price[] = $row['price'];
        $title[] = $row['title'];

    }
...

在页面的正文部分为网格或表选择好的设计,并使用for循环填充数据:

<table>
<tr>
<td>ID</td>
<td>Title</td>
....
<?php for ($i =0; $i < count($id); $i++){ ?>
<tr>
<td><?php echo $id[$i];?></td>
<td><?php echo $title[$i];?></td>
....
<?php } ?>
</table>