Download Subtitles and Closed Captions (CC) from YouTube

Enter the URL of the YouTube video to download subtitles in many different formats and languages.


3 12 PHP and MySQL Process Answers with English subtitles   Complain

all right so what we're going to do in

this video is actually process the users

answer we want to check if the answer

that they submit is the right one and if

it is we want to add it to our score now

the score is going to be held in a PHP

session all right so a session is

basically you can look at it as a global

variable you can use it on any page

anywhere on the script all right so

we're just going to create one session

variable called score and we'll just add

to it as the user gets correct answers

all right so let's go ahead and open up

process dot PHP all right so the first

thing we want to do here is include the

database file

all right

so like I said we're going to have a

session variable so in order to do that

we need to start a session right so at

the top here are reading to say session

underscore start and this has to go on

every page where you're going to use the

session variable alright so basically

it's going to go at the top of every

page along with this database include

alright so now what we're going to do

here is we're going to first check for

the score check to see if score is set

all right and we're going to do that

with an if statement and basically we're

going to say if it's not set all right

so this is set as a PHP function to see

if is to see if some value is set all

right but we're going to check and see

if it's not set and that's what this

exclamation mark does it makes it the

opposite all right so we're going to see

if that session variable

okay now session is a super global just

like post and get so we can use it like

this all right

and the thieve the variable name will be

score

okay so if score is not set then what do

we want to do we want to set score so

session score is going to be equal to

zero because if it's knife if it's not

there if the score session variable is

not there then we just we want to create

it but we want to set it to zero and

then as the user goes on and gets

correct answers it'll be added to that

superglobal next what we need to do is

we need to get the answer okay so when

the user answers the first question or

any question for that matter it gets

submitted to this file and it gets

submitted in with the post superglobal

array okay so we need to check for that

we need to see if if post was submitted

all right so we'll say if

if post and we're going to check for the

submit button because obviously if if

the submit button has been clicked then

we want to process all right so actually

we're going to say if is set post submit

all right now we can just do a simple

test here let's say echo I've been

submitted alright so let's go ahead and

go to our question we'll reload this

um

if it's that post submit

actually you know what let's just say if

hosts

all right so this tells us the post has

been submitted if we just go to process

PHP without post then it does nothing

all right so we want to make sure that

it's being submitted so we want to get

two things really we want to get the

question number and we want to get the

selected choice all right the question

number is up here in the URL so what I

want to do is I want to pass it through

through post not through get not through

the URL so all we need to do is go to

the forum the question forum and create

a hidden input all right so right under

the submit input I'm just going to say

input type equals hidden and the name is

going to be number

okay another value is going to be I I'm

sorry

number right here okay because we're

putting the get variable into the number

variable so I want to pass that down

here

all right so let's make sure that these

two these go through number we're going

to set to post number and then say

selected choice

host bigots choice all right so these

keys right here are coming from the name

attribute on the question form so you

can see the the choice input has a name

of choice and then the number input has

a name of number so that's where we're

getting these values right here all

right now I just want to make sure that

these go through so we're going to echo

number and we'll just put in a line

break here so we can read it and echo

select a choice

all right

undefined index number

input type hidden' number

huh

undefined index number you know what

let's just look at the post array

you can do a print are and that's going

to print out all the values of the array

oh you know what I don't think I

reloaded this page before I submitted it

yeah because you see we're getting both

here so let me try that again

all right good so we're getting number

one question number one and we selected

the choice to you see if I select this

one it's four so we're grabbing the

whatever the user submits so that's good

so you can get rid of that

another value that I want to have is the

next number because once that we go

through and we check the answer we want

to redirect the user to the next

question so it'll be it will look like

this the next question so let's create

another variable and I'm just going to

call it next and we're just going to set

it to number plus one all right so now

we want to run another query we want to

get the correct answer for this question

okay so let's just put a little bit

information here all right so we're

going to get correct choice

so query so we want to say select all

from choices by the way these are not

quotes these are lope the little back

ticks that the key is located right

above the tab if you yeah you don't need

shift just the key above the tab

I know some people might get confused

and think that those are actually single

quotes but they're not all right so

select all from choices where question

number equals number and is correct

equals one all right so if we go and

look at the database

okay so we're looking in the choices

table we're looking for the question

with the current number which is one for

us so we're getting just from that query

alone we're getting these four answers

because they're they all have the

question number of one but we're also

saying where is correct is equal to one

and you can see that only one of those

is equal to one all right so that's the

correct answer that's what it should

grab for us all right so now we need to

run the query and put it into a result

variable all right so we're going to say

result equals

Cleary and we're going to say or die and

inside here we just want to shoot off

the error and the line

all right so that should do now we want

to get that row

result fetch Assoc

all right so now we're going to just set

the correct choice

