2016-12-06 124 views
1

我需要這陣幫助,我想做的事是循環通過家庭,類別和子類別

  1. ILUMINATION
    1. LAMPS
  2. 傢俱
    1. 椅子
      1. 娛樂

我不能通過第二級循環,但我不知道該怎麼做了第三級,我認爲它很容易,但我弄不明白。

這裏是鏈接到我的網頁,你可以看到我去通過量1和2D層面我想還以檢索一個3級 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']; 

    } 

,這是我怎麼retrive信息

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

回答

0

要通過for循環,你可以做這樣的進入您的陣列深處。這裏的if語句用於確保在數組的一部分不具有相同深度的情況下不存在erros存在。看到網上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 
        } 
       } 
      } 
     } 
    } 

?> 

要通過簡單的選擇存取權限呢,看看下面這個例子:還是看DEMO here

<?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"); 
+0

我有這個想法,但我不知道怎麼做了這樣 $教士[$行[「idPadre」] [」我的意思是這樣的我嘗試,但我貓得到正確執行 $ padres [$ row ['idPadre']]我想表示如何將數組添加到這個第二級別的數組中。 ['categoria'] [$ row ['idCategoria'] [$ row] [idSubcate]] = $ row ['nomCategoria']; – goeknax

+0

@goeknax這是否回答你的問題?這將在「sofas」鍵下方的'傢俱'中添加一個新的'數組'。 – Nytrix

+0

我這麼認爲... 所以它會像這樣的somtehing? $ padres [$ row ['idPadre']] ['idCategoria'] = $ row ['iSubcate']; ?? 感謝您花時間回答我。 – goeknax