1

I am trying to create a unique slug (while adding new data) and update slug (while updating existing data).

I want to have a mySQL query like:-

"SELECT COUNT(*) FROM cms WHERE (slug = '$slug' OR slug LIKE '$slug-{{numeric-value}}%'"

The idea is, slug = '$slug' will check whether an exact match exists and slug LIKE '$slug-{{numeric-value}}%' will check whether slugs like about-us-2, about-us-3 exists. However, about-us-more will treated as be a different slug.

How can I create the accurate mySQL query?

Saswat
  • 12,320
  • 16
  • 77
  • 156
  • you probably aren't using codeigniter, but still, this may be interesting to you: https://github.com/ericbarnes/CodeIgniter-Slug-Library/blob/master/Slug.php – Alex Jan 10 '18 at 08:07
  • MySQL supports regular expressions (at least to find rows) which you could use to look for numeric values, see e.g. [here](https://stackoverflow.com/questions/16922248/get-only-digits-using-regexp) (and many similar ones), although you may also add a table that contains a list of slugs and its current number. – Solarflare Jan 10 '18 at 11:29

0 Answers0