gpt4 book ai didi

php - 尝试与房间匹配

转载 作者:行者123 更新时间:2023-11-30 01:11:41 24 4
gpt4 key购买 nike

StackOverflow 成员(member)大家好,

我正在尝试修复 friend 的一些代码,但我无法摆脱它。我们已经朝着这个目标迈出了一步,但这并不是确切的顺序。我先解释一下。

我们想要创建一个匹配系统。但不是基于用户基础,而是基于实际房间基础。匹配是根据最高租金、最小平方米和选择的城市进行的。

现在我们创建了以下代码,该代码输出每个用户及其匹配的房间。

$members = $db->query("Select g.gFirstname, g.gLastname,g.gEmail, v.vMeter, v.vMaxrent, v.vWhen, v.vCity From users as g LEFT JOIN users_pref as v ON(g.gId=v.vGid)");

$count = $members->num_rows;

while($fetch = $members->fetch_assoc()){
echo '<br /><br />';
echo 'Rooms found for: <strong>'.stripslashes($fetch['gFirstname']).' '.stripslashes($fetch['gLastname']).'</strong><br /><br />';
$selectKamers = $db->query("Select * From rooms Where kWhen >= ".$fetch['vWhen']." AND kCity = ".$fetch['vCity']." AND kMeter >= '".$fetch['vMeter']."' AND kMaxrent <= ".$fetch['vMaxrent']."");

while($kamer = $selectKamers->fetch_assoc()){
echo '<h1>'.$kamer['kStreet'].' '.$kamer['kHomenumber'].'</h1>';
}
}

正如我所说,这段代码首先显示所有用户,然后在每个用户下方的一列中显示用户应用的符合其偏好的每个房间。

现在的问题是。我们想要创建一个函数来显示所有房间,以及与该房间匹配的所有用户。

也许是因为缺乏洞察力,但是有人可以帮助我如何实现这一目标吗?

我的数据库表如下:

--users
gId | gFistname | gLastname | gEmail
1 | Bob | Hope | Email
2 | John | Doe | Email

--users_pref
vGid | vMeter | vMaxrent | vWhen | vCity
1 | 10 | 400 | time() | cityId
2 | 20 | 500 | time() | cityId

--rooms
kWhen | kStreet | kHomenumber | kMeter | kMaxrent | kCity
time() | street | 100 | 15 | 300 | cityid
time() | street | 200 | 25 | 400 | cityid

我的解释尽可能完整,如果我遗漏了什么,请告诉我。

非常感谢!

凯文。

最佳答案

考虑以下...

DROP TABLE IF EXISTS users;

CREATE TABLE users
(user_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,firstname VARCHAR(12) NOT NULL
,lastname VARCHAR(12) NOT NULL
,email VARCHAR(12) NOT NULL UNIQUE
);

INSERT INTO users VALUES
(1 ,'Bob','Hope','Email1'),
(2 ,'John','Doe','Email2');

DROP TABLE IF EXISTS user_preferences;

CREATE TABLE user_preferences
( user_id INT NOT NULL
, city_id INT NOT NULL
, date VARCHAR(30) NOT NULL
, meter INT NOT NULL
, maxrent INT NOT NULL
, PRIMARY KEY(user_id,city_id,date)
);

INSERT INTO user_preferences VALUES
(1,1,'time()',10,400),
(2,1,'time()',20,500);

DROP TABLE IF EXISTS rooms;

CREATE TABLE rooms
(city_id INT NOT NULL
,street VARCHAR(12) NOT NULL
,homenumber INT NOT NULL
,date VARCHAR(30) NOT NULL
,meter INT NOT NULL
,maxrent INT NOT NULL
,PRIMARY KEY(city_id,street,homenumber)
);

INSERT INTO rooms VALUES
(1,'street',100,'time()',15,300),
(1,'street',200,'time()',25,400);

要获取所有房间以及任何兼容用户的列表,我们可以这样做...

SELECT r.*,u.* 
FROM rooms r
LEFT
JOIN user_preferences ru
ON ru.city_id = r.city_id
AND ru.date = r.date
AND ru.meter <= r.meter
AND ru.maxrent >= r.maxrent
LEFT
JOIN users u
ON u.user_id = ru.user_id;

+---------+--------+------------+--------+-------+---------+---------+-----------+----------+--------+
| city_id | street | homenumber | date | meter | maxrent | user_id | firstname | lastname | email |
+---------+--------+------------+--------+-------+---------+---------+-----------+----------+--------+
| 1 | street | 100 | time() | 15 | 300 | 1 | Bob | Hope | Email1 |
| 1 | street | 200 | time() | 25 | 400 | 1 | Bob | Hope | Email1 |
| 1 | street | 200 | time() | 25 | 400 | 2 | John | Doe | Email2 |
+---------+--------+------------+--------+-------+---------+---------+-----------+----------+--------+

关于php - 尝试与房间匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19422963/

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