2016-07-31 90 views
1
$sql="SELECT * FROM seat WHERE type=$type AND seat=$seat";  
$getseats=Yii::$app->db->createCommand($sql)->queryAll();  
echo json_encode(array('status' => TRUE, 'getseats'=>$getseats)); die; 

用ajax jquery的這給$ getseats值array.so我能做到這一點:與ojects

success: function(response){ 
      var res=$.parseJSON(response); 
      if(res.status == true) 
      { 
       var seat=''; 
       for(var i=0; i<res.getseats.length; i++) 
       {      
        seat += '<option value='+res.getseats[i].seat_id+'>'+res.getseats[i].seat+'</option>'; 
        $('#seats').html(seat); 
       } 
      } 

,但如果我用的活動記錄?

$getseats=Seat::where(['type'=$type])->where(['seat'=>$seat])->all();  
echo json_encode(array('status' => TRUE, 'getseats'=>$getseats)); die; 

回答

1

ActiveRecord的,如果你要像一個數組的結果,你可以使用

$getseats=Seat::Find()->where(['type'=$type, 'seat'=>$seat])->asArray()->all(); 
+0

我的意思是jQuery的一部分,你應該使用(該回報模型)

$getseats=Seat::Find()->where(['type'=$type, 'seat'=>$seat])->all(); echo json_encode(array('status' => TRUE, 'getseats'=>$getseats)); die; 

。 – micky

+0

你是什麼意思? jQuery的一部分應該是相同的使用ActiveRecord或createCommand ..在我的回答中,我發佈了使用activeRecord獲取數據的正確方法..更好地解釋.. – scaisEdge

+0

好的。我一直想用ActiveRecord。我猜''getseats'這裏給出的對象和'res.getseats [我] .seat'將失敗。 – micky