【发布时间】:2017-09-25 21:00:33
【问题描述】:
我从数据库中获取了一堆数据,并想在一个表中显示所有数据,但数据没有排序。有没有一种方法可以对数组对象进行排序或映射,以便我知道什么内容与什么内容相匹配,从而在每一行中显示正确的数据?
这是我获取数据的 ajax 调用:
//get the content, display all pages or specified page num
$.ajax({
type: 'POST',
url: 'qry/getRawText.php',
async: true,
data: {FileId: fileId, PageNum: pageNum},
success: function(response) {
getRawTextResponse = JSON.parse(response);
drawTextCodeContent();
}
});
这里是我用数据绘制表格的地方:
function drawTextCodeContent() {
fileText = "<table id='fileTextTableId'><tbody>";
//loop through text files
for(i=0;i<getRawTextResponse.length;i++) {
//remove null from text
if(getRawTextResponse["RowData"][i] == null) {
getRawTextResponse["RowData"][i] == "";
}
//draw row
fileText += "<tr class='rowToEdit " + getRawTextResponse["RowStatus"][i] + "'><td class='hidden'>"+ getRawTextResponse["PageNum"][i] +"</td><td class='rowNumTd'>"+ getRawTextResponse["RowNum"][i] +"</td><td class='rowContentTd'><pre>"+ getRawTextResponse["RowData"][i] +"</pre></td><td class='rowCheckboxTd'><label class='mdl-checkbox mdl-js-checkbox mdl-js-ripple-effect mdl-data-table__select' for='row[" + getRawTextResponse["RowNum"][i] + "]'><input type='checkbox' id='row[" + getRawTextResponse["RowNum"][i] + "]' class='mdl-checkbox__input' /></label></td></tr>";
}
fileText += "</tbody></table>";
//display table in div
$("#textCodeDiv").html(fileText);
}
这是数据实际外观的预览:
Object {
PageNum: Array(66),
RowNum: Array(66),
RowStatus: Array(66),
RowData: Array(66),
length: 66
}
PageNum 和 RowNum 是不按顺序排列的数字数组。 行状态和行数据是不按顺序排列的字符串数组。
这是从数据库中获取数据的整个 PHP 文件:
$fileId = $_POST["FileId"];
$pageNum = $_POST["PageNum"];
// Populate the query string
$tsql = "Exec TxRrcT1.GetRawText @FileId = ?, @PageNum = ?";
// Attempt to connect to SQL Server
if ( strtoupper(substr(PHP_OS, 0, 3)) == "WIN" ){
$conn = odbc_connect($connection_string, $user, $pass, SQL_CUR_USE_ODBC);
if ($conn === false ){ die(); }
}
else {
$conn = odbc_connect($connection_string, $user, $pass);
if ($conn === false ){ die(); }
}
// Prepare the stored procedure
$sp = odbc_prepare($conn, $tsql);
$params = array(
$fileId, //@FileId
$pageNum //@PageNum
);
// Execute procedure
$qryResults = TransformToObjectWithArrays($sp, $params);
// Close the connection
if ($conn !== false){ odbc_close($conn); }
// Return the query results
if (isset($qryResults)){ echo json_encode($qryResults); }
这是我最初的 PHP 函数,用于获取数据并将其转换为数组对象。我怎样才能将其更改为仅返回数组数组中的数据?
// This function will prepare a tsql procedure and return the ODBC result
// identifier that can be used once the procedure is executed
function GetQryReturn($sp, $params){
$qryReturn = odbc_execute($sp, CleanInputs($params));
if ($qryReturn === false){ die(); }
return $sp;
}
function CleanInputs($InputArray){
return array_map(function($value){return $value === "" ? NULL : $value;}, $InputArray);
}
// This function takes a prepared stored procedure and any parameters and
// returns the query results as an objects with arrays
// example:
// {key1: [va1,val2], key2: [val1,val2]}
function TransformToObjectWithArrays($sp, $params){
// Execute query
$qryReturn = GetQryReturn($sp, $params);
// Populate array with results
$qryResults = array();
if( odbc_num_rows($qryReturn) ){
for($i=1; $i<=odbc_num_fields($qryReturn); $i++){
$qryResults[odbc_field_name($qryReturn, $i)] = array();
}
$qryResults["length"] = 0;
while( $row = odbc_fetch_array($qryReturn) ){
foreach($row as $key => $value){
array_push($qryResults[$key], $value);
}
$qryResults["length"] += 1;
}
}
else{
$qryResults["length"] = 0;
}
return $qryResults;
}
【问题讨论】:
-
如果从db查询数据时对数据进行排序怎么办?就像使用参数来选择它必须对其进行排序的方式一样。我认为如果它是一堆数据并且没有分页,那么在 js 中排序可能会给您的应用在渲染之前带来糟糕的性能。
-
你要传递什么给
TransformToObjectWithArrays? IE。你的$sp和$params? -
@MajidFouladpour 我更新了问题
-
所以...忽略您可能只是要求 SQL 对您的数据进行排序这一事实,让我确保我明白了:您收到来自
odbc_fetch_array的响应,这是一个行数组,每一行都是一个关联的列数组,一个非常适合排序的结构;然后将其转置为一个关联的列数组,每一列都是一个值数组;然后你把它发送到客户端?为什么不直接将json_encode的结果odbc_fetch_array发送出去?它更容易显示为表格,更容易排序,更容易......一切。 -
我不是数据库专家,我被告知要在客户端对其进行排序。如果我理解你,我相信我已经对 $qryResults 进行了 json_encoding .. 这是我给出的第一个 php 示例
标签: javascript php jquery arrays sorting