0

ボグ標準検索を実行する 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)

わかりました、これについて誰かが私に与えることができる助けに本当に感謝します.

4

2 に答える 2

0

以下の関数を使用して、constituency

$constituencies  = array_map(function($val){
  return $val['constituency'];
}, $results);

以下のコードを使用constituencyして変数を取得します

$sql = "SELECT name from constituency WHERE id = ".$pdo->quote($constituencyid);
$stmt = $pdo->query($sql);
$row  = $stmt->fetch();
$constituency = $row['name'];
于 2012-09-29T14:31:48.233 に答える
0

Actually, there is a PHP function called array_unique() you should have a look at over at the PHP Manual: http://php.net/manual/en/function.array-unique.php

于 2012-09-29T14:03:17.213 に答える