所以,我连接到一个数据库,我从同一个数据库中的两个不同的表中提取数据。我正在运行foreach来生成行,但表格不是我想要的方式。我希望第二个foreach在第一个的右边创建第二列,然后向下创建所有这些动态行。
<!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>
答案 0 :(得分:0)
这样的事情应该有用,可能需要一点点。
基本上只是将值存储在数组中,然后打印数组。
<?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();
?>