[英]how to get data in single row in <td>, multiple entry in database with same id
table structure is like as: 表结构如下:
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
My query is 我的查询是
$query = "SELECT med_name, component_name FROM medicine left JOIN component ON medicine.id=component.medicine_id order by medicine.med_name";
PHP Code 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>';
}
with this code my html output is like: 使用此代码,我的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
but I want html output as: 但我想要html输出为:
medicine | component1 | component2 | component3 | component4
--------------------------------------------------------------
a | aaaa | bbbb | cccc |
b | abab | baba | |
c | zzzz | | |
What you're trying to do is just a bit more complicated that what you're doing. 您正在尝试做的事情比您正在做的事情复杂一些。 Your query seems fine.
您的查询似乎很好。 But after you fetch the results there's a little more work to do to determine how the table should be produced.
但是,在获取结果之后,还有更多工作要做,以确定应该如何生成表。 I would start by fetching the rows into a multidimensional array, where multiple component rows are stored under each medicine name, like this:
我将从将行提取到多维数组开始,在该数组中,每种药物名称下都存储有多个成分行,如下所示:
while ($row = $result->fetch_array()) {
$medicines[$row['med_name']][] = $row;
}
Then you'll need to find the maximum number of components for any of the medicines you fetched, so you can know how many columns your table should have. 然后,您需要找到所获取的任何药物的最大成分数,这样您才能知道表格应该有多少列。
$component_columns = max(array_map('count', $medicines));
Then you can iterate your array of medicines and output each one as a row with its components as additional columns. 然后,您可以迭代您的药品数组,并将每一种药品作为一行输出,其成分作为附加列。
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>';
}
By the way, it looks like you're using the medicine id in a link in your HTML, but if you want to add that, you'll need to be sure to include that in the selected columns in your query as well. 顺便说一句,看起来您在HTML链接中使用的是药品编号,但是,如果要添加该编号,则必须确保在查询的所选列中也包含该编号。
when i apply this pagination it break any random single row to double row. 当我应用此分页时,它会打破任何随机的单行到双行。 And its pagination is like"1|2|3|4|5|6|so on" not like "1|2|3|4|5|last" or "First|4|5|6|last".
并且其分页类似于“ 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.