Reputation: 2013
I want to replace this code from:
var viewModel = function(){
var self = this;
self.people = ko.observableArray([
{ firstName: 'James', lastName: 'Smith', age: 38 },
{ firstName: 'Susan', lastName: 'Smith', age: 36 },
{ firstName: 'Jeremy', lastName: 'Smith', age: 10 },
{ firstName: 'Megan', lastName: 'Smith', age: 7 },
{ firstName: 'James', lastName: 'Jones', age: 40 },
{ firstName: 'Martha', lastName: 'Jones', age: 36 },
{ firstName: 'Peggy', lastName: 'Jones', age: 10 }
]);
with select sql query that i used in PHP
$result=mysql_query("SELECT * FROM table ");
while($fak = mysql_fetch_array($result)){}
the result will echo in this code
<table>
<thead>
<tr data-bind="foreach: headers">
<th data-bind="click: $parent.sort, text: title"></th>
</tr>
</thead>
<tbody data-bind="foreach: filteredPeople">
<tr>
<td data-bind="text: firstName"></td>
<td data-bind="text: lastName"></td>
<td data-bind="text: age"></td>
</tr>
</tbody>
</table>
help please thanks a lot
Upvotes: 1
Views: 317
Reputation: 1870
If you want to pass the results of the select query to JavaScript, which is what you say in the title of your question, this is how you could do that based on the sample JS you provided:
<?php
$jsArr = '';
$result=mysql_query("SELECT * FROM table xxxx");
$rowCount = mysql_num_rows($result);
$i = 0;
while($fak = mysql_fetch_array($result)) {
$i++;
$jsArr .= '{firstName:\'' . $fak["firstName"] . '\',lastName:\'' . $fak["lastName"] . '\',age:' . $fak["age"] . '}';
if ($i<$rowCount) { $jsArr .= ','; } // Add comma after all but the final row
}
?>
var viewModel = function() {
var self = this;
self.people = ko.observableArray([<?php echo $jsArr; ?>]);
});
It looks like you've already got something that inserts the JavaScript data into the HTML.
Upvotes: 1