Thursday, November 10, 2016

Take a csv file as input argument and print it in table form using PHP ,JavaScript language and JSON

///////////////////////////////////////////////////////////////////////////////////////////////////
//// index.html
///////////////////////////////////////////////////////////////////////////////////////////////////
<html>
<head>
<script type="text/javascript" src="detail.js"></script>
<script type="text/javascript">

  document.write("<table>");
  ajax( {
    url: 'fetchData.php',
  type: 'POST',
dataType: "json",
  data: {"fileName":"details.csv"},
    success: function(response) {
response=response.data;
document.write("<table border=\"2\">");
Object.keys(response[0]).forEach(function(key) {
     
document.write("<th bgcolor=\"red\">"+key+"</th>");
    });


for (var i = 0, len = response.length; i < len; i++) {
document.write("<tr bgcolor=\"yellow\">");
             Object.keys(response[i]).forEach(function(key) {
     
document.write("<td>"+response[i][key]+"</td>");
    });
document.write("</tr>");
        }
document.write("</table>");

},
    error: function( response ) { alert( response ); }
   } );
   document.write("</table>");
</script>
</head>
<body>
</body>
</html>

///////////////////////////////////////////////////////////////////////////////////////////////////
//// End index.html
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
//// fetchData.php
///////////////////////////////////////////////////////////////////////////////////////////////////

<?php
$isRequest = true;
if( !$isRequest ) {
if( count( $argv ) == 1 ) {

echo "Enter a csv file name ";

$in = fopen( "php://stdin", "r" );
$fileName = trim( fgets( $in ) );

} else {
$fileName = trim( $argv[1] );
}
} else {
$fileName = ( isset( $_REQUEST['fileName'] ) ? $_REQUEST['fileName'] : "deviceList2.csv" );
}

if( !$isRequest ) echo "Reading CSV from  $fileName.\n";

$fp = fopen( $fileName, "r" );

$addNewLine = false;

$fields = false;

$json = "{ ";
if( !$fp ) {
$json .= "\"status\": 0, \"statusString\": \"Could not open specified file.\" } ";
echo $json;
die;
}

$json .= " \"data\": [";

if( $addNewLine ) $json .= "\n";

$total = 0;

while( $line = fgets( $fp ) ) {

$rowValues = preg_split( "*,*", trim( $line ) );

if( !$fields ) {
$fields = $rowValues;
} else {

$json .= " { ";
if( $addNewLine ) $json .= "\n";

for( $i = 0; $i < count( $fields ); $i++ ) {

$field = $fields[ $i ];
$value = $rowValues[ $i ];

$json .= "   \"$field\": \"$value\", ";
if( $addNewLine ) $json .= "\n";

}

$json = substr( $json, 0, -2 );
if( $addNewLine ) $json .= "\n";
$json .= " }, ";

$total++;

}

}

$json = substr( $json, 0, -2 );
$json .= " ], ";
$json = substr( $json, 0, -2 );
$json .= " }";
if( $addNewLine ) $json .= "\n";
echo $json;
?>
///////////////////////////////////////////////////////////////////////////////////////////////////
//// End fetchData.php
///////////////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////////////
//// detail.js
///////////////////////////////////////////////////////////////////////////////////////////////////
function buildAjaxArrayObject( par, obj ) {

var dataSt= '';

for( var what in obj ) {

if( typeof( obj[ what ] ) == 'array' || typeof( obj[ what ] ) == 'object' ) {
dataSt += buildAjaxArrayObject( par + '[' + what + ']', obj[ what ] );
} else {
dataSt += par + '[' + what + ']=' + obj[ what ] + '&'
}

}

return dataSt;

}

function ajax( data ) {

if( data.async == undefined ) {
data.async = true;
}

if( data.beforeSend ) {
data.beforeSend( data );
}

var dataString = '';
if( data.data ) {
for( var what in data.data ) {
if( typeof( data.data[ what ] ) == 'array' || typeof( data.data[ what ] ) == 'object' ) {
dataString += buildAjaxArrayObject( what, data.data[ what ] );
} else {
dataString += what + '=' + data.data[what] + '&';
}
}
}
dataString = dataString.substr( 0, dataString.length - 1 );

var xmlHttp;
try {
xmlHttp = new XMLHttpRequest();
} catch( e ) {
return;
}

var responseFunction = function() {
if( xmlHttp.readyState == 4 ) {

var reponse = '';
if( xmlHttp.responseText ) {
if( xmlHttp.getResponseHeader( 'Content-Type' ) == 'text/xml' ) {
response = xmlHttp.responseXML;
} else {
if( window.JSON && window.JSON.parse ) {
response = window.JSON.parse( xmlHttp.responseText );
} else {
response = (new Function("return " + xmlHttp.responseText))();
}
}
}
if( xmlHttp.status == 200 ) {
if( data.success ) {
data.success( response );
}
} else {
if( data.error ) {
data.error( response );
}
}
if( data.complete ) {
data.complete.call( response );
}

}

}

xmlHttp.open( 'POST', data.url, data.async );
xmlHttp.setRequestHeader( 'Content-type', 'application/x-www-form-urlencoded' );
if( data.async ) {
xmlHttp.onreadystatechange = responseFunction;
}
xmlHttp.send( dataString );
if( !data.async ) {
responseFunction.call();
}
return true;
}

///////////////////////////////////////////////////////////////////////////////////////////////////
//// End detail.js
///////////////////////////////////////////////////////////////////////////////////////////////////


No comments:

Post a Comment