![](/img/trans.png)
[英]Get data from <td> on the same table row specified by row id and perform computation
[英]how to get data in single row in <td>, multiple entry in database with same 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 | | |
您正在嘗試做的事情比您正在做的事情復雜一些。 您的查詢似乎很好。 但是,在獲取結果之后,還有更多工作要做,以確定應該如何生成表。 我將從將行提取到多維數組開始,在該數組中,每種葯物名稱下都存儲有多個成分行,如下所示:
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> </td>";
}
echo '<tr>';
}
順便說一句,看起來您在HTML鏈接中使用的是葯品編號,但是,如果要添加該編號,則必須確保在查詢的所選列中也包含該編號。
當我應用此分頁時,它會打破任何隨機的單行到雙行。 並且其分頁類似於“ 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) . '">«</a></li> ';
} else {
$page_links .= '<li class="disabled"><a href="#" >« </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) . '">»</a></li>';
} else {
$page_links .= '<li class="disabled"><a href="#" >»</a></li>';
}
return $page_links;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.