我试图在网页上显示数组中最近的项目,但我很困惑,我应该如何做到这一点。我不能改变任何查询,因为我仍然需要数组中的其他项目的其他东西。
我的查询
$arrWezen = array();
$query_wezen =
"SELECT
MythischeWezens.ID as idWezen,
MythischeWezens.naam as wezenNaam,
Afkomst.ID as idAfkomst,
Afkomst.Afkomst as Afkomst
FROM MythischeWezens
JOIN Afkomst
ON MythischeWezens.idAfkomst = Afkomst.ID";
$query_verhalen =
"SELECT
MythischeWezens.ID as idWezen,
MythischeWezens.naam as wezenNaam,
Verhaal.ID as idVerhaal,
Verhaal.Titel as Titel,
Verhaal.Verhaal as Verhaal
FROM LinkWezenVerhaal
JOIN MythischeWezens
ON LinkWezenVerhaal.idWezen = MythischeWezens.ID
JOIN Verhaal
ON LinkWezenVerhaal.idVerhaal = Verhaal.ID";
$query_traits =
"SELECT
MythischeWezens.ID as idWezen,
MythischeWezens.naam as wezenNaam,
Traits.ID as idTrait,
Traits.trait as Traits
FROM LinkWezenTraits
JOIN MythischeWezens
ON LinkWezenTraits.idWezen = MythischeWezens.ID
JOIN Traits
ON LinkWezenTraits.idTrait = Traits.ID";
$result_wezen = $conn->query($query_wezen) or die($conn -> error);
$result_verhalen = $conn->query($query_verhalen) or die($conn -> error);
$result_traits = $conn->query($query_traits) or die($conn -> error);
if($result_wezen->num_rows > 0){
while($row = $result_wezen->fetch_assoc()){
$arrWezen[$row['idWezen']] = array('wezenNaam' => $row['wezenNaam'],'Afkomst' => $row['Afkomst']);
}
}
if($result_verhalen ->num_rows > 0){
while($row = $result_verhalen ->fetch_assoc()){
$arrWezen[$row['idWezen']]['verhaal'][$row['idVerhaal']] = ['Titel' => $row['Titel'], 'Verhaal' => $row['Verhaal']];
}
}
if ($result_traits->num_rows > 0) {
while ($row = $result_traits->fetch_assoc()) {
$arrWezen[$row['idWezen']]['traits'][$row['idTrait']] = $row['Traits'];
}
}
?>
字符串
我的函数
<?php
include("getWezens.php");
function Wezen($arrWezen){
$returnstring = "";
foreach($arrWezen as $key => $val){
if($val['idWezen'] ==$key){
$returnstring .=
"<div class=\"col-md-4\">
<div class=\"card\">
<div class=\"card-body\">
<h5 class=\"card-title\">{$val['wezenNaam']}</h5>
<p class=\"card-text\">afkomst: {$val['Afkomst']}</p>
</div>
</div>
</div>";
}
}
return $returnstring;
}
?>
型
我的索引页
<?php
include("functions.php");
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
<title>Mythical creatures</title>
</head>
<body>
<!-- Navigation Bar -->
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container">
<a class="navbar-brand" href="#">Your Brand</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<a class="nav-link" href="#">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">About</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">
Dropdown
</a>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="#">Action</a></li>
<li><a class="dropdown-item" href="#">Another action</a></li>
<li><hr class="dropdown-divider"></li>
<li><a class="dropdown-item" href="#">Something else here</a></li>
</ul>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Contact</a>
</li>
</ul>
</div>
</div>
</nav>
<div class="container my-4">
<h1>Dorjee Website</h1>
<p>See a bunch of mythical creatures!</p>
<h2>Mythical Creatures</h2>
<?php print Wezen($arrWezen) ?>
</div>
<footer class="bg-dark text-light text-center py-3">
<p>© 2023 Your Website. All rights reserved.</p>
</footer>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
型
我试过在if函数中使用end()
,但它并没有把$key当作数组,也没有把$val['idWezen']
当作数组,它只是告诉我它的参数中需要一个数组。
1条答案
按热度按时间e4yzc0pl1#
你可以只按ID对原始数组进行排序,
字符串
就像这样:
型