The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End How to write this condition? Re: How to write this condition?

Kitty Giraudel

I’m really not that good with PHP but here is what I would do (pretty much what you did).

$title = mysql_real_escape_string($_POST);
$slug = mysql_real_escape_string($_POST);

// If slug is not defined
if(empty($slug)) {
// … create it
$slug = strtolower(str_replace(‘ ‘, ‘-‘, $title));

// Check if slug already exists in DB
$q = media_query(“SELECT * FROM pages WHERE slug LIKE ‘$slug%'”);

// If it already exists
if(mysql_num_rows($q) != 0) {
// … append it a number
$slug .= “-” . mysql_num_rows($q);

// If slug is defined
} else {
// … check proof it
$slug = strtolower(str_replace(‘ ‘, ‘-‘, $slug));

// Insert everything
$q = mysql_query(“INSERT INTO pages(pagename, content, pageId, slug) VALUES (‘$title’, ‘$content’, ‘$pageId’, ‘$slug’)”);


With this method, if my-slug already exists in the DB, the script will create my-slug-1 (because mysql_num_rows($q) returns 1).
Then, if you try to insert my-slug again, mysql_num_rows will return 2 and append -2 to the new slug, thus creating my-slug-2.