John Smith John Smith - 1 year ago 47
HTML Question

Calling a JavaScript function based on PHP, PHP in JavaScript

I have this JavaScript code that creates a table, and puts in the rows and columns.
I want to call this function, based upon some data in a database. So the amount of rows is based upon the amount of data in the database.
My data in my database is a markscheme, I want the the table to fill depending upon the amount of questions are in the database

I have tried and here is my code:

hasLoaded = true;//page loaded set to true

$query = "SELECT maxMark, criteria FROM mark ";
$result = mysql_query($query);

while ($result != null)
addRow();//add row with everything in

Answer Source

The correct answer is json_encode your data from PHP and store it in a JavaScript variable like this.

  // populate the php rows array
  $rows = array();
  while ($row = mysql_fetch_assoc($result)) {
    $rows = array_push($rows, $row);
// populate the javascript rows array
var rows = <?php echo json_encode($rows); ?>;
for (var = 0; i < rows.length; i++) {