0
我目前正在創建一個菜單項,並且大部分它工作正常,但所提取的信息對於二級子菜單是不正確的(字母)。我已經嘗試了很多不同的連接,但只是拉我的一個子菜單項或全部。不確定我現在需要做些什麼來改變它的工作方式,而不確定如何更好地解釋這個問題。如何從兩個表中匹配來自表的外鍵與另一個主鍵的查詢
至於我表
menu : columns id, name
people : id, people_name, href, menu_id
letters : id, letters_names, href, people_id
我的代碼是在這裏:
<?php
try{
$pdo = new PDO("mysql:host=localhost;dbname=menu",'root','');
} catch (PDOException $ex) {
echo $ex->getMessag();
}
$sql = "SELECT * FROM menu ORDER BY id";
$stmt = $pdo->prepare($sql);
$stmt->execute();
?>
<!DOCTYPE html>
<html>
<head>
<meta name="author" content="Kayla Lindstrom">
<title>Lindstrom Letters</title>
<!-- <link rel="stylesheet" type="text/css" href="style.css">-->
</head>
<body>
<div id="page">
<ul>
<?php while ($row = $stmt->fetch(PDO::FETCH_OBJ)) {
$sub_sql = "SELECT * FROM people WHERE menu_id=:id";
$sub_stmt = $pdo->prepare($sub_sql);
$sub_stmt->bindParam(':id', $row->id,PDO::PARAM_INT);
$sub_stmt->execute();
?>
<li><a href=""><?php echo $row->name; ?></a>
<?php if($sub_stmt->rowCount()){ ?>
<ul>
<?php while($sub_row = $sub_stmt->fetch(PDO::FETCH_OBJ)) {
$sub2_sql = "SELECT * FROM letters WHERE people_id=:id"; /*Here is where I am stuck.*/
$sub2_stmt = $pdo->prepare($sub2_sql);
$sub2_stmt->bindParam(':id', $row->id,PDO::PARAM_INT);/*and here*/
$sub2_stmt->execute(); ?>
<li><a href="<?php echo $sub_row->href; ?>">
<?php echo $sub_row->people_name;?></a>
<?php if($sub2_stmt->rowCount()){ ?>
<ul>
<?php while($sub2_row = $sub2_stmt->fetch(PDO::FETCH_OBJ)) { ?>
<li><a href="<?php echo $sub2_row->href; ?>">
<?php echo $sub2_row->letters_name;?></a></li>
<?php } ?>
</ul>
<?php } ?>
</li>
<?php } ?>
</ul>
<?php } ?>
</li>
<?php } ?>
</ul>
</div>
</body>
</html>
> _ <總是一次我偶然忽略的小事情。謝謝!有時候我只需要另一雙眼睛就能看到我失蹤的東西! – kll