1

I am having difficulty with getting the JavaScript portion of this code to work the way I would like.

I have managed to pull the array from a MySQL table and convert it using JSON.

However, when I try to get the data into a HTML table, I get individual characters in each cell.

For example, my database has a table with a keyword labels. Inside this are 4 colors Blue, Green, Brown, Hazel exactly as I have entered it here.

When I output via JavaScript, I get one character in each cell instead of 4 cells.

I am "almost" there on my own:

<?php
include 'conn.php';
$sql = "SELECT * FROM variables WHERE id= '1' ";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        $labels = $row["labels"];
    }
    foreach (explode(',', $labels) as $newLabel) {
        echo "{$newLabel} ";
    }
} 
?>
<script>

<?php $newLabel = $labels ?>
var n = <?php print (json_encode($newLabel)); ?>;
var index;
var myTable= "<table class='hover'>";


myTable+= "<tr><td class='smalltd'style='width: 20px; font-weight:bold;'></td>";
for (index = 0; index < newLabel.length; index++) {
    myTable+= "<td class='no' style='width: 100px; font-weight:bold;'>" 
           + newLabel[index] + "</td>";
}

myTable+="</table>";

document.write(myTable);
</script>

I suspect the culprit lies in the newLabel.length but I am not certain

3
  • What are you trying to with javascript Commented Dec 15, 2014 at 16:33
  • You know you can write Javascript with php ? You can then write... with php your needed javascript this time o_o Commented Dec 15, 2014 at 16:33
  • Where do you define 'newLabel' in your javascript code? Commented Dec 15, 2014 at 16:34

1 Answer 1

1

First of all, you need to collect your labels into an array, not a string:

$labels[] = $row["labels"];

Second, you've never defined the newLabel javascript variable. Change

for (index = 0; index < newLabel.length; index++) {

to

for (index = 0; index < n.length; index++) {

and use n or rename n to newLabel

Sign up to request clarification or add additional context in comments.

4 Comments

But i really wonder why he should write the table through JS and not echo through php
@IndraKumarS You are right. I've just try to find the errors in his code. Who knows, maybe he has some reason for it.
Thank you for all of the comments. The reason behind my insanity is that I try to create tough questions for myself and then solve them using a variety of languages. I feel this not only helps me to learn, but also keeps me in practice. My ultimate goal here is to place a user generated table into <canvas> and store the variables in a database.
lolka_bolka, you code above did help, but it is only adding one column in the table. Each comma separated word is still acting like one string. Is there a way to get each word in this array into separate columns?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.