0

我有一个 php 函数可以满足我的需要,但现在我发现它有可能会失败。我使用 mod rewrite 来重写 url,这个函数需要重写 url 才能工作。如果我有 2 个页面被重写为相同的页面,即使它们不一样,它也可能会失败。

该函数读取 url 并使用 / 作为分隔符将其拆分为多个部分。

我想要做的是检查网址中是否包含“论坛”一词,以及它是否使用查询中的以下网址部分查询数据库。

假设我有一个像http://www.mydomain.com/forum/1/2/6这样的网址,我想为我的查询获取 1 和 2,这将是 boardid 和 topicid。最后一位是页码。

这是一个重写的 url,通常看起来像http://www.mydomain.com/topic.php?boardid=1&topicid=2&pagenum=6但使用该函数我无法拆分,因为没有 /。

我可以很容易地完成查询,它只是检查 url 以确保“论坛”在那里,然后进行查询。如果它不在 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 中的每个变量

4

2 回答 2

1

URL 后面的变量字符串称为查询字符串。检查查询字符串中是否存在变量的最佳方法是查看 $_GET 数组。

所以......如果你有:

http://google.com/?key=value&another_key=another_value&forum=22

您将像这样检查变量“论坛”:

if (isset($_GET['forum'])){

  // 'forum' has been set in the query string, do something...

} else {

  // 'forum' has NOT been set.
}
于 2010-08-02T00:59:21.813 回答
0

我设法按照我的需要修复它,这并不难

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
    ) {
        global $host,$dbUser,$dbPass,$dbName;

        require_once("php/database/connection.php");
        require_once("php/database/MySQL.php");

        // Connect to the database and grab the email
        $db = & new MySQL($host,$dbUser,$dbPass,$dbName);

        $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'];

        $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) {
                    if($parts[$i-1]=="forum") {
                        $board = substr($parts[$i],6);
                        $sql = "SELECT boardname FROM boards WHERE boardid='".$board."'";
                        $result = $db->query($sql);

                        while($row=$result->fetch()) {
                            extract($row, EXTR_PREFIX_INVALID, '_');
                            $breadcrumb .= $boardname;
                        }
                    } 
                    else if($parts[$i-2]=="forum") {
                        $topic = substr($parts[$i],10);
                        $sql = "SELECT topicname FROM topics WHERE topicid='".$topic."'";
                        $result = $db->query($sql);

                        while($row=$result->fetch()) {
                            extract($row, EXTR_PREFIX_INVALID, '_');
                            $breadcrumb .= $topicname;
                        }
                    }
                    else {
                        $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;
    }

如果上一个是“论坛”,我必须检查当前部分,连接到数据库,获取我需要的信息,然后替换当时面包屑中的内容。下一个面包屑也是如此。

现在它应该如何工作。

于 2010-08-02T17:58:46.313 回答