简体   繁体   English

如果结果集中的值与当前登录的用户不匹配,则有条件地使用回退值

[英]Conditionally use fallback value if value in resultset does not match currently logged in user

I have the php script below to get data from a database and return it to a calendar as part of a booking system.我有下面的 php 脚本来从数据库中获取数据并将其作为预订系统的一部分返回到日历。 The title field, $row["title"] , is actually the username of different people for each booking. title 字段$row["title"]实际上是每个预订的不同人的用户名。

Everything works well, but I want to change things so that each user can only see their own username on the calendar, not each other's.一切正常,但我想改变一些事情,以便每个用户只能在日历上看到他们自己的用户名,而不是彼此的。 I want them to see 'booked' instead.我希望他们看到“已预订”。

I'm pretty new to php, but my guess is that I need to iterate over the created $data array, changing only the title field if it doesn't match the logged in user.我是 php 的新手,但我的猜测是我需要遍历创建的$data数组,如果它与登录用户不匹配,则仅更改title字段。 I'm thinking this would come from this in my login script:我认为这将来自我的登录脚本:

$_SESSION["username"] = $username;     <=== I think this needs to be incorporated into the script and the php loop.

What I am trying to do is replace the title field with 'booked' if it doesn't match the logged in user.我想做的是,如果title字段与登录用户不匹配,则将其替换为“已预订”。

I also need to allow all users to see public entries too, say, unavailable , holiday -- so those title values should always be shown.我还需要允许所有用户也看到公共条目,比如unavailableholiday —— 所以应该始终显示这些title值。

<?php
$connect = new PDO('mysql:host=localhost;dbname=xxx', 'xxx', 'xxx');
$data = array();
$query = "SELECT * FROM events ORDER BY id";
$statement = $connect->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
foreach($result as $row)
{
    $data[] = array(
        'id'    => $row["id"],
        'title' => $row["title"],
        'start' => $row["start_event"],
        'end'   => $row["end_event"]
    );
}
echo json_encode($data);
?>

Let's say Mary is logged in. The data array will look like this:假设 Mary 已登录。数据数组将如下所示:

[
    {"id":"365","title":"Kerry","start":"2021-08-19 20:00:00","end":"2021-08-19 20:40:00"},
    {"id":"366","title":"John","start":"2021-08-19 19:00:00","end":"2021-08-19 19:40:00"},
    {"id":"367","title":"Mary","start":"2021-08-20 10:00:00","end":"2021-08-20 10:40:00"},
    {"id":"368","title":"Mary","start":"2021-08-20 12:00:00","end":"2021-08-20 12:40:00"},
    {"id":"369","title":"Betty","start":"2021-08-20 15:00:00","end":"2021-08-20 15:40:00"}
]

But I want to change it to this before sending it to the calendar:但我想在将它发送到日历之前将其更改为:

[
    {"id":"365","title":"booked","start":"2021-08-19 20:00:00","end":"2021-08-19 20:40:00"},
    {"id":"366","title":"booked ","start":"2021-08-19 19:00:00","end":"2021-08-19 19:40:00"},
    {"id":"367","title":"Mary","start":"2021-08-20 10:00:00","end":"2021-08-20 10:40:00"},
    {"id":"368","title":"Mary","start":"2021-08-20 12:00:00","end":"2021-08-20 12:40:00"},
    {"id":"369","title":"booked","start":"2021-08-20 15:00:00","end":"2021-08-20 15:40:00"}
]

If you want to access session data you'd first need to start the session. Then you can just use the session variables in the script如果你想访问 session 数据,你首先需要启动 session。然后你可以在脚本中使用 session 变量

<?php
    session_start();
    
    $connect = new PDO('mysql:host=localhost;dbname=xxx', 'xxx', 'xxx');
    $data = array();
    $query = "SELECT * FROM events ORDER BY id";
    $statement = $connect->prepare($query);
    $statement->execute();
    $result = $statement->fetchAll();
    
    foreach($result as $row) {
        $data[] = array(
            'id'    => $row['id'],
            'title' => isset($_SESSION['username']) && $row['title'] == $_SESSION['username'] ? $row['title'] : 'booked',
            'start' => $row['start_event'],
            'end'   => $row['end_event']
        );
    }
    echo json_encode($data);

sidenote, this will only work properly if all the usernames are unique though旁注,这只有在所有用户名都是唯一的情况下才能正常工作

If the the username in the SESSION is the same as the row's title, then show the title, otherwise show booked.如果 SESSION 中的用户名与行的标题相同,则显示标题,否则显示预订。

Extension: To show the title value when it matches the logged in user's name OR if it matches so communal/public strings, pile them all into an IN() condition.扩展:要在匹配登录用户名时显示title值,或者如果它匹配公共/公共字符串,请将它们全部放入IN()条件中。

Recommendation:推荐:

$sql = "SELECT id,
               IF(title IN (?,'unavailable','holiday'), title, 'booked') AS title,
               start_event AS start,
               end_event AS end
        FROM events
        ORDER BY id";
$statement = $connect->prepare($sql);
$statement->execute([$_SESSION['username']]);
echo json_encode($statement->fetchAll(PDO::FETCH_ASSOC));

If you want this to be a dynamic condition, you can prepare your whitelist array in advance:如果你希望这是一个动态条件,你可以提前准备好你的白名单数组:

$allowTitles = [
    $_SESSION['username'],
    'unavailable',
    'holiday',
];

Then you create the necessary number of placeholders and feed the array to execute() .然后创建必要数量的占位符并将数组提供给execute()

$placeholders = implode(',', array_fill(0, count($allowTitles), '?'));
$sql = "SELECT id,
               IF(title IN ($placeholders), title, 'booked') AS title,
               start_event AS start,
               end_event AS end
        FROM events
        ORDER BY id";
$statement = $connect->prepare($sql);
$statement->execute($allowTitles);
echo json_encode($statement->fetchAll(PDO::FETCH_ASSOC));

PS I share @DarkBee's concern regarding unique names in your db table. PS 我和@DarkBee 一样关注您的数据库表中的唯一名称。 Typically you should use ids to avoid any chance of data collisions.通常你应该使用 ids 来避免任何数据冲突的机会。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM