NewPassionnate NewPassionnate - 12 days ago 6
PHP Question

display result sql in columns

I would like to display the results of my sql in three columns like the format below.

How is it possible? Should I add table with tr and td? or div in my code?

CategoryName1 CategoryName2 CategoryName3
subcategoryN subcategoryN subcategoryN



<?php
foreach ($resultLang as $oneRec)
{
$catid= $oneRec["categoryID"];


echo"<strong>".$oneRec["categoryName"]."</strong><br>";


$sqlCmd2 ="SELECT subcategories.subCatID,subcategories.subCatName
FROM subcategories
JOIN categories
ON subcategories.categoryID=categories.categoryID
JOIN languages
ON categories.languageID = languages.languageID
WHERE languages.languageID =:lang
AND categories.categoryID=:cboCat";
$prepQuery2 = $conn ->prepare($sqlCmd2);
$prepQuery2->bindParam(":lang",$langTemp, PDO::PARAM_INT);
$prepQuery2->bindParam(":cboCat",$catid, PDO::PARAM_INT);
$prepQuery2->execute();
$resultLang2= $prepQuery2->fetchAll();

foreach ($resultLang2 as $oneRec)
{
$subCatid=$oneRec["subCatID"];

echo "<a href='displaysubcategory.php?subcat=?$subCatid'>".$oneRec["subCatName"]."</a></br>";

}
echo "<br>";

}

Answer
<?php 
echo"<ul class='maincats'>";
  foreach ($resultLang as $oneRec)
  {             
    $catid= $oneRec["categoryID"];


    echo"<li>";
    echo"<strong>".$oneRec["categoryName"]."</strong>";

    $sqlCmd2 ="SELECT subcategories.subCatID,subcategories.subCatName
              FROM subcategories
              JOIN categories
              ON subcategories.categoryID=categories.categoryID
              JOIN languages
              ON categories.languageID = languages.languageID
              WHERE languages.languageID =:lang
              AND categories.categoryID=:cboCat";
    $prepQuery2 = $conn ->prepare($sqlCmd2);
    $prepQuery2->bindParam(":lang",$langTemp, PDO::PARAM_INT);
    $prepQuery2->bindParam(":cboCat",$catid, PDO::PARAM_INT);
    $prepQuery2->execute();
    $resultLang2= $prepQuery2->fetchAll();
    if($resultLang2) {
        echo"<ul class='subcats'>";
        foreach ($resultLang2 as $oneRec)
        {
            $subCatid=$oneRec["subCatID"];
            echo"<li>";
            echo "<a href='displaysubcategory.php?subcat=?$subCatid'>".$oneRec["subCatName"]."</a>";
            echo"</li>";
        } 
        echo"</ul>";
    }
    echo"</li>";

  }
echo"</ul>";

After that create some CSS rules for maincats and subcats class using float, display:inline-block properties according to your design theme.

Comments