如何在php中创建动态URL? [英] How can I create a dynamic URL in php?

查看:433
本文介绍了如何在php中创建动态URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是PHP和MySQL的新手.

I am new in PHP and MySQL.

这是到目前为止我尝试过的方法,我不明白如何使用PHP创建URL.

Here's what I've tried so far and I can't understand how to make URL with PHP.

我想做的是创建有关特定书籍的动态网页.我已经创建了数据库,并在MySQL数据库中有一些数据可以使用.

What I want to do is to create a dynamic web page about a particular book. I already created and have some data in my MySQL database to play with.

我具有清除书名中特殊字符的功能.

I've got a function to clear the special characters in the book titles.

function seo($s) {
$tr = array('ş','Ş','ı','İ','ğ','Ğ','ü','Ü','ö','Ö','Ç','ç');
$eng = array('s','s','i','i','g','g','u','u','o','o','c','c');
$s = str_replace($tr,$eng,$s);
$s = strtolower($s);
$s = preg_replace('/&.+?;/', '', $s);
$s = preg_replace('/[^%a-z0-9 _-]/', '', $s);
$s = preg_replace('/\s+/', '-', $s);
$s = preg_replace('|-+|', '-', $s);
$s = trim($s, '-');

return $s;
}

示例

echo seo($booktitle);

.htaccess

.htaccess

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^/(.*)-(.*)$ /book.php?id=$1 [L,NC]

链接类型

echo "<a href='http://sitename.com/".$bookid."-".seo($booktitle)."'>".$booktitle."</a>";

我想要的输出

http://sitename.com/id-book-title    

问题是,我不知道如何将$ bookid从url动态传递给php本身.我想我需要从数据库中检索book_id并将其分配给$ bookid变量,对吗?但是如何将其连接到URL?

The thing is, I don't understand how I can pass the $bookid from the url to the php itself dynamically. I think I need to retrieve book_id from my database and assign it to $bookid variable, is that correct? But how can I connect it to the URL?

例如,当我输入网址 http://sitename.com/5-the-trial 我需要获取ID为5的书的页面.

For instance, when I type the url http://sitename.com/5-the-trial I need to get the page for the book that has the id of 5.

这里缺少什么?您可以引导我朝正确的方向创建动态网址吗?如果可能的话,我既需要指导(学习,搜索等),也需要具体回答我的问题.

What's missing here? Can you guide me to the right direction to create dynamic urls? I am both in need of guidance (learn this, search that, etc) and a specific answer to my question, if that's possible.

推荐答案

不清楚您的要求是什么.如果您要创建一个列出书本网址的页面,那么与echo语句就相距不远了.您只需要从数据库中填充$ bookid和$ booktitle. .htaccess不参与.

Its not quite clear what your asking. If you want to create a page that lists the urls of your books, then you are not far off with your echo statement. You just need to populate $bookid and $booktitle from the database. .htaccess is not involved.

echo "<a href='http://sitename.com/".$bookid."-".seo($booktitle)."'>".$booktitle."</a>";

但是,如果要解压缩用户单击的链接的URL,则需要查看传递给页面的查询字符串. .htaccess为您分解URL,并将$ 1参数传递到脚本中.要在PHP中读取网址,请尝试以下

But if you want to unpack the URL of the link the user clicked, then you need to look at the query string passed to the page. .htaccess breaks up the URL for you and passes the $1 parameter into your script. To read the url in PHP try the following

parse_str($_SERVER['QUERY_STRING'],$query);
if (array_key_exists('id',$query)) {
   $books = explode("-",$query['id']);
}

这将创建一个数组,第一个元素的书名($ books [0]),第二个标题的书名,等等.(如果您想使用此方法并在其中包含整个标题您可能想使用第二个字符来将ID从标题分隔为用来替换空格的字符.

This will create an array with the book id in the first element ($books[0]), and the first word of the title in the second etc. (If you wanted to use this approach and have the whole title in the second you might want to use a different character to delimit the id from the title to the character you use to replace spaces.

这篇关于如何在php中创建动态URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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