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

[PHP -jQuery - MySQL] ادخال بيانات اكثر من قائمة متعددة الاختيارات إلى قاعدة البيانات

محمد المصري12

السؤال

السلام عليكم

عندي مجموعة من الاختيارات منهم قوائم منسدلة 

الاختيارات دي نفسها بتكرر حسب اختيار المستخدم و بقوم بادخال بيانات قائمة منسدلة متعددة الاختيارات إلى قاعدة البيانات 

فيديو صغير توضيحي يشرح المشكلة

2022-06-06_13-34-29.mp4

الكود

<div class="col-12 mb-2 row d-flex align-items-center threads-fields">

<div class="col-9" style="margin-bottom:20px">
<label class="h5 text-primary m7md1" style="margin-bottom:20px">اختيار الخيط</label>
<div class="input-group">

<!-- قائمة اختيار الخيط -->
<select class="form-control threads-value show-image p-0" required>

<option selected readonly value="0" disabled>تحديد خيط حياكة</option>

<?php

$result = $conn->query("SELECT * FROM thread");

while ($row = $result->fetch_assoc()) {
if (empty($row['name'])){

echo '<option type="threads" img="' . $row['image'] . '" value="' . $row['id'] . '">كود الخيط T' . $row['id'] . '/ لم يتم تسجيل اسم للخيط</option>';   
}
else {

echo '<option class="text-danger" type="threads" img="' . $row['image'] . '" value="' . $row['id'] . '">كود الخيط T' . $row['id'] . '/ اسم الخيط - ' . $row['name'] . '</option>';
}
}

?>

</select>

</div>

</div>

<!-- ##### عرض الصورة -->

<div class="col-3 d-flex justify-content-center show-image"  style="margin-bottom:20px"></div>
  
  <div class="col-2" style="margin-bottom:30px">
<label class="required">اختيار لون المرياج</label>
<div class="input-group">
  
<!-- قائمة اختيار لون المرياج -->
  
<select class="form-control m color_select_thread" multiple="multiple" required>

<option value="" disabled selected >اختيار اللون</option>

<!-- بداية استعلام الوان المرياجات -->

<?php

$meriag = "SELECT `patron_colors`.`id` AS pcID, `patron_colors`.`patron_id` AS pcPID, `patron_colors`.`color_name` AS pcCN, `patron_colors`.`color_count`AS pcCC FROM `patron_colors` INNER JOIN `patron` ON `patron`.`id` = `patron_colors`.`patron_id` INNER JOIN `orders` ON `orders`.`id` = `patron`.`order_id` WHERE `orders`.`id` =$order_id";

$result = $conn->query($meriag);

while ($row = $result->fetch_assoc()) {


echo '<option class="text-danger color_count_thread" type="buttons"   value="' . $row['pcCC'] . ' ">  ' . $row['pcCN'] . '</option>';

}

?>

</select>

</div>

</div>

<div class="col-2" style="margin-bottom:30px">

<label class="required">العدد</label>
<div class="input-group">

  <input type="text"  class="form-control colorNumber_input_thread" required readonly>

</div>
</div>

<div class="col-2" style="margin-bottom:30px">
<label class="required">عدد الامتار للقطعة</label>
<div class="input-group">

<input type="number" step=".01" class="form-control countPiece_thread" required>

</div>

</div>

<div class="col-2" style="margin-bottom:30px">
<label class="required">المجموع</label>
<div class="input-group">

<input type="text" class="form-control border border-warning total_thread" required disabled>

</div>

</div>

<div class="col-2" style="margin-bottom:30px">
    <label class="required">نسبة السماح</label>
    <div class="input-group">

        <input type="text" class="form-control border border-warning hadr_thread" required>

    </div>

</div>

<div class="col-2" style="margin-bottom:30px">
    <label class="required">الإجمالي</label>
    <div class="input-group">

        <input type="text" class="form-control border border-warning last_thread" required disabled>

    </div>

</div>

</div>

 

كود التمرير JQuery

