admin管理员组

文章数量:1289583

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

$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=".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>

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

$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./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>
Share Improve this question edited Jul 5, 2015 at 10:29 Share fun asked Jul 5, 2015 at 8:36 Share funShare fun 1771 gold badge2 silver badges14 bronze badges 9
  • Your script needs some serous tidy up, your are echoing/printing lots of stuff before even your html starts. if there are purely to test your DB oute then why to mix them the with <br> tags. anyway what is the out put of echo $row['name']."<br>";? when you check the page source will you see any data from the db? change that line to print_r($row['name']); and let me know if the data is fetched and printed (on your html page source) then we will get into solving the problem :) – Ali Commented Jul 5, 2015 at 8:54
  • 1 no @ali it is all fine with database output $row['name'] is perfectly fetching the data but problem is with js how can i show that array data of $row['name'] in angular js – Share fun Commented Jul 5, 2015 at 9:01
  • ok cool, next thing will be including the angular js script, something like <script src="http://ajax.googleapis./ajax/libs/angularjs/1.0.4/angular.js"> – Ali Commented Jul 5, 2015 at 9:07
  • yeah my mistake SORRY but still its showing this "[]" only as a output @Ali – Share fun Commented Jul 5, 2015 at 9:15
  • whats the value of <?php echo htmlspecialchars(json_encode($row)); ?> ? – Ali Commented Jul 5, 2015 at 10:09
 |  Show 4 more ments

3 Answers 3

Reset to default 5

Your php code needs a bit of improvement use this instead of that:

$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)); ?>">

As discussed earlier, you need to format your data in the names's to;

  1. be a valid valid JSON
  2. provide a field name since you are seeking it in your loop (ng-repeat)

So at the moment the value assigned to your "names" is "eden frank matt" as you mentioned in your ment. this needs to be changed to something like:

[
    {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" :

<!DOCTYPE html>
<html>
<head>
    <script src="http://ajax.googleapis./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>

I had similar requirement, and the below worked for me.

data-ng-click="editField(product, '<?php echo $_SESSION["login_user"] ?>')"

本文标签: javascriptPass php variablearray data to angular jsStack Overflow