我想从以下代码中替换此代码:
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 }
]);
使用我在PHP中使用的select sql查询
$result=mysql_query("SELECT * FROM table ");
while($fak = mysql_fetch_array($result)){}
结果将在此代码中回显
<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>
请帮忙
非常感谢
答案 0 :(得分:1)
如果您想将select查询的结果传递给JavaScript,这就是您在问题标题中所说的内容,那么您可以根据自己提供的示例JS执行此操作:
<?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; ?>]);
});
看起来你已经有了将JavaScript数据插入HTML的东西。