duanmianhong4893 2016-11-30 10:30
浏览 44

动态表与PHP pdo和两个foreach

So, I'm connected to a database and I'm pulling data from two different tables within same database. I'm running foreach to generate rows, but the table renders not the way I want. I want the 2nd foreach to create a 2nd column to the right of the first one and all those dynamic rows to go downward.

screenshot of the error

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <link   href="css/bootstrap.min.css" rel="stylesheet">
    <link   href="css/db.css" rel="stylesheet">
    <script src="js/bootstrap.min.js"></script>
</head>

<body>
    <div class="container">
            <div class="row text-center">
                <h3>Schedule of Ms. Kim & Ms. Morly</h3>
            </div>

<?php
  include 'database.php';
  $pdo = Database::connect();

    //Start of table Kim - ID=10
    $sql = 'SELECT * FROM kim ORDER BY id';
    $q = $pdo->prepare($sql);
        $q->execute(array($schd_tut_id));
        $data = $q->fetch(PDO::FETCH_ASSOC);

    $sql2 = 'SELECT tut_img FROM tbl_tutors_profile WHERE tut_profile_id=10 ';
        $q2 = $pdo->prepare($sql2);
        $q2->execute(array($tut_profile_id));
        $data2 = $q2->fetch(PDO::FETCH_ASSOC);
        $imgkim = $data2['tut_img'];
    //End of Table Kim

    //Start of table Morly - ID=16
    $sql3 = 'SELECT * FROM morly ORDER BY id';
    $q3 = $pdo->prepare($sql3);
    $q3->execute(array($schd_tut_id));
    $data3 = $q3->fetch(PDO::FETCH_ASSOC);

    $sql4 = 'SELECT tut_img FROM tbl_tutors_profile WHERE tut_profile_id=16 ';
    $q4 = $pdo->prepare($sql4);
    $q4->execute(array($tut_profile_id));
    $data4 = $q4->fetch(PDO::FETCH_ASSOC);
    $imgmorly = $data4['tut_img'];
    //End of table Morly
?>    


<table class="table table-striped table-bordered">
 <thead>
<th style="width:20px;">Time</th>
<th><img class="img-responsive" src="<?php  echo $imgkim  ?>"</th>
<th><img class="img-responsive" src="<?php  echo $imgmorly  ?>"</th>
<th><img class="img-responsive" src="<?php  echo $imgmorly  ?>"</th>


 </thead>

   <tbody>

<?php
    //tutor kim
    foreach ($pdo->query($sql) as $row ) {
        echo '<tr>';
        //echo '<td hidden>'. $row['id'] . '</td>';
        //echo '<td hidden>'. $row['schd_tut_id'] . '</td>';

        echo '<td>'. $row['time'] . '</td>';
        if($row['status']=='available')
          { echo '<td>'. '<a href="read.php?id='.$row['id'] . '&s_t_id='.$row['schd_tut_id']   .'">available</a>';} 
        else {echo '<td>'. $row['status'] . '</td>';}   
    }
    //echo '</tr>';
    //tutor morly
    foreach ($pdo->query($sql3) as $row ) {
        //echo '<tr>';

        //echo '<td hidden>'. $row['id'] . '</td>';
        //echo '<td hidden>'. $row['schd_tut_id'] . '</td>';
        echo '<td>'. $row['time'] . '</td>';
        if($row['status']=='available')
          { echo '<td>'. '<a href="read.php?id='.$row['id'] . '&s_t_id='.$row['schd_tut_id']   .'">available</a>';} 
        else {echo '<td>'. $row['status'] . '</td>';}   
        echo '</tr>';
    }
Database::disconnect();
?>
       </tbody>
    </table>

        </div>
    </div> <!-- /container -->
  </body>
</html>
  • 写回答

1条回答 默认 最新

  • dragonpeng200811111 2016-11-30 12:42
    关注

    Something like this should work, might need a bit of fiddeling.

    Basically just store the values in an array and then print the array.

    <?php
    
        $row1 = [];
        $row2 = []; 
    
        //table 1
        foreach ($pdo->query($sql) as $row ) {
           $row1[] = $row['status'];  
        }
    
        //table 2
        foreach ($pdo->query($sql2) as $row ) {
           $row2[] = $row['status']; 
        }
    
        echo "<table>";
        foreach ($row1 as $key => $row) {
            echo "<tr><td>" . $row[$key] . "</td><td>" . $row2[$key] . "</td></tr>";
        }
        echo "</table>";
    
    
    Database::disconnect();
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决