Working with DateTime – PHP Standard and Practices Part 4

Working with DateTime – PHP Standard and Practices Part 4


PHP has a core class named date/time. This will help you in reading writing Comparing dates or making dates. I’m rated calculations. There are many date and time rated functions in PHP besides the date zone class But this provides a nice object orientated interface to most of the common uses So let’s start off by opening indexed or PHP on line 3 We’re going to create a new date time object and store it in the date in This case we’ll be creating our object by passing a string into the class constructor this string will contain a date Which can be in one of a bunch of different formats? We’ll look at some of those formats shortly But for now this example has an input format of year comma August 23 on line 7 We are outputting a paragraph with the text the output day is Then using the format method on our date/time object to output the date in our own specific format our example here use m d Capital y is an output format meaning when we run this script We should expect to see 0 8 23 2014. Let’s take a look in preview mode by clicking on the eye icon Cool that works here we have the output date is 0 8 23 2014 just like we expected the benefit Here is that we can change the input date string and have the same output format one alternative Commonly used input format is the SQL date string let’s go back to the workspace Back to index dot PHP and let’s enter a new date in a new format gonna keep the air 2014, and they changed things around a little bit and Enter zero nine twelve which is the 12th of September? I? Save that out and refresh great we have the exact same date format, but it has a different date if We hop back to the workspace we can get a little bit more adventurous with initializing date/time objects using string offsets We can change the constructor to take a string starting with a plus or a minus Then we add a number of units and define the unit with the words second hours days weeks months or even years so if we enter plus two weeks And have a little look-see what happens there Then we will have output which is two weeks from now brilliant You can jump forwards using more human-friendly phrases – we can change this string to contain phrases like next week or tomorrow So so I go over that next week Perfect Tomorrow There we go As I mentioned earlier the string argument passed to the date-time constructor on line three can accept dates in many different input formats To learn more about the date/time formats we can use the PHP manual There is a link in the notes for this video called PHP date formats, so let’s head over there You scroll about halfway down you can see under localized notations. There’s a whole bunch of different formats supported some American ones some more generic a Format here. This is a literal string so that means it’s actually a slash and not some special meaning And then you can see some actual examples of how they look so when you use them they’ll come out without the quotation marks around them and There’s some other fairly common formats here This one like the four-digit year month and day with slashes could be one that you might want to use Sometimes you will need to work with dates that are stored in unsupported formats for example if a string contains to date segments with numeric values below 13 they could both be Days, or months luckily the date/time class has a method called create from format We can provide our custom input format to this method along with our date So that PHP can read the string correctly if we go back to our workspace Paste this code in on line 3 here. We have a string containing an unsupported date format we store it in the raw variable and pass it to the Create from format method if all goes well when we run this once again We should see our date output in the same format this time the date should be

PHP Ajax CRUD Application Tutorial – MySQL & Bootstrap & jQuery DataTables  [Part 1]

PHP Ajax CRUD Application Tutorial – MySQL & Bootstrap & jQuery DataTables [Part 1]


Recently I have got a lot of comments on
how to make nice user interface we are able to add edit delete and view the
data from the MySQL table so I have made a decision to create three part video
series where I will show exactly that and since this is the first part in this
part I am going to show you how to design web side in bootstrap how to
add data to the MySQL table and also how to get all the data from that table
so stay with me hey what’s up guys Senaid here from codingpassiveicome.com the place
where I help others to become a web developer much easier and faster then they
will do it on data so that is something that interests you consider subscribing so
now I’m here at my PHPstorm and created a new project and also I’ve created a
new index.html file so first thing that we need to do is to include
bootstrap libraries and also jQuery library that you need for the small
project so I will go to getbootstrap.com click on the donwload bootstrap and he
I’m just going to use those links to include the libraries ok so the CSS
library I will paste just before closing head tag and the Javascript bootstrap
library I’ll paste does before closing body tag
like this and also we will need jQuery library and I will get the one from the
code.jquery.com so I’ll click here the minified and copy this
okay and this need to be pasted does before bootstrap library so I will quickly
do this okay and here I’m going to just write the basic design for our project
so we will first make a container it’s required by bootstrap and inside this
container will have one a row and in this row I will say to have one column-m-d8 and I will set an offset to the 2 and this should make as a result to
position everything in the center of the screen and that’s it so inside here we
will have one button so I will say input type button I will say class will be
btn btn-success value will be add new and for now that it later will make the
functionality let’s see what we have okay so here we go so let’s say for our
container I will set style-margin-top 30 pixels let’s say you have one page two
with the name MySQL table manager and let’s say this button I will position to the
right of the screen and let’s see so here we go after that we are going to
have one table where we will display old data
so I will say table class table table I’ll make hover and table-bordered so
those are all the classes from the bootstrap and industries I’m not going to
talk too much about those I will have another tutorial that goes deeply about
bootstrap design and in this table I will set table head select the head you’ll
have one row first column this will be ID the next column will be let’s say name
for me the better country names and in the next one it will be options and
inside this option column we will add edit view and delete for each row so
that’s it let’s make a test so let’s move this table I will be done okay
so this is pretty much looking good so now once you have the basic design for
this tutorial I’m going to create a new database inside my PHP my admin’ so see
here MySQL data manager I’ll set the utf-8 unicode c3 and I will set
table say country so first row it will be first column will be ID
integer autoincrement and that’s it the next one
will be country name I’ll set it to the varchar 20 for example the next one will
be short description about this country I’ll set the text and the next one will
be long description about country all set to the long text
and let’s save this so that’s it ok so now our goal with this project is that
when someone click on the add new button or later we will have edit view and
delete we will display a small modal pop up and the the user will be able to
manipulate a date so first we need to design that model pop up so I will say
here this I will set ID to the table manager and I will set class to the model
fade and but should be fine the next part we are
going to write actually the body so this dust I said this so I can display later
from the date way so he I will say model yelped and he would need to set small
heater little set model body and let’s say will have small foot end of it
so let’s in this model put some title later we are going to manipulate but
for now let’s say it will be

