改写?ID = __存储在数据库行网页标题 [英] rewrite ?id=__ to page title stored in db row

查看:134
本文介绍了改写?ID = __存储在数据库行网页标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此先感谢您的帮助。

我试图找出如何重写一个简单的CMS程序我建立创建动态网址。在code输出与ID(www.mysite.com/index.php?id=1)的URL。我想修改code,使其与1,如www.mysite.com/home或www.mysite.com/about这里大约是标题存储的ID输出行的标题在分贝在分贝为1的ID行。

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.

下面是如何code输出的一个例子:

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>";
    }

HTTP:

我从去离CMS教程://fwebde.com/web-design/creating-a-php-cms-part-1/

任何帮助将是很大的AP preciated。我试图无数的.htaccess重写,其中无工作过。请让我知道这很简单,只要开关周围的MySQL code。

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.

推荐答案

您必须修改生成的链接再行:

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>";

或生成 123 +标题链接:

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

这将需要适应实际的不育系调度员code,因为它打破了ID,并从$ _GET参数的称号。

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.

甚至更换整个的index.php ID = 只有标题:?

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

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

这反过来将需要部署一个的.htaccess 规则(类似的 htaccess的URL路由PHP MVC?)。你将不得不再次修改的index.php 脚本来查找 ID 标题。这又只会工作,如果在你的数据库的标题都是唯一的。

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.

PS:其实你应该也可以使用 urlen code($ PAGETITLE)。而用htmlspecialchars() $ PAGETITLE 将用作链接文本。

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

这篇关于改写?ID = __存储在数据库行网页标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