2

データベースに 2 つのクエリを送信して投稿 (op_ideas 16 列) を戻し、その後に別のクエリを投稿ごとに保持し (op_idea_vote 列 4)、idea_id が一致するようにします。

データの例:

クエリ: op_ideas:

[{"idea_id":"2211","author_id":"100000", "date":"2012-09-06 10:02:28","idea_title":"別のテスト","4" など

クエリ: op_idea_votes:

idea_id = 2211、賛成=3、反対=1、棄権=0

以下のコードは、op_ideas を調べてから、「idea_id」の下で一致が見つかるまで op_ideas_vote を繰り返す必要があります。次に、op_ideas の下の次のレコードに移動し、再びその idea_id を使用して op_idea_vote リスト内で検索し、一致するものを見つけて配列に追加します。

これは最初のレコードのみで機能し、他の 3 つのレコードでは機能しません。私はテストしているので、op_idea_vote で異なる結果を持つ idea_id に一致する行がそれぞれ 3 つあります。

$votes = mysql_query($commentVotes);

$result = mysql_query($gl_query);

while ($gce_result = mysql_fetch_array($result)) {
    $voteid = $gce_result['idea_id'];

    while($allvotes= mysql_fetch_array($votes)) {
        if($voteid = $allvotes['idea_id']) 
        {
        //echo $voteid . " main idea and the votes: " . $allvotes;
            $gce_result["agree"] = $allvotes['agree'];
            $gce_result["disagree"] = $allvotes['disagree'];
            $gce_result["abstain"] = $allvotes['obstain'];
        } 
        else 
        {
            $gce_result["agree"] = 0;
            $gce_result["disagree"] = 0;
            $gce_result["abstain"] = 0;
        }
        //print_r($gce_result);
    }
    $data_result[] = $gce_result;
}

echo json_encode($data_result);

私が使用するprint_f(&gce_result)と、phpfiddleで正常に動作します。しかし、上記のコードを使用すると、最初のレコードでは機能しますが、2 番目の 2 つは完全に欠落しています。0 0 0 の結果すら得られないため、秒が欠けているようです。

op_ideas のクエリ:

$gl_query = "SELECT DISTINCT * FROM heroku_056eb661631f253.op_ideas INNER JOIN op_organs ORDER BY date ASC;";
if (!mysql_query($gl_query)) {
    die('Error: ' . $gl_query . " " . mysql_error());
}
$result = mysql_query($gl_query);

op_idea_vote のクエリ:

$commentVotes = "SELECT v.idea_id, COUNT(v.agree = 1 or null) as agree, COUNT(v.disagree =   1 or null) as disagree, COUNT(v.obstain = 1 or null) as obstain FROM op_idea_vote v GROUP BY  v.idea_id";
if (!mysql_query($commentVotes)) {
    die('Error: ' . $commentVotes . " " . mysql_error());
}
$votes = mysql_query($commentVotes);
4

3 に答える 3

1

リソースをスキャンできるのは 1 回だけです。そのため、内側の while は 1 回だけ実行されます。

于 2012-10-14T10:29:51.370 に答える
0

whileループで&の状態をチェックする==代わりに使用 します。$ allvotesの値を割り当てる必要がありますが、割り当てたことはありません。=ifwhile

while ($gce_result == mysql_fetch_array($result)) {
    $voteid = $gce_result['idea_id'];

    while($allvotes== mysql_fetch_array($votes)) {
        if($voteid == $allvotes['idea_id']) 
        {
        //echo $voteid . " main idea and the votes: " . $allvotes;
            $gce_result["agree"] = $allvotes['agree'];
            $gce_result["disagree"] = $allvotes['disagree'];
            $gce_result["abstain"] = $allvotes['obstain'];
        } 
        else 
        {
            $gce_result["agree"] = 0;
            $gce_result["disagree"] = 0;
            $gce_result["abstain"] = 0;
        }
        $data_result[] = $gce_result;
    }

}
于 2012-10-14T10:03:57.660 に答える
0

あなたの問題は、 $votes の結果を複数回スキャンしようとしています。

そのクエリの結果を最初に保存する必要があります。

例えば。

while ($vote = mysql_fetch_array($votes)) {
    $allvotes['idea_id'] = $vote;
}

while ($gce_result = mysql_fetch_array($result)) {
    $voteid = $gce_result['idea_id'];
    if (array_key_exists[$voteid, $allvotes]) {
       //assign results
    } else {
       //default
    }
}

別のオプションは、結合を使用してクエリを実行することです。これにより、1 つのクエリですべてを実行できます。次に、その結​​果をループします。

于 2012-10-14T10:20:26.380 に答える