简体   繁体   English

如何在单行中获取数据 <td> ,具有相同ID的数据库中的多个条目

[英]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>&nbsp;</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) . '">&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;
                }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从中获取数据 <td> 在由行ID指定的同一表行上执行计算 - Get data from <td> on the same table row specified by row id and perform computation 如何在 codeigniter 的单行中显示相同的 id 数据? - How to show same id data in single row in codeigniter? 在一个数据库行中插入多个数据 - Insert multiple data in one single database row PHP:如何从数据库获取多个具有相同ID的复选框值? - PHP: How to get multiple checkbox values from database for same ID? 如何在单行中保存多个图像数据以及如何获取它 - How to save multiple images data in single row and how to get it 如何在表的同一列的多行中从单一格式插入相同名称的多个数据 - How to insert multiple data of same name from single form in multiple row of same column of table 如何从 $row[id] 获取单个元素 - How to get single element from $row[id] 如何从具有多行多行的单个表单中插入多个同名数据 - How to insert multiple data of the same name from single form with multiple row of multiple form 从同一行的数据库中获取数据 - Get data from database in the same row 2个mysql表,单行链接到另一个表中的多个条目。 我怎样才能只获得单行一次而不是其他所有条目 - 2 mysql tables, a single row links to multiple entries in another table. how can I only get the single row once rather than for every other entry
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM