-3

I'm trying to convert seats to an array in the controller.

this is how the seats data is sent to the controller seats : "B7D7G10"

i want to convert it to look something like this "B7 , D7 , G10" and I want to match the seats data with my seats table so i can change the seats values in the seats table

This is my jQuery function.

$("#purchase").on("click",function() {
    var data = {
        seats: $(".seats-selected").text(),
        seats_id: $(".seats-selected").data('sid'),
        theatre_id: $("#theatres").data('tid'),
        movie_id: $(".movie_id").val(),
        movie: $("#m-title").text(),
        theatre: $("#theatres").text(),
        date: $("#date").text(),
        time: $("#time").val(),
        total_price: $("#total-p").text(),
    };
    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });
    $.ajax({
        type: "POST",
        url: "/send-email",
        data: data,
        success: function(response) {
            alert("Your Theatre" + data.theatre + "\n" + data.seats_id + "\n" + data.date + "\n" + data.time + "\n" + data.total_price + "\n" );
        }
    });
});

This is my controller:

    $seats = $request->seats;
    $theatre_id = $request->theatre_id;
    $date = $request->date;
    $time = $request->time;
    $movie_id = $request->movie_id;
    $total_price =  $request->total_price;    
    $bookings = bookings::where('theatre_id', $theatre_id)
                    ->where('date', $date)
                    ->where('time', $time)
                    ->where('movie_id', $movie_id)
                    ->get();

    $reserved_seats = $bookings->pluck('seat_number')->toArray();

    $seat = seats::where('seat_number', $request->seats_id)
                ->where('theatre_id', $theatre_id)
                ->where('movie_id', $movie_id)
                ->first();

    if ($seat && !in_array($seat->id, $reserved_seats)) {
        $seat->available = 0;
        $seat->save();

        // add the booking to the bookings table
        $booking = new bookings;
        $booking->theatre_id = $theatre_id;
        $booking->movie_id = $movie_id;
        $booking->seat_number = explode(',',$seats);
        $booking->date = $date;
        $booking->time = $time;
        $booking->total_price = $total_price;
        $booking->user_id = auth()->id();
        $booking->save();

I tried every method implode, explode, array_map, everything it won't work if i just leave it be it will only change the value of the first seat. What I wanna do is save the values in bookings table (seat_number column) e.g like this: A1, B9, G10 and match the seats the AJAX request is sending to the controller from the seats table and change its available value to 0.

10
  • can you log what your request looks like or even the json data you are posting through ajax? Commented Feb 1, 2023 at 22:30
  • Might need more details here. You say "it will only change the value of the first seat", but you're only querying for a single Seat anyway... Please edit your question with more details. Also, your deadline is not relevant to the question, please don't add it to your title 😅 Commented Feb 1, 2023 at 22:31
  • this is my json data date : February 01 Wednesday" movie : "Hacksaw Ridge" movie_id : "78" seats : "B7D7G10" seats_id : 25 theatre : The Silver Screen " theatre_id : 2 time : "10:00 PM" total_price : "PKR.3000" Commented Feb 1, 2023 at 22:36
  • 1
    explode(',', $seats); is not going to work on "B7D7G10"; there's no , separating them. If you sent "B7,D7,G10", then that explode() call would convert it to an array. But still, I'm not sure what you're trying to do with your data, or what your bookings table looks like, etc., so we won't be able to help much beyond pointing out syntax issues, sorry Commented Feb 1, 2023 at 22:39
  • 1
    As expected, whereIn() needs an array, but you've only got Strings. You're gonna have to have a look at your front-end. Right now you're just doing seats: $(".seats-selected").text(), which will send A1B2C3 (or similar), since that's what .text() returns. You have to change that, somehow... I can't see your front-end code, so I have no idea how you need to change that, but you do. Do some searching on how to either manipulate strings in JS, or how to send an Array via AJAX (which is possible), and then you won't need to do explode() on the back-end at all. Commented Feb 1, 2023 at 22:50

2 Answers 2

1

If you need to separate the string with no delimiting characters, then split on the zero-width space after each sequence of numbers.

\K forgets the previously matched characters so that they are not lost in the splitting process. preg_split()'s advantage over preg_match_all() is that preg_split() only creates a flat array whereas preg_match_all() creates a 2d array -- of which only its first row is used.

Code: (Demo)

$seats = "B7G12D9";

var_export (
    preg_split('/\d+\K/', $seats, 0, PREG_SPLIT_NO_EMPTY)
);

Output:

array (
  0 => 'B7',
  1 => 'G12',
  2 => 'D9',
)

Here's the implementation of the same pattern on a different string for a different effect.

Sign up to request clarification or add additional context in comments.

Comments

0

It's easy enough to split up a string like B7D9H12 with a regular expression.

In PHP you'd have

<?php

$seats = "B7G12D9";
$pat = "/([A-Z]\d+)/";  // Search for A to Z followed by one or more digits and capture
preg_match_all($pat, $seats, $matches);
$seatArray = $matches[0];         // Extract the captured data from the $matches array
var_dump($seatArray);

Giving an array:

array(3) {
  [0]=>
  string(2) "B7"
  [1]=>
  string(3) "G12"
  [2]=>
  string(2) "D9"
}

You can do something similar in Javascript if you prefer.

See https://3v4l.org/bfZka

1 Comment

The capture group in the pattern is not used or necessary.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.