gpt4 book ai didi

php - 在 PHP 中将 JSON 传递给数组

转载 作者:行者123 更新时间:2023-11-29 12:40:51 25 4
gpt4 key购买 nike

我正在开发一个可以立即过滤结果的过滤器,我想知道这是否可能是问题的原因,所以我想我会问一下,看看是否有人可以给我一个关于如何继续的指示.

      <script>
var services = [
<?php
//Variables for connecting to your database.
//These variable values come from your hosting account.
$hostname = "###";
$username = "###";
$dbname = "###";

//These variable values need to be changed by you before deploying
$password = "###";
$usertable = "###";
$url = "permalink";
$title = "Address";
$amount = "rent";
$id = "id";
$status = "Beds";
$nonprofit = "Address";

//Connecting to your database
mysql_connect($hostname, $username, $password) OR DIE ("He's dead Jim");
mysql_select_db($dbname);

//Fetching from your database table.
$query = "SELECT * FROM $usertable";
$result = mysql_query($query);

if ($result) {
while($row = mysql_fetch_array($result)) {
$url = $row["$url"];
$title = $row["$title"];
$amount = $row["$amount"];
$id = $row["$id"];
$status = $row["$status"];
$nonprofit = $row["$nonprofit"];

echo '{"permalink": "';
echo "{$url}";
echo '",';
echo '"title": "';
echo "{$title}";
echo '",';
echo '"amount":';
echo "{$amount}";
echo ',';
echo '"id": "';
echo "{$id}";
echo '",';
echo '"status": "';
echo "{$status}";
echo '",';
echo '"address": "';
echo "{$address}";
echo '",';
echo '},';

}
}
?>

]
//]]>
</script>


<script id="template" type="text/html">
<a title="{{title}}" href="{{permalink}}">
<div class="fs_box hide-for-small-down">
<div class="fs_left">
<span class="fs_head">{{title}}</span>
<span class="fs_id"><img src="images/{{id}}.jpg" width="75%" height="75%" onError="this.onerror=null;this.src='images/logo.png';"></span>
<span class="fs_status">{{status}}</span>
<span class="fs_disc">{{address}}</span>
</div>
<div class="fs_price">${{amount}}+</div>
<div class="clear"></div>
</div>
</a>
</script>

我期望它会产生一堆结果,然后将这些结果过滤到页面其他位置的标准。当我当前尝试将其作为 php 代码时,它输出正常。但是,当我在应该放入的 php 文件中尝试它时,它不会产生任何结果。或者它不喜欢出现在脚本中?感谢您的帮助!

最佳答案

您可以使用json_decodejson_encode将数组转换为 json,然后将 json 转换回数组。

还有人可能会提到你不应该在 PHP 中使用 mysql_* 函数,因为它们已经被贬值了。

类似这样的事情:

 <?php
//Variables for connecting to your database.
//These variable values come from your hosting account.
$hostname = "###";
$username = "###";
$dbname = "###";

//These variable values need to be changed by you before deploying
$password = "###";
$usertable = "###";
$url = "permalink";
$title = "Address";
$amount = "rent";
$id = "id";
$status = "Beds";
$nonprofit = "Address";

//Connecting to your database
mysql_connect($hostname, $username, $password) OR DIE ("He's dead Jim");
mysql_select_db($dbname);

//Fetching from your database table.
$query = "SELECT * FROM $usertable";
$result = mysql_query($query);

if ($result) {
$results = array()
while($row = mysql_fetch_array($result)) {
$results[] = $row;
}

$json = json_encode($results);
}
?>

]

<script>
var services = <?php echo $json; ?>;
</script>

这将为您提供一个 json 对象,用于在脚本中渲染。

关于php - 在 PHP 中将 JSON 传递给数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26149869/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com