I have a MySQL database containing a person’s forename, surname, their country, and whether they are happy for their name to be listed on a website.
The field names are ‘firstname’, ‘lastname’, ‘country’ and ‘listedagree’. In the database, the ‘listedagree’ result is either Y or N.
I’d like to list only the people that have agreed to have their names listed, grouped by country, and ordered by their surname
For each country, the outputted markup should look like this:
<h4>Country name</h4>
<ul>
<li>Amy Adams</li>
<li>Kenneth Branagh</li>
<li>Ray Charles</li>
</ul>
My PHP skills are very limited – I’m really struggling to find the code I need to get this working. Can anybody help please?
Many thanks in advance!
Then with your PHP loop through the records. Every time the country name changes close the preceeding ul and add a h4 and opening ul before echoing the users name. eg:
This code assumes that your country name data is clean and you don’t have things like UK and United Kingdom or USA and United States of America in the database.