在 Javascript 中循环遍历 php
我正在我的硬盘上或我的网站上运行一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这不是 JSON,它是一个数组...
JSON 对象如下所示:
您可以像这样添加动态值:
This isn't JSON, it's an array...
JSON objects look like this:
And you could add your dynamic values like this:
我认为没有办法做到如你所愿。因为,PHP是在服务器上运行的脚本。因此,您无法使用 Javascript 从客户端运行 PHP 脚本。
但你可以像下面这样做:
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: