WEBVTT 00:05.360 --> 00:11.710 In this video we're going to build the get Tripps function which will send an AJAX call to the get trips 00:11.710 --> 00:18.140 to PSP file which will retrieve all the trips from the database and display them on the on the Tripps 00:18.140 --> 00:19.100 page. 00:19.100 --> 00:21.730 So let's create the AJAX call 00:25.780 --> 00:28.020 who's going to copy that's when discussing why customize 00:36.570 --> 00:45.060 guys are going to send the call to the get those PSP File we're using the post method. 00:45.290 --> 00:46.030 OK. 00:46.990 --> 00:47.280 And 00:50.220 --> 00:56.140 well we don't have really to specify the method here because we're not going to be sending any data. 00:56.160 --> 00:56.730 All right. 00:56.970 --> 00:59.790 So we have no data to be sent. 00:59.970 --> 01:00.570 And then 01:04.530 --> 01:06.680 the school is successful. 01:06.820 --> 01:13.030 We're going to have a return data which will will place inside the troops. 01:13.250 --> 01:15.900 They've placed inside the troops. 01:16.080 --> 01:23.810 So the idea of just as much troops that I say if we are looking at writes 01:28.000 --> 01:40.960 We are just going to selectively using its ID then change to the ACBL concerns using the meta data. 01:41.690 --> 01:42.450 Right. 01:42.510 --> 01:50.920 If there is an error then that is going to go to the same They've just display their error message. 01:52.820 --> 02:01.550 OK so now let's go to the PSP file and retrieve the chips from the database. 02:01.560 --> 02:05.740 So first of all we're going to start a session and connect to the database 02:10.630 --> 02:14.640 you just copy the code straight from the previous file. 02:18.330 --> 02:18.930 OK. 02:19.410 --> 02:24.040 Then to retrieve all the trips we are going to need to run a query. 02:24.780 --> 02:29.940 So the query is going to select all the trips from the database for the connected user. 02:30.030 --> 02:34.650 So let's build our query and source inside a variable as Cuil 02:38.480 --> 02:48.170 So we're going to select all records from our table cache trips. 02:48.770 --> 02:53.390 The condition here is we need the much for the user ID. 02:53.660 --> 02:54.230 OK. 02:54.230 --> 02:56.140 So you can get the user ID again. 02:57.770 --> 02:59.030 From the stationary 03:05.180 --> 03:20.610 like we did that. 03:20.860 --> 03:34.250 OK so now let's run the query and store the result in the results variable. 03:35.650 --> 03:41.040 So again this function will take the connection link the query self 03:44.980 --> 03:45.220 OK. 03:45.220 --> 03:49.120 Now let's check if the query is successful. 03:49.190 --> 03:53.310 So we're going to check if your results. 03:53.800 --> 03:54.200 OK. 03:54.230 --> 04:00.040 Let us. 04:00.280 --> 04:03.680 If the query is not successful this is going to return false. 04:03.790 --> 04:05.020 OK. 04:05.100 --> 04:07.840 And we're not going to get here. 04:08.160 --> 04:17.300 So if the query is successful then we're going to check the number of rows inside the result variable 04:17.550 --> 04:23.220 and then fetch the data inside and then show all the trips on the page. 04:23.270 --> 04:29.340 But before we go ahead I'm just going to print this variable just so that we have an idea what's we've 04:29.400 --> 04:30.100 got inside 04:34.970 --> 04:37.730 just comments. 04:38.450 --> 04:42.330 The Tripps table is called CarShare troops. 04:42.420 --> 04:43.440 OK. 04:43.470 --> 04:44.900 So it's save. 04:44.900 --> 04:50.200 So now if we go back to our jobs case while we need to make sure that we call this function get trips 04:50.320 --> 04:51.310 ok. 04:51.490 --> 04:54.630 And just have a quick look at the world we're used here. 04:54.680 --> 04:59.980 So I need that file get trips with one T and it's going to take one to you there. 05:00.050 --> 05:00.450 OK. 05:02.490 --> 05:05.020 So let's call this function. 05:05.040 --> 05:11.260 Once we load the page to get the chips. 05:12.010 --> 05:14.230 All right so now let's save. 05:14.660 --> 05:17.850 There's a page. 05:17.970 --> 05:21.590 OK so that's our results object. 05:21.730 --> 05:31.500 So you can see the number of rows is three lines and that's what we expect for this use because you've 05:31.510 --> 05:33.630 got three records. 05:34.010 --> 05:36.620 So the first thing we're going to do in the PSP file 05:40.190 --> 05:45.290 is check the number of rows inside our results objects. 05:45.290 --> 05:48.770 So to do that we're going to use the function my school-I 05:51.510 --> 05:52.560 the gross 05:55.500 --> 05:55.990 OK. 05:56.230 --> 06:01.520 So you're going to check if the number of rows is greater than zero. 06:01.990 --> 06:05.420 That means that the user has created some chips. 06:05.500 --> 06:09.610 Ok before we are ready to show those trips. 06:09.850 --> 06:18.340 Otherwise we're just going to echo a warning message saying to the user that they have not created any 06:18.340 --> 06:19.240 trips yet. 06:19.580 --> 06:19.800 OK 06:34.340 --> 06:36.030 let's add a couple of classes 06:38.690 --> 06:45.440 alerts and alerts warning. 06:45.740 --> 06:46.030 OK. 06:46.030 --> 06:50.210 So if the number of rows is greater than zero so you've got recourse. 06:50.350 --> 06:57.850 So we're going to do we're going to you're going to fetch data inside the results objects. 06:58.150 --> 07:03.090 So we're going to use the good life which array function as we did before. 07:03.400 --> 07:13.310 So we're going to have a while loop which will loop through the different rows of the results objects. 07:13.740 --> 07:20.580 So every time we're going to store the results inside a row variable. 07:20.580 --> 07:24.180 So that's going to be the first row. 07:24.180 --> 07:28.180 Then the second row and we're going to keep going until the last one. 07:28.210 --> 07:30.140 Inside there is also objects. 07:30.150 --> 07:34.570 OK so we've already covered this many times before so it must be very very familiar with this. 07:34.620 --> 07:35.160 OK. 07:35.310 --> 07:43.200 So the function is choli fetch array. 07:43.990 --> 07:50.470 So it's going to take as a barometer the result object you can give me another but I'm going to give 07:50.470 --> 08:00.200 it a my is Kulai associative because it wants and wants the row array to be right OK with keys. 08:00.280 --> 08:05.700 So I'm going to add my is choli and the school attachés you. 08:06.110 --> 08:08.230 All right. 08:08.600 --> 08:09.270 All right so 08:14.010 --> 08:23.130 now for each row before we display a trip we're going to check if the trip is regular or not. 08:23.250 --> 08:23.520 OK. 08:23.520 --> 08:30.000 Because the way we display regular trips is going to be different from the the the way we display one 08:30.000 --> 08:38.310 of trips the regular trips are going to have the days the weekdays on time. 08:38.390 --> 08:45.100 I'm going to say that wriggler the one of trips are going to have a date and time and we're going to 08:45.100 --> 08:47.140 say that one of journey. 08:47.170 --> 08:47.540 Right. 08:47.710 --> 08:57.800 So let's check the frequency. 08:58.410 --> 09:01.670 So to get the frequency we've got to go inside a row 09:05.780 --> 09:14.440 the frequency is going to correspond to the elements of key regular A quick reminder the row is going 09:14.440 --> 09:18.470 to have all these elements OK for each record. 09:18.490 --> 09:22.090 All the elements inside that we can see. 09:22.170 --> 09:24.790 You know it's a you know table. 09:24.820 --> 09:28.420 So now we're looking at the elements of key regular 09:39.480 --> 09:48.790 This chick first if it's not regular meaning the value is like that one. 09:49.440 --> 09:51.660 So if it's not regular then 09:55.340 --> 10:02.900 to make it easy for us to display all these elements we're going to store this inside a very well I'm 10:02.900 --> 10:04.270 going to quote frequency. 10:04.500 --> 10:05.010 OK. 10:09.440 --> 10:19.010 And let's say that frequency is one of Jenny writes 10:23.520 --> 10:30.320 and we are going to need to display this line as well. 10:30.320 --> 10:35.790 So we're going to have the date it's time. 10:36.490 --> 10:46.850 So to get the dates we're going to go to the rule and guess the elements of key date. 10:46.940 --> 10:49.330 So everything is highly variable and get equal time. 10:49.550 --> 10:54.880 So it going to be again date at time. 10:55.040 --> 10:55.550 OK. 10:55.820 --> 11:04.250 So let's get the dates are we going to go to the grow array and use the elements of key dates then then 11:08.840 --> 11:11.200 next to this 11:13.940 --> 11:16.130 it's like this. 11:17.710 --> 11:19.200 And then it's hard to like. 11:19.570 --> 11:28.170 So that's time is going to be elements of key time. 11:28.480 --> 11:31.680 It's a lot. 11:32.210 --> 11:32.650 Right. 11:32.750 --> 11:33.030 And 11:42.710 --> 11:52.640 now if the trip is regular then the frequency here he's going to say regular 12:10.920 --> 12:13.460 the time is going to be slightly more complicated. 12:13.470 --> 12:14.810 So what we're going to do 12:20.130 --> 12:31.050 is just a trip that is good to see what we've got all of these. 12:31.110 --> 12:47.650 One of journeys so let's get to one. 12:47.740 --> 12:57.430 OK so we need to find a way to get all the week days where the trip is made and then we're going to 12:57.580 --> 13:02.620 create a variable that shows weekdays at time. 13:02.650 --> 13:13.180 The easiest way is to create an array and then go through our row and whenever we find that a weekday 13:13.270 --> 13:18.040 has quoted value one we just add that weekday to the array. 13:18.310 --> 13:20.730 So we're going to end up with an array. 13:20.770 --> 13:22.100 In our case here. 13:22.140 --> 13:23.200 Sharing. 13:23.500 --> 13:25.760 Monday Wednesday and Friday. 13:25.870 --> 13:33.710 So three elements then we're going to use a function to combine all these the weekdays. 13:33.770 --> 13:34.150 OK. 13:34.240 --> 13:35.910 Using a minus sign. 13:36.480 --> 13:37.290 All right. 13:37.430 --> 13:41.120 And then we're going to show them like that and then we'll talk. 13:41.990 --> 13:42.390 OK. 13:44.270 --> 13:47.800 So let's create an empty array call it Harry 13:52.350 --> 13:56.210 so it starts with Monday so we get to go to our row. 13:56.360 --> 14:06.620 Just check if the value of Monday is 1 then we're going to push the value Monday into our array. 14:06.990 --> 14:08.190 So it is with us. 14:08.730 --> 14:28.260 So if the elements of key Monday has got a value one then we're going to push the value 14:30.760 --> 14:32.810 Monday inside our array. 14:32.950 --> 14:36.590 So to do that we can use a function called aere push. 14:36.610 --> 14:42.540 This is very similar to the jobseekers function or the method push of arrays. 14:42.880 --> 14:48.880 So this function is going to take a couple of Ramos's the array itself and the value that we would like 14:48.880 --> 14:50.860 to push into our array. 14:50.860 --> 14:54.570 So in our case we want to have something like this right. 14:58.480 --> 15:05.440 So we're going to do exactly the same thing for all the other weekdays are going to go through day Wednesday 15:05.680 --> 15:06.680 until Sunday. 15:06.890 --> 15:08.270 Okay. 15:08.310 --> 15:11.530 So to save time I'm just going to pause the video. 15:11.540 --> 15:13.060 I'm going to do exactly the same thing. 15:13.160 --> 15:14.300 Okay. 15:14.330 --> 15:17.510 And then just replace Monday with all the other week. 15:19.340 --> 15:23.040 All right now we want to share something like this. 15:23.090 --> 15:23.560 OK. 15:23.780 --> 15:27.050 So we've crossed the elements inside our array in our case. 15:27.320 --> 15:33.770 In that case here the array is going to show one day coma Wednesday coma Friday. 15:33.890 --> 15:35.570 So you have something like this. 15:35.710 --> 15:45.650 We're going to need to concatenate those elements inside the array displace a minus sign between between 15:45.650 --> 15:46.030 them. 15:46.280 --> 15:49.910 So we're going to use a function called implode. 15:49.910 --> 15:56.490 So this function is going to take a couple of problems. 15:57.140 --> 15:58.970 Oh great. 16:00.010 --> 16:00.580 OK. 16:00.890 --> 16:07.040 And whatever you want to apply to place inside the between the elements so in our case you want to place 16:07.040 --> 16:09.140 a minus sign between elements. 16:09.170 --> 16:09.770 All right. 16:10.100 --> 16:15.600 So this function is going to return this OK. 16:15.630 --> 16:20.500 Now the final results we want is all of that. 16:20.670 --> 16:29.220 So is going to place everything inside the variable time that we get to that time. 16:29.420 --> 16:35.390 OK. 16:36.020 --> 16:36.560 So 16:40.440 --> 16:48.120 now we've got the frequency because the Dasen time all the weekdays and time. 16:48.380 --> 16:48.610 Right. 16:48.620 --> 16:55.160 So now everything else would be the same whether the trip is a regular one or a one off one. 16:55.200 --> 17:00.870 So now we are ready to echo the trip. 17:00.900 --> 17:02.510 All right so we're going to do that in the next video.