We can now we late articles to categories in the database using the article category join table article
Ricardos can be related to 0 1.
Are more categories.
Let's display these categories in the individual article page.
In the cold for this page we're calling the get by idees method on the article class passing in the
article eidy from the query string.
In this method in the article class were selecting the columns from the article table based on this
I.D. and returning an article object.
As we saw earlier to get the related categories at the same time we need to execute an ESC U L statement.
Joining the article and category tables via the article category table.
For example this article has no categories so we get one rijkaard.
If we change the eidy in the WHERE clause to an article that does have categories.
Then we get to records.
As this article is related to two categories.
So this Eskew l could result in 0 1.
Are more records.
So there's two problems with using a single query to get both the article and category data first.
The category data aren't part of the article class and pdl were wolters Signed them as attributes of
the article object when we use the fetch class mode.
Also we might get more than one rijkaard back from that esky well which isn't suitable if we want to
return a single article object.
P The O doesn't have the option to return related objects when returning an object of a class.
So instead of using this method let's add a method to return an article along with its category data.
At the same time.
Let's add a new method called Get With categories passing in the database connection and the article
I.D. as parameters.
Let's add the s keywell that we just used that joins the article and category tables together along
with a place holder for the idae.
Then it's the usual code far prepared statement.
Prepare it bind the I.D. value and execute it as this could return multiple Ricardos will return an
associative array of them.
Let's add a p p duck comment.
And then in the article script we can replace the call to get by idae with a call to this new method.
Before we change anything else let's dump out the contents of this variable to see what's returned.
If we view an article that has more than one category then we get more than one array element as expected.
However the title content and other attributes of the array are repeated far the individual article.
So to display these values instead of accessing them as object properties we need to access the array
elements.
We can get these from the first element of the array which has an index of zero followed by the index
of the property.
We want to get in this case the title.
The same for the image file column and the content.
Now when we view the article we can see the title content and so on as before.
As for the categories we've got one array element are one rijkaard for each category.
Also note that we're getting every column from each table in the query including the join table.
Let's change the ASCII rail so we get all the columns from the article table and just the name of the
category.
Let's also rename the category name column in the resultset using the as keyword.
Now were only getting the article columns and the category name column has been renamed as category
name.
Now we can show the categories.
If there are any the category name element of the first array item won't be NULL so we can check for
that.
With this if statement if there are some let's loop around the resultset And for now just print out
the value of the category name.
And before we look at this let's remove the vardhan line we added earlier.
Now if we view an article that has categories there shown below the title.
If an article doesn't have any categories then nothing is shown.