0
// Database Settings 
define('DB_HOST', '******');
define('DB_PORT', '******');
define('DB_USER', '******');
define('DB_PASS', '******');
define('DB_NAME', '******');

// Connection to Database
$database = new MySQLi(DB_HOST, DB_USER, DB_PASS, DB_NAME, DB_PORT);

$sql = 'SELECT AManufactureBrand.brand, AManufactureModel.model, AManufactureEdition.edition'
        . ' FROM AManufactureModel'
        . ' INNER JOIN AManufactureBrand ON AManufactureModel.brand_id = AManufactureBrand.brand_id'
        . ' INNER JOIN AManufactureEdition ON AManufactureModel.model_id = AManufactureEdition.model_id'
        . ' WHERE AManufactureEdition.edition=\'345i\'';

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

// Begin building some HTML output

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

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

$html .= '</table>';

echo $html;
?>

For example this query calls up BMW 3Series 345i, I have two results from mysql printed to a table on my website. The problem the two records print out on one column going down onwards.

Currently I get a result like this on my webpage two mysql records printing vertical down one column.


I'm trying to make it go across like this and print multiple cars next to each-other horizontally across.

2 Answers 2

2

Not sure exactly what you're trying to do, but I would suggest replacing this:

// Begin building some HTML output

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

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

$html .= '</table>';

with this:

// Begin building some HTML output

$html = '<table border="0">
<tr>
<th>Brand</th>
<th>Model</th>
<th>Edition</th>
</tr>\n';

while ($row = $resultSet->fetch_assoc())
{
$html .= '<tr>';
$html .= '<td>' . htmlentities($row['brand']) . '</td>';
$html .= '<td>' . htmlentities($row['model']) . '</td>';
$html .= '<td>' . htmlentities($row['edition']) . '</td>';
$html .= '</tr>\n';
}

$html .= '</table>';

That will give you proper 3 column output. If that's not what you wanted, then clarify your question.

Sign up to request clarification or add additional context in comments.

1 Comment

Still not clear what you're trying to do... Sorry. Hopefully you can work something out from this answer.
1

I know there's gotta be a better way to do it with HTML but this should work:

$ths = $tds1 = $tds2 = $tds3 = '';

while ($row = $resultSet->fetch_assoc())
{
$ths .= '<td>Editions</td>';
$tds1 .= '<td>' . $row['brand'] . '</td>';
$tds2 .= '<td>' . $row['model'] . '</td>';
$tds3 .= '<td>' . $row['edition'] . '</td>';
}

$html = "<table border="0">
<tr>$ths</tr>
<tr>$tds1</tr>
<tr>$tds2</tr>
<tr>$tds3</tr>
</table>
";

You could also just have multiple tables in succession.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.