获取数据库数据到客户端

发布于 2024-09-04 15:29:33 字数 436 浏览 16 评论 0原文

我确信这个问题已经被问过一百万次了,但是公认的方法是什么?

我已经编写 php 代码有一段时间了,直到最近我只复制+粘贴了 javascript 代码,但现在在 YUI 的帮助下,我已经开始了解 javascript 并希望在我现有的网络应用程序中更多地使用它。我想从数据库等获取各种数量的数据到客户端 JavaScript。加载时我可以在我的 php 页面中访问这些数据。

将此数据获取到我的客户端脚本的正确方法是什么?

  1. 在我的 php 或 smarty 模板文件中生成客户端 javascript,在我需要的地方插入数据?
  2. 使用 Ajax 调用从 php 文件中检索我需要的信息 - 返回 JSON 数据?
  3. 生成页面所需数据的 JSON 数据构造,然后通过脚本包含返回它或将其转储到生成的页面?
  4. 一些非常明显的事情我没有想到。

I'm sure this has been asked a million times, but what is the accepted approach to this?

I have been writing php code for a while and up until recently I only copied+pasted javascript code, but now with help from YUI I've begun to understand javascript and want to use it more in an existing web app I have. I want to get various amounts of data from databases etc to the clientside javascript. I have access to this data in my php pages when loading.

What is the correct way to get this data to my client side script.

  1. Generate clientside javascript in my php or smarty template files inserting the data where I need it?
  2. Use an Ajax call to retrieve the information I need from a php file - returning JSON data?
  3. Generate a JSON data construct of the data needed by the page and either return it via a script inclusion or dumping it to the generated page?
  4. Something really obvious I haven't thought of.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

铁轨上的流浪者 2024-09-11 15:29:33

我假设你可能使用 YUI 数据表,如果数据集不是很大并且你在页面请求时拥有它,请将其粘贴在本地 JS 变量中(我更喜欢使用 JSON 格式)并包装 YAHOO.util.LocalDataSource在它周围,将其连接到数据表,然后就完成了。

You are probably using YUI Data Table I would assume, if the data set isn't huge and you have it at page request time, stick it in a local JS variable (I prefer using JSON format) and wrap a YAHOO.util.LocalDataSource around it, hook that up to the Data Table and you are set.

虐人心 2024-09-11 15:29:33

如果您可以通过页面发送数据,我会这样做(即第一),但当页面已加载时,使用 Ajax 检索可能是最佳选择。

If you can send the data with the page, I'd do that, (That is, number one) but retrieving with Ajax is probably the best-choice when the page has already been loaded.

情魔剑神 2024-09-11 15:29:33

如果您始终为数据使用良好的数组结构,那么您可以使用生成的页面和ajax - 要么使用数组输出html(直接php,smarty,等等),然后您可以使用json_encode()返回该数据对于ajax的东西。这样,如果用户没有 javascript,您的页面仍然可以工作:)

If you consistently use a good array structure for your data, then you can use both generated pages and ajax - either use the array to output the html (straight up php, smarty, whatever) and then you can return that data using json_encode() for ajax stuff. That way, if the user has no javascript your page still works :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文