Sort by surname if a text field contains a full name

2 posts by 2 authors in: Forums > CMS Builder
Last Post: November 17, 2017   (RSS)

By leo - November 17, 2017

Hi,

It is definitely possible. There are two ways to do that:

1. Sort them after you get the list of records through PHP. You can use function like usort(): http://php.net/manual/en/function.usort.php. And get record's surname by preg_match(). This won't work perfectly if you have pagination setup and don't have the full list of records.

2. Sort them before you get the list of records through MySQL. You might need something like SUBSTRING_INDEX(): https://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_substring-index

Let me know if you have any questions!

Leo - PHP Programmer (in training)
interactivetools.com