- This topic is empty.
-
AuthorPosts
-
February 27, 2013 at 7:11 am #43017
sayedtaqui
MemberHi,
( I m creating a cms and having a little diffculty writing a condition. I m genrating new pages from a single page by passing different slug(id) values in url and then I have written rewrite rule in .htaccess file , so that the name of the title becomes the slug of the url.)So I have to ensure that no two slugs become the same and also they look good in the url. Im generating slugs from the title of the page itself(created by admin) or he would also have the option to manually enter the slug(optional). So how do I write a condition for the following things.
1- No two slugs should become same in database.
2- The first slug should be simple like for (About Us) it should be about-us and if the admin enter the same page name(if he is stupid) the slug should become about-us-1 and about-us-2 and so on…I tried to write some script but I m not able to figure out the exact way.
$title=mysql_real_escape_string($_POST);
if($_POST==NULL){
$slug=str_replace(‘ ‘, ‘-‘, $title);$query=mysql_query(“SELECT * FROM pages WHERE slug=’$slug’ “);
if(mysql_num_rows($query)==0){
$slug2= $slug;
}else{
$slug2= $slug;
$slug2.=”-“;
$slug2.=”1″;
}}else{
$slug= mysql_real_escape_string($_POST);
$slug2=str_replace(‘ ‘, ‘-‘, $slug);
}$query= mysql_query(“INSERT INTO pages(pagename, content, pageId, slug) VALUES(‘$title’, ‘$content’, ‘$pageId’, ‘$slug2’)”);
?>
February 27, 2013 at 8:41 am #126293Kitty Giraudel
ParticipantI’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 createmy-slug-1
(becausemysql_num_rows($q)
returns 1).
Then, if you try to insertmy-slug
again,mysql_num_rows
will return 2 and append -2 to the new slug, thus creatingmy-slug-2
.February 27, 2013 at 9:01 am #126295sayedtaqui
MemberThanks Hugo , the script did not work but that gave me an idea
I have written another script$title= mysql_real_escape_string($_POST);
if($_POST==NULL){
$slug=str_replace(‘ ‘, ‘-‘, $title);$query=mysql_query(“SELECT * FROM pages WHERE slug=’$slug’ “);
if(mysql_num_rows($query)==0){
$slug2= $slug;
}else{
if(preg_match(‘/[a-zA-Z0-9-]+~[0-9]/’, $slug)){
$dash_pos= strrpos($slug, ‘~’);
$str_len= strlen($slug);
$after_dash= substr($slug,$dash_pos+1, $str_len );
$slug2= str_replace($after_dash,$after_dash+1,$slug);
}else{
$slug2=$slug;
$slug2.=’~’;
$slug2.=’1′;
}
}
//main if statememt ENDS
}else{
$slug= mysql_real_escape_string($_POST);
$slug2=str_replace(‘ ‘, ‘-‘, $slug);
}$query= mysql_query(“INSERT INTO pages(pagename, content, pageId, slug, date, time, count) VALUES(‘$title’, ‘$content’, ‘$pageId’, ‘$slug2’, ‘$date’,’$time’, ‘$count’)”);
The code above is creating another slug
First it creates just ‘about-us’
then ‘about-us~1’
and if I do it again , it returns
‘about-us~1’ only.
What do I do?? ):February 27, 2013 at 9:20 am #126298Kitty Giraudel
ParticipantI couldn’t test my code, but I believe it’s only a syntax mistake. What’s the problem with it?
February 27, 2013 at 9:28 am #126300sayedtaqui
MemberHey Wow Hugo, you solved the problem , actually earlier I ran that code and it was giving an error but I didn’t notice that there was syntax error (mediaquery= mysql [probably you do css a lot.]) anyways, the script you wrote is perfect and well explained. But if you could please point out the problem with second script that I wrote , I would gratefull to you.
Thanks a lot.February 27, 2013 at 9:30 am #126301Kitty Giraudel
Participant> (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 bemy-slug~X
. It will bemy-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 insertmy-slug
; no problem
2. You want to insertmy-slug
again; there is already one in DB, let’s call itmy-slug-1
3. You want to insertmy-slug
again; there are already 2 slugs in DB starting bymy-slug
(my-slug
andmy-slug-1
) so let’s call itmy-slug-2
4. You want to insertmy-slug
**again**; there are already 3 slugs in DB starting bymy-slug
(my-slug
,my-slug-1
andmy-slug-2
) so let’s call itmy-slug-3
5. …February 27, 2013 at 9:40 am #126302sayedtaqui
MemberI meant probably you write CSS a lot because you wrote media_query(“SELECT *..”) like in CSS we do @media_query for different screen resolution. was just kidding.
Thanks againFebruary 27, 2013 at 9:43 am #126304Kitty Giraudel
ParticipantDon’t worry I got it. Spent a couple of hours playing around with CSS media queries, so you must be right. :)
-
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.