ボグ標準検索を実行する SQL があり、コントローラー (INDEX.PHP) でこれらの結果を配列に格納します。抜粋を以下に示します。
INDEX.PHP 抽出
foreach ($s as $row)
{
$results[] = array(
'id' => $row['id'],
'name' => $row['name'],
'gender' => $row['gender'],
'county' => $row['county'],
'constituency' => $row['constituency'],
'qualifications' => $row['qualifications'],
'bio' => $row['bio'],
'monday' => $row['monday'],
'tuesday' => $row['tuesday'],
'wednesday' => $row['wednesday'],
'thursday' => $row['thursday'],
'friday' => $row['friday'],
'saturday' => $row['saturday'],
'sunday' => $row['sunday'],
'filename' => $row['filename'],
'mimetype' => $row['mimetype']
);
}
include 'profiles-small.html.php';
exit();
次に、「profiles.html.php」ページで、foreach ループを使用して次のように結果を出力します。
PROFILES-SMALL.PHP 抽出
<?php if (isset($results)): ?>
<?php foreach ($results as $result): ?>
<h1 class="margin-top">Search Results for <span class="red"><?php htmlout($result['constituency']); ?></span></h1>
<ol class="small-profile">
<img class="small-img" src="<?php if (!empty($result['filename'])) echo('?action=view&id=' . $result['id']); else echo "/img/profile_coming_soon.jpg" ?>" width="100" height="100" />
<li><?php htmlout($result['name']); ?></li>
<!--<li class="list-left2">Constituency:</li>-->
<li><?php htmlout($result['constituency']); ?></li>
<li><?php htmlout($result['qualifications']); ?></li>
</ol>
<form action="?" method="post">
<div id="buttons">
<input type="hidden" name="id" value="<?php htmlout($result['id']); ?>">
<a href="?more&id=<?php htmlout($result['id']); ?>">View full profile</a>
<!--<input type="submit" name="action" value="Edit" class="buttons">
<input type="submit" name="action" value="Delete" class="deletebutton">-->
</div>
</form>
<?php endforeach; ?>
<?php endif; ?>
上記の抜粋には、ページの上部に一度だけ表示する必要がある h1 タグがあります。
foreachループの中なので、結果ごとにこのh1タグも出力されることがわかっています。
問題は、おそらく非常に単純であることはわかっていますが、この値をループの上に出力する方法がわからないため、一度だけ表示されます。
値は $results という配列にあるので、次を使用できると思いました。
<?php htmlout($results['constituency']); ?>
これを見ると、$results 配列には複数の構成結果が含まれる可能性があるため、これは意味をなさないことがわかります。
いくつかの結果が返されたとしても、それらにはすべて同じ構成値が含まれているため、次のようなものを探していると思います。
- 一意の値を返すループ (これが存在するとは思わないでください)
- コントローラー (INDEX.PHP) で、この構成要素を出力する変数として格納する方法。
2 番目のオプションはおそらく進むべき道ですが、これには別の問題があります。
これまで学んできた PHP は、常に結果を配列に出力します。だから私は次のようなものがあります:
$sql = "SELECT * FROM pt";
$s = $pdo->query($sql);
次に、上記のように使用します。
foreach ($s as $row)
{
$results[] = array(.........
クエリの結果を変数に格納するにはどうすればよいですか? 例えば
$constituency = "SELECT name from constituency WHERE id = $constituencyid";
$s = $pdo->query($constituency);
$result = $s (Not allowed as this is a PDO object but hopefully you can see what I am thinking)
わかりました、これについて誰かが私に与えることができる助けに本当に感謝します.