简体   繁体   中英

loop through family, category and sub-categories

i need help with this array, what i want to do is

  1. ILUMINATION
    1. LAMPS
      1. Desk
      2. Wall
  2. Furniture
    1. chairs
    2. tables
      1. entertainment

i cant loop through second level but i dont know how to do it for third level, i think its pretty easy but i cang figure out.

Here is the link to my page as you can see i go throug 1st and 2d level i would like to retrive also a 3rd level http://communita.com.mx/marca_producto.php?id=6

Hope you can help me. Thanks in advance.

$qry = "
    SELECT nomPadre,pp.idPadre, pathImgPadre,c.idCategoria,nomCategoria
    FROM productos p
    LEFT JOIN productos_categoria pc ON pc.idProducto = p.idProducto
    LEFT JOIN productos_padre pp ON pp.idProducto = p.idProducto
    LEFT JOIN categorias c ON c.idCategoria = pc.idCategoria
    LEFT JOIN padre pa ON pa.idPadre = pp.idPadre
    LEFT JOIN imagenesPadre i ON i.idPadrePa = pp.idPadre
    WHERE marProducto = :idMarca AND i.idMarcaPa = :idMarca AND c.catPadre = pp.idPadre";
    $stmt = $db->prepare($qry);
    $stmt->execute(array(':idMarca' => $_GET['id']));

while ($row=$stmt->fetch()) {
        //  usa el idPadre como key para agrupar el arreglo

        // these two values will just overwrite with the same thing if they're repeated
        $padres[$row['idPadre']]['nomPadre'] = $row['nomPadre'];
        $padres[$row['idPadre']]['idPadre'] = $row['idPadre'];
        $padres[$row['idPadre']]['idMarca'] = $row['idMarca'];
        $padres[$row['idPadre']]['pathImgPadre'] = $row['pathImgPadre'];
        $padres[$row['idPadre']]['categoria'][$row['idCategoria']] = $row['nomCategoria'];

    }

and this is how i retrive info

<ul class="grid cs-style-4 col-xs-12 col-sm-12 col-md-12 text-center">
        <?php foreach ($padres as $padre): ?>
            <li class="col-md-3 col-lg-3">
                <figure>
                    <div class="">
          <img src="<?= $padre['pathImgPadre']?>" alt="<?= $padre['nomPadre']?>">
        </div>
                    <figcaption class="subcate">
                        <?php foreach ($padre['categoria'] as $id => $categoria): ?>

                        <a href="cateXmarca.php?ma=<?= $_GET['id']?>&pa=<?= $padre['idPadre']?>&ca=<?=$id?>"><h6>•&nbsp<?= $categoria ?></h6></a><br>
                        <?php endforeach ?>
                    </figcaption>
                </figure>
                <div class="col-xs-12 col-sm-12 col-md-12 text-center">
                    <span class="caption"><?= $padre['nomPadre']?></span>
                </div>
            </li>
            <?php endforeach ?>
        </ul>

To acces your array depths through for loops you can do it like this. The if statements are there to secure that there are no erros exisiting in the case a part of the array does not have the same depth. See an online DEMO here

<?php
    $arr  = array(
        "ILUMINATION" => array(
                "LAMPS" => array(
                        "desk",
                        "wall"
                    )
            ),
        "Furniture" => array(
                "chairs",
                "tables" => array(
                    "entertaiment"
                    )
            )
    );

    foreach($arr as $catagory){
        //first layer, will output: ILUMINATION, Furniture
        if(is_array($catagory)){
            foreach($catagory as $sub_catagory){
                //second layer, will ouput: LAMPS, chairs, tables
                if(is_array($sub_catagory)){
                    foreach($sub_catagory as $item){
                        //third layer, will output: desk, wall, entertaiment
                    }
                }
            }
        }
    }

?>

To acces it through simple selecting, look at this example: Or see the DEMO here

<?php
    $arr  = array(
        "ILUMINATION" => array(
                "LAMPS" => array(
                        "desk",
                        "wall"
                    )
            ),
        "Furniture" => array(
                "chairs",
                "tables" => array(
                    "entertaiment"
                    )
            )
    );

    echo $arr["Furniture"]["tables"][0];

?>

This will give you entertaiment as a result. You need to remember to put an extra depth in there with the 0 as it still is an array item you are trying to acces.

To add to multidimensional arrays follow this example right here:

$arr["Furniture"]["sofas"] = array("leather", "fabric");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM