dphw5101 2019-02-02 10:27
浏览 78
已采纳

我可以遍历数据表列对象并添加+2的新行添加

I am fetching data from mysqli table using ajax jquery and fill that in Datatable as row using column object , can i able to loop through that data which i get in response from server and then make a new row addition by X number which i define.

I had tried but with this code i got same rows repeated in X times loop runs but don't add any kind of increment in other row.

My PHP API THAT GET DATA FROM MYSQL

if (isset($_POST['what_need']) AND $_POST['what_need'] == 'signup_getslabs') {

  $getTotalcountry = $obj -> getSlabSignup($_POST['placeid'], $_POST['range']);
  $return_ardr = array();

  foreach($getTotalcountry as $row) {
    $same_city_per_kg = $row['wc_slabr_hkg'];
    $diff_city_per_kg = $row['dc_slabr_hkg'];
    $fnfsamecityperkg = $same_city_per_kg - 50;
    $fnfdifferentcityperkg = $diff_city_per_kg - 50;
    $id = $row['id_slabr'];

    for ($i = 3; $i >= 0; $i--) {
      $samecit = $fnfsamecityperkg + 50;
      $diffcit = $fnfdifferentcityperkg + 50;
      $return_ardr[] = array(
        "samecity" => $samecit,
        "diffcity" => $diffcit,
        "weight" => 0.5,
        "idslab" => $id
      );
    }
  }

  function utf8ize($d) {
    if (is_array($d)) {
      foreach($d as $k => $v) {
        $d[$k] = utf8ize($v);
      }
    } else if (is_string($d)) {
      return utf8_encode($d);
    }
    return $d;
  }

  echo json_encode(utf8ize($return_ardr));
}

MY DATATABLE CODE THAT SEND AJAX REQUEST TO API AND PRINT DATA IN TABLE DEFINED BY ID

$('#ratestd').DataTable({
  "bDestroy": true,
  "serverSide": false,
  "ajax": {
    "url": "ajax-requests/ajaxm.php",
    "type": "POST",
    "dataSrc": function(d) {
      return d
    },
    "dataType": "json",

    "data": function(data) {
      data.what_need = 'signup_getslabs';
      data.placeid = placeid;
      data.range = range;
    }

  },
  dom: 'Bflrtip',
  "buttons": [
    'copyHtml5', 'excelHtml5', 'pdfHtml5', 'csvHtml5', 'colvis'
  ],
  "columns": [{
      "data": "weight"
    },
    {
      "data": "samecity"
    },
    {
      "data": "diffcity"
    }
  ]
});

I Expect output like this

WEIGHT     SAMECITY   DIFFERENT CITY
0.5 KG     50         100
1.0 KG     100        150
1.5 KG     150        200
2.0 KG     200        250

Expected Output

But What i get is

WEIGHT     SAMECITY   DIFFERENT CITY
0.5 KG     50         100
0.5 KG     50         100
0.5 KG     50         100
0.5 KG     50         100

What i Get

  • 写回答

1条回答 默认 最新

  • dtxzwdl08169 2019-02-02 11:01
    关注

    The problem looks like its coming from your for loop.

    for ($i = 3; $i >= 0; $i--) {
      $samecit = $fnfsamecityperkg + 50;
      $diffcit = $fnfdifferentcityperkg + 50;
      $return_ardr[] = array(
        "samecity" => $samecit,
        "diffcity" => $diffcit,
        "weight" => 0.5,
        "idslab" => $id
      );
    }
    

    Assuming this is where you are creating the 4 values you probably need to try something like:

    $samecit = $fnfsamecityperkg;
    $diffcit = $fnfdifferentcityperkg;
    $weight = 0;
    
    for ($i = 3; $i >= 0; $i--) {
        $samecit +=  50;
        $diffcit +=  50;
        $weight += 0.5;
        $return_ardr[] = array(
            "samecity" => $samecit,
            "diffcity" => $diffcit,
            "weight" => $weight,
            "idslab" => $id
        );
    }
    

    Returns:

    array (size=4)
      0 => 
        array (size=4)
          'samecity' => int 50
          'diffcity' => int 100
          'weight' => float 0.5
          'idslab' => null
      1 => 
        array (size=4)
          'samecity' => int 100
          'diffcity' => int 150
          'weight' => float 1
          'idslab' => null
      2 => 
        array (size=4)
          'samecity' => int 150
          'diffcity' => int 200
          'weight' => float 1.5
          'idslab' => null
      3 => 
        array (size=4)
          'samecity' => int 200
          'diffcity' => int 250
          'weight' => float 2
          'idslab' => null
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)