2015-11-03 58 views
0

我只是一個業餘的腳本編碼。我需要幫助從有人在這裏.. 我有麻煩在這裏,我試圖表明2條件從我的代碼合併,看看這裏,請...從2查詢合併2條件數組1

<ul class="nav navbar-nav"> 
 
    <?php    
 
    $main=mysql_query("SELECT * FROM mainmenu WHERE aktif='Y'"); 
 
    while($r=mysql_fetch_array($main)){ 
 
    $t=$r['']; 
 
    $tm="<a href='$r[link]' class='dropdown-toggle' data-toggle='dropdown' role='button' aria-expanded='false'>$r[nama_menu]<span class='caret'></span></a>"; 
 
    $th="<a href='$r[link]'>$r[nama_menu]</a>"; 
 
    if ($t!= ""){ 
 
    $tombol=$th; 
 
    }else{ 
 
    $tombol=$tm; 
 
    } 
 
    echo "<li class='dropdown'>$tombol 
 
     <ul class='dropdown-menu' role='menu'>"; 
 
    \t $sub=mysql_query("SELECT * FROM submenu, mainmenu 
 
     WHERE submenu.id_main=mainmenu.id_main 
 
     AND submenu.id_main=$r[id_main]"); 
 
    \t while($w=mysql_fetch_array($sub)){ 
 
    echo " <li><a href='$w[link_sub]'>$w[nama_sub]</a></li>"; 
 
    \t } 
 
    echo "</ul></li>";} 
 
    ?> 
 
</ul>

我已經把它分解以嘗試在任何條件下展示他們,而這更相關,但我不能合併它,我不知道我有什麼做,請看這個請

這是第一個條件 - >

<ul class="nav navbar-nav"> 
 
<?php    
 
    $main=mysql_query("SELECT DISTINCT a.* FROM mainmenu a 
 
    INNER JOIN submenu b ON a.id_main = b.id_main AND a.aktif = 'Y'"); 
 
    while($r=mysql_fetch_array($main)){ 
 
    echo "<li class='dropdown'><a href='$r[link]' class='dropdown-toggle' data-toggle='dropdown' role='button' aria-expanded='false'>$r[nama_menu]<span class='caret'></span></a><ul class='dropdown-menu' role='menu'>"; 
 
    $sub=mysql_query("SELECT * FROM submenu, mainmenu 
 
    WHERE submenu.id_main=mainmenu.id_main 
 
    AND submenu.id_main=$r[id_main]"); 
 
    while($w=mysql_fetch_array($sub)){ 
 
    echo " <li><a href='$w[link_sub]'>$w[nama_sub]</a></li>"; 
 
    } 
 
    echo "</ul></li>";} 
 
?> 
 
</ul>

而且Thise第二個條件 - >

<ul class="nav navbar-nav"> 
 
<?php  
 
    $menu=mysql_query("SELECT DISTINCT a.* FROM mainmenu a 
 
    LEFT OUTER JOIN submenu b ON a.id_main = b.id_main WHERE b.id_main is null AND a.aktif = 'Y'"); 
 
    while($s=mysql_fetch_array($menu)){ 
 
    echo "<li class='dropdown'><a href='$s[link]'>$s[nama_menu]</a></li>";} ?> 
 
</ul>

我想什麼時候合併內與LEFT OUTER JOIN JOIN是:

if (bla, bla, bla){ 
 
echo "Show INNER JOIN"; 
 
}else{ 
 
echo "Show LEFT OUTER JOIN"; 
 
}

回答

0

也許有人需要這樣的東西,當他會做出不同的菜單,當他們的菜單有一些菜單子菜單.... 終於我找到了這樣的解決方案。

而這是腳本:

<ul class="nav navbar-nav"> 
 
      <?php    
 
     $main=mysql_query("SELECT * FROM mainmenu WHERE aktif='Y'"); 
 
\t \t while($r=mysql_fetch_array($main)){ 
 
\t \t $main2=mysql_query("SELECT id_main FROM submenu WHERE submenu.id_main=$r[id_main] "); 
 
\t \t ($s=mysql_fetch_array($main2)); 
 
\t \t if ($r['id_main'] == $s['id_main']){ 
 
     $tombol="<a href='$r[link]' class='dropdown-toggle' data-toggle='dropdown' role='button' aria-expanded='false'>$r[nama_menu]<span class='caret'></span></a>"; 
 
     }else{ 
 
     $tombol="<a href='$r[link]'>$r[nama_menu]</a>"; 
 
     } 
 
\t   echo "<li class='dropdown'>$tombol 
 
        <ul class='dropdown-menu' role='menu'>"; 
 
\t   $sub=mysql_query("SELECT * FROM submenu, mainmenu 
 
          WHERE submenu.id_main=mainmenu.id_main 
 
          AND submenu.id_main=$r[id_main]"); 
 
\t   while($w=mysql_fetch_array($sub)){ 
 
       echo " <li><a href='$w[link_sub]'>$w[nama_sub]</a></li>"; 
 
\t   } 
 
\t  echo "</ul> 
 
      </li>";} ?> 
 
</ul>

0

通常當兩個查詢使用相同的表,它是使用OR加入兩組條件一件簡單的事情。但是在這種情況下,一個查詢有JOIN,另一個查詢有LEFT OUTER JOIN

我認爲你可以使用UNION這樣的:

SELECT DISTINCT a.* 
FROM mainmenu a 
INNER JOIN submenu b ON a.id_main = b.id_main AND a.aktif = 'Y' 
UNION 
SELECT DISTINCT a.* 
FROM mainmenu a 
LEFT OUTER JOIN submenu b ON a.id_main = b.id_main 
WHERE b.id_main is null AND a.aktif = 'Y' 

也許你可以rewite它:

SELECT DISTINCT a.* 
FROM mainmenu a 
LEFT OUTER JOIN submenu b ON a.id_main = b.id_main OR b.id_main is null 
WHERE a.aktif = 'Y' 

無採樣數據&結果我無法測試並確認你。

+0

我想我需要陣列的2結果打印INNER JOIN和LEFT OUTER JOIN在不同 INNER有另一個

,和外部沒有..但是,謝謝你的答案..我會嘗試在我的本地.. –