php - Member filter in TwigGet the solution ↓↓↓
Depending on your application, there are a couple of things you can do.
My preference would be to show the delete links for all categories and do the validation on your actual delete page. This has a couple of added benefits:
- Makes you think about this logic on the delete page. Many people get caught up in hiding the link that they forget to actually check that logic when the user goes to delete a category. That means if they manually change a delete link to include the ID of a category with items, your application will gladly delete it.
- Reduces the complexity of your entire application. Not that a simple MySQL query makes your application more complex, but it definitely helps the view layer act a little cleaner without the conditional logic to show the link
- Reduces server load. You don't have to check for children in every category before printing the delete link (although, depending on your setup this may be a cheap operation)
- It lets users know what's going on. Otherwise, without knowing your application inside and out, they'll wonder why there's no delete link. Having a delete link that shows them an error if they click on it will tell them that they need to remove all children if they want to delete a category.
With that being said, you can just do this:
SELECT c.category_name, c.category_id, COUNT(f.food_id) AS children FROM categories c LEFT JOIN food f ON (c.category_id = f.food_id) GROUP BY c.category_id
Which is obviously taking a guess at your database structure, but that'll get you a list of categories with a count of children for each
Share solution ↓
Link To Answer People are also looking for solutions of the problem: trying to access array offset on value of type null
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.