Forums

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?

  • This topic is empty.
Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #43017
    sayedtaqui
    Member

    Hi,
    ( 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’)”);

    ?>

    #126293
    Kitty Giraudel
    Participant

    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.

    #126295
    sayedtaqui
    Member

    Thanks 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?? ):

    #126298
    Kitty Giraudel
    Participant

    I couldn’t test my code, but I believe it’s only a syntax mistake. What’s the problem with it?

    #126300
    sayedtaqui
    Member

    Hey 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.

    #126301
    Kitty 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 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. …

    #126302
    sayedtaqui
    Member

    I 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 again

    #126304
    Kitty Giraudel
    Participant

    Don’t worry I got it. Spent a couple of hours playing around with CSS media queries, so you must be right. :)

Viewing 8 posts - 1 through 8 (of 8 total)
  • The forum ‘Back End’ is closed to new topics and replies.