and let’s say class will be model title and
he will say country name in the model footer who does have one button but if
you want to save all the information so I say my base class they can begin success and that should be fine
and inside the body we need to display some information so I will set you will
have one input type will be ‘text we will set called class the form
dialogue and let’s see placeholder will be country name and we also need some ID
for this subset country names ok break here I will since in the table we also
have short description and long description so I will put our two text areas so I
can see again for dialogue I’m in short description
placeholer short country description and but it right at the end output break
and how that duplicate is and that’s it to the long description alright so
let’s see what we have right now refresh and as you can see this model is hidden
it’s not visible on the page but for the button add new let’s put on click method
and you’ll see add new we’ll call that function and give of the script type
text/javascript and I love this work document.ready function and instead of making one click I will
just change this to the ID and and inside this jQuery function we will wait
for complete document rule and after that we will set and on click event on
this add new button so say here addnew.on click function and here I will
just say table 900 got more show and let’s not dead this functionality so
add new and as you can see now we have some design but obviously I have made
somewhere small mistake so let’s see what is wrong so here I have forgot to
put this here class model content and I am going to move this on the footer okay
like this so let’s make a test again and you and now it is looking really good we
have country name we will mind with this with the Drake query and here we have
some options and we have button C which doesn’t work at the moment so let’s so I
started to improve this anime small stake here I said dialogue but it seems
to be control so let’s make a test to fresh and new and not this is worth much
nice okay so now let’s make functionality for this Save button so when someone press this Save button
let’s say we are going to call the function to save and not here
or maybe it’s better to make this function ask manage data and let’s write
this function function to manage the impact and let’s say that this function
will receive one parameter so that we know what we are going to do so say like
key and by that inside a text file that we are going to create it will know what
is the Ajax code that we are making at the moment and heal that key will be Ed
new so we are going to make an a first let’s check to be having any information
inside those three so like this and here I will first say war name equal country
name and let’s see short description and let’s make long description and not
let’s test if name dot o people and two it means if there isn’t any name for
this country then we are going to select this and we are not going to make an a
tax code so the sake name.CSS and we’ll say bottle is one
pixel solid red and you will say return it means
you’ll stop executing this code so let’s try and you save and as you can see now
it is green okay and the same thing we are going to
do for the other two so we can say he felt if show description okay and here I
got the same thing for the long description okay and health if Alton is
okay we will does a name dot CSS border is H and we are going to do the same for
show description and put a long description okay for maybe we should
move this to the function this way as is a mnemonic technique test so save
country and there’s a mistake if you write something here click Save it
selects again but the problem is as you can see now this input is selected to
which means we should improve this so we can say maybe by this and same thing with books too
maybe even if we write the function for this it will be even better
so maybe the function will be very simple you will see trick beta and he
will see core and we will just do this okay this is the best way to make heaven and
here we’ll just see my be cool and let’s see what I want working here so we will
test it maybe if is not empty so it is not empty
name and is not empty for description and is not until long description you
are going to do this let the are see so let’s make a test and
you say ok so now it works just fine say it is here and as you can see all red
borders has minimal so this is just the best way to optimize this code and make
it a lot nicer and easier to read so we have one function that is taking for the
empty inputs if it’s empty we will set the border to the red and it’s not we will
remove the border and return true ok so if it’s not empty we are going to
make an ajax call to the server so our URL movie let’s say we are going to
create a new page with PHP file so set method to the post data type will be
texts and data will be dust key is key it will be the name it is name.val short description and long description and of course we need to have some
success function response from sir so once we received the response from
the server this part of the code here will execute so let’s not that our
response that to see what will happen and not let’s create an ajax file and write
the PHP code so I will say new PHP file ajax and here I will set safe improve see
if isset post key which means if there is this key we are going to check keep
since we are going to have multiple keys ignored orders making the one for adding
new so if post key equal admin will able to this part of the code and first we
need to make connection to our database connection=new mysqli
and he will say localhost my username is root
my password at moment is empty I don’t have one but if you do have just make
him and our dead business is just make sure it’s correct is MySQL data
manager okay so now let’s first escape all the thing that we are getting
from the jQuery so name=connection and I lose real escape string force name
the same thing borders show description this and the same thing for the long
description this is all to protect ourselves and now let’s first check do
we have any data with the same name in the database so say here as sql=connection query select let’s say ID from country with country name in=
name and let’s make sure we have the call name correct yes country name so
here if sql->num_rows>0 turn we will say here the return to the
jQuery country with this name already exists else you are going to insert so will say
as well way insert country layers so he will see country name short description and long description and for the values will say country name show
description and of course a long description exit country has been inserted so now
let’s make a test we have written a lot of the code so I hope so we done didn’t
make any mistake so refresh engine and he will say for example Austria should conscription long description safe and
let’s see what doesn’t say great so problem is on line 15 correction gray sea like RV from
countries where country nickel man came in here so I have seen it as well but it
actually needs to be the connection let’s try again Austria show this on description save
and there is from again so I’m using clear country name but it needs to be
the name try again and finally country has been insect ok
so now if we go to out leave it now you can see that we have multiple but the
first time we had a problem that this variable didn’t exist so you can believe
it they try again and that’s it everything is looking good ok so now
let’s make the final functionality for this part of this product which is to
list all the data inside this table so what they are going to do we can write
the take where the PHP code directly here going you can write the code
directly here but since we are already working with jQuery and not other things
I will show how to do our occur the way to get all the data from the table so
what I will do here is I will declare new function so the function get a
linking data and this function will receive three parameters first on
we’ll be start and the next one will be Flint I would have used to without nutri
okay and here I’ll check with it’s okay let’s
meet two parameters and I will say here are going to make ajax code again
so may oh that’s right okay
hi this so this part here is the same so say like this and here this part same 2
okay so the data that we will send to the sever the kid will be get existing
data so you have start that and alert when okay so and here all the response
from severed will death attend to the table body I first wrote if response==reachedMax but no IDs you to table body append all
the data on the response after that we will just increase our start for the
length side and we are going to call the get existing data function again so why
we have tables in the road they will just keep doing this and for those of
you that are having problem with this on to understand what I am doing here I
have just another tutorial where I will show how to get dynamically all the data
from the table so make sure toward that video to better understand this concept
so and allocate all the same path and now inside this ajax let’s write this
functionality so here I remove this part now here and here I will say if key=
get existing data to only this so safety start oh okay and now we are going to tell do
we have any rules so the SQL=connection query select ID country name
from country limit start limit you’ll see again get asked well number
of rows because then here we will get a bit else you’ll see exit reachedmax which
will give to the tape very information that there isn’t any more data inside
this MySQL table and for that reason we are not going to make another ajax and if there is we will just get all the data cells use while complete
data=sql fetch_array and here I will just concatenate full data
together so say here we are going to have table row okay and inside this row
I’m going to have one call some slicky by this so the first one is let’s check
our design so first one is I mean second one is country name so first I will
append ID of that country to name and after that will append the buttons for
which we are going to lay to make the functionality it is edit view
and delete so let’s try those sub button and reply this with victory the code for
you guys value and class button button primary
so the next one is to delete I will set button to danger and you’ll help the
ones you let’s say little bitter but so let’s see will this work
probably not because we don’t live in and call this function so let’s do it
here to get in this data and let’s say we’re starting from zero and we’ll get
say we are getting ten countries that 8 ajax code you can make it bigger material
like to the plants on the table data so now lets me there’s small mistake in
ajax because here I didn’t return the response okay refresh and as you can see now we have
ID in tree we have country name its Austria and also we have some options
here ok so at the moment those but that thought but in the next part we are
going to make this functionality so guys I hope so that this tutorial is
extremely simple and beautiful for you and I will suggest to you to watch the
part 2 and 3 we will finish this small project and also if you have any
questions feel free to ask in the comments below and if you liked this
video please like it and share your friends take care

