php - Printing matrix table from MySQL with PDO -
i have 3 tables countries
, product_categories
, vats
including following information
product_categories
includes 2 columns product_category_code
, product_category_name_en
and 1 view vats_view
contains following query
select countries.country_code, product_categories.product_category_code, product_categories.product_category_name_en, vats.vat, vats.editedby, vats.editedtimestamp vats, countries, product_categories countries.country_code = vats.country , product_categories.product_category_code = vats.product_category
i'd create html matrix table based on information. in case table like
--------------------------------------------------- | | al | de | dk | se | --------------------------------------------------- | category 10 | 14.00 | | | 10.00 | | category 20 | 15.00 | | | | | category 30 | | | | | ---------------------------------------------------
i have done following code. know it's totally wrong , not working yet, i'm asking ideas how this.
echo "<table class=\"table table-bordered\"><tr><th></th>"; $sql = $pdo->query("select country_code countries active = 1 order country_code asc"); while($country = $sql->fetch(pdo::fetch_assoc)) { echo "<th class=\"text-center\">".$country['country_code']."</th>"; } echo "</tr>"; $sql = $pdo->query("select * product_categories"); while($vat = $sql->fetch(pdo::fetch_assoc)) { echo "<tr><td>".$vat['product_category_name_'.$lang]."</td>"; $sqlvats= $pdo->query("select * vats_view product_category_code = '".$vat['product_category_code']."'"); while($row = $sqlvats->fetch(pdo::fetch_assoc)) { echo "<td>".$row['vat']."</td>"; } echo "</tr>"; } echo "</table>";
any ideas welcome. in advance.
ok, fixed second query use proper $vat variable, replaced third query prepared statements (just practice) , replaced while if take care of empty results
echo "<table class=\"table table-bordered\"><tr><th></th>"; $sql = $pdo->query("select country_code countries active = 1 order country_code asc"); while($country = $sql->fetch(pdo::fetch_assoc)) { echo "<th class=\"text-center\">".$country['country_code']."</th>"; } echo "</tr>"; $vat = $pdo->query("select * product_categories"); while($vat = $sql->fetch(pdo::fetch_assoc)) { echo "<tr><td>".$vat['product_category_name_'.$lang]."</td>"; $sqlvats = $pdo->prepare("select * vats_view product_category_code = ?"); $sqlvats->bindparam(1, $vat['product_category_code']); $sqlvats->execute(); if ($row = $sqlvats->fetch(pdo::fetch_assoc)) { echo "<td>".$row['vat']."</td>"; } else { echo "<td></td>"; } echo "</tr>"; } echo "</table>";
Comments
Post a Comment