【问题标题】:Using loop to display all the patients under single doctor使用循环显示单个医生下的所有患者
【发布时间】:2015-08-25 02:39:52
【问题描述】:

这是我第一次来这里,我希望得到您的建议。我正在用 php 准备一个系统,注册患者可以与医生(也已注册)预约。到目前为止,我已经完成了我想要的,但现在我被卡住了。

在医生的个人资料页面上,我放置了一个“查看预约”页面(显示为该医生预约的预约)。我什至处理了医生看到他/她有“新约会”的通知(在mysql查询中使用count)。

现在,我的问题是,如果那个医生有多个预约怎么办?我想使用循环呈现数据,以便他/她可以在一个页面中一次看到所有数据。

我需要一些指导..

这里是后端..

<?php
require_once '../inc/connect.php'; //my database connection, which also holds the functions file
if(!$doctor->is_loggedin())
{
$doctor->redirect('index.php');
}
// Getting the doctor id
$dct_id = $_SESSION['dct_session'];

//If need, further actions can be done using this query
$stmt = $DB_con->prepare("SELECT * FROM dct WHERE dct_id=:dct_id");
$stmt->execute(array(":dct_id"=>$dct_id));
$userRow=$stmt->fetch(PDO::FETCH_ASSOC);

//this part is for viewing the appointments
$aptid=$_GET['apt_id'];
$stmt2=$DB_con->prepare("SELECT COUNT(dct_id) as aptid FROM apt WHERE dct_id=:aptid");
$stmt2->execute(array(":aptid"=>$aptid));
$aptRow=$stmt2->fetch(PDO::FETCH_ASSOC);
$newApt=$aptRow['aptid'];

if($newApt>0)
{ ?>

<div class="alert alert-info">You have <?php print ($newApt) ?> New Appointment! <br>
<button type="button" class="btn btn-info btn-sm" data-toggle="modal" data-target="#vwApt">View Appointment</button>
</div>

<?php }
else{ ?>

<div class="alert alert-warning">You have <?php print($newApt); ?> appointments!!</div>

<?php
}

?>

注意“模态”,我希望约会详细信息出现在模态框中。 我是一个初学者,使用互联网和一些教程,做了很多工作,但我真的需要指导。

数据库有“约会”表,其中包含“医生 ID”和“患者 ID”的外键,所以如果我需要从多个表中获取数据,我可以这样做。预约表包含患者姓名、患者详细信息、预约时间/日期和确认/拒绝字段。

我希望我能详细解释这个问题。

/* 已编辑 */

这是我最终用来获得所需内容的代码。我创建了函数并在我的视图中使用。

<?php
$apts=$this->db->prepare($query);
$apts -> execute();

if($apts->rowCount() > 0)
{
while($row=$apts->fetch(PDO::FETCH_ASSOC))
{
?>
<div class="modal-body">
<table class="table table-bordered table-responsive">
<tbody>
<tr>
<td><?php echo $row['ptnt_fname']; ?></td>
<td><?php echo $row['ptnt_ds']; ?></td>
<td><?php echo $row['apt_day']; ?></td>
<td><?php echo $row['apt_time']; ?></td>
<td><button type="button" name="confirm" class="btn btn-info btn-sm"></button></td>
</tr>
</tbody>
</table>
</div>
<?php
}
?>
<?php
}
?>

这就是我用来调用这个查询的部分

<?php
$query = "SELECT * FROM apt WHERE dct_id='$aptid'";
$doctor->viewApt($query);
?>

感谢大家的热心回复和指导。非常感谢。

【问题讨论】:

    标签: php mysql loops


    【解决方案1】:

    我们将以这张表为例,我们将获取属于供应商 1 的商品。

    $sth = $conn -> prepare("select * from items where vendor_id = 1");
    $sth -> execute();
    $results = $sth -> fetchAll();
    

    会产生这个

    +----+---------+---------------------+-----------+
    | id | name    | date                | vendor_id |
    +----+---------+---------------------+-----------+
    |  1 | apples  | 2015-08-19 11:27:27 |         1 |
    |  2 | ornages | 2015-08-19 11:27:36 |         1 |
    |  4 | bananas | 2015-08-19 11:27:41 |         1 |
    +----+---------+---------------------+-----------+
    

    获取每个所选供应商的商品数量

    echo count($results); // 3
    

    现在让我们循环遍历结果并显示项目名称。

    $html = '<ul>';
    foreach ($result as $key => $result) {
        $html .= '<li>'.$result['name'] .'</li>' ;
    }
    $html .='</ul>';
    

    输出应该是这样的

    . apples
    . ornages
    . bananas
    

    希望对您有所帮助。

    【讨论】:

    • 我一定要试试 :)
    • 我可能做错了,因为它不适合我。我猜.. echo $apt->time;这里的时间代表表中的列,对吧??
    • 我已经通过在我的“视图区域”中编写此代码来使其工作 prepare("SELECT * FROM apt WHERE dct_id='$newApt'"); $stmt2->执行(); if($stmt2->rowCount() > 0) { while($row=$stmt2->fetch(PDO::FETCH_ASSOC)) { echo $row['ptnt_fname']; } } ?> 我会尽力按照我的意愿去解决它,但如果我失败了,我肯定会再问你一次。如果您有更优雅的解决方案,请告诉我。另外,如果您能详细说明您给定的解决方案,我将不胜感激。
    • 我编写的代码从表格中获取患者姓名,而不是完全分开显示。您会看到,如果 2 位患者与同一位医生预约,则两位患者的姓名都显示在一个列中。否则,从表中选择 (all *) 以显示新约会,现在只显示 1。而且我迷失了我如何排序这个..
    • @Rafique,我用表格示例和代码更新了代码。
    【解决方案2】:

    使用

    $stmt2->execute();
    foreach(new TableRows(new RecursiveArrayIterator($stmt2->fetchAll())) as $k=>$v) { 
    echo $v;
    }
    

    将您的代码放入 try 块中。来这里之前用谷歌搜索一下 W3c select data向下滚动到底部 PDO 代码在那里

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-30
      • 1970-01-01
      • 2020-05-09
      • 1970-01-01
      • 1970-01-01
      • 2022-10-13
      • 1970-01-01
      相关资源
      最近更新 更多