【问题标题】:Ajax data not passing looped dataAjax 数据不传递循环数据
【发布时间】:2017-09-28 10:48:30
【问题描述】:

我正在尝试实现一个产品购物车,下面是我的代码

$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

【问题讨论】:

    标签: php ajax yii


    【解决方案1】:

    如下更新循环。

    foreach($mycart as $i=>$cart){
    
     echo $i; // your  code. $i is the key value here.$i declaration is not required.
    }
    

    【讨论】:

    • 感谢您的回复@Sucharitha,但仍然面临同样的问题,下面是购物车数组。数组 ( [0] => 数组 ( [0] => 2 [1] => 1 ) [1] => 数组 ( [0] => 993 [1] => 1 ) [2] => 数组 ( [ 0] => 993 [1] => 1) [3] => 数组 ([0] => 992 [1] => 1))
    • 你最后删除了 $i++ 吗?
    • 确定我删除了它
    • $a=array ('0' => array('0' => 2, '1' => 1), '1' => array ('0' => 993,' 1' => 1)); foreach($a as $i=>$val){ echo $i ."
      "; } 它将打印数组键。执行此代码。您可以了解您错过了什么以及您确切需要什么。
    • 好的,谢谢,但返回 0 和 1。正如预期的那样......并且这些值将作为 ajax 数据传递到变量索引中,但它每次只传递最后一个值......! !!!
    猜你喜欢
    • 2015-08-14
    • 2023-03-23
    • 2021-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-06
    • 2017-04-11
    • 1970-01-01
    相关资源
    最近更新 更多