Okay, so after successfully inserting the data into this messages table, it's time that we fetch this
data to display it on the guest book page.
So that's a page we don't have right now.
We need to create it.
And we also need to add to the db php file a function that will be able to fetch this from the database.
So let's begin with this.
So we start with a select keyword.
Then we need to either list the columns or use asterisk which means just select all the columns.
Then we go to the from keyword saying from which table we want the data.
This is called messages.
And then we can add some conditions.
We don't have any conditions because we want to select everything though we would like to order by so
we can skip everything else.
And now we get to order by.
So we type order plus by.
And here the column we would like to order the records by.
In our case this is Createdat.
And we want it in a descending order so that we get the most recent messages first.
And this can be achieved by typing test.
So the order comes right after the column name by which we want to order.
Okay.
So that's the SQL query.
Now we create a statement by calling PDO query with the SQL.
We don't have any arguments in here.
You can also add the prepared arguments for select queries.
But we don't have any here because we just want everything.
And now we can just run this statement by calling.
Fetch all.
That's a method that will get everything.
And we just pass one argument, which is how you would like to receive those results using mode.
And we select fetch as SoC, which is a shortcut for fetch into an associative array, which means that
we're going to get an array of associative arrays where every key is the column name and the value the
value of the column.
Okay, so let's think how we should call the page where we're going to display this messages.
Let's go back to the page so it can be guestbook or messages.
Not sure.
What do you think.
Maybe we can just go with guestbook.
The first idea and this is a Get method because that's just a normal page Okay.
And here we need some special handling of this.
It's not just a template, so we need to well we need to connect to the database.
So we use connect DB.
But we can just fetch the entries by using get messages and just pass the connect db function to it.
And then we're going to use our render view function.
Our template name is the same as the route name.
That's our convention.
And remember that we have this second parameter called data can also pass it just this way.
It's an array.
By default it is an empty array.
But we have it so that we can pass some additional data like Messages equals
entries.
Now some people also like to use the compact function.
Now the compact function.
Um, maybe let's also use the name named.
Okay.
So it's called template not name.
Okay.
That's a template.
So if this variable would be called not entries but instead messages.
And I would like to do something like this.
I'm passing the key messages with the variable messages.
Instead I can use a shortcut and call a compact function.
So what this would produce is essentially what you've seen before.
This would be an array where key would be the name of the variable and the value the value of that variable.
That's optional.
It's up to you.
Personally, I don't like this function.
I don't like using it, but it's just an interesting thing to know.
Some people prefer that.
For me.
I prefer the absolute clarity.
That's why I just want to be explicit.
Anyway, it's time that we also add a template.
So this is a guest book.
Get PHP.
So we just make it easier on ourselves by calling the template same as the root.
Now we won't have a huge logic section of this template right now, because later on I'd like to obfuscate
some data.
So just hide the actual real email by replacing it with asterisks.
But that's an optional thing for later.
For now, let me just create a helper variable.
Entries From data messages.
Or maybe I'm just going to call it messages.
So a quick reminder this data is an array.
It's a variable available in this template because the render view function is receiving this as an
argument and only then including the template.
That's why it is just available just like that.
Okay, now outside PHP tag, let's add the actual HTML.
So we are using the HTML section element for every page.
And we are adding a subheader using h2 element.
So I can call it guest messages.
And I think it would be nice if I could add a link to this page inside the header.
So it's called Guest book.
And Let me just call the link the same way.
Guest book.
We can now visit our new page, which doesn't really do anything at this point, but it seems that we
should have the messages.
Let me vardump the messages.
So this would be a confirmation that we have successfully read them from the database.
And indeed that's the case.
We have our one and only message.
Unless you have added more, I think that we should begin by checking if there are any messages at all.
So let's use this if statement and the empty function would return true if the array is empty.
So if that's true, we're going to display a paragraph saying no messages yet.
Be the first to leave a message.
And this can also be a link to the form.
But let's ignore this for now.
And then we just add an else.
So as a quick reminder, I'm using the PHP alternative syntax for control structures here.
Why don't we just add an endif at the end.
And now we would have to use for each to iterate over every message.
So this is messages as message.
And I need to close.
And for each at the very end.
Okay.
So nothing is displayed because we indeed have one message inside the database.
Okay.
So I might use an H3 element and just output something.
So I'm going to use the HTML special chars Us for the message name to escape any unsafe content, like
if anyone would try to use HTML.
So we've seen that before.
And someone did try to use HTML.
That's why we are using HTML special chars.
And then let's just use two paragraphs.
So let me copy this function.
And now there is a message email.
So next up I should display the message.
So I'm copy pasting this for it to be quicker.
The message.
And also I'm going to wrap the HTML special chars with another function.
This is called an L to BR.
It's just a handy PHP function that will replace all newline characters in the original input as.
Remember that the message is a multi-line text input text area, so it will just convert this to BR
elements, which would actually let us render those new lines.
So if this was a multi-line message, it would just be formatted correctly.
And then finally let's use a small element and say it was posted on.
And again we can use HTML special chars with the message created at.
Okay.
So it's almost fine except that I've used the wrong variables here.
So it's not messages, it is a message.
So after refreshing everything is fine.
Also let's get rid of this vardump.
Okay, so let's summarize maybe what we have learned in this video.
So we have learned how can we fetch data and how can we sort the data in ascending or descending order.
So the default is ascending.
Then we've created this endpoint.
And we have also passed the data for the first time.
And then finally we have actually reminded ourselves that we need to escape the data because users might
just supply malicious data, intentionally or not intentionally.
So it is in our best interest to make sure that any unsafe code, HTML or JavaScript is escaped, so
it won't be interpreted as HTML by the browser, but displayed as text instead.