我需要使用google visualization api从mysql数据库创建dataTable。所以我这样做:
try {
$rez = $db->prepare("SELECT naziv FROM track_aktivnosti WHERE id_akt = :id_akt AND prvi=1 AND user_id=:user_id");
$rez->execute(array(':id_akt' => $_POST['id_akt'], ':user_id' => $user_id));
$kol = $rez->fetchAll();
$rows = array();
$rows1 = array();
$table = array();
$table = array();
$rows[] = array('label' => 'Datum', 'type' => 'string');
foreach ($kol as $r) {
$rows[] = array('label' => (string) $r['naziv'], 'type' => 'string');
}
$table['cols'] = $rows;
// convert data into JSON format
$result = $db->prepare("SELECT datum FROM track_aktivnosti
WHERE id_akt = :id_akt
AND user_id=:user_id
GROUP BY datum ORDER BY datum");
$result->execute(array(':id_akt' => $_POST['id_akt'], ':user_id' => $user_id));
$dates = $result->fetchAll();
$final_result = array();
$rows1 = array();
foreach ($dates as $date) {
$result = $db->prepare("SELECT vrednost, naziv
FROM track_aktivnosti
WHERE id_akt = :id_akt
AND datum = :datum
AND user_id= :user_id");
$result->execute(array(':id_akt' => $_POST['id_akt'],
':datum' => $date['datum'],
':user_id' => $user_id));
$m = array(array('v' => $date['datum']));
foreach ($result as $r) {
$m[] = array('v' => (int) $r['vrednost']);
}
$rows1[] = array('c' => $m);
$table['rows'] = $rows1;
}
} catch (PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
echo json_encode($table);
所以一切都很好但是这段代码改变了行值......正如你可以从mysql数据库和前端dataTable中看到的那样:所以DataTable是:(对于id_akt = 82)
和mysql数据库是:
所以你可以在数据表中看到我得到的fdfdfd
值为56而不是76和类似......我怎么能解决这个问题?
使用JS代码更新:
function drawTroskovnik() {
var cssClassNames = {
'headerRow': 'zaglavlje',
'tableRow': 'red',
'oddTableRow': 'red',
'selectedTableRow': 'orange-background large-font',
'hoverTableRow': 'prekoreda',
'headerCell': 'gold-border',
'tableCell': 'cell',
'rowNumberCell': 'underline-blue-font'
};
// Create and populate the data table.
var JSONObject = $.ajax({
url: 'getTroskovnik.php', // make this url point to the data file
dataType: 'json',
data: {id_akt: ajdi},
async: false,
type: 'POST',
}).responseText;
var data = new google.visualization.DataTable(JSONObject, 0.5);
for (var y = 0, maxrows = data.getNumberOfRows(); y < maxrows; y++) {
for (var x = 1, maxcols = data.getNumberOfColumns(); x < maxcols; x++) {
data.setValue(y, x, '<input vr="' + data.getValue(y, 0) + '" kol="' + data.getColumnLabel(x) + '" class="form-control costRedovi" value="' + data.getValue(y, x) + '">');
}
}
for (var y = 0, maxrows = data.getNumberOfRows(); y < maxrows; y++) {
data.setValue(y, 0, '<input class="span2 form-control" id="pocetak1" size="16" type="text" value="' + data.getValue(y, 0) + '" readonly>');
}
data.addColumn('string', '');
for (var y = 0, maxrows = data.getNumberOfRows(); y < maxrows; y++) {
var mc = data.getNumberOfColumns() - 1;
data.setValue(y, mc, '<div data-toggle="tooltip" data-placement="top" title="Delete this day data"><i class="fa fa-trash-o" ></i></div>');
}
var table = new google.visualization.Table(document.getElementById('tpltroskovnik'));
new google.visualization.events.addListener(table, 'ready', function() {
$(".costRedovi").focusout(function() {
var vrednost = $(this).val();
var datum = $(this).attr('vr');
var tabela = $(this).attr('kol');
console.log(datum + " " + tabela + " " + vrednost);
$.ajax({
url: "updateCost.php",
type: "POST",
async: true,
data: {ajdi: ajdi, datum: datum, tabela: tabela, vrednost: vrednost}, //your form data to post goes here as a json object
dataType: "html",
success: function(data) {
console.log(data);
},
});
});
});
table.draw(data, {'allowHtml': true, cssClassNames: {
'headerRow': 'zaglavlje',
'tableRow': 'red',
'oddTableRow': 'red',
'selectedTableRow': 'orange-background large-font',
'hoverTableRow': 'prekoreda',
'headerCell': 'gold-border',
'tableCell': 'cell',
'rowNumberCell': 'underline-blue-font'}
});
};
答案 0 :(得分:3)
在您提供的PHP代码中,列名和行值之间没有相关性。因此,哪些值落在哪些列取决于从数据库中提取数据的顺序(可能基于性能或插入顺序),并且可能因查询而异。为了获得可预测的订单,您需要在查询中指定要拉取的订单。需要对齐的两个查询是:拉取名称的查询和拉取值的查询。
缺乏可预测的订单:
$rez = $db->prepare("SELECT naziv FROM track_aktivnosti WHERE id_akt = :id_akt AND prvi=1 AND user_id=:user_id");
...
$result = $db->prepare("SELECT vrednost, naziv FROM track_aktivnosti WHERE id_akt = :id_akt AND datum = :datum AND user_id= :user_id");
在每个末尾添加ORDER BY naziv
以将结果排序为可预测的对齐,然后在关联数组中正确加载,然后是JSON对象。
以下是您的代码的调整版本,清理了一些其他非关键问题:
<?php
try {
$res = $db->prepare('SELECT naziv FROM track_aktivnosti WHERE id_akt = :id_akt AND user_id = :user_id AND prvi = 1 ORDER BY naziv');
$res->execute(array(':id_akt' => $_POST['id_akt'], ':user_id' => $user_id));
$kol = $res->fetchAll();
$rows = array();
$table = array();
$rows[] = array('label' => 'Datum', 'type' => 'string');
foreach ($kol as $r) {
$rows[] = array('label' => (string) $r['naziv'], 'type' => 'string');
}
$table['cols'] = $rows;
$res = $db->prepare('SELECT datum FROM track_aktivnosti WHERE id_akt = :id_akt AND user_id = :user_id GROUP BY datum ORDER BY datum');
$res->execute(array(':id_akt' => $_POST['id_akt'], ':user_id' => $user_id));
$dates = $res->fetchAll();
$rows = array();
foreach($dates as $date){
$res = $db->prepare('SELECT vrednost FROM track_aktivnosti WHERE id_akt = :id_akt AND user_id = :user_id AND datum = :datum ORDER BY naziv');
$res->execute(array(':id_akt' => $_POST['id_akt'], ':user_id' => $user_id, ':datum' => $date['datum']));
$vals = $res->fetchAll();
$m = array( array( 'v' => $date['datum'] ) );
foreach($vals as $r) {
$m[] = array('v' => (int) $r['vrednost']);
}
$rows[] = array('c' => $m);
$table['rows'] = $rows;
}
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
echo json_encode($table);
?>