如何在php中获取单个列前面的所有数据

jdg4fx2g  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(304)

获取患者数据时,我的代码有问题。我要把病人的所有资料放在病人名字前面。例如,我有5行患者,我希望患者在下一列中获取患者姓名和所有时间就诊。

________________________________________
| Name |  Visits Date  |  Product      |
----------------------------------------
| Ali  |  1 May        | test          |
|      |  2 May        | test 2        |
|      |  3 May        | test 3        |
----------------------------------------

这是我的代码,但不工作,只显示最后一列。

<table class="table" border="1" id="data-table5">
              <thead>
              <tr>
                <th>Name</th>
                <th>Date</th>
                <th>Product</th>
              </tr>
              </thead>
              <tbody>
              <?php  
              //Fetching Data From DB (Sale)
              $queryex = "SELECT * FROM `dep_sale` GROUP BY `c_name`";
              $result2 = mysqli_query($link, $queryex);
               while ($row = mysqli_fetch_array($result2)) { ?>  
                <tr>
                  <td><?php echo $row['c_name']; ?></td>
                  <td><?php echo $row['date']; ?></td>
                  <td><?php echo $row['frame']; echo ",", $row['size']; echo ",", $row['lense']; ?></td>
                </tr>

              <?php } ?> 
              </tbody>
            </table>

在这种情况下请帮助我。谢谢

insrf1ej

insrf1ej1#

我想你可以用它:

$queryex = "SELECT `c_name` * FROM `dep_sale` GROUP BY `c_name`";
gab6jxml

gab6jxml2#

你应该使用排序方式而不是分组方式。如果使用group by,则每个“cname”只能有一条记录

<table class="table" border="1" id="data-table5">
  <thead>
  <tr>
    <th>Name</th>
    <th>Date</th>
    <th>Product</th>
  </tr>
  </thead>
  <tbody>
<?php 
//Fetching Data From DB (Sale)
$queryex = "SELECT * FROM `dep_sale` ORDER BY `c_name` ASC";
$result2 = mysqli_query($link, $queryex);
$cname = '';
while ($row = mysqli_fetch_array($result2)) {
    // compare values of $row['cname'] with $cname;
    $name = ($row['cname'] != $cname)? $row['cname'] : '';
?>
    <tr>
      <td><?php echo $name; ?></td>
      <td><?php echo $row['visit_date']; ?></td>
    </tr>
<?php
    // update $cname value with $row['cname'] within the while loop
    $cname = $row['cname'];
}
?>

样本输出:

<tr>
  <td>Ali</td>
  <td>1 May</td>
</tr>
<tr>
  <td></td>
  <td>2 May</td>
</tr>
<tr>
  <td></td>
  <td>3 May</td>
</tr>
<tr>
  <td></td>
  <td>4 May</td>
</tr>
<tr>
  <td>peter</td>
  <td>1 May</td>
</tr>
<tr>
  <td></td>
  <td>2 May</td>
</tr>
<tr>
  <td></td>
  <td>3 May</td>
</tr>
<tr>
  <td></td>
  <td>4 May</td>
</tr>

运行原始查询

"SELECT * FROM `dep_sale` GROUP BY `cname`"
"SELECT * FROM `dep_sale` GROUP BY `cname` ASC"
"SELECT * FROM `dep_sale` GROUP BY `cname` DESC"

分别用phpmyadmin观察不同的结果

相关问题