来自 url 和查询数据库的 php 面包屑
我有一个 php 函数可以满足我的需要,但现在我发现它有可能失败。我使用 mod rewrite 来重写 url,并且此函数需要重写 url 才能工作。如果我有 2 个页面被重写为相同的,即使它们不相同,它也可能会失败。
该函数读取 url 并使用 / 作为分隔符将其分成几部分。
我想要做的是检查 url 中是否包含“forum”一词,以及是否使用查询中的以下 url 部分来查询数据库。
假设我有一个像 http://www.mydomain.com/forum/1/ 的网址2/6 我想获取查询的 1 和 2,即 boardid 和 topicid。最后一位是页码。
这是一个重写的网址,通常看起来像 http: //www.mydomain.com/topic.php?boardid=1&topicid=2&pagenum=6 但使用该函数我将无法拆分,因为没有 /。
我可以做的查询很简单,只需检查网址以确保“论坛”在那里,然后进行查询。如果它不在 url 中,则照常进行。
这是我的 php 面包屑的代码
function breadcrumb(
$home = 'Home', // Name of root link
$division = ' / ', // Divider between links
$hidextra = true, // Toggle hide/show get data and fragment in text
$index = false, // Toggle show/hide link to directory if it does not contain a file
$indexname = 'index.php' // The definition of the file the directory must contain
) {
$breadcrumb="";
// Requested addons...
$extension = '.php'; // Extension to cut off the end of the TEXT links
$ifIndex = 'index.php'; // Filename of index/default/home files to not display
// End requested addons
$whole = $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
if(stristr($whole,"contact-bidder")) {
$whole = substr($whole,0,strrpos($whole,'/'));
}
$parts = explode('/', $whole);
$parts[0] = 'http://'.$parts[0];
$array = array('-', '%20');
$breadcrumb .= "<a href=\"{$parts[0]}\">{$home}</a>{$division}";
$k = 1;
for ($i=1;$i < sizeof($parts);$i++) {
$uri = '/';
while ($k <= $i) {
$uri .= $parts[$k];
if ($k != (sizeof($parts)-1)) $uri .= '/';
$k++;
}
if (($index && is_dir($_SERVER['DOCUMENT_ROOT'].$uri) && is_file($_SERVER['DOCUMENT_ROOT'].$uri.$indexname)
|| !$index
|| !is_dir($_SERVER['DOCUMENT_ROOT'].$uri)) && $parts[$i] != $ifIndex) {
$breadcrumb .= "<a href=\"$uri\">";
if ($hidextra) {
$breadcrumb .= rtrim(preg_replace("/\?.*$/", '', ucwords(str_replace($array," ",$parts[$i]))), $extension);
}
else {
$breadcrumb .= rtrim(ucwords($parts[$i]), $extension);
}
$breadcrumb .= '</a>';
}
else {
$breadcrumb .= ucwords(str_replace($array," ",$parts[$i]));
}
if (isset($parts[($i+1)])) {
$breadcrumb .= $division;
}
$k = 1;
}
return $breadcrumb;
}
如果这不可能或容易,有没有办法我可以拆分?和&并只获取 ? 之前的内容url 中每个变量的 = 之后
I have a php function that works for what i need but now i've found that there is chance for it to fail. I use mod rewrite to rewrite urls and this function needs urls to be rewritten for it to work. If i have 2 pages that have been rewritten as the same even though they are not the same, it could fail.
The function reads the url and splits it into parts using the / as the separator.
What i want to be able to do is check if the url has the word 'forum' in it and if it does query the database using the following url parts in the query.
Say i have a url like http://www.mydomain.com/forum/1/2/6 i would like to get the 1 and 2 for my query which would be the boardid and topicid. The last bit would be the page number.
This is a rewritten url which would normally look like http://www.mydomain.com/topic.php?boardid=1&topicid=2&pagenum=6 but using the function I wouldn't be able to split becuase there are no /.
The query i can do easy enough, it's just checking the url to make sure that 'forum' is in there then do the query. If it's not in the url then carry on as normal.
here is the code for my php breadcrumb
function breadcrumb(
$home = 'Home', // Name of root link
$division = ' / ', // Divider between links
$hidextra = true, // Toggle hide/show get data and fragment in text
$index = false, // Toggle show/hide link to directory if it does not contain a file
$indexname = 'index.php' // The definition of the file the directory must contain
) {
$breadcrumb="";
// Requested addons...
$extension = '.php'; // Extension to cut off the end of the TEXT links
$ifIndex = 'index.php'; // Filename of index/default/home files to not display
// End requested addons
$whole = $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
if(stristr($whole,"contact-bidder")) {
$whole = substr($whole,0,strrpos($whole,'/'));
}
$parts = explode('/', $whole);
$parts[0] = 'http://'.$parts[0];
$array = array('-', '%20');
$breadcrumb .= "<a href=\"{$parts[0]}\">{$home}</a>{$division}";
$k = 1;
for ($i=1;$i < sizeof($parts);$i++) {
$uri = '/';
while ($k <= $i) {
$uri .= $parts[$k];
if ($k != (sizeof($parts)-1)) $uri .= '/';
$k++;
}
if (($index && is_dir($_SERVER['DOCUMENT_ROOT'].$uri) && is_file($_SERVER['DOCUMENT_ROOT'].$uri.$indexname)
|| !$index
|| !is_dir($_SERVER['DOCUMENT_ROOT'].$uri)) && $parts[$i] != $ifIndex) {
$breadcrumb .= "<a href=\"$uri\">";
if ($hidextra) {
$breadcrumb .= rtrim(preg_replace("/\?.*$/", '', ucwords(str_replace($array," ",$parts[$i]))), $extension);
}
else {
$breadcrumb .= rtrim(ucwords($parts[$i]), $extension);
}
$breadcrumb .= '</a>';
}
else {
$breadcrumb .= ucwords(str_replace($array," ",$parts[$i]));
}
if (isset($parts[($i+1)])) {
$breadcrumb .= $division;
}
$k = 1;
}
return $breadcrumb;
}
If that isn't possible or easy, is there a way i can split on the ? and & and get only what is before the ? and after = for each variable in the url
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
URL 后面的变量字符串称为查询字符串。检查查询字符串中是否存在变量的最佳方法是查看 $_GET 数组。
那么...如果您有:
http://google.com/? key=value&another_key=another_value&forum=22
您将像这样检查变量“forum”:
The string of variables after the URL is called the query string. The best way to check for the existence of variables in the query string is to look in the $_GET array.
So... if you have:
http://google.com/?key=value&another_key=another_value&forum=22
You would check for the variable 'forum' like this:
我设法按照我需要的方式修复它,这并不难,
我必须检查当前部分是否是“论坛”,连接到数据库,获取我需要的信息,然后替换面包屑中的内容在那一点上。下一个面包屑也是如此。
现在它可以正常工作了。
I managed to fix it how i needed and it wasn't that hard
I had to check on the current part if the previous was 'forum', make a connection to the database, grab the info i needed then replace what is in the breadcrumb at that point. It was the same for the next breadcrumb.
It now works how it should.