<script>
// Submit Threads
(function() {

'use strict';

window.addEventListener('load', function() {

var forms = document.getElementsByClassName('needs-validation-threads');

var validation = Array.prototype.filter.call(forms, function(form) {

form.addEventListener('submit', function(event) {

if (form.checkValidity() === false) {

event.preventDefault();

event.stopPropagation();

} else {

let threads = []; // تعريف نوع الخيط

let colorSelect =[]; // تعريف لون المرياج


  

$(".threads-value").each(function () {
threads.push($(this).val());
});

// تمرير لون المرياج
  
$(".color_select_thread option:selected").each(function() {

let text = $(this).text();


if(text.indexOf(text) >= 0 && colorSelect.indexOf(text) < 0) {
    colorSelect.push(text);
} else if(text.indexOf(text) < 0 && colorSelect.indexOf(text) >= 0) {
    colorSelect.splice(colorSelect.indexOf(text) , 1);
}
  //  colorSelect.push($(this).text());
});





console.log(threads);

		$.ajax({

		url: "requests/ordersAddAccessories.php",

		dataType: 'text',

		data: {

"action": "add_threads",

"order": <?php echo $order_id; ?>,

"threads": threads,

"colorSelect": colorSelect,



},

		type: 'post',

		success: function(data) {

		if (data == "done") {

		Swal.fire({

		icon: 'success',

		title: 'تمت الاضافة بفضل الله - قم بتحديث الصفحة بعد الانتهاء',

		confirmButtonText: 'حسنا',

		showCloseButton: true

        }).then((result) => {

            if (result.isConfirmed) {

                location.reload();

            }


		})
		console.log(data);

		} else {

		Swal.fire({

		icon: 'error',

		title: 'حدث خطأ حاول مجددا',

		confirmButtonText: 'حسنا',

		showCloseButton: true

		});

		console.log(data);

		}

		}

		});

		}

		form.classList.add('was-validated');

		}, false);

		});

		}, false);

		})();
		</script>

كود الباك ايند

$order = $_POST['order'];
$colorSelect = $_POST['colorSelect'];
$threads = $_POST['threads'];
$hadr = $_POST['hadr'];
$last = $_POST['last'];
$colorNumber = $_POST['colorNumber'];
$countPiece = $_POST['countPiece'];
$total = $_POST['total'];

// $result = $conn->query("DELETE FROM order_threads WHERE order_id='$order'");


for ($i =0; $i < count($threads); $i++){ // لتكرار القائمة حسب اختيار المستخدم

foreach ($colorSelect AS $meriag){ //دالة التكرار لادخال بيانات القائمة المتعددة الاختيار

$result = $conn->query("INSERT INTO order_threads(
    order_id,
    thread_id,
    color_select,
    colorNumber_input,
    countPiece,
    total,
    hadr,
    egmaly
)
VALUES(
    '$order',
    '$threads[$i]',
    '$meriag',
    '$colorNumber[$i]',
    '$countPiece[$i]',
    '$total[$i]',
    '$hadr[$i]',
    '$last[$i]'
)");

}

}

$result2 = $conn->query("UPDATE orders SET thread=1 WHERE id='$order'");

if ($result2){
echo 'done';
}

 

اين الخطأ الذي وقعت فيه ؟

و لكم خالص الشكر

رابط هذا التعليق
شارك على الشبكات الإجتماعية

Recommended Posts

  • 0

عند التعامل مع بيانات ذات بنية معقدة، من الأفضل استخدام صيغة الكائنات في التعامل معها حيث تسهل علينا التعامل مع تعدد الخاصيات و ربطها مع الكائن الهدف لها، بالإضافة للمصفوفات لتجميع الكائنات المتشابهة.

سيكون لدينا مصفوفة من النماذج و كل نموذج يحمل مواصفات المنتج الجزئي الخاص به.

let order = [
  {
  	color: red
    thread: T1
  },
  {
  	color: green
    thread: T1
  },
  {
  	color: yellow
    thread: T3
  },
  {}
]

let forms = [order1, order2]

يمكن عمل كائن يمثل الخط و صفاته، و كائن يحمل عدة منتجات لنفس الطلبية، ثم عمل مصفوفة طلبيات.

ثم في PHP يمكن عمل json_encode لعمل مصفوفة ترابطية فيها نفس بنية المعطيات و يمكن المرور عليها بسهولة.

رابط هذا التعليق
شارك على الشبكات الإجتماعية

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

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

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

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   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.

  • إعلانات

  • تابعنا على



×
×
  • أضف...