I have slugs formatted like my-cool-slug
however I need to auto increment some to make a similar slug unique such as my-cool-slug-2
-3, -4 etc. I am following this post https://stackoverflow.com/a/15972027/635027 to do this but I ran into an issue.
The slug stops at a -1 count when inserting into MySQL and keeps entering that over an over instead of increment to the next integer when finding a duplicate. Below is my query and associated lines. What am I doing wrong?
$slug = 'my-cool-slug';
$slug_query = "SELECT * FROM pv_events WHERE event_slug LIKE '".$slug."%'";
$slug_result = mysqli_query($conn,$slug_query);
$slugs = mysqli_fetch_array($slug_result,MYSQLI_ASSOC);
if(mysqli_num_rows($slug_result) !== 0 && in_array($slug, $slugs)){
$max = 0;
//keep incrementing $max until a space is found
while(in_array( ($slug . '-' . ++$max ), $slugs) );
//update $slug with the appendage
$slug .= '-' . $max;
}