0

I'm trying the following AngularJS tutorial at w3schools.

Link to w3schools tutorial

I changed the script to make it work with one of my php script:

<!DOCTYPE html>
<html >
<style>
table, th , td  {
  border: 1px solid grey;
  border-collapse: collapse;
  padding: 5px;
}
table tr:nth-child(odd) {
  background-color: #f1f1f1;
}
table tr:nth-child(even) {
  background-color: #ffffff;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="customersCtrl">

<table>
  <tr ng-repeat="x in names">
    <td>{{ x.name }}</td>
  </tr>
</table>

</div>

<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
       $http.get("http://wirtschaftsinformatik.web2page.ch/selectModule.php")
//my url
       .then(function (response) {$scope.names = response.data.records;});
    });
    </script>

    </body>
    </html>

This is actually not working. I guess its because of the php file, here is the code:

<?PHP
    //Verbindung zur Datenbank
    include "inc_mysql.php";

    //SQL-String
    $select = "SELECT * FROM module ORDER BY name ASC";

    // Codierung
    mysql_set_charset("utf8");

    //SQL-Befehl in Variable speichern
    $sql = mysql_query($select);

    //Array erstellen
    $jsonArray = array();

    while ($row = mysql_fetch_array($sql)){ 
        $jsonArray[] = $row;
    }

    echo json_encode($jsonArray, JSON_UNESCAPED_UNICODE);   
?>

Do you figure out what is missing?

Here is the JSON output

[{"0":"12","id":"12","1":"Betriebsbuchhaltung","name":"Betriebsbuchhaltung","2":"2016-10-19 15:44:16","timestamp":"2016-10-19 15:44:16"},{"0":"13","id":"13","1":"Datenbanken","name":"Datenbanken","2":"2016-10-28 20:35:49","timestamp":"2016-10-28 20:35:49"},{"0":"6","id":"6","1":"Logistik","name":"Logistik","2":"0000-00-00 00:00:00","timestamp":"0000-00-00 00:00:00"},{"0":"14","id":"14","1":"Statistik","name":"Statistik","2":"2016-11-02 15:13:13","timestamp":"2016-11-02 15:13:13"},{"0":"1","id":"1","1":"System Engineering","name":"System Engineering","2":"0000-00-00 00:00:00","timestamp":"0000-00-00 00:00:00"}]
4
  • what is not working ? table does not display records? Commented Nov 26, 2016 at 13:44
  • Yes, the table is not displayng the records. Commented Nov 26, 2016 at 13:45
  • can you post the json Commented Nov 26, 2016 at 13:45
  • @Sajeetharan I added it. Commented Nov 26, 2016 at 13:47

2 Answers 2

1

the returned json has no records property, here's a simple solution:

echo json_encode(["records"=>$jsonArray], JSON_UNESCAPED_UNICODE); 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, but something is still missing because it doesnt work still. I create a new file: wirtschaftsinformatik.web2page.ch/test.php
1

HTML

 <div ng-controller="listController">
    <table>
      <tr>
        <th>Id</th>
        <th>Name</th>
        <th>Time</th>
      </tr>
      <tr ng-repeat="person in names">
        <td>{{person.id}}</td>
        <td>{{person.name}}</td>
        <td>{{person.timestamp}}</td>
      </tr>
    </table>
  </div>

DEMO

4 Comments

Thank you very much! Its works great! I see you created a JSON File. Isnt there a way to get it directly from php script? I
yes you can use the get http call and do the same. mark as answer if it has helped
Thank You! How to you mean? I put wirtschaftsinformatik.web2page.ch/selectModule.php but it didnt work.
@EroStefano you cant directly get the data from angular, you need to do that in php . check this link stackoverflow.com/questions/25064570/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.