Making Parkwood Vale · Part 13 of 13
Part 12
Finish the library: reserve and return books, with emails and a loans page.
We complete the loans system. Users can reserve a book, see their loans and return it, and the number of copies available stays correct.
In this part:
0:00 The plan for loans
0:34 The browse page, filtered by author or publisher
8:49 The book details page and its Reserve button
13:02 Reserving: adding a loan with a due date 7 days later
22:03 Reducing the number of copies available
27:32 A confirmation email with PHPMailer
34:39 The loans page, using JOIN queries
40:55 Returning a book and sending a thank you email
47:38 Outstanding and returned loans in Bootstrap tabs
Software: Adobe Dreamweaver, UwAmp, HeidiSQL, PHP, MySQL, PHPMailer, Bootstrap.
Transcript
- Okay, in this session then we're going to look at how we set up the loaning ability
- within our library system. We're going to look at how people can reserve a book, how
- they can publicly browse through all of the books held in our library currently, and
- how they can return the book as well, making sure that when they loan a book out and
- when they return the book, it affects the number of available copies stored within
- our database.
- Okay, let's get into it.
- Okay, so here I am back in my student desktop and this one's going to be a little
- bit of a Blue Peter style session because here is what I was working
- on earlier.
- Now, I've actually recorded this on the Monday where I usually record on a
- Sunday because I wasn't too well over the weekend so I wanted to make sure all of
- that was clear out before I tried to record anything.
- I've just set up UwAmp, I've launched UwAmp, it's just starting, my local host has
- just come into life and Apache has stopped for a second, let me just stop that
- entirely and find out why
- just simply cycling, there you go, up and running, both sides are running and now
- local host should pick up
- eventually, there we go So what I did earlier in preparation for the session was I
- built this Browse Book system.
- Now if I very quickly jump onto Parkwood Vale, you'll see that the Browse Book
- system has a similar sort of layout.
- Again, it's not identical to it because there are some subtle differences, but where
- I clicked on Andy Weir, and I can close off Andy Weir, Dan Brown, close off Dan
- Brown, it is a similar system.
- So Andy Weir, I can clear the Andy Weir, I can go Bloomsbury Publishing, I can clear
- Bloomsbury, I can go Dennis Taylor, all that same sort of functionality.
- Now I'm going to show you the code I use within this functionality and talk you
- through it, but unlike in previous sections, I'm not going to code it live in front
- of you, just to speed up time ever so slightly.
- I also created this section here, where I could go in and see all the details about
- a book. I can go back to browse books.
- Let's go for one with a bit more details and you can see those details in place.
- I can log in. So that's my usual CPD
- password system. Submit it. Now if
- I'm browsing books and I'm clicking onto there now, I can reserve that book.
- If I click on reserve at the moment, it doesn't do anything.
- That's the part I've left ready to work on this session.
- So let's get Dreamweaver open and have a look at what it is I did
- earlier today.
- If I minimize this out then and I scroll down the first thing we're going to look at
- is the browse book page which was on my testing server let's grab that from
- there browse book and let's pull that
- down and do the same for book and I also put in a loan system so
- let's get loans as well and return to my local view
- now I'm on ad book there let's get rid of these close these previous session
- elements out so they don't confuse us and open up the browse book page
- I'm going to zoom out a little bit just so we can see everything and take you
- through what it's all doing so to start with at the top I create my PHP tag I
- include my header tag I don't need any session details at the moment because this
- page can be open to anybody. It would create a session within my header anyway but
- that's by the by. I've got my book SQL code here which says select star which
- just means everything from my books table.
- I've got my author SQL which is select a distinct author.
- Now what that distinct command will do is it'll make sure that when it pulls back
- all of the authors it only pulls back unique records
- so if I was to look back on here a second browse books if I go on to
- the JK Rowling page even though she is there twice it only
- brings back one element for her on this side here same goes with Bloomsbury
- Publishing it publishes both the Harry Potter books but only appears in my list once
- that is through that distinct command.
- I've done the same thing then for publisher, using the distinct command there and
- set that from books. Now, I've included two elements to check
- if author is in get or if publisher is in get.
- If author is set in our get, so that's in that URL field across the top there, what
- we want to do is a query where the author is what's in our get command.
- Same goes for our books all three of these are repeated to make sure we've got those
- in place saying those are a publisher there so I could say publisher and include
- those elements now the final part of PHP in this section is looking
- at our books doing our connection and bringing that query back based around the SQL
- statements we have there I then use the same layout the container the
- mb4 class background for my color a class p4 here for my
- spacing for my padding and then a div row in place
- inside my div row i've got my call md3 with the title of author where it says if
- author isn't null give me the ability to clear which takes me back to browse books
- same happens here for if
- publisher isn't doesn't equal null then give me the clear in the publisher section
- and give me
- I've got a while loop two or three while loops in this section actually all of them
- use row as the
- container their variable container but the first one is for author fetch associate
- and all it does is echo out a a href giving our author code
- as an encoded URL and then that goes out the name of that author same process
- here for publisher and we don't need this a second let me jump that aside can see
- here I have while row equals publisher fetch associated rows publisher itself they
- come in from this publisher command up there don't want to go too quick for anybody
- and I've got while each publisher in the fetch associated array in row
- looped through so if there's only one publisher it'll only do it once if there's 10
- it'll do it 10 times however many you've got in place it sets up an ahref that
- goes back to our standard page of browse books with publisher and it echoes out that
- URL encoded publishing it. Remember that URL encoding is important for situations
- where we have Bloomsbury Space Publishing,
- HarperCollins Space UK,
- J.R.R. Tolkien. Again, that would be important to make sure that when we
- put that in, it sets up our percentage 20% across the top here to
- search through our database correctly.
- That's all done with our URL encoded section.
- Now that section there, that div call MD3 is my sidebar in this section here.
- That's this section on the side here.
- And if I clear that a second, what we're looking at next then is the call MD9,
- which is set up for each of the books.
- We do include a div of 3 and a div of 9 in their own rows.
- One is to provide with my jacket image, set to 100% of its parent container.
- And the other just sets up a link to the book, our book page.
- And the ISBN is our element there.
- Now, I could include URL encode here
- just to be on the safe side. It shouldn't
- have an issue. It shouldn't be a situation where we need URL encode because there
- shouldn't be any spaces in our ISBNs.
- But depending on what your identifiers are, it may be a different situation.
- Now, as you go through, you've got the name being echoed out there.
- You've got the author, publisher, and description.
- And if I come over here, we have the name as our link, which if I hover over it down
- in the bottom corner, you can see the ISBN,
- the author, the publisher, and the description.
- Obviously, with the real books, we have a better description because we took that
- directly from Google during our section about APIs.
- Now, if I click onto Ready Player One, that takes me to my book page with the ISBN
- set as bar number across the top there.
- On my PHP side of things, then, so if I save that a second and close that and open
- up my book page, it's very similar in code.
- At the moment, it has an empty post section here.
- that's purely in place ready for what we're going to do tonight and then we've got
- our connection query here saying select all the information so again we're selecting
- star from books where the isbn is equal to what's in our get command here
- and then i've taken the same elements from my previous section
- and i've just removed one of my outlines so instead of having a nine by
- three and then a 3 by 9 I've simply got the 3 by 9 element in place.
- Starting off at the top then we have our div of curl 12 in there it tells
- us our row here with a md3 with our jacket image
- and just to reiterate and show you that is the same let me zoom back up
- same code we have in this bottom section here.
- Alongside that then we have the same information echoed out.
- The only main difference here is inside this first section
- here we have available copies set to the row available copies.
- That allows us down in this bottom section then to put in some search facilities.
- In these search facilities we have things such as if our session email it
- doesn't equal null so there's something in session then check to see if copies is
- greater than zero if copies is greater than zero set up our form to reserve the book
- if copies is less than zero so we have our else here say sorry the book is not
- available please check back and if our session information so that's all that if
- else if there if our session information isn't available it then takes us to our
- account page to log in and then just ends itself so there's two forms i've
- created ready in place to be used today i will show the last one i created as well
- which is based around loans now again this has an empty post in place let me just
- zoom that back out i will revisit this when it comes to this section and i will also
- explain exactly what's going on with this join here because that's a little bit more
- complicated
- But as we go through, we're just repeating the same process.
- We're searching for something, we're calling it loans this time rather than books,
- but we're doing the same thing.
- A row with a 3 and a 9. The image goes in the 3 with 100% width, and then the
- information about the book goes in the 9.
- Again, there's no difference realistically between this one and this one, just our
- query that we'll be performing.
- So what I'm going to do is just very quickly zoom fully out onto this one here.
- I know that is quite small, but the idea here is that I'm going to just pause it
- here, give you a couple of seconds to look at that code, see what it is I've done.
- You can pause the videos yourselves and you can have that on your screens to see
- what it is I've done in the different sections.
- let's repeat that process then so let's zoom this one out so you can see all of the
- code again there's a lot more code on this one so i don't actually think you can see
- all of that code so what i'm going to do is just give you a couple of seconds on
- this top section now obviously this is going out via youtube so if you want to you
- can pause it and then there's the bottom section that's
- where we finished so this is all the new stuff that should have been shown
- and then this last one here again let's zoom out so you can see
- all of that code nice and clearly in place
- brilliant so our first step then is going to be on book and it's going to be
- capturing this our post which we are posting here, hidden information of ISBN and
- email and updating our tables correctly to make sure that we record all of that
- information.
- I'm just gonna pause ever so slightly there just to make sure that I don't lose any
- elements of these recordings, have a quick drink and we'll get back to it in a
- couple seconds time.
- So what we need to do then at this stage is capture that information that we're
- posting back to ourselves in book and use it to update our database.
- The part of our database that we're specifically looking to update is our loans
- table. So let's just open up hide the SQL a second
- and look at what's currently in loans.
- My test data from earlier, as I expected it to be.
- Now, there is no return date because the book hasn't been returned.
- Return is set to zero because it hasn't been returned, but we have in place the same
- date and time for our loan. We'll be dealing with that in about five minutes time or
- so. What we can see is we have our email and our library ID storing in
- our information here. So we want to insert into that table.
- So let's go into our SQL, so dollar sign SQL as usual.
- And let me just zoom that back in so you can see it nice and clearly.
- There we go. And what I want to do is insert into,
- and the table is called loans.
- and the information I will be inserting isn't limited in any way.
- It is going to be email, library ID.
- Now let's just double check. I've got that as a capital L.
- Sometimes it can be very specific on these things.
- Library ID, let's check capital E for email.
- Email, library ID, let's go, comma.
- We want the loan date. Was it?
- Yep.
- long date, we want the return date,
- and that's about it. The values that we
- are going to be passing it are going to be important.
- Now I'm going to put a break there, my SQL end there, and what I'm going to do is
- simply put a return in here just to make it a little bit easier to deal with.
- Now the first thing we're going to be passing through to it is going to come from
- post.
- dollar sign underscore post and I want to take in email
- which has simply come from my hidden information here
- in type hidden email there. ISBN is also there I'm going to put that in next.
- So email will come first that goes into our email field.
- I'm going to put in comma now I'm going to put in again single quotations I'm going
- to break out of that PHP into dollar sign underscore post
- And in that post section there, then I want ISBN.
- Now, those two elements come from what we posted to it.
- The next element is going to be working on date time provided by PHP.
- So what I'm going to do is put that in place there.
- Again, break out of it. And I want to put in date time.
- So I simply use the now command
- in PHP.
- And that should have gone green if it was happy with what we wanted.
- Let's just double check.
- PHP current date time.
- And let's get stack overflow. Always a good starting point.
- Let's see. Date time. Time rather than now.
- Okay. So I'm quite happy.
- Time zone. There we go. Time. Yeah.
- that's what we're going to go with i'm going to go into here and simply put that in
- this time there i want it as more than just time though because if we look back at
- this let's see we want our date time to be set out similar to this
- we can specify how we want it laid out so let's go
- date and it is going to be this element here
- but before it i'm going to tell it how to lay it out
- is expecting it as year month day then time so i'm going to say
- capital y for year i want let's double check forward
- slashes i think it is no it's minor symbol month day
- and then we have hours
- minutes I believe if I do capital it's
- the there we go capitalization should make it double
- numbers so 0 7 for 7 o'clock or 19 for 7 p.m and go from
- there now I just want to check how that looks a second so I'm going to echo dollar
- sign sql and just see what's going on there let's go I'm currently
- Let's push that up, jump back into here, and click Reserve.
- Now, that has echoed out. Insert into loans.
- Now, it's echoing it into this page here, which has no formatting, so I've got to
- highlight it to see it. Insert into loans, email, library, loan date and return
- date, CPD of Mr. J. Jones, the ISBN, and then the date and time.
- Looking how I'd expect it to, which is perfectly fine.
- Next up then I want to use date time and I want to say let's say PHP
- current date time add seven days
- and what I can do
- is simply use the string to time function and do seven
- days plus so I'm going to steal that little tiny bit of code from there I'm going to
- put it into by here and explain what it's doing so
- second now this date function i have here if i bring this down onto its own line and
- talk you through it what that's saying is i want it to be a date formatted as year
- month day hour minute and then i for seconds of the current time
- which is brilliant the next one up i want
- is going to do a similar function so i'm going to steal this function again
- copy that into this section here but instead of having date time as my
- current date time i'm going to say i want it to be time
- plus seven days
- now with all of that in place if i save that and now put that up again
- and simply go back here refresh this page yes
- continue to resubmit that I now get 2023,
- the 7th of the 17th, which is correct.
- And then I get 2023, the 24th of the 7th, which is
- 7 days on from my current timestamp.
- So that allows me to put that in place.
- And that would fill in email, loan date, due date, and then return and return date
- are empty. What we need to do as well, I need to verify this one, is in our Heidi
- SQL section, null return date should allow null if I take this
- query then very quickly and just check it's working by running it through Heidi
- and click run due date doesn't have a default value okay
- let's see did I I did return date my bad it should be due date not return date
- due date there we go and if I run that
- what happens is when I refresh my table it's now inserted that second ID.
- Let's correct our code here then because I said there return date that should be due
- date and let's put that in place.
- Now if that was to be executed that would
- correctly insert that in. I do need to do a $SEN connection,
- query and I need to that
- there just simply to ensure that happens as well as
- ensuring that I'm recording the fact that that's been booked I want to query
- and this time I want to update so let's go update
- and I want to update the books table I want to set
- the available copies so if I go into books a second we have available copies
- here i'm going to be extremely lazy and just copy that and say set
- available copies equals available copies minus one
- so i can already use the information i don't have to go off and find what available
- copies are available i can just say take whatever's in available copies remove one
- from it and i'm going to say where now let me just capitalize that way because it
- should be capitalized and so should set it won't break them not being capitalized
- but it's good practice to have them capitalized as a uh let me double check and
- believe it's isbn yep isbn equals
- equals and i'm going to break out of this
- here a couple of full stops and back into it very quickly just there that should be
- fine one single quote and one single quote there always difficult putting single
- quotes in place it doesn't quite like things I say ISBN here
- so what that should do is it should allow me to
- record the fact that I've rented that book for a seven day period it should also
- allow me to then go through and update it to remove one from those copies.
- So the book I'm currently on, if I go back to my loans table, ended in 432.
- If I go back onto my books table, 432 has four copies.
- Let's just, we'll leave that as it is.
- And for a second, I'm going to go back.
- There you go. Ready player one, as is there.
- Four copies. Refresh that.
- Ready player one. Perfect.
- If I've done this right, it should load with nothing.
- Nothing's being echoed out at the moment, but it should reduce one from there, and
- it should add a new record into here.
- Just to make this a little bit easier on this side of things, I'm going to remove
- both of those records because they're not really needed at the moment, so we can
- clearly see if it has inserted that correctly.
- Reserve.
- It's printed something. That means I didn't press the put up button after I saved
- it.
- a problem. Save it. Let's put it up.
- Now let's try again.
- Reserve.
- Nothing's been printed. Looking a lot more positive.
- Let's refresh this. We have our record added.
- And let's go into books and refresh this
- it didn't deduct one let's have a look at why
- let's take this SQL here then cut it ever so slightly do dollar sign SQL
- again equals paste it so we can see what it's doing and put in dollar sign SQL here
- put that up and refresh this page
- yes continue to submit that information okay nothing's
- happening
- It helps if you actually echo out what it is you're trying to echo out and
- check.
- Post.
- No. Refresh. Yes. And here's my SQL.
- Let's copy that and run it through Heidi and see what Heidi has to say.
- Run.
- Ah. Very subtle. A little mistake I seem to have made.
- I have an open and close, an open and close there.
- And what that's caused is an error where it's taken that literally and then put our
- number in place. If I was to remove one of those elements and have that go green
- like it has done and run it, it updates no problem.
- if I go back into here and remove that second one there save it put
- it up we don't need to echo it because I'm extremely confident put that up
- and let's go back reserve
- okay in books for copies now becomes
- two okay let's just double check that's not going crazy let's go back one
- of course it would be two because they did one through hiding one through that if i
- go reserve in here it should now be one and if i go back a page
- reserve back a page we are now below zero so it's now telling us that the book
- cannot be reserved and to check back in which is brilliant complete functionality
- exactly what we wanted to do it is recording into our loans section when we reserved
- our book, giving us a month's due date and sending us a no return date, but it's
- also setting it up there to reduce that element.
- Now what we can do at this point here is steal a little bit of code from our
- password reset and I want to include or use our PHP
- mailer element. So let's copy that into books, top here,
- and I'm going to only use them and only require them if I'm reserving a
- book.
- And from the password reset, then if I scroll down, I'm actually looking at this try
- email section here. So let's zoom out just so we can make sure we don't miss
- anything. I want email or mail to equal new PHPMailer.
- I can take all of element here, copy it onto here and
- underneath this paste it. So it says mail is a new mailer.
- It is an SMTP going from a Gmail which is true.
- It's going through my Gmail account there which I set up for this demo with that one
- -time password. They port elements there.
- It's emailing from Parkwood Vale to the post email.
- We have used post email so that will all work correctly.
- the HTML Parkwood Vale library instead of password reset I'm gonna say book
- loan
- and all I'm gonna say in the body here is thank you for
- loaning a book from us today
- and send that if there's an error it's gonna tell
- me what the error is so So let's go book a second, let's go up with that information
- and I need to change that to a one just so I can actually reserve the book again.
- Refresh this, whoops, it doesn't like something on line four.
- Why what's on line four?
- Use okay, I believe these two because they are essential
- they do need to go at the very top there.
- save them put that up and refresh
- there we go now if I was to reserve this it should send me an
- email so let's go reserve it's gone off is it gonna send me an email
- let's have a look and
- not sure how well this is gonna come up with there but you can should be able to see
- in there it does show that I have
- from Parkwood Vale saying thank you for learning the book.
- It doesn't really tell us any information about the book.
- It's a little bit bland, but we could just put in a new query to grab
- that book's information. So I want, again, let's go back to where we had it.
- We already had it in book. I just want this section here.
- Copy this entirely
- and paste it there. So I'm going to grab that where the ISBN is set.
- it's not going to be in a GET because this section of here only works when it's a
- POST. So I'm going to say $ underscore
- POST and it's capitalized ISB and it does
- make a difference. Don't fall for that mistake.
- I've done it plenty of times and can't figure out why things aren't working.
- Now in there then we've got our different elements.
- I'm going to say that I want to fetch the associated array just like I have done
- down by here so let's grab that code no point rewriting it in this section here
- while we are fetching our associated array I'm going to say
- that dollar sign book name
- equals dollar sign row name let's
- keep it simple and then underneath here I'm going to say in this
- a book from us today.
- Let's go break line.
- Please come to the library
- to pick up and then in that place there I can do
- dot dollar sign book name.
- And Parkwood Vale, Central Parkwood Vale, yes, subject, book loan
- for, and then I'm going to say the same thing, dot, dollar sign,
- book name.
- Now, save that, and let's put that back up again.
- I do need to adjust that to two, just so I can't reserve it.
- And let's go back.
- Hit reserve yet again, and that's gone through.
- Give it a couple of seconds to pull through on my phone and hopefully I should get
- an email. Book loan 4. Oof. That's come through.
- I'm not sure how well you can see that, but it doesn't actually show you the name of
- that book name. It's gone through this section here.
- And it's not pulled it through.
- Capitalization. That's all it was.
- I'm glad I put that to 2
- I mean if you look at my reservations I'm a massive fan of Ready Player One I've
- reserved it about 14 times so far Come on And
- now This time round we do include Hopefully you can see that
- It says clearly Thank you for your reservation of Ready Player
- One Come to the library to collect it
- we've got that in place. Once we get to this section then all we need to do is
- redirect back to our previous element just like we did in our password
- reset we redirected back to the forgot password page this
- time around in books here we just need to redirect to a different page and the page
- I'm going to redirect to not forgotten password but that additional page that I've
- created it called loans up here as you can see a lot of ready player one
- so that's going directly to loans so after i reserve the book it takes
- me to my loans page that redirect should be in place
- and let's go refresh add it in again
- and browse book well it could be
- any book actually it doesn't
- So that lets go for the Martian and reserve.
- And it's brought me to this page.
- I've got all of those books there, all of those books.
- And the last one is the Martian.
- I should pick up another email in a couple of seconds time.
- There it is.
- And on the bottom there, you should be able to see that that is showing you my
- reservation.
- Okay, in the next section then, we're going to do the opposite system.
- Doing pretty much the same thing, just saying thank you for the return on an email
- and sending it so that the books go back into our library system.
- right so let's get back into this then we're going to go into our loans page now
- just to go through this with you again very briefly there's a session start at the
- top the empty header to make sure that we are logged in and redirect to the login
- page if we're not then we include our headed up PHP.
- We have this empty post for email at this section here.
- That's where we're going to be working in a second but let's run through the rest
- first.
- We then have a loans query connection that checks and returns all of
- the information for the books that we have loaned out
- using that loans table. Now we've got a special element here called a join.
- If I was to remove that section just there where we put that join in place
- and just look at that information that I get what will come
- back in my loans page will be very very blank if I actually remember to
- push that up as well and it's giving me
- the fact I can return these books but it can't find the pictures and it actually has
- no return IDs either. That's because without that join section
- it doesn't really do very much.
- Let's come down by here a second, let's go echo and I want to echo all of
- this just so I can check it into HeidiSQL and talk you through some of the elements
- that are involved there. Let's push that up, refresh the page, now the join is back
- in place, go to the top the page find that SQL command and copy it
- okay so what we're actually saying at this point is select
- all the information from this book here can I zoom in at all I
- don't think I can I'll see I can do it in post and see how that goes so I'm
- saying select star stack all from loans and if I actually just remove that join in a
- second and go there I'll show you what it looks like that selects everything from
- loans but that's just my loans data table that's
- nothing different to what I got there what the join will allow me to do is
- connect two books when there is a foreign foreign and
- public key as our key elements
- so So where you have a key field here, if I go into my table made up here, I have
- my primary key, not public. I knew there was a correct word for it.
- I have a primary key of ISBN. I have a foreign key of library ID.
- And those two there join and link to bring back that extra information.
- So when I run that, it's effectively saying, bring back all the information from my
- loans table. and bring back all the information from my books table where the
- ISBN is meeting here. But I don't have to tell it to do the ISBN element.
- It does that itself from that section there.
- I can control this. I can reduce it down.
- So I could say bring back name and run that and it would just give me the book
- names.
- I can also go a bit further with it.
- I could do a join there saying join that information.
- and I could join also here so I could say join
- and I want to join users on
- users dot email equals
- loans dot email and when I run that
- email closes ambiguous of course this is by here I'm say loans
- .email and if I run that now
- it brings back my loans table, my book table and if I
- carry on going across it also brings back my users table.
- So in this section what I could effectively say is bring back forname
- from my users table,
- surname and name
- and if I run that I get James Jones for forname and surname and the name of the book
- is Ready Player One Allot and The Martian is one off.
- I can include ISBN because it's a singular
- item. I could also
- email and pull that back. It'll say email is ambiguous.
- I want to say loans.email and that'll show me the email address
- used. So I can join using different elements.
- Joins can be particularly difficult.
- It takes a very long time to wrap your head around and make sure they're all working
- correctly, especially if you start looking at things like inner joins and outer
- joins. We don't need inner and outer joins here a simple join will do and we don't
- need a complex join like that one there we just need the simple join that i have in
- place which is if i come back here a join on the isbn and the library id so i can
- see all of the books that i've got in place i'll get rid of that for a second then
- and i'll zoom back in there so you can see it nice and clearly i got select star
- from loans join books on books.isbn i always put the
- joining table first equals so it's the same as loans.libraryid
- where my email is in my session email so i can only see my own books in place
- i could swap that out if i had a different user and i wanted to see their books i'd
- swap that to their id their email not mine anyway that all in place with
- our element in our bottom section here
- We are seeing that we have hidden information for ISBN and email.
- And I'm going to put in place the submit button that will take me back to this page
- here.
- Now, I'm going to cheat. I'm going to go back to books.
- I'm going to scroll to the top of books.
- And I'm going to say that I'm going to use my SQL.
- And actually, I can't cheat. Never mind.
- I'm going to say SQL, dollar sign, SQL equals
- update.
- and I want to update loans. I want to set return date.
- Let's capitalize set.
- And it was a capital R for return if I remember rightly.
- Set return date equals and I can steal this here.
- I just want my current date and time.
- I want to set my
- to that and I also want to set uh
- so I'm going to say comma here and I closed Heidi I shouldn't
- have closed Heidi I want to double check I'm pretty sure it's just the word returned
- but I want to make sure I'm doing this right so loans in here data it is returned
- set returned
- equals one where
- now this is where we're going to be putting in two where commands I'm going to say
- email equals
- and
- that's fine let me just check I've done all of these right because these always
- catch me out that would cause an error
- that's perfectly fine right email equals dot dollar sign underscore
- post and inside that post element I want it to be email
- and let's go full star that one of those
- there we go and so we can use that joining and element
- and you say ISBN, no, is library ID
- is equal to,
- and in there, I want to say dot dollar sign underscore
- post because we're talking about that post array again, and in there, simply put in
- ISBN.
- Okay.
- Let's have a look at that.
- need a full stop to that
- I
- want a second SQL so that will be connected so dollar sign will say SQL
- no I want to do connection sorry not SQL and I
- want to query and simply dollar sign SQL
- and actually library ID
- okay yeah I'll deal with that in a second there's something else one add in there
- but give me two seconds no no do it now um long set
- return date there return equals one and I'm going to say one other,
- no,
- where email is that and library is that, and I say and again
- returned equals zero.
- Now realistically, in a real world environment, you should never be able to loan the
- same book twice while you've still got it loaned out.
- So that should capture that and make sure that we don't have any issues.
- I have a lot of copies of Ready Player One loaned out, 10 of them.
- nonetheless that should stop any sort of anomalies and here then i've got
- connection equals query and that should send that off i'm going to say dollar sign
- sql equals up and then again it's going to be an update
- this time around it's going to update books it's going to set
- and we can do the complete opposite of this one let me just copy the bottom
- end of that
- So instead of saying minus one, I'm going to say set available copies equals
- available copies plus one, where ISBN is equal to post ISBN.
- That's in place. And again, I'm going to query it and I'm going to set that through.
- The last part is, like I've done here,
- just taking all of this email section all the way down to here and to my loans
- page. And I'm going to paste it below.
- And what I've got is in this section, it says select start from books where ISBN is
- the ISBN. Fetch the row, put it into book name and put it in place.
- Set up a new mail as a PHPMailer from my SMTP settings.
- All the way down.
- book lung I say book return for book name thank you
- for returning a book from us today please
- thank you for returning a book to us
- today and I'm just gonna say we hope
- you have enjoyed and then I've got book name now that should
- send that email now if I go push up on that one and on this section
- here then by refresh my loans page that should disappear by click on return
- I've got an error on line 20 what does line 20 say
- ah of course I can't include PHPMailer elements if I don't include PHP
- mailer. So I need the two users and loans
- at the top here, two users, and I need the requires, and I'm going to put the
- requires again, only used if they are Need it.
- Up that.
- Refresh that. Yep.
- And there we go. So I should now have return
- dates in my copy, in my book, sorry.
- I now have, if I refresh that, Ready Player One with copies.
- and in my email I have an email that does specify
- thank you for returning Ready Player One I hope you enjoyed it.
- So we have that system in place we can now return a book and reserve a book from
- our library system. They are all shown in one section all the books that I've got in
- place are shown in that one section let me just tidy that up because let's face it
- that looks very messy I
- the exampler there we go and I just have two books now
- my return book and my not return book
- I could and I have done actually if I look at Parkwood Vale
- I could look at splitting this section here so I have my div container here for
- these elements it's got all of our books It's got all of the information from my
- box. So in this section here, while that is in place,
- it's got the loan information as well, so it should know if it's been returned or
- not. I can simply add in a tag here and say echo returned.
- And now if I push that up and refresh this, we get one and a
- zero.
- very helpful so let's make that a bit nicer and say
- instead of echoing there I'm gonna say if returned
- there is equal to zero then I want to
- echo
- now let's flip it on its head let's say if it's equal to one then I want to
- echo
- returned.
- I can say on and then I could also include
- in that section by using dollar sign row and do
- returned date.
- Oh, is it returned date I used?
- Return date I used, not returned date.
- Return date there.
- And save that for that.
- And when I refresh that, it's not happy with line 90.
- I broke something here. No, I didn't.
- I forgot to close that if off.
- And now put that there.
- So it says returned on and then it's got that date in a very hideous fashion.
- But it's got the full date in place and that one doesn't have a date.
- I can tidy that date up by using the date command again.
- Open it up and say I want it in.
- Let's go a bit more British, shall we?
- So day, month, year.
- and I'm not going to put in the time because the exact time I returned it isn't
- massively relevant to me. Let's put that up and refresh this now
- and it says, it's not happy with it, we need to convert it, we need to use around
- this one string to time.
- I think.
- This part's gone off script, I'll be completely honest with you.
- worked okay so what I've done there is I've used a command called string to time is
- taking the string element that was stored in my database and converted it to a
- timestamp and then use the date formatter to put it in place I could include the
- hour let's capitalize it see if that works our minute
- in the seat and And save that, up that,
- and... I mean it didn't quite work out.
- I want to say it's a J.
- Let's have a look. Date, time,
- PHP, date.
- It's the date function.
- Format. PHP manual, let's have a look.
- HIS, yes that's fine.
- I'm sure there used to be a list of these things.
- Never mind.
- Let's just take that out and just put it as h i
- s
- or entirely remove it. Like I said, I've gone off script a bit with that one so that
- wasn't exactly what I was planning on doing.
- Now that's there, it's got the return date in place.
- What we can also do is bring this in a little bit.
- Again, this is completely off script, so do bear with me about errors.
- And I'm going to repeat this if returned is one.
- If returned is one, then I want to echo this section out.
- And let's just keep our bracket in nice and tidy so we can
- always tell where we are. So now it will only return or display my returned
- box. Pop that up, refresh this,
- it's only my returned box. I'm going to put in place by here then a heading 2,
- h2,
- and say returned box.
- I'm going to copy the whole of this div,
- paste it and change that to
- outstanding
- books and change that to zero.
- Now with that in place then if I put that up and refresh this, return
- books there, outstanding books there, we have a bit of an issue.
- Ah.
- Once you've gone through your row, it tends to delete what you've done.
- So what I would do is I'd actually move that query
- down to here,
- inline there, and we can actually reduce it ever so slightly.
- we can reduce it ever so slightly as I was saying.
- Finish your sentence. And that is that where email is there and
- returned
- equals zero. No, equals one.
- Now, realistically, we shouldn't need this if, but it's always good to keep it there
- just in case. And if I copy that and come down here with
- it and just paste it by there,
- swap that round so now return is equal to zero and put that up what we
- should have nice and clearly is our outstanding and our returned box
- and you can see the difference between the two of them if i click on return
- i now have two return books no outstanding books and a
- another email eventually there we go just come through so we can now
- return and reserve
- Let's go Harry Potter. I want to reserve it.
- It brings me to my books of reserves.
- I've got my returned books and my outstanding Harry Potter book that when I return
- it, returns that book into the library section.
- All working perfectly fine. slightly different to what I'd set up initially within
- Parkwood Vale where if I log into that one my loans section have outstanding
- and returned in two different tabs but all they are is using bootstrap
- tabs bootstrap and in here again examples
- docs even sorry layouts columns,
- content
- components
- Let's go on to the component section, let's go to the accordion and we can scroll
- through it.
- I can't actually remember which one of these I used.
- List groups?
- No, it's not that.
- Nav and Tabs is what I used.
- So you can see these here and what they allow you to do is have these little funky
- elements set up here. So you have a UL for Nav Tabs, LI for the titles
- and the LIs for where they are set out to.
- And it all allows you to put it in place.
- how you wish to design and how you wish to lay out your website.
- So now, with that, we have the final functionality element of our site in place.
- You can now reserve and return books.
- Thank you for watching. A much easier session than some of the previous ones.
- Hopefully it all makes sense. As always, any issues, just drop me a message and I'll
- do my best to help you out as much as I can.