我想在一个查询中连接这三个表,在这之后,我使用php在Web浏览器中发出一个json文件(localhost)
以下是图片的描述;
1. Movie Table
2是Cinemax Table
3.是Showtime Table
下面是我想要的JSON脚本的输出,在加入Mysql中的所有3个表后,从php脚本输出这些字段。
[{
"Cinemax":"Ntinda", "Contacts":"0704594180","Movie":[{"MovieName":"Zootopia", "ShowTime":[
{"Monday":"1:30 p.m , 3:40 p.m",
"Tuesday":"1:30 p.m , 3:40 p.m",
"Wednesday":"1:30 p.m , 3:40 p.m",
"Thursday":"1:30 p.m , 3:40 p.m",
"Friday":"1:30 p.m , 3:40 p.m",
"Saturday":"1:30 p.m , 3:40 p.m",
"Sunday":"1:30 p.m , 3:40 p.m"},
{"MovieName":"Ice Age 3", "ShowTime":[
{"Monday":"4:30 p.m , 6:40 p.m",
"Tuesday":"1:30 p.m , 3:40 p.m",
"Wednesday":"1:30 p.m , 3:40 p.m",
"Thursday":"1:30 p.m , 3:40 p.m",
"Friday":"1:30 p.m , 3:40 p.m",
"Saturday":"1:30 p.m , 3:40 p.m",
"Sunday":"1:30 p.m , 3:40 p.m"}
], "Synopsis:"Its really a nice animation movie", "photo":"zootopia.jpg",
"others":"Animation"},
"Cinemax":"Kisementi", "Contacts":"0700304850","Movie":[{"MovieName":"Zootopia","ShowTime":[
{"Monday":"1:30 p.m , 3:40 p.m",
"Tuesday":"1:30 p.m , 3:40 p.m",
"Wednesday":"1:30 p.m , 3:40 p.m",
"Thursday":"1:30 p.m , 3:40 p.m",
"Friday":"1:30 p.m , 3:40 p.m",
"Saturday":"1:30 p.m , 3:40 p.m",
"Sunday":"1:30 p.m , 3:40 p.m"},
{"MovieName":"Ice Age 3", "ShowTime":[
{"Monday":"4:30 p.m , 6:40 p.m",
"Tuesday":"1:30 p.m , 3:40 p.m",
"Wednesday":"1:30 p.m , 3:40 p.m",
"Thursday":"1:30 p.m , 3:40 p.m",
"Friday":"1:30 p.m , 3:40 p.m",
"Saturday":"1:30 p.m , 3:40 p.m",
"Sunday":"1:30 p.m , 3:40 p.m"}
], "Synopsis:"Its really a nice animation movie", "photo":"zootopia.jpg",
"others":"Animation"}
}]
这是我目前的Php脚本,所以我怎样才能尝试获取Json的格式
<?php
$username="root";
$password="namungoona";
$hostname = "localhost";
//connection string with database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "";
// connect with database
$selected = mysql_select_db("UgandaEntertainment",$dbhandle)
or die("Could not select examples");
//query fire
$result = mysql_query("select Movie.MovieName,
Movie.Synopsis,
Movie.photo,
Movie.Others,
Cinemax.CinemaName,
Cinemax.Longitude,
Cinemax.Latitude,
Cinemax.Contact,
Cinemax.CinemaxPhoto,
ShowTime.Monday,
ShowTime.Tuesday,
ShowTime.Wednesday,
ShowTime.Thursday,
ShowTime.Friday,
ShowTime.Saturday,
ShowTime.Sunday,
Actor.ActorsName,
Actor.ActorsPhoto
from Movie
inner join Cinemax
ON Movie.CinemaxId=Cinemax.CinemaxId
inner join ShowTime
ON Movie.MovieId=ShowTime.MovieId
inner join Actor
ON Movie.MovieId=Actor.MovieId
;
");
$json_response = array();
// fetch data in array format
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
// Fetch data of Fname Column and store in array of row_array
$row_array['MovieName'] = $row['MovieName'];
$row_array['Synopsis'] = $row['Synopsis'];
$row_array['Others'] = $row['Others'];
$row_array['CinemaName'] = $row['CinemaName'];
$row_array['photo'] = $row['photo'];
$row_array['CinemaxPhoto'] = $row['CinemaxPhoto'];
$row_array['Longitude'] = $row['Longitude'];
$row_array['Latitude'] = $row['Latitude'];
$row_array['Contact'] = $row['Contact'];
$row_array['Monday'] = $row['Monday'];
$row_array['Tuesday'] = $row['Tuesday'];
$row_array['Wednesday'] = $row['Wednesday'];
$row_array['Thursday'] = $row['Thursday'];
$row_array['Friday'] = $row['Friday'];
$row_array['Saturday'] = $row['Saturday'];
$row_array['Sunday'] = $row['Sunday'];
$row_array['ActorsName'] = $row['ActorsName'];
$row_array['ActorsPhoto'] = $row['ActorsPhoto'];
//push the values in the array
array_push($json_response,$row_array);
}
//
echo json_encode($json_response);
?>
这是我的新OutPut For JSON
[{"Cinemax":"Ntinda Cinemax","Contacts":"0704594180","Movie":[{"MovieName":"Ice Age 3","ShowTime":{"Monday":"1:30 p.m , 3:40 p.m","Tuesday":"1:30 pm , 3:40 p.m","Wednesday":"1:30 p.m., 3:40 p.m","Thursday":"1:30 p.m., 3:40 p.m","Friday":"1:30 p.m., 3:40 p.m","Saturday":"1:30 p.m., 3:40 p.m","Sunday":"1:30 p.m., 3:40 p.m"},"Synopsis":"Manny, Diego and sid","photo":"ice_age_3.jpg","others":"cinema and comedy"}]},{"Cinemax":"Ntinda Cinemax","Contacts":"0704594180","Movie":[{"MovieName":"Ice Age 3","ShowTime":{"Monday":"1:30 p.m , 3:40 p.m","Tuesday":"1:30 pm , 3:40 p.m","Wednesday":"1:30 p.m., 3:40 p.m","Thursday":"1:30 p.m., 3:40 p.m","Friday":"1:30 p.m., 3:40 p.m","Saturday":"1:30 p.m., 3:40 p.m","Sunday":"1:30 p.m., 3:40 p.m"},"Synopsis":"Manny, Diego and sid","photo":"ice_age_3.jpg","others":"cinema and comedy"}]},{"Cinemax":"Ntinda Cinemax","Contacts":"0704594180","Movie":[{"MovieName":"Ice Age 3","ShowTime":{"Monday":"1:30 p.m , 3:40 p.m","Tuesday":"1:30 pm , 3:40 p.m","Wednesday":"1:30 p.m., 3:40 p.m","Thursday":"1:30 p.m., 3:40 p.m","Friday":"1:30 p.m., 3:40 p.m","Saturday":"1:30 p.m., 3:40 p.m","Sunday":"1:30 p.m., 3:40 p.m"},"Synopsis":"Manny, Diego and sid","photo":"ice_age_3.jpg","others":"cinema and comedy"}]}]
我想要这样的东西,而不需要修复Same Cinemax
[{"Cinemax":"Ntinda Cinemax","Contacts":"0704594180",
"Movie":[{"MovieName":"Zootopia","ShowTime":{"Monday":"1:30 p.m , 3:40 p.m","Tuesday":"1:30 pm , 3:40 p.m","Wednesday":"1:30 p.m., 3:40 p.m","Thursday":"1:30 p.m., 3:40 p.m","Friday":"1:30 p.m., 3:40 p.m","Saturday":"1:30 p.m., 3:40 p.m","Sunday":"1:30 p.m., 3:40 p.m"},"Synopsis":"Manny, Diego and sid","photo":"ice_age_3.jpg","others":"cinema and comedy"}]},{"Cinemax":"Ntinda Cinemax","Contacts":"0704594180","Movie":[{"MovieName":"Ice Age 3","ShowTime":{"Monday":"1:30 p.m , 3:40 p.m","Tuesday":"1:30 pm , 3:40 p.m","Wednesday":"1:30 p.m., 3:40 p.m","Thursday":"1:30 p.m., 3:40 p.m","Friday":"1:30 p.m., 3:40 p.m","Saturday":"1:30 p.m., 3:40 p.m","Sunday":"1:30 p.m., 3:40 p.m"},"Synopsis":"Manny, Diego and sid","photo":"ice_age_3.jpg","others":"cinema and comedy"}]},{"Cinemax":"Ntinda Cinemax","Contacts":"0704594180","Movie":[{"MovieName":"Ice Age 3","ShowTime":{"Monday":"1:30 p.m , 3:40 p.m","Tuesday":"1:30 pm , 3:40 p.m","Wednesday":"1:30 p.m., 3:40 p.m","Thursday":"1:30 p.m., 3:40 p.m","Friday":"1:30 p.m., 3:40 p.m","Saturday":"1:30 p.m., 3:40 p.m","Sunday":"1:30 p.m., 3:40 p.m"},
"Synopsis":"Manny, Diego and sid","photo":"ice_age_3.jpg","others":"cinema and comedy"},{"MovieName":"Ice Age 3","ShowTime":{"Monday":"1:30 p.m , 3:40 p.m","Tuesday":"1:30 pm , 3:40 p.m","Wednesday":"1:30 p.m., 3:40 p.m","Thursday":"1:30 p.m., 3:40 p.m","Friday":"1:30 p.m., 3:40 p.m","Saturday":"1:30 p.m., 3:40 p.m","Sunday":"1:30 p.m., 3:40 p.m"},"Synopsis":"Manny, Diego and sid","photo":"ice_age_3.jpg","others":"cinema and comedy"}]},{"Cinemax":"Ntinda Cinemax","Contacts":"0704594180","Movie":[{"MovieName":"Ice Age 3","ShowTime":{"Monday":"1:30 p.m , 3:40 p.m","Tuesday":"1:30 pm , 3:40 p.m","Wednesday":"1:30 p.m., 3:40 p.m","Thursday":"1:30 p.m., 3:40 p.m","Friday":"1:30 p.m., 3:40 p.m","Saturday":"1:30 p.m., 3:40 p.m","Sunday":"1:30 p.m., 3:40 p.m"},"Synopsis":"Manny, Diego and sid","photo":"ice_age_3.jpg","others":"cinema and comedy"}]},{"Cinemax":"Ntinda Cinemax","Contacts":"0704594180","Movie":[{"MovieName":"Ice Age 3","ShowTime":{"Monday":"1:30 p.m , 3:40 p.m","Tuesday":"1:30 pm , 3:40 p.m","Wednesday":"1:30 p.m., 3:40 p.m","Thursday":"1:30 p.m., 3:40 p.m","Friday":"1:30 p.m., 3:40 p.m","Saturday":"1:30 p.m., 3:40 p.m","Sunday":"1:30 p.m., 3:40 p.m"},
"Synopsis":"Manny, Diego and sid","photo":"ice_age_3.jpg","others":"cinema and comedy"}]}]
这个目前是Out Put
string(440) "[{"Cinemax":null,"Contacts":null,"Movie":[{"CinemaxId":null,"MovieName":null,"ShowTime":{"Monday":null,"Tuesday":null,"Wednesday":null,"Thursday":null,"Friday":null,"Saturday":null,"Sunday":null},"Synopsis":null,"photo":null,"others":null},{"CinemaxId":null,"MovieName":null,"ShowTime":{"Monday":null,"Tuesday":null,"Wednesday":null,"Thursday":null,"Friday":null,"Saturday":null,"Sunday":null},"Synopsis":null,"photo":null,"others":null}]}]"
答案 0 :(得分:0)
你可以试试这个:
$json_response = array();
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$movieArray = array(
"MovieName" => $row['MovieName'],
"ShowTime" => array(
"Monday" => $row['Monday'],
"Tuesday" => $row['Tuesday'],
"Wednesday" => $row['Wednesday'],
"Thursday" => $row['Thursday'],
"Friday" => $row['Friday'],
"Saturday" => $row['Saturday'],
"Sunday" => $row['Sunday']
),
"Synopsis" => $row['Synopsis'],
"photo" => $row['photo'],
"others" => $row['Others']
);
$cinema = array(
"Cinemax" => $row['CinemaName'],
"Contacts" => $row['Contact'],
"Movie" => array($movieArray)
);
$key = array_search($row['CinemaName'], array_column($json_response, 'Cinemax'));
if($key !== false)
{
$json_response[$key]["Movie"][] = $movieArray;
} else {
$json_response[]= $cinema;
}
}
var_dump(json_encode($json_response));
好的,我会尝试解释我的所作所为: 首先你有输入女巫是一个阵列:
$row['MovieName'] = 'Suicide Squad';
$row['Synopsis'] = "Bla bla tests";
$row['Others'] = "others";
$row['CinemaName'] = "CinemaRX";
$row['photo'] = 'test.jpg';
$row['CinemaxPhoto'] = 'cinemax.jpg';
$row['Longitude'] = '123';
$row['Latitude'] = '321';
$row['Contact'] = '0731565131';
$row['Monday'] = '10:00';
$row['Tuesday'] = '11:00';
$row['Wednesday'] = '12:00';
$row['Thursday'] = '13:00';
$row['Friday'] = '14:00';
$row['Saturday'] = '15:00';
$row['Sunday'] = '16:00';
$row['ActorsName'] = "Tom";
$row['ActorsPhoto'] = "tom.jpg";
我添加了一些随机数据:
所以你有多个这样的数组来自数据库,所以我将我的方法放在for()中进行测试:
$json_response = array();
for($i = 0; $i<2; $i++) {
$movieArray = array(
"MovieName" => $row['MovieName'],
"ShowTime" => array(
"Monday" => $row['Monday'],
"Tuesday" => $row['Tuesday'],
"Wednesday" => $row['Wednesday'],
"Thursday" => $row['Thursday'],
"Friday" => $row['Friday'],
"Saturday" => $row['Saturday'],
"Sunday" => $row['Sunday']
),
"Synopsis" => $row['Synopsis'],
"photo" => $row['photo'],
"others" => $row['Others']
);
$details = array(
"Cinemax" => $row['CinemaName'],
"Contacts" => $row['Contact'],
"Movie" => array($movieArray)
);
//here you check if there is already another entry with the cinema name
$key = array_search($row['CinemaName'], array_column($json_response, 'Cinemax'));
//if there is another entry with the same Cinema name you add the movie to that one.
if($key !== false)
{
$json_response[$key]["Movie"][] = $movieArray;
} else {
//if there isn't you add the cinema to the array
$json_response[]= $details;
}
}
var_dump(json_encode($json_response));
这适用于来自数据库的任意数量的Cinema条目。 这是实际输出:
string(526) "[{"Cinemax":"CinemaRX","Contacts":"0731565131","Movie":[{"MovieName":"Suicide Squad","ShowTime":{"Monday":"10:00","Tuesday":"11:00","Wednesday":"12:00","Thursday":"13:00","Friday":"14:00","Saturday":"15:00","Sunday":"16:00"},"Synopsis":"Bla bla tests","photo":"test.jpg","others":"others"},{"MovieName":"Suicide Squad","ShowTime":{"Monday":"10:00","Tuesday":"11:00","Wednesday":"12:00","Thursday":"13:00","Friday":"14:00","Saturday":"15:00","Sunday":"16:00"},"Synopsis":"Bla bla tests","photo":"test.jpg","others":"others"}]}]"
The Movie Suicide Squat在json中出现2次,电影院名称只出现一次。