I am using php database connectivity with "$row" as an array that stores name as a field in it .But after lots of searching i dint get how to pass php variable in angular js here's the code ,I am trying to achieve.how is it possible i am new to this your help will be appreciated.
我使用PHP数据库连接与“$ row”作为一个数组,存储名称作为一个字段。但经过大量的搜索,我得到如何在角js传递php变量这里的代码,我试图实现。我是新手,我的帮助将不胜感激。
<?php
$con = mysqli_connect('localhost','root','','practice');
if (!$con) {
die("couldnt connect". mysqli_error);
}
$q= "select * from test";
$result= $con->query($q);
if ($result->num_rows>0) {
while ($row = $result->fetch_assoc()) {
echo $row['name']."<br>";
}
}
print_r($result);
echo json_encode($result);
?>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.4/angular.js">
</head>
<body>
<ul ng-init="names = <?php echo htmlspecialchars(json_encode($row)); ?>">
<li ng-repeat="x in names">
<p>{{x.name}}</p>
</li>
</ul>
</body>
</html>
3 个解决方案
#1
5
Your php code needs a bit of improvement use this instead of that:
您的PHP代码需要一些改进使用它而不是:
$arr = array();
if ($result->num_rows>0) {
while ($row = $result->fetch_assoc()) {
$arr[] = $row['name'] ;
}
}
Then echo it as
然后回复它
<ul ng-init="names = <?php echo htmlspecialchars(json_encode($arr)); ?>">
#2
1
As discussed earlier, you need to format your data in the names's to;
如前所述,您需要将名称格式化为;
- be a valid valid JSON
- provide a field name since you are seeking it in your loop (ng-repeat)
是一个有效的有效JSON
提供一个字段名称,因为你在循环中寻找它(ng-repeat)
So at the moment the value assigned to your "names" is "eden frank matt" as you mentioned in your comment. this needs to be changed to something like:
所以目前分配给你的“名字”的价值是你在评论中提到的“eden frank matt”。这需要改为:
[
{name: 'eden'},
{frank: 'eden'},
{matt: 'eden'}
]
so when you check the html source we should have a collection of lets say "people" and then going through all of its element and point to their field "name" :
所以当你检查html源代码时,我们应该有一个让人们说“人”的集合,然后遍历它的所有元素并指向它们的字段“name”:
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.4/angular.js">
</script>
</head>
<body ng-app>
<ul ng-init="people = [{name:'eden'}, {name:'frank'}, {name:'matt'}]">
<li ng-repeat="person in people">
<p>{{person.name}}</p>
</li>
</ul>
</body>
</html>
#3
0
I had similar requirement, and the below worked for me.
我有类似的要求,下面对我有用。
data-ng-click="editField(product, '<?php echo $_SESSION["login_user"] ?>')"
#1
5
Your php code needs a bit of improvement use this instead of that:
您的PHP代码需要一些改进使用它而不是:
$arr = array();
if ($result->num_rows>0) {
while ($row = $result->fetch_assoc()) {
$arr[] = $row['name'] ;
}
}
Then echo it as
然后回复它
<ul ng-init="names = <?php echo htmlspecialchars(json_encode($arr)); ?>">
#2
1
As discussed earlier, you need to format your data in the names's to;
如前所述,您需要将名称格式化为;
- be a valid valid JSON
- provide a field name since you are seeking it in your loop (ng-repeat)
是一个有效的有效JSON
提供一个字段名称,因为你在循环中寻找它(ng-repeat)
So at the moment the value assigned to your "names" is "eden frank matt" as you mentioned in your comment. this needs to be changed to something like:
所以目前分配给你的“名字”的价值是你在评论中提到的“eden frank matt”。这需要改为:
[
{name: 'eden'},
{frank: 'eden'},
{matt: 'eden'}
]
so when you check the html source we should have a collection of lets say "people" and then going through all of its element and point to their field "name" :
所以当你检查html源代码时,我们应该有一个让人们说“人”的集合,然后遍历它的所有元素并指向它们的字段“name”:
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.4/angular.js">
</script>
</head>
<body ng-app>
<ul ng-init="people = [{name:'eden'}, {name:'frank'}, {name:'matt'}]">
<li ng-repeat="person in people">
<p>{{person.name}}</p>
</li>
</ul>
</body>
</html>
#3
0
I had similar requirement, and the below worked for me.
我有类似的要求,下面对我有用。
data-ng-click="editField(product, '<?php echo $_SESSION["login_user"] ?>')"