0

Lets say I have this table mytable

id | name | x | y

I pull the the rows from mytable and create JavaScript objects with it like so:

PHP

$sql = "SELECT * FROM mytable";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {  
        echo '<script type="text/javascript">';
        echo "new Object({$row["id"]}, '{$row["name"]}', {$row["desk_x"]}, {$row["desk_y"]});";
        echo '</script>';            
    }
}

JS

function Object(id, name, x, y) {
    var obj = {
        id:id, 
        name:name, 
        x:x, 
        y:y
    };
}

At the moment this is fine but lets say I want to add another column color to mytable

Basically I'm asking what do I write in PHP and JS to make this object dynamically, so you can have any columns and the Object object will just add a new property with the name of the column?

2
  • Don't call your function Object! Object already exists, and you will overwrite it, losing access to the original Object. And also, Object isn't really an expressive name for a constructor, is it? Commented Jul 18, 2017 at 12:25
  • I know. That was an example not my actual code. Commented Jul 18, 2017 at 12:52

2 Answers 2

2

You can use AJAX to send data from serverside to clientside. Lets say you have a PHP file called script.php with your code:

if(isset($_GET['action']) && $_GET['action'] == 'testing'){
    $sql = "SELECT * FROM mytable";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
          echo json_encode($result);
    }
}

And lets say you have a JS file called script.js. Here you will do your AJAX call towards the PHP script file like this:

$.ajax({
  type: 'GET',
  url: 'script.php',
  data: { action: 'testing' },
  success: function(response){
     console.log(response); //here you will have access to the object returned from the PHP script
  }
})
Sign up to request clarification or add additional context in comments.

5 Comments

Im looking for a way to get the columns from the table and make a java-script object with the same properties
The code from my answer should do just what you need.
The console is printing my entire index.php file
It depends on what is in that PHP file. It should be only the part of code I just added to my answer. Or you can check the $_GET request.
@AdamStück, please check the updated code from my answer.
1

I guess what you're looking for is how to do this without AJAX (which you don't really need here).

Put the objects in an array, then output your JS variable:

PHP:

$rows = [];
if ($result->num_rows) 
  while($row = $result->fetch_assoc()) $rows[] = $row; // append to array
echo "<script> var objArray = " . json_encode($rows) . "; </script>";

4 Comments

How would you do this using AJAX if it is better?
That sounds as if you have no idea what AJAX is/does. And you didn't say whether you tried my code. Or whether it works.
i tried your code and it works but if AJAX is the better option could you show me what you would write? I tried the AJAX code from @Ionut above but it didn't work well. I'm using AJAX to update data on my database already by the way.
I have no idea if AJAX is the better option or if you even need it. I'm sorry but it's kind of hard for me to estimate your level of knowledge, and I have no idea how much more info you need (and SO isn't a beginner's coding school). Asking me to transform my code to AJAX means you have no idea how to use it, and this question (or website) is not the place to learn how to use AJAX.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.