PHP Ajax CRUD Application Tutorial – MySQL & Bootstrap & jQuery DataTables [Part 1]


Recently I have got a lot of comments on
how to make nice user interface we are able to add edit delete and view the
data from the MySQL table so I have made a decision to create three part video
series where I will show exactly that and since this is the first part in this
part I am going to show you how to design web side in bootstrap how to
add data to the MySQL table and also how to get all the data from that table
so stay with me hey what’s up guys Senaid here from codingpassiveicome.com the place
where I help others to become a web developer much easier and faster then they
will do it on data so that is something that interests you consider subscribing so
now I’m here at my PHPstorm and created a new project and also I’ve created a
new index.html file so first thing that we need to do is to include
bootstrap libraries and also jQuery library that you need for the small
project so I will go to getbootstrap.com click on the donwload bootstrap and he
I’m just going to use those links to include the libraries ok so the CSS
library I will paste just before closing head tag and the Javascript bootstrap
library I’ll paste does before closing body tag
like this and also we will need jQuery library and I will get the one from the
code.jquery.com so I’ll click here the minified and copy this
okay and this need to be pasted does before bootstrap library so I will quickly
do this okay and here I’m going to just write the basic design for our project
so we will first make a container it’s required by bootstrap and inside this
container will have one a row and in this row I will say to have one column-m-d8 and I will set an offset to the 2 and this should make as a result to
position everything in the center of the screen and that’s it so inside here we
will have one button so I will say input type button I will say class will be
btn btn-success value will be add new and for now that it later will make the
functionality let’s see what we have okay so here we go so let’s say for our
container I will set style-margin-top 30 pixels let’s say you have one page two
with the name MySQL table manager and let’s say this button I will position to the
right of the screen and let’s see so here we go after that we are going to
have one table where we will display old data
so I will say table class table table I’ll make hover and table-bordered so
those are all the classes from the bootstrap and industries I’m not going to
talk too much about those I will have another tutorial that goes deeply about
bootstrap design and in this table I will set table head select the head you’ll
have one row first column this will be ID the next column will be let’s say name
for me the better country names and in the next one it will be options and
inside this option column we will add edit view and delete for each row so
that’s it let’s make a test so let’s move this table I will be done okay
so this is pretty much looking good so now once you have the basic design for
this tutorial I’m going to create a new database inside my PHP my admin’ so see
here MySQL data manager I’ll set the utf-8 unicode c3 and I will set
table say country so first row it will be first column will be ID
integer autoincrement and that’s it the next one
will be country name I’ll set it to the varchar 20 for example the next one will
be short description about this country I’ll set the text and the next one will
be long description about country all set to the long text
and let’s save this so that’s it ok so now our goal with this project is that
when someone click on the add new button or later we will have edit view and
delete we will display a small modal pop up and the the user will be able to
manipulate a date so first we need to design that model pop up so I will say
here this I will set ID to the table manager and I will set class to the model
fade and but should be fine the next part we are
going to write actually the body so this dust I said this so I can display later
from the date way so he I will say model yelped and he would need to set small
heater little set model body and let’s say will have small foot end of it
so let’s in this model put some title later we are going to manipulate but
for now let’s say it will be

