1

Thanks in advance for any help.

I am trying to figure out how to rewrite dynamic urls created with a simple cms programming I am building. The code outputs the URL with the ID (www.mysite.com/index.php?id=1). I would like to modify the code so that it outputs the title of the row in the db with the id of 1 such as www.mysite.com/home or www.mysite.com/about where about is the title stored in the row of the db with an id of 1.

Here is an example of how the code outputs:

$result = mysql_query("SELECT id, title FROM pages");

while ($row = mysql_fetch_array($result)) {
    // Do not list the home page twice
    if ($row['id'] != $homeID) {
        $pageID = $row['id'];
        $pageTitle = $row['title'];

        echo "<li><a href='" . BASE_URL . "/index.php?id=$pageID'>$pageTitle</a></li>";
    }

I am going off of the CMS tutorial from: http://fwebde.com/web-design/creating-a-php-cms-part-1/

Any help would be greatly appreciated. I have attempted numerous .htaccess rewrites and NONE of them have worked. Please let me know if this is as simple as switching around the MySQL code.

KGDD
  • 99
  • 2
  • 10
  • I'm not sure what the question is? Are you having an error? If you're executing the above code when the document loads you can just put $pageTitle in and it will display. – Tyler Ferraro Oct 17 '11 at 05:11
  • Whats your problem .. please explain in detail – ravi404 Oct 17 '11 at 05:26

2 Answers2

1

You will have to modify the line that generates the links then:

echo "<li><a href='" . BASE_URL . "/index.php?id=$pageID'>$pageTitle</a></li>";

You can either add the title as separate and unused &title= parameter, which is a nice hack to avoid having to modify anything else:

echo "<li><a href='" . BASE_URL . "/index.php?id=$pageID&title=$pageTitle'>$pageTitle</a></li>";

Or generate 123+title links:

echo "<li><a href='" . BASE_URL . "/index.php?id=$pageID+$pageTitle'>$pageTitle</a></li>";

This would require to adapt the actual "cms" dispatcher code, because it has to break up the id and the title from the $_GET parameter.

Or even replace the whole index.php?id= with just the title:

echo "<li><a href='" . BASE_URL . "/$pageTitle'>$pageTitle</a></li>";

Which in turn would require to deploy a .htaccess rule (something like htaccess URL routing for PHP MVC?). And you would have to modify the index.php script again to look up the id from title. Which again would only work if the titles in your database were all unique.

P.S.: You should actually also use urlencode($pageTitle). And htmlspecialchars() on the $pageTitle that becomes the link text.

Community
  • 1
  • 1
mario
  • 144,265
  • 20
  • 237
  • 291
  • Your solution was simple for adding the &title=$pageTitle to the link generator part. I was able to build the .htaccess rewrite and correct that and remove the id=__ portion to just leave the $pageTitle. It's the simplest things you overlook sometimes :) Thanks!!! – KGDD Oct 18 '11 at 23:31
  • Another question, do you know how to adjust the .htaccess to allow for any range of id number? Such as ?id=1 all the way up to triple digits i.e. ?id=999? This is what I have so far, sorry for commenting twice. RewriteRule ^([^_]*)$ /goober/?id=1&title=$1 [L], I would like the ?id=__ portion to allow for any range of id numbers all the way up to 999. Thanks in advance!! – KGDD Oct 19 '11 at 00:51
  • Don't strip the `id=???` then. If you just fixate it to `1`, then that's the problem. Without knowing your current incoming URL, I can't fix your RewriteRule. – mario Oct 19 '11 at 07:07
  • the incomiing URL is: http://www.mysite.com/folder/?id=(any number)&linklabel=index Any help with doing the.htaccess or slug so that the url looks like mysite.com/index, this is what i'm after. Thanks! – KGDD Oct 22 '11 at 15:43
  • See the linked link again: http://stackoverflow.com/questions/7693788/htaccess-url-routing-for-php-mvc – mario Oct 22 '11 at 15:46
0

It wont be as easy as you would like. The simplest way would be to include the page number in your final url. http://www.mysite.com/1/articles but I don't know if that would be exactly what you are looking for.

The rewrite rule would be.

RewriteRule ^([0-9]*)/.*$ index.php?id=$1

What I have done on my own CMS systems is to add another field to the pages table called urlname. This would be what the URL would be to access that page. You would need to change all the references to $_GET['id'].

Add this to functions.php.

function thisPageId(){
    $sql="SELECT id FROM pages WHERE urlname = '" . $_GET['urlname'] ."'";
    $result=mysql_query($sql);
    $row=mysql_fetch_assoc($result);
    return $row['id'];
}

htaccess rewrite rule

This will redirect any request that does not exist to index.php?urlname=...

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule (.*) index.php?urlname=$1

Replace any references to $_GET['id'] with the value from thisPageId. For your listPages function and anywhere else that the url is displayed, you'll do something like this.

echo "<li><a href='" . BASE_URL . "/" . $row['urlname'] . "'>$pageTitle</a></li>";

Remember you'll also have to edit your administration pages to add the new field.

AndrewR
  • 6,668
  • 1
  • 24
  • 38
  • 1
    Please please please don't encourage use of unsanitized globals in queries. This is wide open to injection attacks. – Will Vousden Oct 17 '11 at 06:40
  • I have another question focusing on the .htaccess portion. RewriteEngine on RewriteCond %{SCRIPT_FILENAME} !-f RewriteCond %{SCRIPT_FILENAME} !-d RewriteRule ^([^_]*)$ /sitefoldernamehere/?id=1&title=$1 [L] How do I tell the RewriteRule to recognize ANY id number, whether it be 1, 2 or 25 or 100? – KGDD Oct 19 '11 at 00:06