2017-07-19 35 views
0

表結構是一樣的:如何讓​​單列數據,用相同的ID數據庫的多次入境

medicine table     component table 
id | name      id | medicine_id | component_name 
----------------    ----------------------------------- 
1 | a       1 |  1  |  aaaa 
2 | b       2 |  1  |  bbbb 
3 | b       3 |  1  |  cccc 
            4 |  2  |  abab 
            5 |  2  |  baba 
            6 |  3  |  zzzz 

我的查詢是

$query = "SELECT med_name, component_name FROM medicine left JOIN component ON medicine.id=component.medicine_id order by medicine.med_name"; 

PHP代碼

$result = $mysqli -> query($query); 

while ($row = $result -> fetch_array()) { 

echo '<tr><td>' . $row['med_name'] . '</td><td>' . $row['component_name'] . '</td><td>' . $row['component_name'] . '</td><td>' . $row['component_name'] . '</td><td>' . $row['component_name'] . '</td><td><a href="edit.php?id=' . $row['id'] . '"><i class="fa fa-edit text-success text-active"></i> Edit</a></td></tr>'; 

} 

與此代碼我的HTML輸出是這樣的:

medicine | component1 | component2 | component3 | component4 
------------------------------------------------------------- 
    a  | aaaa | aaaa | aaaa | aaaa 
    a  | bbbb | bbbb | bbbb | bbbb 
    a  | cccc | cccc | cccc | cccc 
    b  | abab | abab | abab | abab 
    b  | baba | baba | baba | baba 
    c  | zzzz | zzzz | zzzz | zzzz 

但我希望HTML輸出:

medicine | component1 | component2 | component3 | component4 
-------------------------------------------------------------- 
    a  | aaaa | bbbb | cccc | 
    b  | abab | baba |   | 
    c  | zzzz |   |   | 
+0

在您的餐桌結構中,您的id爲2&3,其名稱爲'b'。這是一個錯誤,你的意思是#3是'c'? –

+0

對不起!它寫錯了 – Learner

回答

0

什麼你想要做的是更復雜的只是有點你在做什麼。你的查詢似乎很好。但是在獲取結果之後,還需要做更多的工作來確定如何生成表格。我會通過獲取的行成一個多維數組,在多個部件的行存儲在每個藥品的名稱,像這樣開始:

while ($row = $result->fetch_array()) { 
    $medicines[$row['med_name']][] = $row; 
} 

然後,你需要找到組件的最大數量對於任何藥物你提取了,所以你可以知道你的表應該有多少個列。

$component_columns = max(array_map('count', $medicines)); 

然後,您可以迭代您的藥物數組,並將每個藥物作爲一行連同其組件一起作爲附加列輸出。

foreach ($medicines as $medicine => $components) { 

    // start the row and echo the medicine name 
    echo "<tr><td>$medicine</td>"; 

    // echo the components, keeping track of how many you've done so far 
    $i = 0; 
    foreach ($components as $component) { 
     echo "<td>$component[component_name]</td>"; 
     $i++; 
    } 

    // then echo empty cells to fill the rest of the row 
    for (; $i < $component_columns; $i++) { 
     echo "<td>&nbsp;</td>"; 
    } 
    echo '<tr>'; 

} 

順便說一句,它看起來像你正在使用的藥品ID在你的HTML鏈接,但如果你想補充一點,你需要一定要包括在選定列在你的查詢中也是如此。

+0

thanku它的工作 – Learner

+0

你能告訴我在下面的分頁代碼中的錯誤 – Learner

0

當我應用這個分頁它打破任何隨機單行到雙行。 其分頁類似於「1 | 2 | 3 | 4 | 5 | 6 |等等」不像「1 | 2 | 3 | 4 | 5 | last」或「First | 4 | 5 | 6 | last 」。

function generate_page_links($cur_page, $num_pages) { 
        $page_links = ''; 

        // If this page is not the first page, generate the "previous" link 
        if ($cur_page > 1) { 
         $page_links .= '<li><a href="' . $_SERVER['PHP_SELF'] . '?page=' . ($cur_page - 1) . '">&laquo;</a></li> '; 
        } else { 
         $page_links .= '<li class="disabled"><a href="#" >&laquo; </a></li>'; 
        } 

        // Loop through the pages generating the page number links 
        for ($i = 1; $i <= $num_pages; $i++) { 
         if ($cur_page == $i) { 
          $page_links .= ' <li class="active"><a href="#" >' . $i . '</a></li>'; 
         } else { 
          $page_links .= ' <li><a href="' . $_SERVER['PHP_SELF'] . '?page=' . $i . '"> ' . $i . '</a></li>'; 
         } 
        } 

        // If this page is not the last page, generate the "next" link 
        if ($cur_page < $num_pages) { 
         $page_links .= ' <li><a href="' . $_SERVER['PHP_SELF'] . '?page=' . ($cur_page + 1) . '">&raquo;</a></li>'; 
        } else { 
         $page_links .= '<li class="disabled"><a href="#" >&raquo;</a></li>'; 
        } 

        return $page_links; 
       } 
相關問題