【问题标题】:Perform CRUD operation on json using angular.?使用 Angular 对 json 执行 CRUD 操作。?
【发布时间】:2017-09-10 07:53:40
【问题描述】:

使用我的 SQL 数据库中的 php 生成了一个.json 文件。 我在前端使用 angular,在后端使用 php。 `

$query = "SELECT * FROM users";
$rows = array();
$result = mysqli_query($connection,$query);

while($r = mysqli_fetch_assoc($result)){

    $rows[] = $r;
}

print json_encode($rows);
$fp = fopen('results.json', 'w');
fwrite($fp, json_encode($rows));
fclose($fp);

我需要执行 CRUD。我应该怎么做才能让我的 .json 文件自动更新。?

【问题讨论】:

  • 为什么要从数据库生成 .json 文件?你能不能简单地将 json 从 php 传递给 angular 并进行操作,然后相应地更新数据库?
  • 我正在使用角度显示我的 json 文件的内容。不想直接通过 php 显示内容
  • 好吧..你能不能从你的 .json 文件中抓取整个 json 并执行操作并再次覆盖整个文件?
  • 你不认为每次数据库更改都会重新加载整个 json 文件。
  • 您的意思是说您对将更改从 angular 传递到 php 有疑问?

标签: php mysql angularjs json


【解决方案1】:

您可以创建一个change.phpupdate.php 文件,在执行时它会在服务器端覆盖您的.json 文件。

<?php
$data = json_decode(file_get_contents("php://input"),true);
file_put_contents('file.json', $data);
?>

现在,您可以通过 $http.get() 从服务器获取 file.json 并将其存储到您的 $scope 或 angularjs 中的任何其他变量中。

然后您可以对该变量进行 CRUD 操作。

您现在只需拨打$http.post('URL/chage.php',YOUR_VARIABLE)

这只会用新内容覆盖您的 .json 文件。 所以每当你请求从.json文件中获取json时,你总会得到更新的内容。

【讨论】:

  • 是否有任何其他过程我可以使用而不是生成 json 文件
  • 是的。为此类操作生成 .json 文件不是一个好习惯。 @RinkuMalik
  • 如果您想了解它,请告诉我@RinkuMalik
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-24
  • 1970-01-01
  • 1970-01-01
  • 2021-08-07
  • 2016-07-01
相关资源
最近更新 更多