SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Thread: Dynamic URLs

  1. #1
    SitePoint Enthusiast geeklove's Avatar
    Join Date
    Jun 2007
    Location
    Iowa, USA
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Dynamic URLs

    I want to create (secure) dynamic urls that pull an article from my article database by its url field (just the title with hyphens instead of spaces) so I have a url like "article.php?title=article-title" and then I will use mod_rewrite to rewrite the url to "articles/article-title.php"- how I do this securely?

    Or should I use "?id=1234" and then pull the url field out of that specific ID? I have no idea.

    PHP Code:
    // connection variables hidden
    $connection mysql_connect("$hostname"$user"$pass");
    $db mysql_select_db($dbase $connection);
    $query "SELECT id,title,category,DATE_FORMAT(date, '%d/%M/%Y') as date,article,url FROM article_db WHERE url=$url ORDER BY category,title asc";
    if(!
    $result mysql_query($query)) die("Query problem: $query." mysql_error());
    if (
    mysql_num_rows($result) == 0) {
      die(
    "No articles in the database!");
    }
    while(
    $row mysql_fetch_array($result)) {
            
    $id $row["id"];
            
    $title $row[""];
            
    $category $row["category"];
            
    $date $row["date"];
            
    $article $row["article"];
            echo 
    "<h5>$category &raquo; $title</h5>";
            echo 
    "<b>Posted on $date</b><br /><br />";
            echo 
    "$article";

    ~ Hannah

  2. #2
    SitePoint Addict
    Join Date
    Jul 2007
    Location
    San Jose, California
    Posts
    355
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You should pull the field by the primary key, which should be $id and seems alot easier.

  3. #3
    SitePoint Enthusiast geeklove's Avatar
    Join Date
    Jun 2007
    Location
    Iowa, USA
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, I know how to do that. But then how do I get my url to contain the url field? Because I want that to be in the url instead of the ID.
    ~ Hannah

  4. #4
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    That depends on how your url is built in the first place..

    PHP Code:
    # Link builder #
    $result# query the article list from DB.

    while ($row mysql_fetch_assoc($result)) {
        echo 
    "<a href='article.php?id=$row[article_id]'>$row[article_title]</a>";
    }

    # Article view #
    $articleData mysql_fetch_assoc(mysql_query("SELECT this, that, and those FROM article_table WHERE article_id = ".intval($_GET['id'])));

    print_r($articleData); 

  5. #5
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    if you want to use the title of the article and not have the id displayed anywhere. Then you need to lookup the article title, what I do is set up two versions of the title in the DB. A display version and a URL version.

    Probably not the best means but one i'm more comfortable with.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  6. #6
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    982
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    if you want to use the title of the article and not have the id displayed anywhere. Then you need to lookup the article title, what I do is set up two versions of the title in the DB. A display version and a URL version.

    Probably not the best means but one i'm more comfortable with.
    I do this as well. Wordpress calls them "slugs" so I've used that term in my databases as well. Throw a unique key on the "slugs" and you'll have a fairly quick lookup -- but it will never be as quick as a numeric key.
    MySQL v5.1.58
    PHP v5.3.6

  7. #7
    SitePoint Wizard wheeler's Avatar
    Join Date
    Mar 2006
    Location
    Gold Coast, Australia
    Posts
    1,369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can't you do: article.php?id=3&title=the-title

    use id for the lookup and title in the rewrite? argh I hate mod_rewrite
    Studiotime - Time Management for Web Developers
    to-do's, messages, invoicing, reporting - 30 day free trial!
    Thomas Multimedia Web Development

  8. #8
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    982
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if your URL is /article/the-title/, you can't use the id (unless you use a static lookup in your htaccess file, but that will be slower than a database and much harder to upkeep). If you went the route of /article/3--the-title/ then you could write a rule in your htaccess file to pull the article id and title from the URL. But this isn't as pretty. I think looking up by title should be plenty fast.
    MySQL v5.1.58
    PHP v5.3.6


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •