i want use pivottable.js on data pulled sql server using php. examples(http://nicolas.kruchten.com/pivottable/examples/) files json , csv. data connection html page below. how can directly use data on pivottable.js?
$server = "sqlserver"; $connectioninfo=array("database"=>"sqldatabase","characterset" => "utf-8"); $conn= sqlsrv_connect($server,$connectioninfo); $sql ="select column1,column2,column3,column4 table"; $stmt = sqlsrv_prepare( $conn, $sql, array(), array('scrollable' =>'buffered')); if( $stmt === false ) { die( print_r( sqlsrv_errors(), true)); } sqlsrv_execute($stmt); while($row = sqlsrv_fetch_array($stmt)) { echo "<li>" . $row["column1"] . $row["column2"] . "</li>"; } echo "<table border=1><tr><th>head1</th><th>head2</th><th>head3</th><th>head4</th></tr>"; while( $row = sqlsrv_fetch_array( $stmt, sqlsrv_fetch_numeric)) { echo "<tr><td>".$row[0]."</td><td>".$row[1]."</td><td>".$row[2]."</td> <td>".number_format($row[3], 0, ',', '.')."</td></tr>"; } echo "</table>"; sqlsrv_free_stmt( $stmt); sqlsrv_close( $conn);
you generate json array. should like
[ ["head1", "head2", "head3", "head4"], ["va1", "val2", "val3", 123.45], ... ]
on page may query php via $.ajax , use pivottable.js result. $ajax conviniet show 'waiting' gif , hide in 'success' finction.
$.ajax({ type: 'post', url: 'https://path_to_your_php_script', data: { param1: "value1", param2: "value2", ... }, success: function (response) { $("#output").pivotui(eval(response), { rows: ["head1", "head2"], cols: ["head3"], vals: ["head4"] }); } });
other way
$.getjson("https://path_to_your_php_script", function(mps) { $("#output").pivotui(mps, { rows: ["head1", "head2"], cols: ["head3"], vals: ["head4"] }); });
Comments
Post a Comment