skip to Main Content

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.

2

Answers


  1. 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

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search