如何在ajax中使用多个for循环。

时间:2017-05-01 14:23:39

标签: jquery mysql ajax codeigniter

我想创建只有一次不重复的记录。我正在尝试使用多个for loop并创建两个相同数据库记录的记录,First for循环执行此操作一次,child / inner for循环执行此操作第二次。所以记录生成重复。任何人都可以帮助我如何为一条记录使用两个for loop条件。

这是我的代码。

<script>
function createDiv(data) {
      var dynamicHTML = '';
      var innerContent = '';

      alert(data.res4.length);
      for (var i = 0; i < data.res2.length; i++) {
      for(var j =0; j< data.res4.length; j++){
                if(data.res2[i].paid_type == 1) {
                    if(data.res3[0].Status == "success") {

                            if(data.res4[j].Status == 'success'){
                              innerContent += '<div class="input-group" style="border-color:#3D8EB9; box-shadow: 1px 1px 1px 1px #888888; margin-bottom: 3px; background-color: #fff;">'+
                              '<label style="padding:10px 10px;">' + data.res2[i].Paper_Name + '</label>' +
                                  '<span class="input-group-btn" style="padding-top:10px;">' +
                                        '<button class="btn btn-info" type="button">Analysis</button>' +
                                  '</span>'+
                              '</div>';
                            } else {
                                innerContent += '<div class="input-group" style="border-color:#3D8EB9; box-shadow: 1px 1px 1px 1px #888888; margin-bottom: 3px; background-color: #fff;">'+
                                '<label style="padding:10px 10px;">' + data.res2[i].Paper_Name + '</label>' +        
                                    '<span class="input-group-btn" style="padding-top:10px;">' +
                                        '<button class="btn btn-info" type="button">Take Test </button>' +
                                    '</span>'+
                                '</div>';
                            }
                        }

                 else {
                            innerContent += '<div class="input-group" style="border-color:#3D8EB9; box-shadow: 1px 1px 1px 1px #888888; margin-bottom: 3px; background-color: #fff;">'+
                            '<label style="padding:10px 10px;">' + data.res2[i].Paper_Name + '</label>' +        
                                '<span class="input-group-btn" style="padding-top:10px;">' +
                                    '<button class="btn btn-info" type="button">Buy</button>' +
                                '</span>'+
                            '</div>';               
                    }
                }
                else {          

                            alert(j+ ' j is here');
                                if(data.res4[j].A_Submit_Status == "success"){
                                  innerContent += '<div class="input-group" style="border-color:#3D8EB9; box-shadow: 1px 1px 1px 1px #888888; margin-bottom: 3px; background-color: #fff;">'+
                                  '<label style="padding:10px 10px;">' + data.res2[i].Paper_Name + '</label>' +
                                      '<span class="input-group-btn" style="padding-top:10px;">' +
                                            '<button class="btn btn-info" type="button">Analysis 1</button>' +
                                      '</span>'+
                                  '</div>';
                                } else {
                                    innerContent += '<div class="input-group" style="border-color:#3D8EB9; box-shadow: 1px 1px 1px 1px #888888; margin-bottom: 3px; background-color: #fff;">'+
                                    '<label style="padding:10px 10px;">' + data.res2[i].Paper_Name + '</label>' +        
                                        '<span class="input-group-btn" style="padding-top:10px;">' +
                                            '<button class="btn btn-info" type="button">Take Test 1</button>' +
                                        '</span>'+
                                    '</div>';
                                }                               
                        }
                    }   
                }

      dynamicHTML += '<div id="' + data.res2.Paper_Type + '" class="tabcontent" style="border:none;">' + innerContent + '</div>';
      $('div#tabs_data').append(dynamicHTML);
    }
</script>

这是我的重复记录图片。

enter image description here

我从Codeigniter控制器传递json数据..

$tabs_data['res2'] = $this->db->select('*')->from('tb1')->where('Paper_Type', $paper_name)->get()->result();
            $tabs_data['res3'] = $this->db->select('*')->from('tb2')->where($UP)->get()->result();
            $tabs_data['res4'] = $this->db->select('*')->from('tb3')->where($PA)->get()->result();

被修改

如果paid_type == 1已付款,那么首先检查用户是否出现了考试,然后显示Analysis其他人显示Buy

在其他部分if(paid_type != 1)的下方相同,然后首先检查考试然后显示Analysis其他显示Take test

在行名称数据来自tb1,按钮数据来自tb3。任何人都可以帮助我解决这个问题。

json输出

{"user":"abc@gmail.com",
    "res2":[{"id":"1","Paper_Name":"sm1","Paper_Type":"ss","paid_type":"0"},{"id":"2","Paper_Name":"    ss-2","Paper_Type":"ss","paid_type":"1"},{"id":"3","Paper_Name":"ss-3","Paper_Type":"ss","paid_type":"1"},{"id":"4","Paper_Name":"ss-4","Paper_Type":"ss","paid_type":"1"}],
    "res3":[{"User_id":"abc@gmail.com","Name":"mahi mahi","Phone":"2147483647","Txnid":"2147483647","Amount":"120","Product_Info":"ss","encryptedPaymentId":"CBEC29993C2DAE0B37453BFAD285CB87","Status":"success","Date_Time":"2017-04-20 12:22:49"}],
    "res4":[{"A_User_id":"abc@gmail.com","A_Paper_Name":"ss-1","A_Paper_Type":"ss","A_Submit_Status":"success","A_Date_Time":"2017-04-21 04:33:11"}]}

0 个答案:

没有答案