and let’s say class will be model title and
he will say country name in the model footer who does have one button but if
you want to save all the information so I say my base class they can begin success and that should be fine
and inside the body we need to display some information so I will set you will
have one input type will be ‘text we will set called class the form
dialogue and let’s see placeholder will be country name and we also need some ID
for this subset country names ok break here I will since in the table we also
have short description and long description so I will put our two text areas so I
can see again for dialogue I’m in short description
placeholer short country description and but it right at the end output break
and how that duplicate is and that’s it to the long description alright so
let’s see what we have right now refresh and as you can see this model is hidden
it’s not visible on the page but for the button add new let’s put on click method
and you’ll see add new we’ll call that function and give of the script type
text/javascript and I love this work document.ready function and instead of making one click I will
just change this to the ID and and inside this jQuery function we will wait
for complete document rule and after that we will set and on click event on
this add new button so say here addnew.on click function and here I will
just say table 900 got more show and let’s not dead this functionality so
add new and as you can see now we have some design but obviously I have made
somewhere small mistake so let’s see what is wrong so here I have forgot to
put this here class model content and I am going to move this on the footer okay
like this so let’s make a test again and you and now it is looking really good we
have country name we will mind with this with the Drake query and here we have
some options and we have button C which doesn’t work at the moment so let’s so I
started to improve this anime small stake here I said dialogue but it seems
to be control so let’s make a test to fresh and new and not this is worth much
nice okay so now let’s make functionality for this Save button so when someone press this Save button
let’s say we are going to call the function to save and not here
or maybe it’s better to make this function ask manage data and let’s write
this function function to manage the impact and let’s say that this function
will receive one parameter so that we know what we are going to do so say like
key and by that inside a text file that we are going to create it will know what
is the Ajax code that we are making at the moment and heal that key will be Ed
new so we are going to make an a first let’s check to be having any information
inside those three so like this and here I will first say war name equal country
name and let’s see short description and let’s make long description and not
let’s test if name dot o people and two it means if there isn’t any name for
this country then we are going to select this and we are not going to make an a
tax code so the sake name.CSS and we’ll say bottle is one
pixel solid red and you will say return it means
you’ll stop executing this code so let’s try and you save and as you can see now
it is green okay and the same thing we are going to
do for the other two so we can say he felt if show description okay and here I
got the same thing for the long description okay and health if Alton is
okay we will does a name dot CSS border is H and we are going to do the same for
show description and put a long description okay for maybe we should
move this to the function this way as is a mnemonic technique test so save
country and there’s a mistake if you write something here click Save it
selects again but the problem is as you can see now this input is selected to
which means we should improve this so we can say maybe by this and same thing with books too
maybe even if we write the function for this it will be even better
so maybe the function will be very simple you will see trick beta and he
will see core and we will just do this okay this is the best way to make heaven and
here we’ll just see my be cool and let’s see what I want working here so we will
test it maybe if is not empty so it is not empty
name and is not empty for description and is not until long description you
are going to do this let the are see so let’s make a test and
you say ok so now it works just fine say it is here and as you can see all red
borders has minimal so this is just the best way to optimize this code and make
it a lot nicer and easier to read so we have one function that is taking for the
empty inputs if it’s empty we will set the border to the red and it’s not we will
remove the border and return true ok so if it’s not empty we are going to
make an ajax call to the server so our URL movie let’s say we are going to
create a new page with PHP file so set method to the post data type will be
texts and data will be dust key is key it will be the name it is name.val short description and long description and of course we need to have some
success function response from sir so once we received the response from
the server this part of the code here will execute so let’s not that our
response that to see what will happen and not let’s create an ajax file and write
the PHP code so I will say new PHP file ajax and here I will set safe improve see
if isset post key which means if there is this key we are going to check keep
since we are going to have multiple keys ignored orders making the one for adding
new so if post key equal admin will able to this part of the code and first we
need to make connection to our database connection=new mysqli
and he will say localhost my username is root
my password at moment is empty I don’t have one but if you do have just make
him and our dead business is just make sure it’s correct is MySQL data
manager okay so now let’s first escape all the thing that we are getting
from the jQuery so name=connection and I lose real escape string force name
the same thing borders show description this and the same thing for the long
description this is all to protect ourselves and now let’s first check do
we have any data with the same name in the database so say here as sql=connection query select let’s say ID from country with country name in=
name and let’s make sure we have the call name correct yes country name so
here if sql->num_rows>0 turn we will say here the return to the
jQuery country with this name already exists else you are going to insert so will say
as well way insert country layers so he will see country name short description and long description and for the values will say country name show
description and of course a long description exit country has been inserted so now
let’s make a test we have written a lot of the code so I hope so we done didn’t
make any mistake so refresh engine and he will say for example Austria should conscription long description safe and
let’s see what doesn’t say great so problem is on line 15 correction gray sea like RV from
countries where country nickel man came in here so I have seen it as well but it
actually needs to be the connection let’s try again Austria show this on description save
and there is from again so I’m using clear country name but it needs to be
the name try again and finally country has been insect ok
so now if we go to out leave it now you can see that we have multiple but the
first time we had a problem that this variable didn’t exist so you can believe
it they try again and that’s it everything is looking good ok so now
let’s make the final functionality for this part of this product which is to
list all the data inside this table so what they are going to do we can write
the take where the PHP code directly here going you can write the code
directly here but since we are already working with jQuery and not other things
I will show how to do our occur the way to get all the data from the table so
what I will do here is I will declare new function so the function get a
linking data and this function will receive three parameters first on
we’ll be start and the next one will be Flint I would have used to without nutri
okay and here I’ll check with it’s okay let’s
meet two parameters and I will say here are going to make ajax code again
so may oh that’s right okay
hi this so this part here is the same so say like this and here this part same 2
okay so the data that we will send to the sever the kid will be get existing
data so you have start that and alert when okay so and here all the response
from severed will death attend to the table body I first wrote if response==reachedMax but no IDs you to table body append all
the data on the response after that we will just increase our start for the
length side and we are going to call the get existing data function again so why
we have tables in the road they will just keep doing this and for those of
you that are having problem with this on to understand what I am doing here I
have just another tutorial where I will show how to get dynamically all the data
from the table so make sure toward that video to better understand this concept
so and allocate all the same path and now inside this ajax let’s write this
functionality so here I remove this part now here and here I will say if key=
get existing data to only this so safety start oh okay and now we are going to tell do
we have any rules so the SQL=connection query select ID country name
from country limit start limit you’ll see again get asked well number
of rows because then here we will get a bit else you’ll see exit reachedmax which
will give to the tape very information that there isn’t any more data inside
this MySQL table and for that reason we are not going to make another ajax and if there is we will just get all the data cells use while complete
data=sql fetch_array and here I will just concatenate full data
together so say here we are going to have table row okay and inside this row
I’m going to have one call some slicky by this so the first one is let’s check
our design so first one is I mean second one is country name so first I will
append ID of that country to name and after that will append the buttons for
which we are going to lay to make the functionality it is edit view
and delete so let’s try those sub button and reply this with victory the code for
you guys value and class button button primary
so the next one is to delete I will set button to danger and you’ll help the
ones you let’s say little bitter but so let’s see will this work
probably not because we don’t live in and call this function so let’s do it
here to get in this data and let’s say we’re starting from zero and we’ll get
say we are getting ten countries that 8 ajax code you can make it bigger material
like to the plants on the table data so now lets me there’s small mistake in
ajax because here I didn’t return the response okay refresh and as you can see now we have
ID in tree we have country name its Austria and also we have some options
here ok so at the moment those but that thought but in the next part we are
going to make this functionality so guys I hope so that this tutorial is
extremely simple and beautiful for you and I will suggest to you to watch the
part 2 and 3 we will finish this small project and also if you have any
questions feel free to ask in the comments below and if you liked this
video please like it and share your friends take care

Converting errors to exceptions – PHP Standard and Best Practice Part 19


