我试图让这段代码工作,我似乎无法弄明白......我试图加入多个表并获得一个json out of usernames>播放列表&gt ;轨道
我错了什么?我对这个级别的编码还是比较陌生的,并且仍然试图让我的脑袋缠绕在一起,所以任何帮助都会非常感激
谢谢
<?php
// variable in which to store your results to be serialize
$result_array = array();
// some index and temp variables we will use to build the data structure
$current_user_id = 0;
$user_idx = -1;
$current_playlist_id = 0;
$playlist_idx = -1;
//Create Database connection
$db = mysql_connect("localhost","USERNAME","PASSWORD");
if (!$db) {
die('Could not connect to db: ' . mysql_error());
}
//Select the Database
mysql_select_db("DATABASE",$db);
// Your database fetch logic here.
// I have not shown where actual query would be made.
// The query would obviously need to succeed before getting to this code.
// Note that you REALLY should be considering mysqli or PDO here
// instead of deprecated mysql extension.
while($row =
//Replace * in the query with the column names.
$result = mysql_query("SELECT
u.`idu` AS `user_id`,
u.`username` AS `user_name`,
u.`first_name` AS `first_name`,
u.`last_name` AS `last_name`,
u.`country` AS `user_country`,
u.`image` AS `user_image`,
u.`cover` AS `user_cover`,
u.`description` AS `user_description`,
pl.`id` AS `playlist_id`,
pl.`name` AS `playlist_name`,
t.`id` AS `track_id`,
t.`title` AS `track_title`,
t.`name` AS `track_name`,
t.`art` AS `track_art`,
t.`likes` AS `track_likes`,
t.`downloads` AS `track_downloads`,
t.`views` AS `track_plays`
FROM users AS u
INNER JOIN playlists AS pl
ON u.idu=pl.by
INNER JOIN playlistentries AS ple
ON pl.id=ple.playlist
INNER JOIN tracks AS t
ON ple.track=t.id
ORDER BY `user_id` ASC, `playlist_id` ASC, `track_id` ASC
LIMIT 10", $db)
) {
// is this a new user?
$user_id = (integer)$row['user_id'];
if($user_id !== $current_user_id) {
// manage our indexes
$current_user_id = $user_id;
$user_idx++;
$playlist_idx = -1;
// start a new object to store data for this user
$user_obj = new stdClass();
// set properties on user from current row
// name these properties whatever you want for final JSON structure
$user_obj->idu = $current_user_id;
$user_obj->username = $row['user_name'];
$user_obj->first_name = $row['first_name'];
$user_obj->last_name = $row['last_name'];
$user_obj->country = $row['user_country'];
$user_obj->image = $row['user_image'];
$user_obj->cover = $row['user_cover'];
$user_obj->description = $row['user_description'];
$user_obj->playlists = array();
// set object in overall result array
$result_array[$user_idx] = $user_obj;
}
// does this playlist already exist for this user?
$playlist_id = (int)$row['playlist_id'];
if($playlist_idx === -1 || $playlist_id !== $current_playlist_id ) {
// manage our indexes
$current_playlist_id = $playlist_id;
$playlist_idx++;
// start a new object to store data for this user
$playlist_obj = new stdClass();
// set properties on playlist from current row
// name these properties whatever you want for final JSON structure
$playlist_obj->id = $current_playlist_id;
$playlist_obj->name = $row['playlist_name'];
$playlist_obj->by = $current_user_id;
$playlist_obj->tracks = array();
// set object in overall result array
$result_array[$user_idx]->playlists[$playlist_idx] = $playlist_obj;
}
// create track object
// we do this for every row
$track_obj = new stdClass();
$track_obj->id = $row['track_id'];
$track_obj->title = $row['track_title'];
$track_obj->name = $row['track_name'];
$track_obj->art = $row['track_art'];
$track_obj->likes = $row['track_likes'];
$track_obj->downloads = $row['track_downloads'];
$track_obj->plays = $row['track_plays'];
$track_obj->uid = $current_user_id;
// set track object in result set
$result_array[$user_idx]->playlists[$playlist_idx]->tracks[] = $track_obj;
}
// serialize to JSON
$result_json = json_encode($result_array);
?>