2个PHP变量中的最大值,如何从脚本传递到jQuery?

时间:2022-10-15 15:13:04

I want to use gmaps.js to add map markers onto a map on my site. I'm using a PHP Script to get the latitude and longitude from my DB, however I'm using

我想使用gmaps.js将地图标记添加到我网站上的地图上。我正在使用PHP脚本从我的数据库获取经度和经度,但我正在使用

while($dbrowinfo = mysqli_fetch_assoc($rowfields))

Therefore the information comes to an end after the Database query has been ran. Per page there will be a maximum of 10 sets of data, both with a long and lat value (therefore 20 individual variables).

因此,在运行数据库查询后,信息将结束。每页最多有10组数据,都有long和lat值(因此有20个单独的变量)。

Is there any way to use the variables from PHP to pass to jQuery before the script is ran? Can't really get my head around it as jQuery is client and PHP is serverside...

有没有办法在脚本运行之前使用PHP中的变量传递给jQuery?无法真正理解它,因为jQuery是客户端而PHP是服务器端...

Thanks for your time

谢谢你的时间

2 个解决方案

#1


1  

If you are asking if there is a way to pass variables from PHP to JavaScript before the JQuery script has run, the answer is yes, definitely. All the PHP will run before any of the JavaScript, so, you can simply output the data you want as JavaScript variables, like so:

如果您在QQuery脚本运行之前询问是否有办法将变量从PHP传递到JavaScript,那么答案肯定是肯定的。所有PHP都将在任何JavaScript之前运行,因此,您只需输出您想要的数据作为JavaScript变量,如下所示:

<html>

<?php
// Load the data into your PHP variables
$dataA = "some data";
$dataB = "some more data";
$dataC = 245; // numeric data
?>
<body>
    <!-- now open a script tag -->
    <script>
    var jsVar1 = "<?= $dataA ?>";// If outputting string, you need to add quotes for js
    var jsVar2 = "<?= $dataB ?>"; 
    var jsVar3 = <?= $dataC ?>; // No quotes needed for a number

    $(document).ready(function(){
        console.log("data from php: " + jsVar1 + " " + jsVar2 + " " + jsVar3);
    });
    </script>
</body>

</html>

#2


1  

Put the values in a PHP array, and then encode them into a Javascript array with json_encode

将值放在PHP数组中,然后使用json_encode将它们编码为Javascript数组

$coords = array();
while ($row = mysqli_fetchassoc($rowfields)) {
    $coords[] = array('lat' => $row['lat'], 'lng' => $row['lng']);
}
echo '<script>var coords = ' . json_encode($coords) . ';</script>';

This defines a Javascript variable coords that contains an array of objects, like:

这定义了一个包含对象数组的Javascript变量coords,例如:

var coords = [
    { lat: 50.0, lng: 101.1 },
    { lat: -20.8, lng: 55.3 },
    ...
];

You can then use this array in your jQuery code.

然后,您可以在jQuery代码中使用此数组。

#1


1  

If you are asking if there is a way to pass variables from PHP to JavaScript before the JQuery script has run, the answer is yes, definitely. All the PHP will run before any of the JavaScript, so, you can simply output the data you want as JavaScript variables, like so:

如果您在QQuery脚本运行之前询问是否有办法将变量从PHP传递到JavaScript,那么答案肯定是肯定的。所有PHP都将在任何JavaScript之前运行,因此,您只需输出您想要的数据作为JavaScript变量,如下所示:

<html>

<?php
// Load the data into your PHP variables
$dataA = "some data";
$dataB = "some more data";
$dataC = 245; // numeric data
?>
<body>
    <!-- now open a script tag -->
    <script>
    var jsVar1 = "<?= $dataA ?>";// If outputting string, you need to add quotes for js
    var jsVar2 = "<?= $dataB ?>"; 
    var jsVar3 = <?= $dataC ?>; // No quotes needed for a number

    $(document).ready(function(){
        console.log("data from php: " + jsVar1 + " " + jsVar2 + " " + jsVar3);
    });
    </script>
</body>

</html>

#2


1  

Put the values in a PHP array, and then encode them into a Javascript array with json_encode

将值放在PHP数组中,然后使用json_encode将它们编码为Javascript数组

$coords = array();
while ($row = mysqli_fetchassoc($rowfields)) {
    $coords[] = array('lat' => $row['lat'], 'lng' => $row['lng']);
}
echo '<script>var coords = ' . json_encode($coords) . ';</script>';

This defines a Javascript variable coords that contains an array of objects, like:

这定义了一个包含对象数组的Javascript变量coords,例如:

var coords = [
    { lat: 50.0, lng: 101.1 },
    { lat: -20.8, lng: 55.3 },
    ...
];

You can then use this array in your jQuery code.

然后,您可以在jQuery代码中使用此数组。