Previously we discuss the differences between errors and exceptions PHP can do something pretty cool and convert its errors into exceptions The benefit here is the sometimes an error is unavoidable For example if you try to read a file that cannot be read it could throw an error If you check to see if that file exists before you try to read it then you can avoid that error But in the milliseconds between checking to see if a file exists and actually trying to read that file. It could have been deleted This is called a race condition, and then have fun at all Sometimes you need to try and do something and recover if it goes wrong Converting errors to exceptions lets you do that Let’s take a look and see how we do that with a bit of code Here we have a tiny snippet of code on line 4 here as we’ve seen in many other videos We’re turning our display errors on you might always have to do this yourself, but on workspaces we do on Line 7 we’re trying to open a file called nope txt which in this example Doesn’t exist the code tries to deal with a file Maybe not existing on line 10 by checking to see if the handle variable is set to false But at that point PHP would already have thrown an error. Let’s try this code out by clicking on the eye icon for a preview So here we have a warning from the F open core function saying it failed to open the stream no such file or directory Now this is annoying. It’s displayed a error message even though We are trying to display our own error message, and that’s what this is here Normally it look a little bit prettier. This is just an example so How do we convert errors to exceptions here in a way that can help us? Well PHP has a function called set error handler which accepts an anonymous function as a callback Every time a PHP error of any severity is about to be thrown instead of doing what PHP normally does it will run this callback Let’s have a look at how this callback works with a really simple error handler that throws an exception If we go back to our workspaces here and just make a little space and Throw the set error handler in place So here we have an anonymous function and it accepts these four arguments We have error. No which is a number relating to the type of arrow that’s being thrown of arrow string here which contains a human-readable message which will explain what the error is we have error file here and then at the end we have error line all of these arguments are passed through and As we should be familiar with we are throwing a new error exception This error exception is a core PHP exception type and it’s designed for exactly this sort of behavior Then we simply pass on all the arguments in this specific order and we should be good to go let’s save this and see what happens when we run it I Don’t know That’s quite hard to read as for the page source So now we have a fatal error. That’s a bit worse than a warning, so let’s see what’s going on There’s an uncaught exception of error message with the message And this is the same message we had before right this was the warning we had But it’s now been converted to a error exception, and we’re not catching it Let’s go back to our code and see we can work out a way to catch it Now when working with exceptions you need to put anything contentious in a try block and Then the result needs to go in a catch block Let’s just get this comment on the way make life easier So we want to catch an error exception We want to assign it to an Iver evil whether we use this or not we have to assign it to a variable So now we are trying to read the file this may throw an exception, and if it does we’re getting ready to catch it Right here, let’s try this out and see if I got it, right Perfect now we have no warnings no fatal errors Nothing weird on the screen when F open comes across a problem it will throw an exception And we know something went wrong and we can handle the error properly We can do all of this without having to stoop to hiding errors in development Which is a huge pain and can really cause problems when you’re trying to debug something? It might not always make sense to confer errors to exceptions in a situation where you are building a component Which could be installed on anyone’s application you might not be able to rely on them being converted But in your own applications, you can absolutely do this as you have total control Exceptions can help you catch tricky stuff in development and Help you work around some of those errors where PHP would rather keep on truckin than stop processing Exceptions make PHP complain more noticeably and more conveniently and that’s usually a really helpful thing

How To Create Rating System Using PHP & MySQL & jQuery

How To Create Rating System Using PHP & MySQL & jQuery


