在PHP和Javascript 2D数组之间传输DB数据

时间:2012-01-15 21:03:50

标签: php javascript mysql arrays

我试图通过PHP从MySQL数据库中获取所有数据,初始化一个2D java数组并用PHP数据填充它。

我在PHP中嵌入JS时遇到了麻烦。我已经标记了什么是有效的,什么不是。

正如您将看到的,一些嵌入式Java可以工作,但不是全部。

有什么想法吗?

<body>

<?php
    $con = mysql_connect("XXXXXX.COM","guest","password");
    mysql_select_db("HHG", $con);

    if (!$con)
    {
        die('Could not connect: ' . mysql_error());
    }
    else 
    {
        $result = mysql_query("SELECT * FROM articles", $con);
        $numrows = mysql_num_rows($result);
        echo "DB connection OK <br/>";
        echo "Found ";
        echo $numrows;
        echo " records <br/><br/>";
    } // EVERYTHING WORKS UP TO HERE
?> 


<script type="text/javascript">

document.write("THIS IS THE FISRT JS DOING SOMETHING"); // THIS DOES NOTHING
numrows = <?php echo $numrows; ?>; // THIS DOES NOTHING
string [][] hhgdata = new string[numrows][4]; // THIS DOES NOTHING
document.write("Records = " + numrows + "<br/>"); // THIS DOES NOTHING

</script>

<?
    $counter = 1;
    while ($row = mysql_fetch_assoc($result))
    {
        echo $row["idimg"]; echo "<br/>";  //THIS WORKS
        $hhgtitle = $row["hhgtitle"]; //THIS WORKS
        echo $hhgtitle; echo "<br/>"; //THIS WORKS

        ?>

        <script type="text/javascript"> //THIS WORKS
            counter = <?php echo $counter; ?>; //THIS WORKS
            document.write("counter = " + counter + "<br/><br/>"); //THIS WORKS

            hhgtitle = <?php echo $hhgtitle; ?>; // THIS DOES NOTHING
            document.write("Title: "); // THIS DOES NOTHING
            hhgdata[counter][1]= hhgtitle; // THIS DOES NOTHING
            document.write(hhgdata[counter][1]); // THIS DOES NOTHING
        </script>

        <?
        $counter++; // THIS WORKS
    }
?>

</body>

2 个答案:

答案 0 :(得分:2)

您正在混淆Java和JavaScript。例如,这是Java语法,您不能在只应包含JavaScript的脚本标记中编写它:

string [][] hhgdata = new string[numrows][4];

JavaScript数组是动态的,这应该足够了:

var hhgdata = [];

如果您想在其中添加另一个数组,就像您在代码中稍后要做的那样,只需执行此操作:

hhgdata[counter] = [];

然后分配给内部数组:

hhgdata[counter][1] = hhgtitle;

你也在创建多个赋予变量的不带引号的字符串文字(假设$hhgtitle包含一个字符串):

hhgtitle = <?php echo $hhgtitle; ?>;

它应该是这样的:

hhgtitle = <?php echo '"' . $hhgtitle .'"'; ?>;

最后,虽然它不正确,但您的PHP while循环正在HTML中创建多个script元素。

修改

我已经进行了上述和评论中所做的更改,完全复制粘贴,看看它是如何进行的:

<body>

<?php
    $con = mysql_connect("XXXXXX.COM","guest","password");
    mysql_select_db("HHG", $con);

    if (!$con)
    {
        die('Could not connect: ' . mysql_error());
    }
    else 
    {
        $result = mysql_query("SELECT * FROM articles", $con);
        $numrows = mysql_num_rows($result);
        echo "DB connection OK <br/>";
        echo "Found ";
        echo $numrows;
        echo " records <br/><br/>";
    } // EVERYTHING WORKS UP TO HERE
?> 

<script type="text/javascript">
    document.write("THIS IS THE FISRT JS DOING SOMETHING"); // THIS DOES NOTHING
    numrows = <?php echo $numrows; ?>; // THIS DOES NOTHING
    var hhgdata = new Array(numrows); // THIS DOES NOTHING
    document.write("Records = " + numrows + "<br/>"); // THIS DOES NOTHING
</script>

<?php
    $counter = 1;
    while ($row = mysql_fetch_assoc($result))
    {
        echo $row["idimg"]; echo "<br/>";  //THIS WORKS
        $hhgtitle = $row["hhgtitle"]; //THIS WORKS
        echo $hhgtitle; echo "<br/>"; //THIS WORKS
?>
<script type="text/javascript"> //THIS WORKS
    var counter = <?php echo $counter; ?>; //THIS WORKS
    document.write("counter = " + counter); //THIS WORKS
    hhgtitle = <?php echo '"' . $hhgtitle . '"'; ?>; // THIS DOES NOTHING
    document.write("Title: "); // THIS DOES NOTHING
    hhgdata[counter] = [];
    hhgdata[counter][1]= hhgtitle; // THIS DOES NOTHING
    document.write("<br />hhgdata[counter][1]: " + hhgdata[counter][1]); // THIS DOES NOTHING
</script>
<?php
        $counter++; // THIS WORKS
    }
?>

</body>

答案 1 :(得分:2)

为什么不采取PHP数组和json_encode呢?然后你可以在Javascript中使用它,见下文:

var json = <?php echo json_encode($foo); ?>;

您可以在此处详细了解如何执行此操作:http://www.openjs.com/scripts/data/json_encode.php