php MySQL查询将酒店房间列为子组

lc8prwob  于 2023-03-07  发布在  PHP
关注(0)|答案(1)|浏览(152)

这是我在这里的第一篇文章,我希望你能给我一个如何解决问题的想法。我有一个酒店房间数据库,我想把它们列在一个网格上。每个房间有x张床,它们以下面的格式存储在数据库中:

101A
101B
101C
102A
102B
103A

前三个数字代表房间号,A,B,C ......代表床号。
我将使用以下查询列出所有房间:

$stmt = $db->prepare("SELECT PokojID, Symbol, Opis, Lozka, Active 
                        FROM pokoje 
                        WHERE Lozka = :Lozka 
                        OR :Lozka = '0' 
                        AND Active = '1' 
                        ORDER BY Symbol;");`

如果我只想显示第一个房间,我发现这个查询有效:

$stmt = $db->prepare("SELECT PokojID, 
                            Left(Symbol,3) as Symbol, 
                            Opis, count(Lozka) as Lozka, 
                            Active  
                        FROM pokoje 
                        WHERE Lozka = :Lozka 
                        OR :Lozka = '0' 
                        AND Active = '1' 
                        GROUP BY Left(Symbol, 3) 
                        ORDER BY Symbol;");

我尝试以数组形式返回房间列表,格式如下:

dp.resources = [
    { 
        name: "Room 101A", id: "1", expanded: true, children:[
            { name : "Room 101B", id : "2" },
            { name : "Room 101C", id : "3" }
        ] 
    },
    { 
        name: "Room 102A", id: "4", expanded: true, children:[
            {name : "Room 102B", id : "4" },
        ] 
    },
    { name: "Room 103a", id: "5" }
];

这是我的PHP代码:

$json = file_get_contents('php://input');
$params = json_decode($json);

$Lozka = isset($params->Lozka) ? $params->Lozka : '0';
//Hostel Mode
$stmt = $db->prepare("SELECT PokojID, Symbol, Opis, Lozka, Active 
                        FROM pokoje     
                        WHERE Lozka = :Lozka 
                        OR :Lozka = '0' 
                        AND Active = '1' 
                        ORDER BY Symbol;");
// Hotel Mode
//$stmt = $db->prepare("SELECT PokojID, Left(Symbol,3) as Symbol, Opis, count(Lozka) as Lozka, Active  FROM pokoje WHERE Lozka = :Lozka OR :Lozka = '0' AND Active = '1' GROUP BY Left(Symbol, 3) ORDER BY Symbol;");

$stmt->bindParam(':Lozka', $Lozka); 
$stmt->execute();
$rooms = $stmt->fetchAll();

class Room {}

$result = array();

foreach($rooms as $room) {
    $r = new Room();
    $r->id = $room['PokojID'];
    $r->name = $room['Symbol'];
    $r->capacity = intval($room['Lozka']);
    $r->status = $room['Opis'];
    $result[] = $r;
}

header('Content-Type: application/json');
echo json_encode($result);

先谢谢你。

enxuqcxy

enxuqcxy1#

RiggsFolly的answer referenced是一个很好的例子,值得一阅读,但与您的问题无关。
我理解你的疑问,但是这里给那些不理解的人造成的困惑,是一个很好的例子,说明了为什么用“标准”的方式做事是一个好主意。
你几乎肯定会从房间和床的关系正常化中受益,把床放在一张叫做房间的table上会散发出一股难闻的气味。
我已经在查询中为列添加了别名,以避免事后重新分配它们。SELECT列表中的所有列都将包含在最终JSON输出的房间对象中。如果您不希望输出中包含这些数据,请将其从SELECT列表中删除。

$json = file_get_contents('php://input');
$params = json_decode($json);

$Lozka = isset($params->Lozka) ? (int)$params->Lozka : 0;

// initialize array to hold our SQL params
$sqlParams = [];

// Alias the columns to whatever you want them to be, instead of reassigning them later
$sql = 'SELECT PokojID AS id, Symbol AS name, Opis AS status, Lozka AS capacity, Active
        FROM pokoje
        WHERE Active = 1';

// Let's only add the `Lozka` criterion when we want it to be applied
if ($Lozka > 0) {
    $sql .= ' AND Lozka = :Lozka';
    $sqlParams['Lozka'] = $Lozka;
}
$sql .= ' ORDER BY Symbol';

// Prepare, execute and fetch
$stmt = $db->prepare($sql);
$stmt->execute($sqlParams);
$rooms = $stmt->fetchAll(PDO::FETCH_OBJ);

$prevRoom = null;

foreach ($rooms as $i => &$room) {
    if ($prevRoom && (int)$room->name === (int)$prevRoom->name) {
        $prevRoom->expanded = true;
        $prevRoom->children[] = $room;
        unset($rooms[$i]);
    } else {
        $prevRoom = $room;
    }
}

header('Content-Type: application/json');
// the use of array_values() here is just to re-index the array so the keys are
// not included in the output from json_encode()
echo json_encode(array_values($rooms));

相关问题