簡體   English   中英

循環遍歷家庭,類別和子類別

[英]loop through family, category and sub-categories

我需要這個數組的幫助,我想做的是

  1. ILUMINATION
    1. LAMPS
  2. 家具
    1. 椅子
      1. 娛樂

我不能循環通過第二級但我不知道如何做到第三級,我認為它很容易但我能搞清楚。

這是我的頁面的鏈接,因為你可以看到我通過第一和第二級我想要回溯到第三級http://communita.com.mx/marca_producto.php?id=6

希望您能夠幫助我。 提前致謝。

$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'];

    }

這就是我如何追溯信息

<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>

要通過for循環訪問數組深度for您可以這樣做。 if語句用於確保在數組的一部分不具有相同深度的情況下不存在錯誤。 在這里查看在線演示

<?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
                    }
                }
            }
        }
    }

?>

要通過簡單的選擇來訪問它,請查看此示例:或者在此處查看演示

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

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

?>

這將為您帶來娛樂 您需要記住在0額外的深度,因為它仍然是您嘗試訪問的數組項。

要添加到多維arrays在此處按照此示例操作:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM