Home › Forums › Back End › How to write this condition? › Re: How to write this condition?
> (mediaquery= mysql [probably you do css a lot.])
God… Way too much CSS for me.
Regarding your 2nd script, I’m pretty sure if(preg_match('/[a-zA-Z0-9-]+~[0-9]/', $slug)){ }
will never match.
The slug (given by the user or self made on line 3) won’t be my-slug~X
. It will be my-slug
, thus won’t match your regexp.
That’s a problem solved by the method I gave you: it looks for every item in the DB starting by my-slug
and returns the number of result.
If it’s 0, it means the slug hasn’t been used yet and thus can be inserted as is.
If it’s 1, it means the slug is already used and has to be suffixed by -1
.
If it’s X (!= 0), it means the slug is already used and has to be suffixed by -X
.
**Example**: empty database.
1. You want to insert my-slug
; no problem
2. You want to insert my-slug
again; there is already one in DB, let’s call it my-slug-1
3. You want to insert my-slug
again; there are already 2 slugs in DB starting by my-slug
(my-slug
and my-slug-1
) so let’s call it my-slug-2
4. You want to insert my-slug
**again**; there are already 3 slugs in DB starting by my-slug
(my-slug
, my-slug-1
and my-slug-2
) so let’s call it my-slug-3
5. …