1

I have a table in a database with three columns, prop_no, prop_name, prop_sc in a database and I moved the entries of the last two columns into 2 PHP arrays $propertyNameList and $propertyCodeList. Now I have a JavaScript class Property and an array propList. I need to move all the values from the two PHP arrays to this JS array using

  propList.push(new Property(1,<?php  echo json_encode($propertyNameList[0]); ?>,
                        <?php echo json_encode($propertyCodeList[0]); ?>));

using a for loop, where the 1st argument would be the counter variable and the PHP array indices must change with every iteration. How can I do this?

0

1 Answer 1

3

I would probably suggest you restructure things a bit, but you can fairly easily do it with temporary parallel arrays.

Here we dump those parallel arrays out to the client-side code and then do the loop there in JavaScript:

(function() {
    var names = <?php echo json_encode($propertyNameList) ?>;
    var codes = <?php echojson_encode($propertyCodeList) ?>;

    for (var n = 0; n < names.length; ++n) {
        propList.push(new Property(names[n], codes[n]));
    }
})();

Or if you prefer the loop on the PHP side:

<?php
    $index = 0;
    for ($index = 0; $index < count($propertyNameList); ++$index) {
        echo 'propList.push(new Property(' . json_encode($propNameList[$index]) . ', ' . json_encode($propCodeList[$index]) . '));';
    }
?>

(I think, my PHP is fairly weak.)

1
  • 1
    Thanks for your answer! I did this way and got exactly what I wanted. Commented Mar 24, 2016 at 9:59

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.