2015-08-08 160 views
0

我有一個顯示我的數據庫記錄的問題,因爲我使用中間表。但我會試着解釋一下。 我在我的數據庫表的旁邊:顯示來自數據庫的結果

  • ID,NAAM
  • 商店ID,NAAM,TYPE_ID ...並不重要)
  • 產品id,naam,...不重要)
  • product_shop_ttID,PRODUCT_ID,shop_id) - 這是我的中間表 產品之間的連接商店
  • 訂單ID,USER_ID,狀態
  • ORDER_DETAILSID,ORDER_ID, USER_ID,product_shop_tt_id
  • 用戶ID,NAAM,並不重要

現在我有下面的代碼:

<?php 

$rezKor = mysqli_query($kon, "SELECT * FROM korisnici WHERE email = '". $_COOKIE["korisnik"] ."' LIMIT 1"); 
$redKor = mysqli_fetch_assoc($rezKor); 
$user_id = $redKor["id"]; 

$rezOrders = mysqli_query($kon, "SELECT * FROM orders WHERE user_id = ". $user_id ." AND status = 0 ORDER BY id DESC LIMIT 1"); 
$redOrders = mysqli_fetch_assoc($rezOrders); 
$brRez = mysqli_num_rows($rezOrders); 

$rezOrdDetails = mysqli_query($kon, "SELECT * FROM order_details WHERE order_id = ". $redOrders["id"] .""); 

while($redOrdDetails = mysqli_fetch_assoc($rezOrdDetails)){ 
    $rezTT = mysqli_query($kon, "SELECT * FROM product_shop_tt WHERE id = ". $redOrdDetails["product_shop_tt_id"] .""); 
    $redTT = mysqli_fetch_assoc($rezTT); 
    $brTT = mysqli_num_rows($rezTT); 

    $i = 0; 
    $rezShop = mysqli_query($kon, "SELECT * FROM shops WHERE id = ". $redTT["shop_id"] .""); 
    while($redShop = mysqli_fetch_assoc($rezShop)){ 
     if($i == 0){ 
      echo $redShop["naam"] . "<br />"; 
     } 
     $i++; 
     $rezProdukt = mysqli_query($kon, "SELECT * FROM producten WHERE id = ". $redTT["product_id"] .""); 
     while($redProduct = mysqli_fetch_assoc($rezProdukt)){ 
      echo "<br />Ime produkta : " . $redProduct["naam"] . "<br />"; 
     } 
    } 
} 

echo "<div style=\"clear:both;\"></div><div class=\"footer\" style=\"position: fixed;bottom: 0;width: 100%;left:0;\"> 
    <a href=\"home.php\" title=\"Ga terug\" class=\"col-xs-6 col-sm-6 btn btn-info\"><span class=\"glyphicon glyphicon-chevron-left\"></span> Niets toevoegen</a> 
    <button class=\"col-xs-6 col-sm-6 btn btn-danger\" type=\"submit\" name=\"btnNaruci\" id=\"btnNaruci\"> 
     Leg in winkelmand <span class=\"glyphicon glyphicon-chevron-right\"></span><span class=\"glyphicon glyphicon-chevron-right\"></span><span class=\"glyphicon glyphicon-chevron-right\"></span> 
    </button> 
</div>"; 

?> 

我想下一個輸出:

<h1>Type of the shops</h1><br /> 
 
<h3>The name of the first shop</h3> 
 
<ul> 
 
    <li>Product from the first shop</li> 
 
    <li>Product from the first shop</li> 
 
</ul><br /> 
 
<h3>The name of the second shop</h3> 
 
<ul> 
 
    <li>Product from the second shop</li> 
 
    <li>Product from the second shop</li> 
 
</ul><br /><br /> 
 

 
<h1>Type of the shops</h1><br /> 
 
<h3>The name of the next shop</h3> 
 
<ul> 
 
    <li>Product from shop</li> 
 
    <li>Product from shop</li> 
 
</ul><br /> 
 
<h3>The name of the new shop</h3> 
 
<ul> 
 
    <li>Product from the new shop</li> 
 
    <li>Product from the new shop</li> 
 
</ul>

但是我的代碼我得到下一個:

<div>Type of the shops</div> 
 
<div>The name of the first shop</div> 
 
<div>Product from the first shop</div><br /> 
 

 
<div>Type of the shops</div> 
 
<div>The name of the first shop</div> 
 
<div>Product from the first shop</div><br /> 
 

 
<div>Type of the shops</div> 
 
<div>The name of the first shop</div> 
 
<div>Product from the first shop</div><br />

因此,我想顯示從同一商店的所有產品低音商店的名稱。現在我得到每個產品的商店名稱和商店的類型。

我希望我能解釋得很好。

在此先感謝您的幫助。

+0

這段代碼可以通過使用'JOIN'語句來減少。但是,如果您想保留當前的格式,則需要存儲您的商店名稱,即。 '$ redShop [「naam」]'給一個變量,只有當它不是當前值時纔會回顯。例如 - '$ currentShop =''; ... if($ redShop [「naam」]!= $ currentShop){echo $ redShop [「naam」]; $ currentShop = $ redShop [「naam」];}'。 – Sean

+0

感謝您的回覆。我要去嘗試一下。 – Boky

回答

1

感謝@Sean。

<?php 

    $rezOrders = mysqli_query($kon, "SELECT * FROM orders WHERE user_id = ". $user_id ." AND status = 0 ORDER BY id DESC LIMIT 1"); 
    $redOrders = mysqli_fetch_assoc($rezOrders); 


    $rez = mysqli_query($kon, "SELECT product_shop_tt.*, shops.*, shops.naam as shopNaam, producten.*, producten.naam as prodNaam, order_details.*, type.*, type.naam as typeNaam 
           FROM order_details INNER JOIN product_shop_tt ON order_details.product_shop_tt_id = product_shop_tt.id 
           INNER JOIN shops ON shops.id = product_shop_tt.shop_id 
           INNER JOIN producten ON producten.id = product_shop_tt.product_id 
           INNER JOIN type ON type.id = shops.type_id 
           WHERE order_id = ". $redOrders["id"] .""); 

    $currentType = ""; 
    $currentShop = ""; 
    while($red = mysqli_fetch_assoc($rez)){ 
     if ($red["typeNaam"] != $currentType){ 
       echo "Type : " . $red["typeNaam"] . "<br />"; 
       $currentType = $red["typeNaam"]; 
      } 
     if ($red["shopNaam"] != $currentShop){ 
       echo "Shop : " . $red["shopNaam"] . "<br />"; 
       $currentShop = $red["shopNaam"]; 
      } 
       echo "Product : " . $red["prodNaam"] . "<br />"; 
     } 
?> 

這是做我所需要的代碼。

非常感謝@Sean!歡呼聲