www.webdeveloper.com
Results 1 to 3 of 3

Thread: How do I remove article ID from URL?

  1. #1
    Join Date
    May 2011
    Posts
    10

    How do I remove article ID from URL?

    I've managed to set up my links so that they run to the url I want. Originally they were running to 'myweb.com/blog/1/title' but i've decided that's not quite good enough and want to remove the article id from the url so it reads 'myweb.com/blog/title'. I understand I need to get the blog reading from the $title row and not the $id but I'm not quite sure how to do this with my code, which is this:

    if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
    die("Invalid ID specified.");
    }

    $id = (int)$_GET['id'];
    $sql = "SELECT * FROM php_blog WHERE id='$id' LIMIT 1";

    $result = mysql_query($sql) or print ("Can't select entry from table php_blog.<br />" . $sql . "<br />" . mysql_error());

    while($row = mysql_fetch_array($result)) {

    $date = date("l F d Y", $row['timestamp']);

    $title = stripslashes($row['title']);
    $entry = stripslashes($row['entry']);
    $password = $row['password'];
    $get_categories = mysql_query("SELECT * FROM php_blog_categories WHERE `category_id` = $row[category]");
    $category = mysql_fetch_array($get_categories);

    if ($password == 1) {
    if (isset($_POST['username']) && $_POST['username'] == $my_username) {
    if (isset($_POST['pass']) && $_POST['pass'] == $my_password) {
    ?>
    <p><strong><?php echo $title; ?></strong><br /><br />
    <?php echo $entry; ?><br /><br />
    Posted in <?php echo $category['category_name']; ?> on <?php echo $date; ?></p>

    <?php
    }
    else { ?>
    <p>Sorry, wrong password.</p>

    <?php
    }
    }
    else {
    echo "<p><strong>" . $title . "</strong></p>";

    printf("<p>This is a password protected entry. If you have a password, log in below.</p>");

    printf("<form method=\"post\" action=\"blog_single.php?id=&#37;s\"><p><strong><label for=\"username\">Username:</label></strong><br /><input type=\"text\" name=\"username\" id=\"username\" /></p><p><strong><label for=\"pass\">Password:</label></strong><br /><input type=\"password\" name=\"pass\" id=\"pass\" /></p><p><input type=\"submit\" name=\"submit\" id=\"submit\" value=\"submit\" /></p></form>",$id);
    print "<br /><br />";
    }
    }
    else { ?>
    Any help would be gratefully appreciated, thank you in advance.

  2. #2
    Join Date
    Dec 2010
    Posts
    59
    So you want the PHP to find the article on the basis of the title, rather than the record ID? Dangerous stuff. Always select a record by the unique field - usually the ID. Titles are not always unique.

  3. #3
    Join Date
    May 2011
    Posts
    10
    I'm not necessarily saying that's what I want to happen, I've just heard that's one way to do it. This is my blog so all the titles will be unique. However if there is a better way to remove the number from my url then I'm certainly willing to listen to that.

    At the moment, to achieve 'myweb.com/blog/article-title/' i have this in my .htaccess

    RewriteRule ^blog/([^/\.]+)/?$ blog_single.php?title=$1 [L]
    This at the moment however doesn't allow the page to call from the database as the code is looking for the ID.

    When i had 'myweb.com/blog/1/article-title' I had this is my .htaccess:

    RewriteRule ^blog/([^/\.]+)/?/([^/\.]+)/?$ blog_single.php?id=$1&title=$2 [L]
    And that called from the database fine.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles