gpt4 book ai didi

PHP while 循环在 while 循环中运行一次

转载 作者:行者123 更新时间:2023-11-29 03:07:43 24 4
gpt4 key购买 nike

我有两个查询发送到数据库带回帖子(op_ideas 16 列),然后是另一个查询每个帖子的投票(op_idea_vote cols 4)与匹配的 idea_id

数据示例:

查询:op_ideas:

[{"idea_id":"2211","author_id":"100000", "date":"2012-09-06 10:02:28","idea_title":"Another test","4" etc etc

查询:op_idea_votes:

idea_id = 2211, agree=3, disagree=1, abstain=0

下面的代码应该查看 op_ideas,然后循环 op_ideas_vote,直到在“idea_id”下找到匹配项。然后它转到 op_ideas 下的下一条记录,并再次使用该 idea_id 在 op_idea_vote 列表中搜索它,找到一个匹配项,并将其添加到数组中。

这仅适用于第一条记录,不适用于其他三个记录。我正在测试,所以我在每行中有 3 行与 idea_id 匹配,在 op_idea_vote 中有不同的结果。

$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 中工作正常。但是当我使用上面的代码时,它适用于第一条记录,但它完全缺少后两条。它似乎错过了第二个 while,因为它甚至没有给我 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);

最佳答案

您只能扫描一次资源。所以内部 while 只会运行一次。

关于PHP while 循环在 while 循环中运行一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12881149/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com