Karthiga
Karthiga

Reputation: 240

Php while and for loop issue

I have issue in while loop ..My problem described below here.

$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 variable having 2,3,4 values. but the values of $rlt['movie'] giving last 4 th id result only. i wand 2,3,4 id values .

Upvotes: 0

Views: 79

Answers (3)

deepak
deepak

Reputation: 346

Do not use explode method you are using extra loop you can solve this one query and get the result in while loop like this

$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++;
}

In where clase we use id IN which is check only from the id which are in variable as a string so there is no use of extra for loop and explod function

Upvotes: 0

Akshay Paghdar
Akshay Paghdar

Reputation: 3629

You should try this:--

$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++;
}

Upvotes: 1

TroyCheng
TroyCheng

Reputation: 571

Because every time you assign $rlts to the same variable $rlt['movie']. try $rlt['movie'][]

Upvotes: 0

Related Questions