I am attempting to get all the data from a MySQL db with PHP, initialise a 2D java array and populate it with the PHP data.

I am having trouble embedding JS in the PHP. I have marked up what is working and what isn't.

As you will see, some of the embedded java works but not all.

Any thoughts?

<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>
share|improve this question
Can you show the actual, final HTML instead of the source code? Are you getting any errors in the console? – Pekka 웃 Jan 15 '12 at 21:10
I'm not generating HTML - I am simply moving MySQL data from PHP to Javascript. Just trying to get the last piece of the 2D array to accept data from PHP variables. – TJS101 Jan 15 '12 at 23:23
Whether you're generating HTML or JS - show the end result, not the PHP source code. – Pekka 웃 Jan 16 '12 at 0:00
the extension of the array hhgdata from var hhgdata = []; to hhgdata[variable] = []; seems to stop things from running. If I use real integers in an example it forms and then displays the data. There are no errors displayed on my HTML page - just the document.write elements. Sorry. Noob. – TJS101 Jan 16 '12 at 0:12

2 Answers

up vote 2 down vote accepted

You are mixing up Java and JavaScript. For example this is Java syntax, you can't write this within a script tag which should only contain JavaScript:

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

The JavaScript arrays are dynamic, this should be enough:

var hhgdata = [];

When you want to add another array into it, as you seem to be doing later in your code, just do this:

hhgdata[counter] = [];

And then assign to the inner array:

hhgdata[counter][1] = hhgtitle;

You are also creating multiple assigning an unquoted string literal to variable with this (assuming $hhgtitle contains a string):

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

It should be something like this:

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

Finally, while it's not incorrect, your PHP while loop is creating multiple script elements in your HTML.

EDIT

I have made the changes described above as well as in comments, copy-paste exactly and see how it goes:

<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>
share|improve this answer
First script changed to: <script type="text/javascript"> document.write("THIS IS THE FISRT JS DOING SOMETHING <br/>"); // THIS WORKS numrows = <?php echo $numrows; ?>; // THIS WORKS var hhgdata = []; document.write("Records = " + numrows + "<br/>"); // THIS WORKS </script> This seems to work. – TJS101 Jan 15 '12 at 22:04
The second script: 'while ($row = mysql_fetch_assoc($result)) { $hhgtitle = $row["hhgtitle"]; //THIS WORKS $hhgtextlong = $row["text_long"]; $hhgtextshort = $row["text_short"]; $hhgidimg = $row["idimg"]; ?> <script type="text/javascript"> counter = <?php echo $counter; ?>; //THIS DOES NOTHING document.write("counter = " + counter + "<br/><br/>"); //THIS DOES NOTHING hhgdata[counter] = []; hhgtitle = <?php echo $hhgtitle; ?>; hhgdata[counter][1] = hhgtitle; document.write(hhgdata[counter][1]); // THIS DOES NOTHING – TJS101 Jan 15 '12 at 22:09
Please edit your original questions and add these changes to the end of the post (preferably under an "Edit" subheading). It's difficult to make out what is and isn't working. – Abbas Jan 15 '12 at 22:13
This is my first post so I can't add anything for a few hours. The second iteration of the array hhgdata[counter] = []; doesn't seem to work, then adding data as hhgdata[counter][1]=hhgtitle; and the subsequent document.write(hhgdata[counter][1]); doesn't do anything that I can see. – TJS101 Jan 15 '12 at 23:12
You are missing the correct PHP declaration in your second and third PHP script segments. They look like this: '<? $counter = 1;' and '<? $counter++;'. Add the word 'php' to them so they look like this: '<?php $counter = 1;' and '<?php $counter++;'. – Abbas Jan 15 '12 at 23:36
show 7 more comments

Why not just take the PHP array of data and json_encode it? Then you can work with it in Javascript, see below:

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

You can learn more about how to do this here: http://www.openjs.com/scripts/data/json_encode.php

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.