In this video you will learn how to
create rating system for your website. We are going to create the rating system
where every person will be able to vote only once and then we are going to store
all the details in our database and later when they revisit the website we
are going to immediately detect that they have already rated and they won’t
be able to do it of course anymore so let’s do it so now I’m here at my PHPStorm and
first thing that we are going to do is you just create a basic page layout so I
will just say okay I want to create HTML website and then let’s say this year
house a rating system and now we are not going to spend out of the time creating
like the content from the page so we are just going to create the place where
they are able to actually read so I will say we will have one there I will just
align everything to the center and maybe let’s just apply some basic okay so this
is black and then let’s see padding will be 50 pixels that’s it okay so now let’s
grab font awesome okay so I’ll go to font awesome I’ll go to the home page
would to start using free and I’m not going to change any of the options so
just grab this edit our head okay and that’s it and now what you are able to
do is to just say okay I class first are and you should get one star icon on our
website okay so probably it’s black now so maybe we can change the color to the
white okay let’s see yeah you can see now it’s we have one icon and for those
of you that are not familiar with the font awesome basically it’s a completely
free library of course there is a paid version but free library where you are
able to actually get a lot of the icons for your website so basically all you
need to do is just check free here and then you can just search there’s like
thousands of difference icons that you can use for your website polling
social media and whatnot okay so there are so so many of those and you will see
them on pretty much any website I’m using to my personal own so it’s good to
know about those okay so let me now create five more so I will just say okay
this will be star and then I want it to be two times bigger than the default one
okay like this something very simple and then let’s add four more okay fine so
we’ll have now five different stars that someone is able to do so now what we are
going to create is I want first to create an effect so when someone goes
with the mouse over I want to make sure that the color of the stars is changing
and in that way we are able to actually feel like real okay I’m going to give
two stars three stars whatever so in order to do that I went first to
download the Drake query okay so I’ll go to code to query calm I will use
minified version I will just copy okay then I will just paste it here at the
bottom so just a second I like to be in one line okay and then here we are going
to write our custom script okay so first I want to wait for complete document
load so I’ll sit on document ready okay and now first thing that I want to do is
I want that always by default we set all of those icons to the white so they are
already like that but in case that you don’t want to use an inline here you can
just put the code here so whatever you like we can even leave the in line as
the fault and then I want for each of these
classes for each star I want okay to set hover effect so I will say mouse or
you’ll pull the function and then let’s for now just bring to the console log so
I’ll say here whatever just so you can see it works and then maybe we can add
mouse leap okay here too so let’s see it work so open so okay or okay it works
for each of those it is good okay so now the next thing that we want to do is
first each time when the mouse is over I want to reset so C star and then or even
what’s what’s even better is that we can delete this
let me show why okay and then we can have one function that you will say okay
reset start colors so that later in case that you want to quickly change the
colors it’s always in one function so we’ll just say here first our CSS color
and then we’ll say Y okay so on the page load we are going to do it so save reset
okay and then each time in each of those two okay and then if the most is for
let’s now find on which of those the mouse is over so maybe what we can do is
to add some data index to each of those salsa date the index
okay so this is one star two three four five okay and then we are going to see
current yeah current index and you will say this dot are triggered
theta index or even see it’s always the same you can say like data index okay
and then what we want to do so first we have reset it to the default now we know
what is the current and now I want to start so we are going to say for war
equal one less then no less and equal current index i plus plus and then all
we need to do is we just one for the icon okay
you want to change the color so let’s say you want to have a green and that’s
it and now on the mostly we will just pass so let’s see what will happen
okay so over okay so it was backwards I’m not sure why is that on the first so this is quite weird
let’s see yep it’s okay because the problem is that we we are starting the
index from the one and this one is at zero position so maybe it’s even better
if we just change those can’t on time that case this won’t happen okay so
let’s see now 2 3 4 5 ok hope this makes sense ok so now we are able to create
that effect you cannot of course put the yellow or whatever the color you want
and let’s not say that when someone click on any of those we want to
remember that rating and we want to save it so much so let’s do it so let’s first
define here like rated rated index let’s say minus 1 which means that person
didn’t rate at all because we have from 0 to 4 and if we said it’s minus 1 that
means that he didn’t would so then on the mouse leave here we will say F rated
index different than minus 1 then in that case they will just do this same
thing but with rated index okay and now we need to just enable click on those so
that in case when someone click we are going just to remember that okay so say
on colic function and then he will say that our
rate that index is bars integer this not data index okay let’s see now so I will
refresh so let’s say here rate that index is minus one so now if I go on
this position go out nothing but if I go and click go out it stays so let’s check
out rate index it’s two which means zero one two it’s the third one okay and now
of course if I go my with mouse over again it means that someone is able to
change his ratio but the problem is he won’t be able to like completely remove
that word so it needs to be one of the options there is no option to trust not
what at all if he is already there okay so now when someone colleague
we have remembered this and what we can do now the best option for this is of
course to have some log a registration system so on only registered people will
be able to rate and in that case we are like hundred percent sure that they
cannot vote multiple times but in the scenario like this where people are not
logged in and we need to remember those we have a couple of options one is try
to find the IP address and store it to the database and verify its time did
they what or not then the next one is told to the cookies or the local storage
and that’s it each of those have its pros and cons each of those can be
bypassed like there isn’t like hundred percent protection so in our case
whatever we use will be good enough for your understanding
it all works so let’s say we are going to use the local storage so I’ll say
does local storage dot set item and then I will just say he rated index and then
I’ll just put okay so let’s see now so when i refresh and go to the
application local storage okay so was this nothing special but we don’t have
here rate index but when I choose and click you can see now that my written
index is 1 and if I treat my mind we have changed this okay so that means
that even if some are refresh the page we know that they already read it and we
will know what is the ratio so what I can do now is that when we are reset we
can see here that if local stores dot get item rate index so let’s see what it
returns in case that there isn’t so see dot get item whatever so it
returns the null so say if is different than null in that case okay
so maybe this part we can move to them yeah I’m just thinking maybe we should
have a function let’s say set stars and then we’ll say here MAX because you can
see that each time it’s the same okay so this here will be called max so here I
can say okay set stars rated index is max then in this case I can say set stars
current index is max okay and then here I can say set stars and then I’ll just
say local storage get target item rate index oh okay so let’s see now refresh boom
you can see now that he knows that I read three stars
so if read two stars and if i refresh it will stay you can see it’s always at two
stars and of course if someone for whatever reason manipulate it is and
then refresh it will work that way but in the database it won’t change
okay so it will be saved in the database only when we make a click so we are
going to implement that too and of course if we delete this and someone visit for the first time you can see it’s not remember okay cool I hope this makes
sense and now the last thing that I want to do is let’s save it to the database
so that we have like datebase in place where all ratings are stored and then we
can calculate the average rate and stuff like that okay so first thing first
let’s create the database so PHP my admin’ new let’s say here writing system ok
Unicode C okay then put these stars whatever okay then maybe just two columns
okay so first one will be ID out auto increment and the next one we use
let’s see new stars maybe writing that’s what they’re to be let’s say tiny
integer okay and we’ll save it that’s it and now here when someone is clicked we
are going to say so it’s out receive here save to the database
okay and now you are going to write this function okay function
okay and then here will just say ajax okay and I’ll see you IRL is our index
dot PHP and then method you’ll just see what else do we need we need data type
know what am i doing datatype let’s say will be JSON and the
data will be the same and rated index index okay and maybe just let me first
find success function response okay and maybe here we should have like user ID
okay so this will be ID from our database in case that someone change
their rate so by default I will say here user ID is equal zero okay which means
that we need to add a new person and in case that person exists we are going to
change this so we’ll do it a little bit later for now we’ll just say that it
will be there okay and let’s your response you can say that user ID=go
like this okay let’s see now now in the PHP B side we
are going to accept so say if is post save so we will have like have like rated the index
equal post rated index and then we are going to have
also user ID and of course we need to have connection say to be local host
will be root password sent to and our database is if I’m not wrong rating
system and rating system starts ok and then here we need of course to do some
real escape ok and then what you are going to do is we need to increase this
rated and index by one because you are starting from the zero so if we have ten
one star rated then in that case it won’t count so that doesn’t make sense
okay and now let’s let’s check if user ID equals zero in that case we are just
going to insert the person so we can say insert into top stars and we have just
rated index values rated index okay and now we have different methods on how we
are going to find the last ID but for the simplicity I will just say like okay
SQL is connection query I’ll say slack ID from stars order by ID –
one means you are getting just the last one so say user data is as well fetch
let’s say associative array and then user ID is use of data I li okay and
else if the person existed we are just going to say okay connection query of
the Stars set rated index equal rate index where ID equal user ID okay
and I think that I have made mistake here
okay we are missing this other than that it looks good okay
and then we are each time going to return an array with ID that this user
ID who so many cool right anyway user ID rated index saved us to verify okay
let’s test this so fresh always go to the console looks good Network let’s try
to what boom okay we have made new request okay once a undefined function
real escape string how is that possible okay let’s see connection oh it’s here my mistake okay let’s try
again okay boom I’m a zero super-interested you’re not
sure why doesn’t warm-up let’s try like this and now it’s great now this is
super super weird anyway we can see now that this is registered and there’s rate
index 3 which is good okay we have sent to ok it’s training Davis that’s okay
now the last thing that we need to do is we need to remember this user ID
so I’ll just say ok local storage dot set item let’s say user ID is
ID and that’s it and then we’re so load for the first time then you are going to say okay local
okay and now let’s try so course not ideal be 2
– yeah of course this is not a problem let’s see now now it shouldn’t be
anymore okay so let’s now just create database
and then we can test out it now okay lets now refresh cause there is I hope
you remembered okay you’re just going to delete those two fresh okay
so now we will try to vote okay now it’s in database it looks good now let’s see
what has happened the local storage it is at user IDs undefined why let’s
return it return it is for okay and let’s see here your ID undefined again
WHY so we are turning the Jason and it’s because of this okay let’s see yep
my mistake let’s try again okay let’s see now what
is the latest okay refresh that’s right okay now user ID is five and rated and
index is 2 okay yeah now it’s okay so now when somehow refresh we will
immediately next time know who that person is
what is his vote and if he decided to change his mind
okay he changed it and now when we go to the database we can see now that it’s
updated but in case that someone new comes in let’s say like this and we
vote now you can see that user ID has been changed and different index and we
have in different number so now in order for us display this rating like average
rating all we will need to do is like just to sum this entire rate index and
displayed and the page divided by the number of those so that’s very simple
so last week so let’s say I don’t know with this here okay and then here I will
say sorry I’ll say okay connection query let’s select Sun rate index
as top down from stars okay and then let’s say rated data is equil as well
fetch array and then I’ll say okay total is the data total and then now we also
will need to get like how many rows we have in the database so let’s see what
I will just duplicate this one so I will just say select ID from stars and then I’ll
say num rated I will say a sql number of rows okay and then we
can see average is what total true number of rated okay and then here we can
say okay you will have two breaks and then let’s all see here color is white
and here I will just say a call average so if we did everything correctly
it should work okay now you can see now this is our average of course now we can
go and change it like use different functions if you want round number two
to like closest value so we can say maybe here round two the two decimal points
precision okay let’s now see and that’s it okay so of course now if someone
changes okay and we refresh you can see it’s changed
and of course so each time it will be different okay so guys I hope that you
have enjoyed this video we had small troubles here and there but that’s
coding that’s how it works and that’s why I’m not cutting the videos because
I want to show what is possible and what happens and how you fix the problems so
if you have enjoyed and if you like this video please like it and share with your
friends and of course if you have any questions or anything please let me know
in the comments below take care

