1

I have a problem with creating multiple sub category with php and mysql where the deeper sub category doesn't get indented. The screenshot is just like:
enter image description here.
The sealer and wall paint should be indented like hobe.

Ex: 1. painting, 1.1 HOBE, 1.1.1 Sealer, 1.1.2 Wall Paint

I have 3 table:

  1. kategori contain: id_kat, nama_kat

  2. subkategori contain: id_subkat, id_kat, nama_subkat

  3. supersubkategori contain: id_supersub, id_subkat, id_kat, nama_supersub

My code is:

include "config.php";
$sql = mysql_query("SELECT * FROM kategori");
while($data=mysql_fetch_array($sql)) 
{
    echo "<li>".$data['nama_kat'].""; // kategori

    $sql2 = mysql_query("SELECT * FROM subkategori WHERE id_kat = '".$data['id_kat']."'"); // sub kategori
    if($sql2) 
    {
        echo "<ul>";
            while($data2=mysql_fetch_array($sql2)) 
            {
                echo "<li>".$data2['nama_sub']."</li>";
            }
        echo "</ul>";
    }
    echo "<li>";
        $sql3 = mysql_query("SELECT * FROM supersubkategori WHERE id_kat = '".$data['id_kat']."'"); // supersub kategori
        if($sql3) 
        {
            echo "<ul>";
                while($data3=mysql_fetch_array($sql3)) 
                {
                    echo "<li><a href='cat.php?kategori=$data3[nama_supersub]'>".$data3['nama_supersub']."</a></li>";
                }
            echo "</ul>";
        }

    else 
    {
        echo "</li>";
    }

}
?>

I really thankful if someone can help me how solve this problem. Thank you

Md. Sahadat Hossain
  • 3,210
  • 4
  • 32
  • 55
  • [Do not use mysql_* functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – Devon Bessemer Apr 26 '15 at 04:30
  • Use mysqli or PDO and then if you are still having trouble, re-post it. No point in fixing a problem with code that is going to need a major overhaul such as conversion to a new mysql api and prepared queries. Make sure you are more clear about the problem and any error messages. – Devon Bessemer Apr 26 '15 at 04:37
  • @Devon the problem is how make the Sealer and Wall Paint get indented after HOBE. There is no error, all i want it just how to make those get indented like HOBE after Painting. Maybe the screenshot that i've attached is already clear about the problem. I new here so i can't post the image directly here. – Azmi Cole Jr Apr 26 '15 at 04:41
  • To be honest - it can be achieved easily with `CSS` but you need to structure it right - "supersubcategory" should be in the "subcategory" loop. BTW, why not just having one `categories` table with a `parent_id` field? I mean - 3 tables, 3 queries - just to print categories...this cannot be a good practice. – Ofir Baruch Apr 26 '15 at 05:57
  • @OfirBaruch i want to make them easily to update/ delete (CRUD), i don't know how to update them from my admin panel (not from cpanel) if i just make 1 table with a parent_id. any suggestion sir? – Azmi Cole Jr Apr 26 '15 at 09:24
  • Every category should have an id, name, parent_id, etc. When you edit a category you fetch its current data from the table by its ID. In order to update the parent_id, just add a simple dropdown. – Ofir Baruch Apr 26 '15 at 09:33
  • i have a new problem with the parent_id sir @OfirBaruch. the link on http://stackoverflow.com/questions/29876848/remove-link-in-supersub-category-php-mysql. Would you help me for that problem. In this thread i think it's so complicated for crud. Thanks – Azmi Cole Jr Apr 26 '15 at 11:11

0 Answers0