I am getting a bunch of data from the db and want to display all the data in a table, but the data doesn't come sorted. Is there a way I can sort or map the object of arrays so I know what content goes with what so it shows the correct data in each row?
Here is my ajax call to get the data:
//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();
}
});
Here is where I draw the table with the data:
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);
}
Here is a preview of what the data actually looks like:
Object {
PageNum: Array(66),
RowNum: Array(66),
RowStatus: Array(66),
RowData: Array(66),
length: 66
}
PageNum and RowNum are arrays of numbers not in order. Row status and RowData are arrays of strings not in order.
Here is the whole PHP file for getting the data from the db:
$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); }
Here's my initial PHP function to take the data and transform it to an object of arrays.. how could I change this to just return the data in array of arrays?
// 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;
}
TransformToObjectWithArrays? I.e. your$sp, and$params?odbc_fetch_arraythat is an array of rows, each row being an associative array of columns, a structure perfect for sorting; then you transpose it into an associative array of columns, each column being an array of values; then you send it to clientside? Why not justjson_encodethe result ofodbc_fetch_arrayas it is and send that? It is much easier to display as a table, easier to sort, easier to... everything.