i have (New post) as a title on my db table, how can i get it from the db using WHERE

Discussion in 'PHP' started by free-designer, Jul 16, 2010.

  1. #1
    Hey experts,
    Well i think the title don't explain enough, so...

    If i have a db table that have 2 columns (id and title) and the title has 3 rows
    (1, First post title), (2, Second post title), (3,Third post title)

    i have a php code that skip the string and make it like the following:
    (1, first-post-title), (2, second-post-title), (3, third-post-title)

    So how can i use a mysql query to get the row by the title like this:
    SELECT * FROM dbname WHERE title = "third-post-title";

    so how can i make this query works and really find the title which have the title (Third post title), by using WHERE = "third-post-title";

    Thanks if anyone want more information just ask?? Thanks so mush
     
    free-designer, Jul 16, 2010 IP
  2. BrettOwnz

    BrettOwnz Member

    Messages:
    286
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    28
    #2
    I'm not sure exactly what your trying to do, but this is how I understood it:

    
    <?php
    
    $string = "(1, first-post-title), (2, second-post-title), (3, third-post-title)";
    $replace = array(")","(");
    $string = str_replace($replace,'',$string);
    $string = explode(",",$string);
    $title = $string[5];
    
    ?>
    
    PHP:
    Now the variable $title is equal to 'third-post-title'. You would have to modify this to work with your variables etc.

    Let me know if you need more help!
    -Brett
     
    BrettOwnz, Jul 16, 2010 IP
  3. free-designer

    free-designer Peon

    Messages:
    79
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for the help but actually this is not what i need, what i need is:

    the problem is that "third post title", will look on the link above like this
    domainname.com/third-post-title.

    How can i get the exact same row from the database using: "third-post-title" to get "third post title"'s row

    Any help thanks
     
    free-designer, Jul 16, 2010 IP
  4. BrettOwnz

    BrettOwnz Member

    Messages:
    286
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    28
    #4
    I would suggest just removing the -'s from the string and replacing them with spaces.

    
    <?php
    
    $string = 'third-post-title';
    $title = str_replace('-',' ',$string);
    
    ?>
    
    PHP:
    $title will now equal third post title. Does this help?

    -Brett
     
    BrettOwnz, Jul 16, 2010 IP
  5. free-designer

    free-designer Peon

    Messages:
    79
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    i know, i think(ed) of that before but the problem is that, my function dose not only make the string have - between words, but it removes all of the unwanted marks like #$%^&*( things like that, how can i get a string and put everything back, it can't happen.

    The problem is that wordpress, doing it that way in the mysql table there is only a title but once you change your wordpress links from post_id to post_slug, the title will be escape to be like this (test-title) and they have the ability to get the post data from the db table with the escape link...

    So is there any way to do it with mysql.

    Thanks
     
    free-designer, Jul 16, 2010 IP
  6. BrettOwnz

    BrettOwnz Member

    Messages:
    286
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    28
    #6
    Hmm since I don't have a copy of wordpress installed on my machine right now I won't be able to really help you with that i'm sorry. Maybe if you post an example of your code right now, tell me what it is doing and tell me what you want it to do EXACTLY i might be able to. Let me know.

    -Brett
     
    BrettOwnz, Jul 16, 2010 IP
  7. free-designer

    free-designer Peon

    Messages:
    79
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Well, im not on my developing machine right now, but i'll be tomorrow so if you are available tomorrow i'll post what i have exactly and tell you, okay?
     
    free-designer, Jul 16, 2010 IP
  8. BrettOwnz

    BrettOwnz Member

    Messages:
    286
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    28
    #8
    Sounds good!
     
    BrettOwnz, Jul 16, 2010 IP
  9. Deacalion

    Deacalion Peon

    Messages:
    438
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Not sure I understand the question. However this is what I think you mean.

    You want to convert a title like 'third-post-title' to it's original form of 'Third post title'?

    If so, what you are asking is impossible. You can write functions all day long to convert spaces to dashes and such - but how are you going to determine where a capital letter was? or symbols that you didn't foresee being in the title? The reason for this is simple, the dashed and URL friendly version of your title is essentially a fingerprint of the original. Can you tell how tall a person is by their fingerprint? no, because the data isn't there. :)

    Internally Wordpress deals with this by holding both versions in the table. One field is called post_title and the other is post_name (they are in wp_posts). They use the function sanitize_title_with_dashes (it's in wp-includes\formatting.php) to convert titles to URL form.
     
    Deacalion, Jul 16, 2010 IP
  10. Deacalion

    Deacalion Peon

    Messages:
    438
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Sorry, forgot to add the solution :).

    Add another field to your table that holds the 'sanitized' title (use sanitize_title_with_dashes() to do this). So you can use that in search queries.
    So the table rows would be:
    
    (1, 'This is Title One',   'this-is-title-one'),
    (1, 'This is Title Two',   'this-is-title-two'),
    (1, 'This is Title Three', 'this-is-title-three')
    
    Code (markup):
     
    Deacalion, Jul 16, 2010 IP