I am a bit stuck with a problem and need your help to identify the wrong part of code.
I will try to explain is short what I am trying to do. My SQL table looks something like this:
make model model_body variant
M1 A ooo Va1
M1 B sss Va2
M1 B sss Va3
M1 A ooo Va4
M1 A ooo Va5
M1 B jjj Va6
M1 A www Va7
I would like to output the table contents in HTML like this:
First, I am trying to list once the contents of model_body
column by using SELECT DISTINCT
and from there I am trying to list each different value from variant
column that matches the same model_body
.
A - ooo Va1
Va4
Va5
A - www Va7
B - sss Va2
Va3
B - jjj Va6
My SQL queries are listed below:
$catalogue_make = $_GET['m'];
$sql_all_models = "SELECT * FROM tbl_catalog WHERE make = '$catalogue_make'";
$rs_all_models = $db -> Execute($sql_all_models);
$sql_main_model = "SELECT * FROM tbl_catalog
WHERE make = '".$rs_all_models->fields['make']."'
GROUP BY model_body";
$rs_main_model = $db -> Execute($sql_main_model);
$sql_variant = "SELECT * FROM tbl_catalog
WHERE model_body = '".$rs_main_model->fields['model_body']."'";
$rs_variant = $db -> Execute($sql_variant);
The PHP code I wrote is this one, I tried several different possibilities but always end up with some kind of mismatch:
<?php while (!$rs_main_model->EOF) { ?>
<div>
<div>
<h2<?php echo $rs_main_model->fields['model']; ?></h2>
</div>
<div>
<?php while (!$rs_variant->EOF) { ?>
<?php echo $rs_variant->fields['variant']; ?>
<?php $rs_variant->MoveNext(); } ?>
</div>
</div>
<?php $rs_main_model->MoveNext(); } ?>
EDIT
This code lists the unique model_body
as I want them, however I encounter one problem:
- I get the
$rs_variant->fields['variant']
listed only for the firstmodel_body
. The<div>
s for the second, third and so on uniquemodel_body
s are empty. How can I make the loop show the variants for the other matching model_body?
This is what I get now with the second loop (I believe the problem is with the loop itself):
A - ooo Va1
Va4
Va5
A - www
B - sss
B - jjj
Thank you!