Doesn't work sortint in 0 index.
Doesn't work sortint in 0 index.
Please help with sorting. All colums work, bit only first doesn't
"columns": [
{
"data": "id",
"title": "
№
<i class='fas fa-filter filter-icon' data-column-index='0' style='position: absolute; bottom: 430px; left: 24px;' onclick='event.stopPropagation();'></i>",
"render": function (data) {
return '
<img src="delete.png" alt="Удалить" class="deleteRow" data-id="' + data + '">';
},
},
<?php
$start = isset($_POST['start']) ? $_POST['start'] : 0;
$length = isset($_POST['length']) ? $_POST['length'] : 20;
$draw = isset($_POST['draw']) ? $_POST['draw'] : 1;
$order = isset($_POST['order'][0]) ? $_POST['order'][0] : null;
$search = isset($_POST['search']['value']) ? $_POST['search']['value'] : '';
$conn = new mysqli("localhost", "root", "HelloWorld2023!", "logbook");
if ($conn->connect_error) {
die("Ошибка: " . $conn->connect_error);
}
$columns = array(
'id',
'date',
'partner',
'category',
'reason',
'fromWho',
'description',
'dateToSend',
'toWho',
'howSend',
'position'
);
$sql = "SELECT * FROM logbook WHERE 1 = 1";
if (!empty($search)) {
$sql .= " AND (";
for ($i = 0; $i < count($columns); $i++) {
if ($i > 0) {
$sql .= " OR ";
}
$sql .= $columns[$i] . " LIKE '%" . $search . "%'";
}
$sql .= ")";
}
if (!empty($order) && !empty($order['column']) && array_key_exists($order['column'], $columns)) {
$columnIndex = $order['column'];
$orderDir = $order['dir'];
$sql .= " ORDER BY " . $columns[$columnIndex] . " " . $orderDir;
}
if ($length != -1) {
$sql .= " LIMIT $start, $length";
}
$result = $conn->query($sql);
if ($result) {
$data = array();
while ($row = $result->fetch_assoc()) {
$data[] = $row;
}
if ($length == -1) {
$totalRecords = count($data);
$filteredRecords = $totalRecords;
} else {
$totalRecords = $conn->query("SELECT COUNT(id) as total FROM logbook")->fetch_assoc()['total'];
$filteredRecords = mysqli_num_rows($result);
}
$response = array(
"draw" => intval($draw),
"recordsTotal" => intval($totalRecords),
"recordsFiltered" => intval($totalRecords),
"data" => $data,
);
echo json_encode($response);
} else {
echo "Ошибка при выполнении запроса: " . $conn->error;
}
$conn->close();
<?php > ?>This question has accepted answers - jump to:
Answers
I fixed
$orderColumn = isset($_POST['order'][0]['column']) ? $_POST['order'][0]['column'] : 0;
$orderDir = isset($_POST['order'][0]['dir']) ? $_POST['order'][0]['dir'] : 'asc';
if (array_key_exists($orderColumn, $columns)) {
$sql .= " ORDER BY " . $columns[$orderColumn] . " " . $orderDir;
}
Thanks for posting back. Good to hear you've got it sorted
Allan