2017-07-19 45 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; 
       } 
相关问题