//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 |.. |
--------------------------------
<?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.
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";
}
and add some css to your HTML
.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
}
If you're not displaying a lot of data at once, you can load all the data into an array first and then output it the way you want. Otherwise From.ME.to.YOU's answer is the way to go.