我的PHP新手,这是修订后的问题。我也问过这个问题,在许多论坛,但是,我根本就不能得到什么错误。
I am PHP newbie and this is revised question. I have asked this question, in many forum but, I simply can't get what is going wrong.
其目的就是要改变这个URL从
The purpose is just to change this url from
的http://localhost/DIRECTORY/AID/news.php A_ID = 24
到
的http://本地主机/目录/ AID /新闻/ this_is_article_title
对于这一点,我有三排 id``title``content
所以,在这个例子中我试图呼应我们的行(ID)24号:
ID = 24
标题=这是文章的标题
内容=这是内容
For this, I have three rows id``title``content
so, for this example I am trying to echo our row (id) number 24 :
id=24
title=This is article title
content=this is content
好了,希望是显而易见的。现在,让我们去的URL。
Ok, hope that was clear. Now, lets to go the URL.
的http://本地主机
是好..本地主机(我已经安装了XAMPP)
http://localhost
is well.. localhost (I have XAMPP installed)
目录/
是保存在我把我的网站项目的其他文件夹中的文件夹。
DIRECTORY/
is a folder that holds other folders in which I put my site projects.
AID /
名为决赛的现场项目根文件夹。它是在目录/
文件夹中找到,这个文件夹里面有文件,如:
AID/
a root folder for the site project called FINAL. it is found inside the DIRECTORY/
folder, and inside this folder there file such as:
的.htaccess
的index.php
news.php
/图像
/ CSS
/ JS
在的.htaccess
我有这个code
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
^news/([A_Za_z0_9_]+)$ DIRECTORY/AID/news.php?id=$1 [QSA,L]
希望,一切都清楚了为止。
现在,在指数PHP的,我有这样的功能,即获得所有新闻标题和内容数据库,并创建一个链接的 news.php
的含义,当你点击一个从索引页标题,它把你带到 news.php?ID = 24
。下面是函数:
Hope, everything is clear so far.
Now in the index php, I have this function, that gets all news title, and content from database and creates a link for the news.php
meaning, when you click on a title from the index page, it takes you to news.php?id=24
. Here is the function:
function news_preview() {
$query = "SELECT * FROM news ORDER BY id DESC LIMIT 5 ";
$result = mysql_query($query) or die(mysql_error());
while($row=mysql_fetch_array($result))
{ echo "<a href="news.php?news=$row[id]"> ". substr($row['title'], 0,26)."...</a><br/>".; }
}
如果你看到上面清楚了,我送行的ID,以news.php在这条线。
If you see the above clearly, I am sending id of row to news.php with in this line.
回声&LT; A HREF = news.php新闻= $行[ID] ?&gt;中。 SUBSTR($行['标题'],0,26)&LT; / A&GT;
现在,在news.php里面没有太多的事,我只是得到了ID和呼应的结果。但是,我需要的URL更改为我做的。而且,我不知道是什么问题。
Now, inside the news.php there is not much to do, I just get the id and echo the result. But, i need the url to change as I do. And, I don't know what the problem is.
首先,您需要更改HTML的HREF,给新的URL格式
First of all, you would need to change the href in the html, to give the new url format
function news_preview() {
$query = "SELECT * FROM news ORDER BY id DESC LIMIT 5 ";
$result = mysql_query($query) or die(mysql_error());
while($row=mysql_fetch_array($result))
{
echo "<a href="/news/$row[id]"> ". substr($row['title'], 0,26)."...</a><br/>".; }
}
喜欢 HTTP的将生成的URL://本地主机/新闻/ 24
请注意,我删除了 /目录/ AID
从URL,如htaccess的建议,你想要的是URL,而不是你的文字说明。
Note that I removed the /DIRECTORY/AID
from the url, as the htaccess suggest you want that to be url, as opposed to what you stated in the text.
但是,现在得到了的http://本地主机/新闻/ this_is_article_title
类型的URL。因为有之间 this_is_article_title
不相关的标识 24
,实现这一目标的唯一途径是通过添加标识该网址也还是有PHP查找与这个称号在数据库中的新闻文章。
But now the get to the http://localhost/news/this_is_article_title
type of url. Because there is no correlation between this_is_article_title
and the id 24
, the only way to achieve this is by either adding the id to the url too, or to have the php lookup the news-article with this title in the database.
这最后的解决方案,但也存在一些问题,因为你不能只是我们的一个url称号。你必须转义字符。同时你必须添加一个索引在数据库中的标题行获得更好的性能。
This last solution however has some problems, as the you can't just us the title in a url. You have to escape characters. Also you'll have to add a index for the title row in the DB for better performance.
所以我会去的第一个解决方案。我们会产生这样的网址
So I'll go with the first solution. We will generate urls like this
的http://本地主机/新闻/ 24 / this_is_article_title
首先PHP的一部分
function news_preview() {
$query = "SELECT * FROM news ORDER BY id DESC LIMIT 5 ";
$result = mysql_query($query) or die(mysql_error());
while($row=mysql_fetch_array($result))
{
$url = "/news/$row[id]/".preg_replace('/[^a-zA-Z0-9-_]/', '_', $row['title']);
echo "<a href="$url"> ". substr($row['title'], 0,26)."...</a><br/>".; }
}
接下来是htaccess的部分。
Next comes the htaccess part.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^news/([0-9]+)/([A-Za-z0-9_-]+)$ DIRECTORY/AID/news.php?id=$1 [QSA,L]
这应该这样做,我认为。
That should do it I think.