correct choice is going to equal Rho and

we want the ID all right so now we have

the correct answer in this variable so

we can actually do a comparison so we'll

have a simple if statement so we're

going to say if correct choice is equal

to the selected choice then they have

the right answer so we're just going to

add to that session session variable

so session score we're going to

increment it by 1 you can say session

score plus 1 or plus plus all right

so actually I'm going to change this up

here two plus plus as well just to be

consistent

so we're not going to have an else here

because basically if it's correct then

we're just going to add to the score if

it's not then it's not going to add to

the score so very simple so the last

thing I want to do in this in this

process file is check and see if it's

the last question because if it's the

last question we don't want to go to the

next question we want to go to our final

page okay so we'll set up in an if-else

statement okay so we're going to say if

number is equal to total now we don't

have this variable total yet we're going

to have to have to get that we'll do

that after so if the number is equal to

the total so basically if there's five

questions and you're on number five you

know that that's the last page and if it

is then we want to redirect okay so

we're going to use the header function

and do just a simple redirect in PHP to

final dot PHP and then we just want to

make sure we exit all right and if it is

if it's not the last question we want to

redirect to the next question

so header

location okay so the location is going

to be our question page question PHP and

then the number is going to equal the

next the next value all right see how

that works all right so let's just write

a quick quick query up here and get the

total

now doing what I'm doing here I think

I've mentioned this before but doing

queries right in the page like this it's

not the best pad practice well this

actually isn't a display page but on a

page like question where we're putting

the queries up here I mean it's not the

best practice but I just wanted this to

be a really easy project just to get you

comfortable with making queries in

future projects we'll create a database

class and we'll just use that as kind of

an abstraction layer to communicate with

the database so this basically is just

just for simplicity really all right so

get total first we want to create our

query select all from questions

that's it really select all and then I'm

going to get results results

let's see query

all right so total is going to be equal

to results I can't type today result and

then numrows all right so let's you give

us the total

all right so let's give this a shot

now we don't have our final page done

yet so it's I just want to make sure

that it's going to pass us to the next

question I like it's supposed to all

right so we'll start the quiz question 1

of 5 do we have 5 lumen five questions

doing oh I didn't make that dynamic yet

did I

let's actually go to the index page real

quick

total wait a minute we did do that

number of questions Oh

oh yeah we want to put the number here

as well in the question dot PHP file

because look we just have question 1 5

so we want to get a couple things here

it looks like in the question variable

we have the we can get the ID I'm sorry

the question number so we'll say PHP

echo question question number

and then the total did we do that here

all right so let's just grab that total

code that we just wrote right here

and then down here we can just say

question number of total okay so let's

see if that alright so now we have

question 1 of 2 which is correct we only

have two questions all right so let's go

to answer that all right so it says

you're done look oh we did do the final

page I'm sorry but it didn't go to

question two for some reason

huh all right so let's say I'm do a

little bit of debugging here so in the

process file it looks like it's not this

isn't working correctly if number is

equal to total then redirect else so

let's do let's echo out total make sure

that that's correct

all right so that's correct is to total

now let's echo out number

number two

all right so I figured out the problem

and it's that this here this next equal

num number plus plus this is changing

the value number to 2 so I just want to

not do that and just do number plus 1

okay and that seems to work so if we

reload well I just need to get this out

of here

okay so we answer question one brings us

to question two we answer question two

it brings us to the final page okay

final score five we obviously need to

change that

that's just static just a static number

but everything else looks good all right

so let's go to the final

all right so instead of just having a

five here we want our actual score and

that's really simple to do all we need

to do is start our session and I'll put

the session variable so up here we're

going to say session start okay so you

want to do this on every page where you

want to use the session all right and

then down here we're just going to say

PHP echo

sessions score all right so let's retake

it we'll get the first one correct and

the second one will get wrong okay so

that's not right

oh actually we need to include that

session start on the question dot PHP

file as well so to copy that and we can

try this again

look okay so that's correct that's

correct

final score one

okay it should be too let's go back to

the process file

is that session

oh I have 3 s's here you guys probably

saw that a long time ago alright so that

should be two S's procession alright

let's try this again reload ok that's

correct that's correct alright so now

we're getting final score - stupid

mistake so that's basically it for the

front end here what I want to do when

the next video is just create a simple

form where we can add questions to our

quiz and that's really it my goal for

this was just to kind of get you

familiar with making queries selecting

data from the database displaying it

obviously this isn't the most secure app

we're not really preventing SQL

injections and things like that we're

going to get into that later on in later

chapters we're going to take more of a

object orientated approach so this is

basically just like a beginner app but

yeah in the next video we'll create a

form and we'll be able to add more

questions

Download Subtitles Download Video Download Audio

↑ Return to Top ↑




ContDict.com - contextual dictionary