在yii框架中存储数组

时间:2016-06-16 11:03:54

标签: javascript php yii yii2

我在视图中有这个表单:

<form method="POST" action="<?php echo Yii::$app->request->baseUrl;?>/telephone/addnow/" role="form" enctype="multipart/form-data">
    <label>Upload your photo:</label><input type="file" name="image" ><br>
    <input type="name" name="name" id="name" placeholder="Name" required><br><br>
    <input type="text" name="address" placeholder="Address"><br><br>
    <input type="text" name="telephone" placeholder="Telephone number" required>
    <br><br>
    <div id="dynamicInput">
        <br><input type="text" name="myinputs[]" placeholder="Secondary Phone #1">
        <span class="glyphicon glyphicon-plus" onClick="addInput('dynamicInput');"></span>
        <br><br>  
    </div>
    <input type="text" name="mobile" placeholder="Mobile number" >  <br><br>
    <input type="email" name="email" placeholder="Email">
    <input type="email" name="altemail" placeholder="Alternative Email"><br><BR>
    <input type="text" name="company_name" placeholder="Company Name"><br><BR>
    <input type="text" name="company_address" placeholder="Company Address"><br><br>
    <input type="text" name="company_phone_primary" placeholder="Company Telephone">    
    <input type="text" name="company_phone_secondary" placeholder="Telephone Secondary "><br><br>
    <input type="text" name="company_email" placeholder="Company Email Address"><br><BR>
    <button type="submit" class="btn btn-default">Submit</button><BR><BR>
</form>

允许用户插入我使用此javascript代码的任意数量的辅助电话:

<script>
    var counter = 1;
    var limit = 10;
    function addInput(divName) {
        if (counter == limit)  {
            alert("You have reached the limit of adding " + counter + " inputs");
        } else {
            var newdiv = document.createElement('div');
            newdiv.innerHTML = "Seconday Phone # " + (counter + 1) + " <br><input type='text' name='myinputs[]' placeholder='Secondary Phone '>";
            document.getElementById(divName).appendChild(newdiv);
            counter++;
        }
    }
</script>

我有这个控制器代码:

public function actionAddnow()
{
    $request = Yii::$app->request;
    $add=new telephone();
    $add->Name=$request->post('name');
    $add->Email=$request->post('email');
    $add->Mobile=$request->post('mobile');  
    $add->Address=$request->post('address');
    $add->Telephone=$request->post('telephone');
    $add->altemail=$request->post('altemail');
    $add->company_name=$request->post('company_name');
    $add->company_address=$request->post('company_address');
    $add->company_phone_primary=$request->post('company_phone_primary');
    $add->company_phone_secondary=$request->post('company_phone_secondary');
    $add->company_email=$request->post('company_email');

    $add->save();

    $getlast=Yii::$app->db->getLastInsertId();
    $myinputs=$request->post('myinputs');
    $totalinputs=sizeof('$myinputs');   

    for ($i=0; $i<=$totalinputs; $i++) {
        $inputs=$myinputs[$i];
        $phones=new phone();
        $phones->secondary_phones=$inputs;
        $phones->id=$getlast;
        $phones->save();    
    }
    return $this->redirect(Yii::$app->request->baseUrl.'/telephone/index');     
}

但只有$myinputs的前两个值插入数据库中。

1 个答案:

答案 0 :(得分:1)

将变量$ myinputs放在单引号(&#39; $ myinputs&#39;)之间,您将变量转换为字符串。 this article是count函数的别名,但是当你将参数作为字符串给出时,你总是得到结果为1.你将循环使用条件($ i = 0; $ i&lt; = $ totalinputs; $ i ++)意味着这个循环当$ i为0且$ i为1时,将运行2次。

而不是:

$totalinputs=sizeof('$myinputs');

您应该使用

$totalinputs=sizeof($myinputs);

另一个错误是你的周期状况。

for ($i=0; $i<=$totalinputs; $i++)

for ($i=0; $i<$totalinputs; $i++)

或者你可以用foreach替换循环

foreach($myinputs as $inputs)
{
        $phones=new phone();
        $phones->secondary_phones=$inputs;
        $phones->id=$getlast;
        $phones->save();
}

改善方面

1.对于表单,您可以使用sizeof小部件

$form = \yii\widgets\ActiveForm::begin([
        'options' => [
           "role" => "form",
           "enctype"=> "multipart/form-data",
        ], ]); 

echo $form->field($add, 'Name'); 
//etc 
$form->end();

2.如果您使用ActiveField在视图中创建输入字段或以ModelClassName [ModelFieldName]等格式手动添加字段名称,那么您将能够使用模型加载来分配值 例如:

$add=new telephone();
if ($add->load(Yii::$app->request->post()))
{
   if ($add->save())
   {//saved
   }
   else
   {//error
   }
}
else
{//no post data
}

3.对于url,可能不需要添加请求baseUrl属性。 ActiveForm中的示例您只需使用

即可
$this->redirect('/telephone/index');

4.当数据库中存在一些将创建其他行的触发器时,使用方法getLastInsertId()可能会导致错误。所以使用它可能更明智:

$getlast=$add->id;//assuming model autoincrement field name is "id"