首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP循环在while循环中工作一次

PHP循环在while循环中工作一次
EN

Stack Overflow用户
提问于 2012-10-14 10:00:12
回答 3查看 550关注 0票数 2

我有两个查询发送到一个数据库,返回帖子(op_ideas 16 cols),然后是另一个查询,它保存每个帖子的选票(op_idea_vote cols 4),并匹配idea_id

数据示例:

查询: op_ideas:

[{"idea_id":"2211“、"author_id":"100000”、“日期”:“2012-09-06 10:02:28”、“idea_title”:“另一个测试”、"4“等等

查询: 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中的不同结果相匹配。

代码语言:javascript
复制
$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),它可以很好地工作在小提琴。但是当我使用上面的代码时,它可以用于第一条记录,但是它完全缺少第二条记录。它似乎错过了第二次,因为它甚至没有给我0 0 0的结果。

查询op_ideas:

代码语言:javascript
复制
$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 :

代码语言:javascript
复制
$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);
EN

回答 3

Stack Overflow用户

发布于 2012-10-14 10:29:51

只能扫描一次资源。所以内在的时候只会运行一次。

票数 1
EN

Stack Overflow用户

发布于 2012-10-14 10:03:57

在while循环中,使用==而不是=来检查if & while的条件,您必须分配$allvotes的值,但您从未分配过,

代码语言:javascript
复制
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;
    }

}
票数 0
EN

Stack Overflow用户

发布于 2012-10-14 10:20:26

您的问题是尝试多次扫描$votes结果。

您应该首先存储该查询的结果。

例如:

代码语言:javascript
复制
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
    }
}

另一种选择是使用一个join来执行查询,这样您就可以在一个查询中完成所有操作。然后再循环一遍结果。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12881149

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档