在 Javascript 中循环遍历 php

发布于 2024-10-31 01:58:12 字数 1319 浏览 0 评论 0原文

我正在我的硬盘上或我的网站上运行一个 javascript 文件,现在,我一直在尝试让它从我的硬盘上运行。 javascript 循环并构建一个具有 x 行数的表。每行都有一个“文本区域”框,用户可以在其中输入共同基金股票代码。我想用 mysql 数据库中的数据填充文本区域框。根据我从 Will 那里得到的建议,我正在使用 PHP 文件返回 JSON,但我不知道如何迭代 JSON 并在 javascript 表的每一行中放置一个基金符号。

下面是我网站上的 php 文件

<?php
$connect = >>>>>my connection string here<<<<<;
mysql_select_db("altatech_grid");
$result = mysql_query("SELECT FSymbol FROM FundSymbols"); 
$to_encode = array(); 
while($row = mysql_fetch_assoc($result)) {
  $to_encode[] = $row['FSymbol'];
} 
$myFund = json_encode($to_encode);
echo ($myFund);
?> 

,它返回以下内容<<<<<<<<

["FDGRX","FVINX","VCVLX","LLPFX "]

这是我的 javascript <<<<<<<<<

document.write("<table>");
nButtons = 0;
for (row = 1; row < nItems; row++)
{
document.write("<tr>");
for (rowButton = 0; rowButton < row; rowButton++)
document.write("<td><textarea class='unorderedlist' wrap='soft' name='possibility" + (row+1) + "' id='p" + (row+1) + "'>")";
########### This Is Where The PHP Needs To Put One Item In Each Row ############
document.write("</textarea></td>");
document.write("</tr>");
document.write("</table>");
}

I am running a javascript file either on my hard drive or from my website, right now, I have been trying to get this to work from my hard drive. The javascript loops through and builds a table with x number of rows. Each row has a "textarea" box where users can type in mutual funds ticker symbols. I would like to populate the textarea boxes with data from a mysql database. From the advice I got from Will, I am using a PHP file to return JSON, but I can't figure out how to iterate through the JSON and put one fund symbol in each row of my javascript table.

below is my php file on my website

<?php
$connect = >>>>>my connection string here<<<<<;
mysql_select_db("altatech_grid");
$result = mysql_query("SELECT FSymbol FROM FundSymbols"); 
$to_encode = array(); 
while($row = mysql_fetch_assoc($result)) {
  $to_encode[] = $row['FSymbol'];
} 
$myFund = json_encode($to_encode);
echo ($myFund);
?> 

which returns this below <<<<<<

["FDGRX","FVINX","VCVLX","LLPFX "]

this is my javascript below <<<<<<<

document.write("<table>");
nButtons = 0;
for (row = 1; row < nItems; row++)
{
document.write("<tr>");
for (rowButton = 0; rowButton < row; rowButton++)
document.write("<td><textarea class='unorderedlist' wrap='soft' name='possibility" + (row+1) + "' id='p" + (row+1) + "'>")";
########### This Is Where The PHP Needs To Put One Item In Each Row ############
document.write("</textarea></td>");
document.write("</tr>");
document.write("</table>");
}

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

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

发布评论

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

评论(2

眼睛会笑 2024-11-07 01:58:12

这不是 JSON,它是一个数组...

var i;
var html = "<textarea>";
var symbols = ["FDGRX","FVINX","VCVLX","LLPFX"];
for (i = 0; i < symbols.length; i++) {
    html += symbols[i] + "\r\n";
}
var html += "</textarea>";
document.write(html);

JSON 对象如下所示:

var jsonObject = { name: "John", location: "UK" };

您可以像这样添加动态值:

var symbols = <?php echo $myFund; ?>;

This isn't JSON, it's an array...

var i;
var html = "<textarea>";
var symbols = ["FDGRX","FVINX","VCVLX","LLPFX"];
for (i = 0; i < symbols.length; i++) {
    html += symbols[i] + "\r\n";
}
var html += "</textarea>";
document.write(html);

JSON objects look like this:

var jsonObject = { name: "John", location: "UK" };

And you could add your dynamic values like this:

var symbols = <?php echo $myFund; ?>;
情丝乱 2024-11-07 01:58:12

我认为没有办法做到如你所愿。因为,PHP是在服务器上运行的脚本。因此,您无法使用 Javascript 从客户端运行 PHP 脚本。
但你可以像下面这样做:

<script type="text/javascript">
    var data = <?php echo json_encode($to_encode);?>;
    //At client it will be:
    //var data = ["FDGRX","FVINX","VCVLX","LLPFX"];
    //Then you can loop:
    for(var row = 0; row < data.length; row++)
    {
        //@Todo
        //.................
        document.write(data[row]);
    }
</script>

I think that there is no way to do like you want. Because, PHP is script run at server. So you could not run a PHP script from client using Javascript.
But you can do like below:

<script type="text/javascript">
    var data = <?php echo json_encode($to_encode);?>;
    //At client it will be:
    //var data = ["FDGRX","FVINX","VCVLX","LLPFX"];
    //Then you can loop:
    for(var row = 0; row < data.length; row++)
    {
        //@Todo
        //.................
        document.write(data[row]);
    }
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文