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

[php]كيفية تكرار نفس المتغيرات دون اعادة كتابتها في كل مرة

محمد المصري12

السؤال

سلام عليكم

مثال 

case 'add_buttons':

//        var_dump($_POST);exit;

        $order = $_POST['order'];

        $buttons = $_POST['buttons'];

        $colorSelect = $_POST['colorSelect'];

        $colorNumber = $_POST['colorNumber'];

        $countPiece = $_POST['countPiece'];

        $total = $_POST['total'];

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

        for ($i =0; $i < count($buttons); $i++){

            try
            {
 $result = $conn->query("INSERT INTO order_buttons(order_id, button_id, color_select, colorNumber_input, countPiece , total ) VALUES('$order','$buttons[$i]','$colorSelect[$i]' ,'$colorNumber[$i]','$countPiece[$i]','$total[$i]')");
            }
            catch (Exception $e)
            {
                var_dump($e->getTrace());
// or to get the full error info, just
                var_dump($e);
            }

و case اخرى 

 case 'add_rubbers':

//        var_dump($_POST);exit;

        $order = $_POST['order'];

        $rubbers = $_POST['rubbers'];
		
		$colorSelect = $_POST['colorSelect'];

        $colorNumber = $_POST['colorNumber'];

        $countPiece = $_POST['countPiece'];

        $total = $_POST['total'];

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

        for ($i =0; $i < count($rubbers); $i++){

            try
            {
                $result = $conn->query("INSERT INTO order_rubbers(order_id, rubber_id, color_select, colorNumber_input, countPiece , total ) VALUES('$order','$rubbers[$i]','$colorSelect[$i]' ,'$colorNumber[$i]','$countPiece[$i]','$total[$i]')");
            }
            catch (Exception $e)
            {
                var_dump($e->getTrace());
                // or to get the full error info, just
                var_dump($e);
            }

        }

عندي ال 4 متغيرات دول المفروض اكررهم في اكتر من 11 case 

		$colorSelect = $_POST['colorSelect'];

        $colorNumber = $_POST['colorNumber'];

        $countPiece = $_POST['countPiece'];

        $total = $_POST['total'];

ازاي تتعمل مع العلم اني حاولت اعملهم جوا دالة مخصصة و استدعيهم بس ما نجحتش للاسف

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

Recommended Posts

  • 1

دعنا نفهم ما هي case لنعرف كيف تعمل أولا:

case تشبة if في بعض اللغات، وما بداخلها هو الكود الذي يتم تنفيذة اذا حدث الشرط الذي يوضع بجوار case.

لذلك فانها تعتبر حلقة مغلقة ، واذا اردت تعريف متغير أكثر من مرة بداخله، عليك أن تكرره أكثر من مرة.

 لذلك أبسط حل هو أن تقوم بتعريف الأربع متغيرات الذي تريد استخدامهم في ال global ، خارج أي case منهم، فتقوم بوضع الكود التالي قبل اول case:

$colorSelect = $_POST['colorSelect'];

$colorNumber = $_POST['colorNumber'];

$countPiece = $_POST['countPiece'];

$total = $_POST['total'];

وبما انه تم تعريفهم في ال global فلا داعي لاعادة تعرفيهم داخل أي case منهم، كمثال:

$colorSelect = $_POST['colorSelect'];

$colorNumber = $_POST['colorNumber'];

$countPiece = $_POST['countPiece'];

$total = $_POST['total'];

case 'add_buttons':

//        var_dump($_POST);exit;

        $order = $_POST['order'];

        $buttons = $_POST['buttons'];



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

        for ($i =0; $i < count($buttons); $i++){

            try
            {
 $result = $conn->query("INSERT INTO order_buttons(order_id, button_id, color_select, colorNumber_input, countPiece , total ) VALUES('$order','$buttons[$i]','$colorSelect[$i]' ,'$colorNumber[$i]','$countPiece[$i]','$total[$i]')");
            }
            catch (Exception $e)
            {
                var_dump($e->getTrace());
// or to get the full error info, just
                var_dump($e);
            }

 هذا يسهل ويقلل من حجم الكود ويجعله أسرع ُأثناء تشغيله.

تم التعديل في بواسطة Ahmed Sharshar
رابط هذا التعليق
شارك على الشبكات الإجتماعية

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

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

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

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

  • إعلانات

  • تابعنا على



×
×
  • أضف...