格式应如下所示,用户名数组和每个用户名中酒店表中的酒店名称及其相应的评级,可从评级表中提取。但是,我需要加入users表以链接订单列表以从中提取用户名。
评级列表代码
$books = array(
"phil" => array("Sandman"=> 2.5, "Jurys Inn Newcastle" =>
3.5,
"Premier Inn Newcastle" => 3, "Sunderland
Marrtiot" => 4,
"Travelodge Sunderland" => 2.5,
"StayBridge Suits London" => 3.5),
"sameer" => array("Apex London Wall" => 2.5, "Jurys Inn Newcastle" => 3.5,
"Mercure Leeds parkaway wilds" => 3, "Sunderland Marrtiot" => 3.5,
"Travelodge Sunderland" => 2.5, "StayBridge Suits London" => 1),
"john" => array("Linthwaite House" => 5, "Britannia Hotel leeds" => 3.5,
"Premier Inn Newcastle" => 1),
"peter" => array("chaos" => 5, "php in action" => 3.5),
"jill" => array("Apex London Wall" => 1.5, "Britannia Hotel leeds" => 2.5,
"Mercure Leeds parkaway wilds" => 4, "the host: a novel" => 3.5,
"the world without end" => 2.5, "StayBridge Suits London" => 3.5),
"bruce" => array("Apex London Wall" => 3, "the hollow" => 1.5,
"Mercure Leeds parkaway wilds" => 3, "Sunderland Marrtiot" => 3.5,
"the appeal" => 2, "StayBridge Suits London" => 3),
"tom" => array("chaos" => 2.5)
);
我在phpmyadminl中的表格 tbl_rating表(id,rate,user_id和hote_id) 酒店表(id,hotel_name,hotel_price,图片,描述) 用户表(身份证,姓名,电子邮件和密码)
我之所以包含三个表的原因是我想加入表以便从users表中提取名称,并使用评级表从hotel表中提取hotel_name
答案 0 :(得分:0)
$usersHotels = getUsersHotels();
function getUsersHotels()
{
//mysqli provider - you can use any your own db connection
$mysqli = getDbConnection();
$query = 'SELECT users_table.name as user_name, hotel_table.hotel_name,
tbl_rating.rate FROM users_table JOIN tbl_rating ON
(users_table.id=tbl_rating.user_id) JOIN hotel_table ON (hotel_table.id =
tbl_rating.hote_id)';
$data = array();
if ($result = $mysqli->query($query)) {
while ($row = $result->fetch_assoc()) {
if(isset($data[$row['user_name']) && is_array( $data[$row['user_name']])){
$data[$row['user_name']] = array_merge( $data[$row['user_name']], array($row['hotel_name'] => $row['rate']));
} else {
$data[$row['user_name']] = array($row['hotel_name'] => $row['rate']);
}
}
}
$result->free();
return $data;
}
//optional code.
function getDbConnection()
{
$mysqli = new mysqli("localhost", "user", "password", "world");
/* check connection */
if ($mysqli->connect_errno) {
printf("Connection failed: %s\n", $mysqli->connect_error);
exit();
}
return $mysqli;
}