Reporte PDF con PHP, MySQL & FPDF  |  PDF Report with PHP, MySQL & FPDF.

Reporte PDF con PHP, MySQL & FPDF | PDF Report with PHP, MySQL & FPDF.


The files will be available in the video description. Proceed to register test user list of users is displayed . Also last record . HTML file with code. information the form is submitted. Forms the data are captured, Create PDF , registration and User List file. The user class shown with search and add methods . It shows how to with connect a database . We last user insersion Remember That All files are available in the video description .

PHP CURL Tutorial – Web Scraping & Login To Website Made Easy

PHP CURL Tutorial – Web Scraping & Login To Website Made Easy


In this video you will learn how to use
PHP CURL, we are going to do three different examples in the first one we
will scrape the results from Google in the second one from my personal website codingpassiveincome.com and then in the third example I’m going to show how to
log into some website so stay with me 🙂 Hey what’s up guys Senaid here codingpassiveincome.com the place where I help others to become a web developer
much easier and faster than they will do it on their own so if that’s something
that interests you, consider subscribing –>So for those of you that don’t
know what curl is basically we use it mainly from PHP when we want to
communicate with different web sites so if you want to get an information from
some website we usually use curl because of its speed so in the first example
that I mentioned in the introduction I said that we are going to scrape the
results from the Google so first thing as you can see if we go to the Google
and then try to search for something let’s say test whatever you can see that
they always use Google dot and then the name from the country that you are
coming it mostly then search and then here they have a pendant of a lot of
different things but the one that we are interested in is this query basic this
query and then what you are searching for so basically we can delete all the
other things like this and then if we make a test let’s say another test you
can see now that it is working and that’s what we are going to do so that’s the
URL that we want to scrape so I will open PHP tags and first that we need to
do is to actually in it call so I’ll create variable curl handler like
shortcut and then I’ll say curl_init and that’s it and now for this curl we
always need to add some options depending on what you are going to do so
you will see through this examples what are the different options
that you have so let’s say the first one that you are going to do so as you can
see first parameter is the curl handler so just right here your
variable name and then the option that you want to set so let’s say this time
we will just set URL and then here we need to specify the URL okay so now our
URL is this one okay and then here are the curl
we just need to specify the keyword that we are searching for so let’s say
Senaid Bacinovic and then I usually use plus just like
URL encoding so the next that we need to set for this example is and then
follow location which means that if Google redirects us from this URL to
some other that they want mainly they do it if you are choosing the wrong domain
so we yes we want to follow location and then we also one that we get the return
from in Google so it says return transfer at one so here I set one you
can set true or 0 or false so it’s saying so true or one and false
for zero whatever you want okay and I think that’s it let’s make a test
so now to make a test we just need to declare a variable where
we will store whatever Google returns so you’ll say call and then execute and
then we want to choose this curl Handler and of course you are going just to
close this handler close the connection okay and then
let’s just the echo response so let’s see what will happen
okay I’ll hit refresh and it works here we go
so as you can see now we have get all the results for this keyword and now if
we do the same thing here and search for me here you guys can see Twitter linkedin
and in my website etc etc so this is how to get but now the thing is let’s see
how we are going to parse for example all texts from this so that you get only
them because we are scraping the google for some reason and now there are
different ways on how we can scrape some website
I mean how we can analyze the return on response HTML document and go through it
and for that reason I want to show you how to use simple dom parser okay it’s
one very lightweight but very powerful library so we will just dowonload it
okay latest version as you can see is just few kilobytes it’s very small but
very very powerful and with this library you basically are able to search through
this document just like you will do in jQuery which is really powerful powerful
okay so just extract here delete the archive and this file there’s any
includes so I don’t think that we need any of those we doesn’t need this part
but we will see and that’s it so at the top I will
include simple HTML dm PHP okay and then here of course we need to utilize
so let’s say i will create HTML variable out there is no variable and then I will just utilize this simple
HTML Dom and then I’m just going to load the response from Google and then let’s
see will we get any error so I hit refresh and it’s empty
okay I’m not sure why so let’s try to say he sorry it’s okay
we didn’t print out anything so that’s why it’s empty that means that there
isn’t any error and then now let’s try to search for the titles so here let’s
see our source code this is from the Google and then let’s try to search for
the one and if we go again to the Google you can see that first one is for the
Twitter so let’s find it so say twitter.com okay and here we go so you
can see here that we have one link and then in that link we also have the title
that we are looking for like here so we can see that every link is starting with
href and then /url question mark and then some URL so we can say something
like here for each and then let’s say HTML fine and then we will do a selector
like we will use in jQuery so say ref is
starting with then /url question mark and then let’s say as link we want to
get to print out link and then I will just say inner or maybe plain text so
you have different options here to extract and you can find it if you go
here and then maybe go to the online document about simple dawn and then here
of course you have explanation on different things and how you can use it
okay so just read the documentation if you want to learn more okay and let’s
make a test for our case scenario and here we go so maybe let’s just add a new
line okay and here just in six link it linkedin twitter programmer and blogger senaid and
as you can see here we have something weird so I’m not sure what this module this is on Bosnian language and
let’s try to find here so it’s here and let’s see we can say that if we have
found URL with this web page that Google user contact.com we don’t want to
include it so maybe we can say here if string position and then let’s say in
our link then let’s see if we find
where catch dot Google etc equally false that means if we don’t find your just
print out this text so let’s see and it’s gone so now we have got only the
results about me in this search which is what you were looking for and now if you
know how to use this simple HTML Dom parser you will know how to extract
anything from that page okay so that is the first example so let’s go to another
one in the next one we are going to try to get the informations from my personal
website so this is my website and here are all the tutorials that I have
there’s a lot of them and let’s try to get and see what will happen so I will just paste the link here so let’s we will use a cell and we will just echo the response and
here will will say exit just test this part here okay and now if you see there isn’t any
tutorial this is just something about my website so the problem is that if you go
to the f12 hit refresh you can see that I’m dynamically loading all the
tutorials so if we may maybe go down and click on the load more you can see here
that I’m going to make and Ajax call and then with a ajax
call I’m going to load more tutorials you know and if we do it again same
thing will happen ok so now it means that we will need to execute the post
request to this URL in order to get more tutorials and for that reason we will
need to modify this script a little bit so I’ll say ajax X dot PHP and now
here you can see that in the phone data I’m sending four different things so we
will need to send them too so let’s say here I will create one variable post
files and then create an array okay so this is associative array and it means
that we need to do it like this can be boring sometimes but okay and now
as you can see I have some start variable and some limit okay so let’s
say I am starting from zero and want to get 10 tutorials okay and here in our
curl now we need to say to the curl that we are going to make a post request
so for that reason we will say here curl Handler and then call off and then let’s
say post files will be our pots files but I will just use and build HTTP query
so say postfiles okay so I will duplicate quickly remove this and then say we are
making post and setting it to the one okay and let’s make a test now so
refresh and as you can see now we are getting only the posts from my website
and if we go to control you there’s a lot of HTML so maybe let’s say a echo
free so the problem is that this is not
nicely sorted because you can see here we have Jason so I will just say here
that you’ll say JSON decode and then here I’ll say response race which is
this here okay so let’s now refresh and here we go so now here you can see
that we have each time different columns and in each column we are getting
different tutorials okay and now we can see here that for every tutorial I do
have one h2 two box title and in this title they will have a link and that
link we are going to have a title of this tutorial so again we are going to
use our simple HTML Dom okay so here I’ll say response recent and then here
we will search for h2 and then we can just copy the class from here and then we will remove this part and
say a echo link plaintext okay and let’s you here we go so as you
can see now even from my own website I have got all the tutorials but now the
difference is that we needed to add post and post fields to this call or it
wouldn’t work and now the last thing that I want to do for this tutorial is
to show you how to log in to some website so let’s say we will again use my
website as an example so in my website I do have a special members dashboard
those are the people that have bought some course from me and now this is the
login page to get an access to some of those courses as you see now here this
drop down you are able to select one of the courses okay and let’s say we are
going to use this one and then we need to provide email and the password okay
so I have set some temporary password enter and if we log end it will show the
message and login so now it’s obvious that this is done over jQuery AJAX
because we didn’t refresh page and we have logged in
so for that reason we will need to know what are the information that you are
standing and we are sending those so of course if you know that it is a AJAX code
then just go to the source code and see what’s happening there and as you can
see here the code is pretty simple and in Ajax we are sending the
for informations which is check login as a key email password and the course
ID and course ID we are getting from this select and we just get an option so
let’s do it I will just get this or maybe we can just quickly write password and
then course ID so email is mine okay that’s what we enter and then course ID
is for this one is number one okay and that’s it so I will save and then we need
to set our URL okay so URL we were send information is login.php which is this
URL so how I will just copy okay and that’s it so let’s again see what will happen and
here I will just say X says sto could not be connected to
string that’s okay let’s remove this at moment and here it goes as you can see we have
got JSON response status is 1 and the first lesson is number one which means
that we have successfully logged in but the thing is that we want to see the
members dashboard and not only this message so that means that we need to
store somewhere the cookie informations and for that reason there is another
option for the call okay call handler call and then we need cookie and then we
need to set the path to that file so let’s say I’ll just say in my case
cookie dot txt okay and now here our I will just say decode this and then I am going
to check so see if response status that means that we are logged in and we are
going to make another call handler another call to get this let’s see where
will it redirect us as we going okay so if we are logged in we are going to make
a test and try to get this page so here I will just remove this because I don’t
want to close this curl that’s very important and then here I will just paste
everything and then post and post fields we don’t need next time we just need
this URL okay okay
and this ID is basically from response first lesson okay and of course you don’t
need JSON decode because now we are turning the entire HTML okay and let’s
not try to get everything from this responds to seem to be logged in
successfully so here I’ll say refresh and here we go as you can see now we have
from the left side in menu that you should have like this and we have here
some content for the first lesson whatever it might be and if we refresh
here is the source code that we are getting okay which is amazing we did
correctly this part and now the next thing that I want to do is let’s
actually try to get the menu items from this dashboard so let’s see how we can
get this those and as you can see here the each time have this link that
which href is dating with view lesson dot PHP so we can say here right and
then we’ll just say starts with view lesson dot PHP IDE okay and let’s make
up test object what is the problem here we
should flow the reason to just need this response and as you can see now we have
all the links from our page so guys pretty much that’s it I hope that you
have enjoyed this video and if you do please like it and share with your
friends and of course if you have any question doubts or anything please ask
in the comments below take care 🙂