为每个主键分配自己的列

时间:2010-10-02 19:28:40

标签: php html-table

//Define your database settings.
define('DB_HOST', '');
define('DB_PORT', '');
define('DB_USER', '');
define('DB_PASS', '');
define('DB_NAME', '');

$database = new MySQLi(DB_HOST, DB_USER, DB_PASS, DB_NAME, DB_PORT);

// Escape the id, incase it's a malicious input.
$id = $database->real_escape_string($id);

$sql = 'SELECT Brand.brand, Model.model, Price.price' 
. ' FROM Model' 
. ' INNER JOIN Brand ON Model.brand_id = Brand.brand_id' 
. ' INNER JOIN Price ON Model.model_id = Price.model_id' 
. ' WHERE Price.price BETWEEN 1 AND 5';

$result = $database->query($sql);

// Begin building some HTML output

$html = '<table border="0">
<tr>
<th></th>
</tr>';

while ($row = $result->fetch_assoc())
{
    $html .= '<tr><td>' . $row['brand'] . '</td></tr>';
    $html .= '<tr><td>' . $row['model'] . '</td></tr>';
    $html .= '<tr><td>' . $row['price'] . '</td></tr>';
}

$html .= '</table>';

echo $html;

示例HTML表格输出到我的网页现在是一列 一路下来

-----------
|ID 1     |
-----------
|Audi     |
-----------
|A3    |
-----------
|$22,000  |
-----------
|ID 2     |
-----------
|BMW      |
-----------
|3Series  |
-----------
|$24,000  |
-----------
| ID3
---------
|Cadillac
-------
|....... keeps going down to ID10

我想要实现的是为每列分配自己的ID

--------------------------------------
|ID 1     |ID2      |ID3      | >>>> so on going across to ID10
------------------------------------
|Audi     |BMW      |Cadillac |
----------------------------------
|A3    |3Series  |....     |
---------------------------------
|$22,000  |$24,000  |..       |
--------------------------------

3 个答案:

答案 0 :(得分:1)

while ($row = $result->fetch_assoc()) {
  $html .= '<div class="container">';
  $html .= '<span class="brand">' . $row['brand'] . '</span>';
  $html .= '<span class="model">' . $row['model'] . '</span>';
  $html .= '<span class="price">' . $row['price'] . '</span>';
  $html .= "</div";
}

并在HTML中添加一些css

.container{
   display:inline-block;
   width: xx;//width in px
   height:xx;//height in px
}

.brand{
   display:block;
   //and add your width and height
}

.model{
   display:block;
   //and add your width and height
}

.price{
   display:block;
   //and add your width and height
}

答案 1 :(得分:1)

<?php

//Define your database settings.
define('DB_HOST', '');
define('DB_PORT', '');
define('DB_USER', '');
define('DB_PASS', '');
define('DB_NAME', '');

$database = new MySQLi(DB_HOST, DB_USER, DB_PASS, DB_NAME, DB_PORT);

// Escape the id, incase it's a malicious input.
$id = $database->real_escape_string($id);

$sql = 'SELECT Brand.brand, Model.model, Price.price' 
. ' FROM Model' 
. ' INNER JOIN Brand ON Model.brand_id = Brand.brand_id' 
. ' INNER JOIN Price ON Model.model_id = Price.model_id' 
. ' WHERE Price.price BETWEEN 1 AND 5';

$result = $database->query($sql);

// Begin building some HTML output


$d = new DOMDocument('1.0', 'UTF-8');
$d->loadHTML('<body></body>'); // to make it quick

$table = $d->createElement('table');
$brand_row = $table->appendChild($d->createElement('tr'));
$model_row = $table->appendChild($d->createElement('tr'));
$price_row = $table->appendChild($d->createElement('tr'));

while ($row = $result->fetch_object())
{
    $brand = $brand_row->appendChild($d->createElement('td'));
    $model = $model_row->appendChild($d->createElement('td'));
    $price = $price_row->appendChild($d->createElement('td'));
    $brand->appendChild($d->createTextNode($row->brand));
    $model->appendChild($d->createTextNode($row->model));
    $price->appendChild($d->createTextNode($row->price));
}

$body = $d->getElementsByTagName('body')->item(0);
$body->appendChild($table);

echo $d->saveHTML();

?>

有关DOMDocument的详细信息,请参阅http://php.net/manual/en/class.domdocument.php。当然它可以做得更短,但我想留下所有平原而没有任何伏都教来表明这一点。总是使用DOM来生成HTML,看起来很复杂,但事实上,如果你为常见任务做一些快捷方式,它可以节省大量时间。如果要在输出中具有特定的HTML风格,只需加载包含所有标题的模板。您可以将其作为字符串或使用html文件提供。好事是loadHTML()方法为你做一些基本的整理。糟糕的是使用HTML5或XHTML需要一些黑客攻击。

答案 2 :(得分:0)

如果您不是一次显示大量数据,则可以先将所有数据加载到数组中,然后按照您希望的方式输出。否则From.ME.to.YOU的回答是要走的路。