Php while和for循环问题

时间:2013-07-25 05:13:41

标签: php for-loop while-loop

我在while循环中遇到问题..我的问题如下所述。

$sql = mysql_query($query, $this->db);
        if(mysql_num_rows($sql) > 0)
        {
            $result = array();
            while($rlt = mysql_fetch_array($sql,MYSQL_ASSOC))
            {

                $theature = explode(",",$rlt['mw_movie_theature']);
                //echo 'count'.count($theature).'<br/>'; print_r($theature);

                for($i = 0; $i<count($theature); $i++ )
                {
                    $sqls = mysql_query("SELECT * FROM mw_theatres WHERE status = 1 AND id='".$theature[$i]."'", $this->db);
                    $rlts = array();
                    while($rlts = mysql_fetch_array($sqls,MYSQL_ASSOC))
                    {
                        $rlt['movie'] = $rlts;
                    }
                }
                $rlt['value'] = 'true';
                $result[] = $rlt;

            }

            echo '<pre>';print_r($result);die;

$ theature变量有2,3,4个值。但$ rlt ['movie']的值只给出了最后4个id。我用2,3,4个id值。

3 个答案:

答案 0 :(得分:1)

你应该试试这个: -

$sqls = mysql_query("SELECT * FROM mw_theatres WHERE status = 1 AND id='".$theature[$i]."'", $this->db);
$rlts = array();
$j=0;
while($rlts = mysql_fetch_array($sqls,MYSQL_ASSOC))
{
     $rlt['movie'][$j] = $rlts;
     $j++;
}

答案 1 :(得分:0)

因为每次你将$ rlts分配给同一个变量$ rlt [&#39; movie&#39;]。试试$ rlt [&#39;电影&#39;] []

答案 2 :(得分:0)

不要使用explode方法,你正在使用额外的循环,你可以解决这一个查询并在while循环中得到结果

$sqls = mysql_query("SELECT * FROM mw_theatres WHERE status = 1 AND id IN (".$theature.")", $this->db);
$rlts = array();
$k = 0;
while($rlts = mysql_fetch_array($sqls,MYSQL_ASSOC))
{
    $rlt['movie'][$k] = $rlts;
    $k++;
}

在where clase中我们使用id IN,它只是从作为字符串变量的id中检查所以没有使用额外的for循环和爆炸函数