简体   繁体   中英

for loop to set javascript variable = to a php variable

I am working on a project that requires create hundreds of variables in javascript with PHP values. I can write each one line by line like so:

var M1 = <?php echo json_encode($$mn[0]); ?>;
var M2 = <?php echo json_encode($$mn[1]); ?>;
var M3 = <?php echo json_encode($$mn[2]); ?>;

As I said there are hundreds of these though and if it is possible to do in a loop I would be very interested in learning. I have searched all over and can't find a direct answer. It may very well be that this is not possible. I am new to coding and still learning what certain code can and cannot do.

Any insight or direction on this topic would be greatly appreciated!

If this is not an option is it possible to use an array index for the javascript variable name? I have created an array for the JS and PHP. The PHP works fine above but if I try to use an array index for the JS like below, it breaks:

var mcirc[0] = <?php echo json_encode($$mn[0]); ?>;

I have output the array and the values are coming up correctly but when I run this I get the message:

[object HTMLDivElement]

instead of the actually value that should show up.

UPDATE

$mn array:

for ($m1 = 1; $m1 < 6; $m1++) {
    $mn[] = 'M'.$m1;
}

UPDATE

Select SQL creating array:

$sqlMC = "SELECT * FROM tblmaincircles";
$result = $conn->query($sqlMC);

    while($row = $result->fetch_assoc()) {
        $$row["mcID"]= $row["mcName"];
}

The array for mcID looks like this:

M1 = "text1"
M2 = "text2"
M3 = "text3"
M4 = "text4"
M5 = "text5"

UPDATE

end result desired:

var M1 = "text1";
var M2 = "text2";
var M3 = "text3";
var M4 = "text4";
var M5 = "text5";

Where "text1, ...2, ...3, ...4, ...5" are coming from the MySQL database.

UPDATE

Here is the final code that got this working:

$sqlMC = "SELECT mcID, mcName FROM tblmaincircles";
$result = $conn->query($sqlMC);


    while($row = $result->fetch_assoc()) {
        $mcID[] = $row["mcID"];
        $mcName[] = $row["mcName"];
}


<?php for ($m1 = 0; $m1 <5; $m1++) { ?>
    var <?php echo $mcID[$m1]; ?> = <?php echo json_encode($mcName[$m1]); ?>;
<?php } ?>

Simply put JSON into variable

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

And then process the JSON way you want:

eg.

var json=[{key:someValue},
          {key:someValue2},
          {key:someValue3}
         ];

json.forEach(function(a){
  console.log(a.key);
})

First in your query part, declare a variable to hold the result that you want. I'm assuming the M1 is mcID in your table and text1 is the mcName . For example:

$sqlMC = "SELECT * FROM tblmaincircles";
$result = $conn->query($sqlMC);

$mac = [];//or $mac = array(); Depends on your PHP version.

while($row = $result->fetch_assoc()) {
    $mac[$row["mcID"]] = $row["mcName"];
}

And then, iterate through the $mac array with foreach loop. I'm assuming you are using PHP codes within HTML. The $key will be the mcID and the $value will be the mcName .

//php tag for the foreach opening
<?php foreach ($mac as $key => $value) { ?>

    var <?php echo $key; ?> = <?php echo "'$value';"; ?>

//php tag for the foreach closing
<?php } ?>

OR, if you want to use javascript associative array.

var macJs = {};

<?php foreach ($mac as $key => $value) { ?>

    macJs.<?php echo $key; ?> = <?php echo "'$value';"; ?>

<?php } ?>

And you can access the element like this in javascript macJs.M1 .

You should use JSON to 'export' your objects/array through different languages, in that case:

var json = '<?= json_encode($your_array); ?>';

After this you can parse this Json, what should return your array:

var your_array = JSON.parse(json);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM