Ajax数据没有传递循环数据

时间:2017-09-28 10:48:31

标签: php ajax yii

我试图实施产品购物车,下面是我的代码

$i=0;
foreach($mycart as $cart){
		$product=AllinOne::ProductDetails($cart[0]);
		$desc='<h5><b>'.CHtml::link($product[2],array('product/details','id'=>$product[0])).'</b></h5>';
		$desc.='<small>'.$product[7].'<br>';
		$subtotal=$product[5]*$cart[1];
		$total=$total+$subtotal;
		echo '<tr><td><span style="float:left; width:40%;">'.CHtml::link('<img class="img-responsive" src="images/home/'.$product[10].'"/>',array('product/details','id'=>$product[0])).'</span><span class=float:right; width:59%;">'.$desc.'</span></td>';
		echo '<td>'.number_format($product[5],2).'</td><td>';
		echo CHtml::NumberField('qty',$cart[1],array('id'=>'qty','min'=>$cart[1],'max'=>$product[17],'style'=>'width:50px;',
				'ajax'=>array(
					'type'=>'POST',
					'update'=>"#subt".$i,//here is the problem,$i takes the //last vaule
					'data'=>array('index'=>$i,'qty'=>'js:this.value'),//'"js:{qty:$('#qty').val()}" js:this.value' 
					'url'=>CController::createUrl('account/twistcart'),
		)));
		echo '</td><td><span id="subt'.$i.'">'.number_format($subtotal).'</span></td><td>'.CHtml::link('<i class="glyphicon glyphicon-remove"></i>',array('product/RmvFromCart','i'=>$i),array('class'=>'btn btn-xs btn-danger')).'</td></tr>';	
		$i++;
	}

问题是在ajax数据中$ i值在循环中取最后一个值?循环时如何将正确的值传递给ajax数据?

there when ever i change the quantity it should pass the index and the quantity but it passes the last index to every record

1 个答案:

答案 0 :(得分:0)

更新循环如下。

foreach($mycart as $i=>$cart){

 echo $i; // your  code. $i is the key value here.$i declaration is not required.
}