Нажмите на ссылку, чтобы отобразить полные результаты на другой странице. - PullRequest
0 голосов
/ 12 октября 2019

Я хочу щелкнуть ссылку с идентификатором из базы данных (на странице posts.html), а затем просмотреть полное содержание этой ссылки в файле display.html, используя AngularJS, MySQLi и PHP. Пожалуйста, смотрите изображение ниже:

enter image description here

вот что мне удалось сделать до сих пор: Posts.html

<table class="table table-bordered" ng-controller="postController"
       ng-init="show_data()">
    <tr>
        <th>Description</th>
    </tr>
    <tr ng-repeat="x in news">
        <td><a href="">{{x.description}}</a></td>
    </tr>
</table>

Вот мой пост-контроллер: postController.js

"USE STRICT";
app.controller('postController', function ($scope, $http) {

    $scope.show_data = function () {
        $http.get("display.php")
            .success(function (data) {
                $scope.news = data;
            });
    }

});

А вот мой код display.php:

<?php
    require_once "config.php";
    $output = array();
    $query  = "SELECT * FROM news";
    $result = mysqli_query($conn, $query);
    if (mysqli_num_rows($result) > 0) {
    while ($row = mysqli_fetch_array($result)) {
    $output[] = $row;
    }
    echo json_encode($output);
    }
    ?>

Мой display.html:

<div >
    <div>{{id}}</div>
    <div>{{title}}</div>
    <div>{{article}}</div>
    <div>{{tag}}</div>
    <div>{{author}}</div>
</div>

Как отобразить результаты, извлеченные из базы данных конкретной ссылки на display.html?

1 Ответ

1 голос
/ 26 октября 2019

Это должно сделать работу:

В вашей конфигурации:

app
  .config(function ($routeProvider ...) {
    $routeProvider
      ...
      .when('/posts', {templateUrl: 'posts.html', controller: function($scope, $http) {
        $scope.getPosts = function() {
          $http.get('posts.php').then(function (response) {
            $scope.posts = response.data;
          });
        };
      }})

      .when('/display/:id', {templateUrl: 'display.html', controller: function($scope, $routeParams, $http) {
        $http.get('display.php', {id: $routeParams.id}).then(function (response) {
          $scope.post = response.data;
        });
      }})
    })

В posts.html:

<table class="table table-bordered" ng-controller="postController" ng-init="getPosts()">
  <tr>
    <th>Description</th>
  </tr>
  <tr ng-repeat="post in posts">
    <td><a ng-href="display/{{post.id}}">{{post.description}}</a></td>
  </tr>
</table>

В display.html:

<div ng-if="post">
  ...
  <div>{{post.title}}</div>
  ...
</div>

В display.php:

$id = $_GET['id'];
// then retrieve post by this id and return as json item
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...