Php Yii::为什么类值只更改一次?;

Php Yii::为什么类值只更改一次?;,php,yii,Php,Yii,我在视图上使用ajaxlink添加submitform的新行。我需要一个索引来指示创建了哪一行。因此我使用一个类变量来保存索引。但是我发现该变量只更改了一次 这是我的密码 public function actionNewrow() { $this->i++; $form = new CActiveForm(); $temp = new exportprice(); array_push($this->exps, $temp); //ech

我在视图上使用ajaxlink添加submitform的新行。我需要一个索引来指示创建了哪一行。因此我使用一个类变量来保存索引。但是我发现该变量只更改了一次

这是我的密码

public function actionNewrow()
{
    $this->i++;

    $form = new CActiveForm();
    $temp = new exportprice();

    array_push($this->exps, $temp);
    //echo count($this->exps);

    $i = count($this->exps)-1;

    $html = '<tr><td>'.
                $this->i.$form->labelEx($this->exps[0],'['.$i.']productname').$form->textField($this->exps[0],'['.$i.']productname').$form->error($this->exps[0],'['.$i.']productname')
            .'</td>'.
            '<td>'.
                $form->labelEx($this->exps[0],'['.$i.']trend').$form->textField($this->exps[0],'['.$i.']trend').$form->error($this->exps[0],'['.$i.']trend')
            .'</td>'.
            '<td>'.
                $form->labelEx($this->exps[0],'['.$i.']margin').$form->textField($this->exps[0],'['.$i.']margin').$form->error($this->exps[0],'['.$i.']margin')
            .'</td></tr>';
    echo $html;
}

echo CHtml::ajaxLink("新增",
    Yii::app()->createUrl( 'InputReport/newrow' ),

    // ajax options
    array(
        'error' => 'function(data)   { alert("error!");  }',
        'success' => 'function(data) { $("#exptbl").append(data); }',
        'cache'=>false,
    ),

    // htmloptions
    array(
        'id' => 'handleClick',
    )
);
公共函数actionNewrow()
{
$this->i++;
$form=新的CActiveForm();
$temp=新出口价格();
数组推送($this->exps,$temp);
//回声计数($this->exps);
$i=count($this->exps)-1;
$html=''。
$this->i.$form->labelEx($this->exps[0],“['.$i.]productname”)。$form->textField($this->exps[0],“['.$i.]productname”)。$form->error($this->exps[0],“['.$i.]productname'))
.''.
''.
$form->labelEx($this->exps[0],'['.$i.]trend')。$form->textField($this->exps[0],'['.$i.]trend')。$form->error($this->exps[0],'['.$i.]trend'))
.''.
''.
$form->labelEx($this->exps[0],'['.$i.]margin')。$form->textField($this->exps[0],'['.$i.]margin')。$form->error($this->exps[0],'['.$i.]margin'))
.'';
echo$html;
}
echo CHtml::ajaxLink(“新增",
Yii::app()->createUrl('InputReport/newrow'),
//ajax选项
排列(
'error'=>'函数(数据){alert(“error!”);},
'success'=>'函数(数据){$(“#exptbl”).append(数据);}',
'cache'=>false,
),
//htmloptions
排列(
'id'=>'handleClick',
)
);

因此,您通过AJAX调用
actionNewrow
到预定义的类varialbe
$i
,每次将其inc?这就是为什么。PHP与客户端不一致,因此
$i
在以这种方式使用时将始终等于先前的值,因为
++
这样做只会使其inc一次

您需要在客户端以某种方式发送来自卸货的
$i

  • 从您拥有的行数+1(这对索引重复是开放的)
  • 在JS中包含$i var,并在自定义函数中使用它(可能在
    ajaxLink
    builder之外),以使用JS中的
    $i
    var作为类var来传播新行
给你举个简单的例子:

var i = <?php echo $this->i // presuming i is a controller car you pass to the view ?>;

$('.add_new_row').on('click', function(){
    $.get('InputReport/newrow', {i:i}, function(data){
        //append your row now
        i++; // inc i here
    });
});
vari=;
$('.add_new_row')。在('click',function()上{
$.get('InputReport/newrow',{i:i},函数(数据){
//现在添加您的行
i++;//我在这里
});
});
然后在控制器中执行以下操作:

public function actionNewrow($i = null)
{
    $i = $i===null ? $this->i++ : $i;

    $form = new CActiveForm();
    $temp = new exportprice();

    array_push($this->exps, $temp);
    //echo count($this->exps);

    $i = count($this->exps)-1;

    $html = '<tr><td>'.
                $this->i.$form->labelEx($this->exps[0],'['.$i.']productname').$form->textField($this->exps[0],'['.$i.']productname').$form->error($this->exps[0],'['.$i.']productname')
            .'</td>'.
            '<td>'.
                $form->labelEx($this->exps[0],'['.$i.']trend').$form->textField($this->exps[0],'['.$i.']trend').$form->error($this->exps[0],'['.$i.']trend')
            .'</td>'.
            '<td>'.
                $form->labelEx($this->exps[0],'['.$i.']margin').$form->textField($this->exps[0],'['.$i.']margin').$form->error($this->exps[0],'['.$i.']margin')
            .'</td></tr>';
    echo $html;
}
公共函数actionNewrow($i=null)
{
$i=$i===null?$this->i++:$i;
$form=新的CActiveForm();
$temp=新出口价格();
数组推送($this->exps,$temp);
//回声计数($this->exps);
$i=count($this->exps)-1;
$html=''。
$this->i.$form->labelEx($this->exps[0],“['.$i.]productname”)。$form->textField($this->exps[0],“['.$i.]productname”)。$form->error($this->exps[0],“['.$i.]productname'))
.''.
''.
$form->labelEx($this->exps[0],'['.$i.]trend')。$form->textField($this->exps[0],'['.$i.]trend')。$form->error($this->exps[0],'['.$i.]trend'))
.''.
''.
$form->labelEx($this->exps[0],'['.$i.]margin')。$form->textField($this->exps[0],'['.$i.]margin')。$form->error($this->exps[0],'['.$i.]margin'))
.'';
echo$html;
}

这应该会对您有所帮助,

最好在clientside js中使用变量
i
,并将其作为参数提供给ajax调用