使用php从base获取行数据

时间:2014-11-03 21:46:09

标签: javascript php mysql pdo

这里我有一个dataRange循环,但对于这种情况,它不重要,比dateRange更重要$i

所以我有:

  $dateString = '09.03.2014';
$startDate = new DateTime($dateString);

$period = new DateInterval('P1M');
$endDate = clone $startDate;
$endDate->add($period);

$interval = new DateInterval('P1D');
$daterange = new DatePeriod($startDate, $interval ,$endDate);
$i=1;
foreach($daterange as $date){
        $temp = array();
        // the following line will be used to slice the Pie chart
        $temp['ID'] = $i;
        $temp['datum'] = $date->format("d/m") . PHP_EOL;

        $rs1 = $db->prepare('SELECT naziv FROM aktivnosti WHERE user_id=:idd');
              $rs1->bindParam(':idd', $i); 

              $rs1->execute();
              $naz = $rs1->fetchColumn();
                      if ($naz != false) {
              $temp['vrsta'] =  $naz;
                      } else {
                         $temp['vrsta'] =  '';
                      }


        $output['data'][] = $temp;
        $i++;
    }
    $jsonTable = json_encode($output);

正如你所看到我有循环和$ temp ['vrsta']我想从表aktivnosti获取数据。

我还在前端有JS UI层,它从数据中生成一个html:

 $('#example').dataTable( {
        "ajax": "table1.php",
        "columns": [
            { "data": "ID" },
            { "data": "datum" },
            { "data": "naziv" },
            { "data": "vrsta" },

        ],
        "lengthMenu": [ 31 ],
        "columnDefs": [ {
    "targets": 3,
    "data": "download_link",
    "render": function ( data, type, full, meta ) {
      if (data != '') {
      return '<button class="btn btn-success">'+data+'</button>';
      }else {
          return data;
      }
    }
  } , etc...

现在我的代码只获取第一行数据所以问题是因为我在表aktivnosti 上有很多行,具有相同的user_id所以当我在aktivnosti上有很多相同的user_id时我还需要创建'' +数据+ '';从表aktivnosti的每一行,但我不知道该怎么做?

一些想法?我怎么能这样做?

2 个答案:

答案 0 :(得分:1)

考虑使用这样的东西:

"columnDefs": [{
  "targets": 3,
  "createdCell": function(td,data,row,rIndex,cIndex) {
   // actions for every cell generated on the column you specified.
  }
}]

更多信息:

https://datatables.net/reference/option/columns.createdCell

答案 1 :(得分:1)

这将是这样的......

      $startDate = date('Y-m-d',strtotime("2013-01-01")); //put first date here
      $endDate = date('Y-m-d',,strtotime("2013-12-31")); //put second date here

      $rs1 = $db->prepare('SELECT * FROM aktivnosti WHERE DATE(start_date)>=DATE('.$startDate.') AND DATE(end_date)<=('.$endDate.') GROUP BY user_id');
      $rs1->execute();
      $naz = $rs1->fetchAll();

      $jsonTable = json_encode($naz);

请注意您的日期列我命名为start_date和end_date。我不知道你的专栏是什么。