اذهب إلى المحتوى

السؤال

نشر

أحتاج إلى تخزين بيانات عدة حقول textField داخل قاعدة بيانات Mysql وذلك باستعمال إطار العمل Yii2.

لدي الكود التالي الخاص بـ filed:

 <tr>
              <td>1</td>
              <td><?= $form->field($coordinatemodel,'lat_degree[]')->textInput(['placeholder'=>'Degree','data-validation'=>'required number','data-validation-error-msg'=>'*Required','required'=>'required'])->label('Degree')->label(false)?></td>
              <td><?= $form->field($coordinatemodel,'lat_min[]')->textInput(['placeholder'=>'Minute','data-validation'=>'required number','data-validation-error-msg'=>'*Required','required'=>'required'])->label('Minute')->label(false)?></td>
              <td><?= $form->field($coordinatemodel,'lat_second[]')->textInput(['placeholder'=>'Second','data-validation'=>'required number','data-validation-error-msg'=>'*Required','required'=>'required'])->label('Second')->label(false)?></td>
              <td><?= $form->field($coordinatemodel,'long_degree[]')->textInput(['class'=>'form-control input-sm','placeholder'=>'Degree','data-validation'=>'required number','data-validation-error-msg'=>'*Required','required'=>'required'])->label('Degree')->label(false)?></td>
              <td><?= $form->field($coordinatemodel,'long_min[]')->textInput(['class'=>'form-control input-sm','placeholder'=>'Minute','data-validation'=>'required number','data-validation-error-msg'=>'*Required','required'=>'required'])->label('Minute')->label(false)?></td>
              <td><?= $form->field($coordinatemodel,'long_second[]')->textInput(['class'=>'form-control input-sm','placeholder'=>'Second','data-validation'=>'required number','data-validation-error-msg'=>'*Required','required'=>'required'])->label('Second')->label(false)?></td>
            </tr> 
            <tr>
              <td>2</td>
              <td><?= $form->field($coordinatemodel,'lat_degree[]')->textInput(['placeholder'=>'Degree','data-validation'=>'required number','data-validation-error-msg'=>'*Required','required'=>'required'])->label('Degree')->label(false)?></td>
              <td><?= $form->field($coordinatemodel,'lat_min[]')->textInput(['placeholder'=>'Minute','data-validation'=>'required number','data-validation-error-msg'=>'*Required','required'=>'required'])->label('Minute')->label(false)?></td>
              <td><?= $form->field($coordinatemodel,'lat_second[]')->textInput(['placeholder'=>'Second','data-validation'=>'required number','data-validation-error-msg'=>'*Required','required'=>'required'])->label('Second')->label(false)?></td>
              <td><?= $form->field($coordinatemodel,'long_degree[]')->textInput(['class'=>'form-control input-sm','placeholder'=>'Degree','data-validation'=>'required number','data-validation-error-msg'=>'*Required','required'=>'required'])->label('Degree')->label(false)?></td>
              <td><?= $form->field($coordinatemodel,'long_min[]')->textInput(['class'=>'form-control input-sm','placeholder'=>'Minute','data-validation'=>'required number','data-validation-error-msg'=>'*Required','required'=>'required'])->label('Minute')->label(false)?></td>
              <td><?= $form->field($coordinatemodel,'long_second[]')->textInput(['class'=>'form-control input-sm','placeholder'=>'Second','data-validation'=>'required number','data-validation-error-msg'=>'*Required','required'=>'required'])->label('Second')->label(false)?></td>
            </tr> 
            <tr>
              <td>3</td>
              <td><?= $form->field($coordinatemodel,'lat_degree[]')->textInput(['placeholder'=>'Degree','data-validation'=>'required number','data-validation-error-msg'=>'*Required','required'=>'required'])->label('Degree')->label(false)?></td>
              <td><?= $form->field($coordinatemodel,'lat_min[]')->textInput(['placeholder'=>'Minute','data-validation'=>'required number','data-validation-error-msg'=>'*Required','required'=>'required'])->label('Minute')->label(false)?></td>
              <td><?= $form->field($coordinatemodel,'lat_second[]')->textInput(['placeholder'=>'Second','data-validation'=>'required number','data-validation-error-msg'=>'*Required','required'=>'required'])->label('Second')->label(false)?></td>
              <td><?= $form->field($coordinatemodel,'long_degree[]')->textInput(['class'=>'form-control input-sm','placeholder'=>'Degree','data-validation'=>'required number','data-validation-error-msg'=>'*Required','required'=>'required'])->label('Degree')->label(false)?></td>
              <td><?= $form->field($coordinatemodel,'long_min[]')->textInput(['class'=>'form-control input-sm','placeholder'=>'Minute','data-validation'=>'required number','data-validation-error-msg'=>'*Required','required'=>'required'])->label('Minute')->label(false)?></td>
              <td><?= $form->field($coordinatemodel,'long_second[]')->textInput(['class'=>'form-control input-sm','placeholder'=>'Second','data-validation'=>'required number','data-validation-error-msg'=>'*Required','required'=>'required'])->label('Second')->label(false)?></td>
            </tr> 

id lat_degree lat_min lat_second long_degree long_min long_second
1  50         50      50          55          55       55
2  60         60      60          65          65       65
3  65         65      65          70          70       70

 

bzL7Q.thumb.png.a9735b210237b285ccfd09f4

كيف ذلك؟

Recommended Posts

  • 0
نشر (معدل)

في هذه الحالة ستحتاج إلى إنشاء مصفوفة Models ، والتي ستمكنك من تغيير actionCreate الموجودة بداخل الكود، بهذا الشكل:

$count = count(Yii::$app->request->post('CoordinateDetails', []));
$coordinateDetails = [new CoordinateDetail()];
for ($i = 1; $i < $count; $i++) {
    $coordinateDetails[] = new CoordinateDetail();
}

لتتمكن فيما بعد من استعمال loadMultiple وَ validateMultiple لتفعيل Models بداخل التطبيق. وسنستعمل حلقة تكرار من نوع foreach لحفظ يبانات Models بالدور، هكذا:

if (CoordinateDetail::loadMultiple($coordinateDetails, Yii::$app->request->post()) 
    && CoordinateDetail::validateMultiple($coordinateDetails)) {
    foreach ($coordinateDetails as $coordinateDetail) {
        //نحفظ في هذه الجزئية البيانات المراد حفظها في قاعدة البيانات
        $coordinateDetail->save(false);
    }
    return $this->redirect('index');
}
else {
    //  error سنظهر هنا رسالة خطأ في حالة
}

 

تم التعديل في بواسطة E.Nourddine

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...