Class Checkpoint

Making Parkwood Vale · Part 3 of 13

Part 3

Set up the Dreamweaver site, learn the PHP basics and connect to the database with mysqli.

We point Dreamweaver at the UwAmp testing server, cover the PHP we will need for the rest of the build, and make a reusable database connection file. In this part: 1:07 Starting UwAmp from the memory stick 1:34 Creating the Dreamweaver site with UwAmp as the testing server 4:48 PHP tags, echo and joining strings 7:32 Indexed and key/value arrays, print_r and var_dump 12:14 for loops 15:44 foreach loops, with if and continue 19:29 A test database and users table in HeidiSQL 21:49 Connecting with mysqli and stopping with an error if it fails 24:08 Fixing connection errors 26:48 Moving the connection into config/connection.php and loading it with require Software: Adobe Dreamweaver, UwAmp, HeidiSQL, PHP, MySQL.
Transcript
  1. In the first of this week's two videos, what I'm going to do is reshoot video number
  2. three. I've had a couple of people message me and tell me that the quality wasn't
  3. quite there, they couldn't understand or see what was going on, so I'm going to go
  4. back and hopefully do an improved version of it.
  5. Video three then focuses on setting up Dreamweaver, making sure that you are
  6. connected correctly to your test development site,
  7. and shows you some basic PHP commands.
  8. There may be some slight changes between what I delivered in video 3 originally and
  9. what I delivered this time around in video 3.
  10. I have the original setup in place, but I may just word things ever so slightly
  11. different. So with that in mind, both video 3 and the original video 3 will still be
  12. available to you. You can watch whichever version you like.
  13. You can watch the two versions, compare them and see what I've done differently.
  14. Hopefully this recasted though should be a bit clearer for you.
  15. Let me know if there are any issues.
  16. Let's jump into it.
  17. Okay, so here I am backing my student account then.
  18. And what I'm going to do is start off by opening up and running UwAmp.
  19. I can do that by going to the directory, go into my USB, find UwAmp and launch the
  20. EXE.
  21. Now there's UwAmp running, it's currently running here and let's just open up the
  22. local host so we can see that everything's in place.
  23. What I'm going to do from there then is I'm going to jump straight into Dreamweaver
  24. and we're going to look at how we could set up Adobe Dreamweaver.
  25. now when we are in adobe dreamweaver and it's in its base form like this we need to
  26. go over to the word site and manage sites that'll allow me to create a new site
  27. ensuring that all the files are kept together i can go to the word new site here and
  28. i'm just going to give it a name so parkwood vale it wants to know
  29. where i'm going to store it in my document so i'm going to use folder icon here just
  30. to locate it in my documents here I'm going to create a new folder called Parkwood
  31. Vale and I'm going to select that folder as
  32. well as setting up my local location for it I'm going to go into my server section
  33. and add in a test server so I'm going to say Parkwood
  34. Vale test and using the connection type of
  35. a local or network folder that's because it is running from that USB so
  36. when I click on local or network folder then it wants to know where the folder is so
  37. I can go into my USB go to the UwAmp folder and it's actually the WWW folder
  38. I want to use so I've got inside folder I'm gonna click select folder.
  39. The web URL is simply localhost and I can click
  40. on save. Now that's giving me my server in place and it's given me the ability to
  41. either have is a remote server or a testing server.
  42. I'm going to set it as my testing server and click save.
  43. And when I click on done then on the side here we'll have our folder structure and
  44. if I was to go on to my testing server side I can see the files that are already in
  45. place on that UwAmp folder. And as you can see, I already have a MyApp folder
  46. and I have an index.php page in place on that UwAmp folder, which is
  47. what gives me my local host view.
  48. So that index.php is what gives me this file here.
  49. When it comes to creating our sites then every website that exists has an
  50. index.php that is how web browsers know where to start on
  51. where to look for content i'm simply gonna as i'm setting my testing server up here
  52. click on my index.php file here and i'm going to download it by using the get file
  53. option and that will bring that into my local view so i have it here and i can open
  54. it up and we can see everything that comes pre-installed on that UwAmp folder
  55. but we don't really want any of this content in place.
  56. What I'm going to do is highlight it all.
  57. I'm going to go in and I'm just simply going to delete everything out from that file
  58. there and when I click on the save then I need to put that up so that's in place and
  59. if I went back to localhost you'll see it is a blank page.
  60. That is because this page here, which should contain all of my code and all of my
  61. text, is currently empty. Now I'm going to zoom in here so hopefully on the
  62. recording you can see this a lot clearer as to what it is I'm going to do next.
  63. I'm going to set up some basic elements within PHP.
  64. First thing I want to show you is how to create a PHP tag, which is just the normal
  65. HTML tag with PHP in place. I do need to put a question mark before the word
  66. PHP and as you can see there it went from being a blue tag to a red tag to show that
  67. it recognizes it as PHP and to end my PHP tag is like every other tag I simply put
  68. in question mark and then close that off.
  69. Anything in here then will be executed server-side before the user gets to see it.
  70. If I wanted to display something I have a variety of different methods.
  71. I could use a simple echo, which means to output something.
  72. And if I echo the usual hello world and terminate it
  73. using a semicolon, save it and put that file.
  74. When I go into my local host and refresh it, you'll see it says hello world.
  75. Inside the speech marks, I can put any HTML tags I like.
  76. So I could have that set as a h1
  77. and end that h1 there save it put it and refresh
  78. it and it'll apply the h1 tag to it
  79. so echoing essentially means anything inside speech marks i want you to output
  80. it
  81. verbatim as it is don't worry about compiling it let the web browser do that so as
  82. you can see there i got speech mark starting and a speech mark ending to put that in
  83. place i can concatenate
  84. So if I put in h1 here and close those speech marks off, you'd see that
  85. actually it starts off, if I save that and I put that, it's going to give me an
  86. error.
  87. And it says that there's an error expecting a semicolon or a comma and is telling me
  88. that is on line two.
  89. Line two has that in place and then even though the semicolon's there, what's
  90. breaking it is the fact that it has this code here that isn't in any form of PHP.
  91. to execute it.
  92. Now like I said just now I can concatenate these so I can do is put a full stop
  93. there and put in more speech marks and put that there
  94. and what that would actually do if I simply put a BR tag here and then a page one
  95. tag here is
  96. effectively create it so that those two are joined as one long command.
  97. Seems a bit of a waste at the moment doesn't really do anything but it will be very
  98. adding in variables so we can throw them in the middle of something as we're doing
  99. it. Now that's the echo command to print something out to our screen.
  100. But if I wanted to print out an array and I wanted to put in place an array of
  101. information, I couldn't use echo.
  102. Echo is only for single strings.
  103. So to create an array in PHP, I use the dollar sign to create a variable.
  104. I give it a property. So I'm going to say name and I'm actually, I'm going to call
  105. this array just to
  106. and I'm gonna say that my array is equal to and I can give it
  107. some properties
  108. now inside here I'm gonna simply say James Jones for my name and I'm gonna say
  109. let's mix it up and say 34 for my age if I save that
  110. put that and refresh my page nothing changes because I've
  111. created my array but I haven't tried to output it in any way.
  112. So if I brought this back, let's check this back as a simple single line, hello
  113. world echoed, and then I tried to echo below and I wanted to echo out my array,
  114. it's going to give me an error.
  115. Now if I refresh that, it tells me hello world and it cannot convert the array to a
  116. string.
  117. What I could do is target a single property within my array.
  118. at position 0 it would print out James Jones and that would put that in place.
  119. I could also print out position 1 and that would print out
  120. my age.
  121. I could say hello world and instead of saying hello world I want to say hello James
  122. Jones. I could use that breakout element I was talking about two seconds ago, add
  123. $array in place at position zero, and then
  124. if I was to put that, rather than saying hello world, it'll now say hello James
  125. Jones. So that's where that concatenation comes into place that I was talking about
  126. just now. It allows you to put information from variables directly into your echo
  127. statements.
  128. But if I wanted to print out the whole content of my array, I'd have to use one of
  129. two methods. The method I prefer is called printR.
  130. And if I put in the dollar sign there and I put in array and I save and put that,
  131. what it's going to do is simply print out the contents of my array.
  132. So as you can see there, my array contains my details and I'm printing my array.
  133. I said there are two methods to do this.
  134. Another would be simply called var_dump.
  135. and if I put in my array there it would do a very similar function
  136. but when it dumps it it gives you all of these additional properties with it.
  137. It can be useful if you want to know the length of something but realistically if
  138. you're trying to output something and check your array var dump gives you much more
  139. information than you actually need.
  140. Handy to go through with your students to talk them through what an integer would be
  141. and and how they're all recorded differently, and the fact that PHP can handle
  142. arrays of different
  143. variable types, but not really going to help much for our project.
  144. Now at the moment, I've been referencing my array using the numbered
  145. position of its array property, so zero and one.
  146. I can provide my array, so I can say, with a key value pair.
  147. So I could say, and I tend to do these ones in single quote, just so that it's
  148. separated and it's easy to tell the difference between the key and its value.
  149. I've got name there, and I can do age here,
  150. and do the same sort of thing.
  151. Now if I put that and I refresh this, now we've
  152. got array contains name and there's my name and age and there's my age.
  153. It makes it a lot easier when I want to echo and understand what I'm saying.
  154. So I could say hello as space concatenate
  155. into it my dollar assigned array and rather than say in a position zero I
  156. can now say a position name and that will allow me to print
  157. saying hello James Jones without
  158. having to worry about knowing and counting the positions within my array our key
  159. value peers then make it a little bit easier to understand what's going on in our
  160. arrays.
  161. Now as well as array mapping one of the important things that we'll cover across the
  162. range of this course are loops within PHP.
  163. So I start off with a basic for loop and I'm going to say for
  164. $c for counter equals 0 while
  165. $c is less than 10 and I'm going to say
  166. $c plus plus
  167. and inside here now inside a bit of curly braces with dual technical term which
  168. i can't remember right now it is the key which is next to the letter p on your
  169. keyboard i can get it to do something 10 times as i wanted to do so i could
  170. echo out and then just say hello and i wanted to go
  171. to a new line so I can say break.
  172. I didn't need that stop there and let's just terminate that.
  173. When I put that up to my database to my server sorry if I refresh this
  174. it prints out hello 10 times.
  175. As useful as that is printing the same word over and over isn't very handy but I
  176. can concatenate onto it just like I did earlier dollar signs a C and I can say hello
  177. number
  178. and then I've got my dollar sign C and let's just
  179. simply concatenate a line break at the end of it so a BR save
  180. that put that and refresh it and hello number zero one to ten
  181. obviously because as my loop starts at zero and is less than 10
  182. it will loop 10 times but counting numbers one zero through to nine
  183. i could do plus one there and that would actually have no
  184. impact i may need to wrap that in brackets a second let's see that bracket
  185. there save it put it and refresh it it will still
  186. loop 10 times but now my numbering is correct our loops will become handy our
  187. loops will become important when we start going through adding in tables putting
  188. contents of books and bits and pieces like that our loops will be used to populate
  189. those but as it stands our simple for loop is in place there
  190. now what i could do is i could contain
  191. lots of values in an array so i for example array
  192. here is equal to and then i can have in place my different
  193. values like i had earlier name for the key and then i could say
  194. james jones or in fact actually let's drop this down to first name
  195. is james i could do comma and
  196. key of last name is equal to jones
  197. comma and key of age is equal to 34
  198. and terminate that.
  199. So I've got my array in place here.
  200. I've got the arrows here, which is why Dreamweaver was giving me an error warning.
  201. I've got that in place. I can use a special type of array, sorry not array,
  202. a special type of loop that has been set up to iterate through the values in my key
  203. value pair. That is called a for each loop.
  204. And with a for each I have to give it what I want to loop through, array, and then I
  205. can do as a dollar sign key value.
  206. Now we will be using a lot of these for each loops as we go through to loop through
  207. the contents of our arrays that we create for our books, our users, all the
  208. different elements within our database system.
  209. And as I loop through here now then, what it's going to do is create a loop that
  210. goes through this, this, and this.
  211. So I'll have three outputs and I can echo from there and simply put in say a p
  212. tag and let's echo again and end that p tag
  213. actually I don't need the equals so we'll tidy that up ever so slightly in a second
  214. let's get rid of that one tidy that up perfect there we go and inside here then what
  215. I'm going to do is I'm going to echo out what my key is.
  216. Key is a equals and I'm going to say dot dollar sign key
  217. because it's referencing this key right here.
  218. I'm going to say space value equals and then
  219. do the same process dot value and
  220. I want to go down to the next line after I've done that so I'm
  221. and terminate that off. There we go.
  222. So in this section here, and it is a little bit big.
  223. Let's see if I can move that over.
  224. Perfectly fits in there. If I save that then, and typical, I go back, put that
  225. up to my server and refresh it.
  226. It now says key equals first name, value equals James.
  227. Key equals last name, value equals Jones.
  228. Key equals age, value equals 34.
  229. looping through every key value pair in this array and telling me its
  230. contents.
  231. So that will become useful when we bring in back values as our books, and we can
  232. loop through all the different elements, and we can target them.
  233. I could even say, if, and put an if statement in place, dollar sign key
  234. equals, I've got two equals to compare.
  235. I'm gonna say in there I want it as age so
  236. I don't want to print out my age I'm just gonna say continue and what that
  237. will do is that will gatekeeper it so if the value is equal to age it
  238. will not print that out and that should have been indented today so now when
  239. I put that what I'll actually get is a printout that just tells me my
  240. two details because as soon as it's hit this condition here for age it's continuing
  241. within my for each loop and no longer doing these elements i could change that
  242. obviously say first name just to show that it doesn't just take off that
  243. last value and if i put that and refresh it
  244. it gets rid of the first one and i keep the other two so gatekeeper did i've
  245. if to remove an element rather than looking for an element and printing an element
  246. so that it doesn't keep going through.
  247. All of that is basic elements of PHP which will possibly come up across the rest
  248. of this course. The most important part of this, the part that we really need to
  249. ensure we can do, is setting up the connection to our server.
  250. And the way we're gonna do that is we're going to set up a variable called server
  251. and we're going to tell it where our server is which is simply our local post
  252. from there then i'm going to set up a variable called user
  253. which is the user who we're accessing our server as at our mysql server
  254. which in this case is root all in lowercase and i'm going to set up the password for
  255. it as well which is also root and what I mean by that is if I was in
  256. HeidiSQL so let's just jump into HeidiSQL a second and I try to connect
  257. to my Parkwood Vale I am using localhost root password root I want to click on open
  258. connection we open up the connection we can see this here now there is no table in
  259. place here because I have put files in place as you are aware this is a re-recording
  260. so I have inside here my temp file with my UwAmp there with my actual correct
  261. database so the moment my database doesn't have anything there this is just going to
  262. show you how we can connect to it so it's not essential that exists
  263. I will not being said though go back into
  264. HeidiSQL and just simply create in this here a
  265. table create a new database sorry and call it Parkwood Vale
  266. and click OK so I've got that
  267. database there and within my Parkwood Vale database there are no tables I'm going to
  268. create a new table for a second and it's got users and let me
  269. maximize that a second add a value and it's gonna add in email as my first
  270. value just so that there's something within here to work with so varchar
  271. set it at 50 that's perfectly fine they don't need any other properties it's just so
  272. that I can connect to this successfully no I don't want to save that query so
  273. I've got my server in place I've got my user in place I've got my password in place
  274. I'm now gonna go through and set up a connection variable which is gonna to be
  275. a new type or new connection to a MySQLi and
  276. MySQLi as a command takes different properties.
  277. The first property is the server that you want to connect to.
  278. The second property is the user you are connecting as and then the final property
  279. is the password or the fourth third property sorry is the password to connect as
  280. that user you could of course not
  281. store these as variables but have these set up so if I was to duplicate that a
  282. second and just have that in there as localhost
  283. and then I could have in the root and in
  284. here root as well and that would work in just the same way
  285. it's just good practice to keep these extracted as separate entities so that when
  286. you are coming through if you need to update any of these details they're simple to
  287. find.
  288. Now I'm going to put that in place there a second I'm just going to use a hashtag
  289. that will comment this out just to show you that this works just as well and I've
  290. got my connection there. I'm going to say if as an if statement to check dollar sign
  291. connection and I want to check if my connection has any connect
  292. errors before I go forward. So I'm saying if my connection has any
  293. connect errors, then I want my whole process, let's check my screen brackets in
  294. place, to die. Seems extreme, but that's what it is.
  295. And say connection failed and I want to tell them why the
  296. connection has failed. I'm going to say dollar sign connection.
  297. Let's just get simply what my connection error is.
  298. So what this is checking is to see if a connection error exists.
  299. And if one does, stop processing anything further.
  300. Tell the user there's a connection error and tell them what that connection error
  301. is. So they can tell the admin or if it was yourself, you could get that fixed.
  302. Now, if I go into here and put that file very quickly, when I refresh it, It's
  303. come back and said it cannot find MySQL for some reason.
  304. Let's have a look. What have I done wrong then?
  305. Equals new MySQL.
  306. Let's just jump that one there a second and see if it was an issue.
  307. It shouldn't be an issue from there.
  308. I can't see why it shouldn't find MySQL.
  309. But let's just give it a retry.
  310. and line six i can't find mysqli
  311. i can tell you why i've said several times mysqli but
  312. i've actually spelled it my slqi so let's just comment that back in place
  313. take that comment away and fix it here as it was the one I wanted to show you to
  314. start with SQL I and let's drop that in as well so that's fixed there
  315. and put that back up now when I refresh that my errors disappeared
  316. there is no error related because it can connect no problem if I was to put in route
  317. 1 and save that it actually make no issue because I'm using this one down here let's
  318. jump back to the route 1 save that and put that up when I refresh this then
  319. it will tell me access denied for user local host using it the password
  320. provided it's telling me here that the connection failed for that user using
  321. password so because I have that die command in place and I'm printing connection
  322. failed you can see that message here connection failed I've got let's put a space in
  323. place and then I'm going to put a hyphen in place and just simply put that just to
  324. tidy it up a little bit. You can see now connection failed, hyphen, access denied
  325. from my user.
  326. If I was to remove that, save that, and refresh that,
  327. it goes some way if you put it up.
  328. It goes back to not having an error because it has fixed that connection issue.
  329. all of that is needed to put that in place like i said we could inline it all so
  330. it's all one big command i don't like doing it that method i like having it set up
  331. so it's using our commands up here and save that and we have that in place if i
  332. put that i get the same lack of an error in place there.
  333. Now in that section there then we have been able to connect to our database and
  334. store that connection but we'd need to do that on a very regular basis.
  335. Any page within our site that requires PHP would need to have this setup here
  336. and rather than having it at the top of every single page where there's a
  337. possibility when we change any of these details that a page might be left out and
  338. cause errors what we're gonna do is store this in a file that we can access like a
  339. header file at the top of our page first thing I want to do is create a folder for
  340. it so there's a new folder and it is gonna be my config folder so we capital
  341. C and put in config now I've already written all of this code up so I'm not gonna be
  342. replacing that code but what I'm gonna do is simply go to file and save as and
  343. inside my config folder I'm gonna call this instead of config connection
  344. dot php and i'm going to click on to save
  345. yes i want to update any links that's perfectly fine there were no links to update
  346. anyway but you can see that has created that and if i was to refresh my page
  347. which i can find refresh local files you'll now see i have config and my
  348. connection here my connection file contains the connection file code that i put in
  349. place but so does my index page.
  350. I don't want to keep this on top of every single page so I'm going to just simply
  351. remove it.
  352. Now with those in place then if I was to reconnect here and refresh this nothing
  353. would really change because I haven't done anything different but if I went in and
  354. changed the password and re-put up my connection file if you remember two seconds
  355. ago we had that error i won't get that anymore that's because these
  356. this element here isn't being used yet
  357. what i want to do is tell my server that i require this to progress any further
  358. so i'm going to put in quite simply require and open up a bracket now
  359. i'm going to tell The path to what I will require is my config folder.
  360. And then I'm going to say forward slash connection dot PHP.
  361. And actually, if I started with four stop forward slash, that means the root of my
  362. project and then this folder.
  363. If I terminate that, save that, it's now created a link between those two pages.
  364. I can put this up here and if I was to refresh this now
  365. I get that error and the error is telling me that the error is on my
  366. UwAmp config connection file even though I'm on my local host I'm on my
  367. index file so in my connection file here then what I can do is put
  368. that back in place save that re-put my connection file and then if I
  369. refresh this I get no errors. I can go back into my index here after
  370. I've required it I can put in some details so me equals and then simply
  371. my key value pairing so name and I'm gonna say James Jones
  372. and I'm gonna say age and I'm gonna say
  373. 34 and just like I did before I can print R
  374. my me array,
  375. dollar sign me. And what it will do is it will require this and process
  376. this connection file first, then come back to my index file and continue with these
  377. elements here. And just to show that it is processing this in full first, I'm going
  378. to say here else echo.
  379. and I'm going to echo as a H1
  380. connected.
  381. So if I save that and I put that file up and I save my index,
  382. I put my index file up and I refresh my page here, I get connected
  383. where it's processing my required connection file first and then the print R from my
  384. array.
  385. I don't need a giant connected every time I connect to my database so I'm going to
  386. remove that out
  387. and put that up.
  388. Refresh it, that disappears and I've still got my printing in place there for my me
  389. array.
  390. Hopefully this will be a lot clearer for you.
  391. Hopefully the quality of this will be vastly improved and like I said both videos,
  392. the original and this recast will both be available on Moodle tag them as
  393. original and recast so you can see the difference between the two.
  394. I have had the original on the side here where I've jumped back and forth just
  395. to make sure I've covered all the elements that I said I would and hopefully then
  396. there's nothing missing.
  397. If there are any further issues or any further issues with any of the videos
  398. whatsoever please don't hesitate to drop me a message, drop me an email and
  399. I'll redo this process for any video to make sure it's clear for you.
  400. Thank you again for watching, have a good day.