Iterating an array replacing values that don't match logged in username

Hi, I have the php script below to get data from a database and return it to a calendar as part of a booking system. The title field, $row[“title”], is actually the username of different people for each booking.

Everything works well, but I want to change things so that each user can only see their own usernames 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. I’m think 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.
Can anyone point me in the right direction?
Thanks in advance :blush:

<?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:

[ {“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”} ]

It would be more efficient to only get back from the database what you’re going to show. Can you show us the structure of your events table?

But each user needs to be able to see which slots are booked and which are empty. Therefore I want to show ‘booked’ on the unavailable slots (already taken). The free slots will remain blank.

The user clicks on an available slot in FullCalendar. I take the logged in username and send it to another script as ‘title’ variable, along with the start and end time. This submits a pdo to the database. The ID is auto-increment in mysql database. To display the database, I get the data back using the script above. It works fine, but I want to replace all instances of title that don’t match the current user. Something like ‘booked’ or ‘unavailable’. I want all users to see what’s booked and available, but not by who.

I didn’t make a booking system, but I did make it where I can check the database table to see if a certain date was used. It’s a simple query, but I’m sure it can be modified for a more robust use.

protected function checkForEntry($calDate)
{


    $sql = 'SELECT 1 FROM cms WHERE DATE_FORMAT(date_added, "%Y-%m-%d")=:date_added';

    $stmt = Database::pdo()->prepare($sql);

    $stmt->execute(['date_added' => $calDate]);

    return $stmt->fetch();


}

I think in a booking system you first want to check to see if the dates are available and display the dates that are available and unavailable?

No worries. Someone on another website provided a working solution:

<?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);

Works perfectly. Thanks for all your input. :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service