php mysql 生成json_PHP - 从MySQL创建一个json文件

这是我的PHP代码: h2>

$host = "localhost"; //Your database host server

$db = "root"; //Your database name

$user = "root"; //Your database user

$pass = "1234"; //Your password

$connection = mysql_connect($host, $user, $pass);

//Check to see if we can connect to the server

if(!$connection)

{

die("Database server connection failed.");

}

else

{

//Attempt to select the database

$dbconnect = mysql_select_db($db, $connection);

//Check to see if we could select the database

if(!$dbconnect)

{

die("Unable to connect to the specified database!");

}

else

{

$query = "SELECT * FROM playlist_builder";

$resultset = mysql_query($query, $connection);

$records = array();

$response = array(); //extra

//Loop through all our records and add them to our array

while($r = mysql_fetch_assoc($resultset))

{

$records[] = $r;

}

//Output the data as JSON

$json = json_encode($records, JSON_PRETTY_PRINT);

$json = str_replace('\\', '', $json);

$json = preg_replace('/"([a-zA-Z]+[a-zA-Z0-9_]*)":/','$1:',$json);

// $json = str_replace('"', "'", $json);

//NOTE: FOLDERS 'url' and 'file' SHOULD BE WRITABLE WITH PERMISSIONS - 777

//IN CASE 'url' FOLDER PLACED IN SERVER'S ROOT

//IF YOU'RE USING SOME FTP BROWSER CHANGE PERMISSIONS FOR 'url'

//FOLDER AND APPLY IT TO ALL ENCLOSED ITEMS

$data = 'var data = [{ tags: ';

$end = '}];';

$script = "// Call Slider function

$(window).load(function () {

$('#slideshow-slider').jSonSlider({

'loadallslides': false,

'auto': [true, '14000'],

'nextprev': false,

'circular': true,

'pagi': false,

'data': data

});

});";

file_put_contents('records.js', $data);

file_put_contents('records.js', $json, FILE_APPEND);

file_put_contents('records.js', $end, FILE_APPEND);

file_put_contents('records.js', $script, FILE_APPEND);

}

}

以下是我的js文件输出: h2>

var data = [{ tags: [

{

id: "1",

volume: "volume1",

name: "a",

content: "image1.jpg",

css_animate: "fadeIn"

},

{

id: "2",

volume: "volume1",

name: "a",

content: "image2.jpg",

css_animate: "fadeIn"

},

{

id: "3",

volume: "volume1",

name: "a",

content: "image3.jpg",

css_animate: "fadeIn"

}

]}];

// Call Slider function

$(window).load(function () {

$('#slideshow-slider').jSonSlider({

'loadallslides': false,

'auto': [true, '14000'],

'nextprev': false,

'circular': true,

'pagi': false,

'data': data

});

});任何帮助真的很感激! :)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值