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
It’s easy enough to split up a string like
B7D9H12
with a regular expression.In PHP you’d have
Giving an array:
You can do something similar in Javascript if you prefer.
See https://3v4l.org/bfZka
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 overpreg_match_all()
is thatpreg_split()
only creates a flat array whereaspreg_match_all()
creates a 2d array — of which only its first row is used.Code: (Demo)
Output:
Here’s the implementation of the same pattern on a different string for a different effect.