gpt4 book ai didi

javascript - 单击帖子链接可在另一页上显示完整结果

转载 作者:行者123 更新时间:2023-12-02 22:46:59 28 4
gpt4 key购买 nike

我想单击数据库中带有 id 的链接(在 posts.html 页面上),然后使用 AngularJS、MySQLi 和 PHP 在 display.html 中查看该帖子链接的完整内容。请看下图:

enter image description here

这是我到目前为止所做的事情:帖子.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>

这是我的帖子 Controller :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上显示从特定链接的数据库中获取的结果?

最佳答案

这应该可以完成工作:

在您的配置中:

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

关于javascript - 单击帖子链接可在另一页上显示完整结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58354471/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com