Hello, i want to make a categories menu which each will have its own sub-categories and that category will have own sub-categories im trying to make with arrays but i'm not really good at it.. im trying right now with bunch if statements but is there any other method of doing this with loops or something eazier ?

<form action="" method="POST">
        <?php
            $category = array("Cars" => 1, "Bikes" => 2, "Phones" => 3);
            $cars_category = array("Honda" => 1, "BMW" => 2, "Mercedes" => 3);
            $bikes_category = array("Suzuki" => 1, "GSXR" => 2, "Yamaha" => 3);
            $phones_category = array("Samsung" => 1, "iPhone" => 2, "LG" => 3);

            foreach($category as $main_cat => $value) {
                ?>
                    <input type="submit" name="main_category[<?= $value ?>]" value="<?= $main_cat ?>" /><br>
                <?php
            }

            if(isset($_POST["main_category"])) {
                echo "<br>";
                $categ = $_POST["main_category"];

                foreach($categ as $value => $name) {

                    if($value == 1) {
                        foreach($cars_category as $cars => $value) {
                            ?>
                                <input type="submit" name="sub_category[<?= $value ?>]" value="<?= $cars ?>" /><br>
                            <?php
                        }
                    } else if($value == 2) {
                        foreach($bikes_category as $bikes => $value) {
                            ?>
                                <input type="submit" name="sub_category[<?= $value ?>]" value="<?= $bikes ?>" /><br>
                            <?php
                        }
                    } else if($value == 3) {
                        foreach($phones_category as $phones => $value) {
                            ?>
                                <input type="submit" name="sub_category[<?= $value ?>]" value="<?= $phones ?>" /><br>
                            <?php
                        }
                    }
                }
            }
        ?>
        </form>

Make DB table for categories and another table for brends with foreigh keys constraints referenced to categories and another table for models with constraints referenced to brends

Is this possible to make without database, i have something in mind which i want to achive.
I want to make a categories tree which user can choose final product of the categories and when he chooses to get that final product its own sub categories for ex.

Vehicles > Cars > BWM > M > M2 Coupe

Make multidimensional array eg

$categories = array(
    'cars' => array(
        'BMW' => array(
            'X1',
            'X3',
            'X5',
            'X6'
        ),
        'AUDI' => array(
            'A3',
            'A6'
        ),
        'MERCEDES'
    ),
    'moto' => array(
        'SUZUKI',
        'HONDA',
        'YAMAHA'
    ),
    'phones' => array(
        'SAMSUNG',
        'LG',
        'MOTOROLA'
    )
);
// etc

You can write data to ini file if it's much readable for you and build array with parse_ini_file() http://php.net/manual/en/function.parse-ini-file.php

commented: How much sub-arrays i can have ? +2

PHP arrays unlimited levels but ini file seems 2 levels only

Member Avatar for diafol

I realise this is recently solved. INI files ok, but you can also store data in something like json files. These have the advantage of being readable and writable, pretty much into and out of array format. Anyhow, here's my 2p:

//READ
$out  = file_get_contents('myfile.json');
$myArray = json_decode($out, true); //true if you want asscoiated array - false if you want object

//WRITE
$in  = json_encode($myArray);
file_put_contents('myfile.json', $in);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.