dsyct08008 2010-10-02 19:28
浏览 19
已采纳

为每个主键分配自己的列

//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;

Example HTML Table output to my webpage right now is one column going all the way down

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

What I would like to achieve is assigning each column its own ID going across

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

3条回答 默认 最新

  • dougui1977 2010-10-02 19:59
    关注
    <?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();
    
    ?>
    

    Refer to http://php.net/manual/en/class.domdocument.php for details on DOMDocument. Of course it could be done way shorter, but I wanted to leave all plain without any voodoo to show the point. Always use DOM to generate HTML, it seems elaborate, but in fact it's a great time saver, if you make some shortcuts for common tasks. If you want to have speciffic flavor of HTML on output, just load a template with all headers. You can provide it as string, or using a html file. Good thing is loadHTML() method does some basic tidying for you. Bad thing is using HTML5 or XHTML requires some hacking.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题