PHP函数绝对URL显示问题

发布于 2024-11-07 12:03:46 字数 1754 浏览 0 评论 0原文

我试图让我的函数显示我的类别绝对网址地址,例如 http://www.example.com/cat/sub-1/sub-2/sub-3/ 但我继续获取 http://www.example.com/cat//sub-3/ 有人可以帮我解决这个问题吗?请尽可能详细,因为我对 PHP 还很陌生。

这是我的 PHP 函数

function allCategories(){
    global $parent_url;
    global $url;
    $nodeList = array();
    $tree = array();
    $query = mysqli_query(database(),"SELECT * FROM categories ORDER BY parent_id, category LIKE '%more%', category ASC");
    while($row = mysqli_fetch_assoc($query)){
        $nodeList[$row['id']] = array_merge($row, array('children' => array()));
    }
    mysqli_free_result($query);

    foreach($nodeList as $nodeId => &$node) {
        if(!$node['parent_id'] || !array_key_exists($node['parent_id'], $nodeList)){
            $tree[] = &$node;
            $url = $parent_url . $node['url'];
            $url = str_replace('?cat=', '', $url);
            echo '<li><a href="http://www.example.com/cat/' . strip_tags($url) . '" title="' . strip_tags($node['category']) . ' Link" class="category-headers">' . strip_tags($node['category']) . '</a>';   
        } else {
            $nodeList[$node['parent_id']]['children'][] = &$node;
            $url = $parent_url . $node['url'];
            $cat_num = array('&#063;cat=','&#038;sub1=','&#038;sub2=');
            $url = str_replace($cat_num, '/', $url);
            echo '<li><a href="http://www.example.com/cat/' . strip_tags($url) . '" title="' . strip_tags($node['category']) . ' Link">' . strip_tags($node['category']) . '</a>';              
        }

    echo '</li>';        
    } 

    echo '</ol>';  
    unset($node);
    unset($nodeList);
}

allCategories();

I'm trying to get my function to display my categories absolute url address for example http://www.example.com/cat/sub-1/sub-2/sub-3/ But I keep getting http://www.example.com/cat//sub-3/ can some one help me correct this problem. Please be detailed as possible since I'm farily new to PHP.

Here is my PHP function

function allCategories(){
    global $parent_url;
    global $url;
    $nodeList = array();
    $tree = array();
    $query = mysqli_query(database(),"SELECT * FROM categories ORDER BY parent_id, category LIKE '%more%', category ASC");
    while($row = mysqli_fetch_assoc($query)){
        $nodeList[$row['id']] = array_merge($row, array('children' => array()));
    }
    mysqli_free_result($query);

    foreach($nodeList as $nodeId => &$node) {
        if(!$node['parent_id'] || !array_key_exists($node['parent_id'], $nodeList)){
            $tree[] = &$node;
            $url = $parent_url . $node['url'];
            $url = str_replace('?cat=', '', $url);
            echo '<li><a href="http://www.example.com/cat/' . strip_tags($url) . '" title="' . strip_tags($node['category']) . ' Link" class="category-headers">' . strip_tags($node['category']) . '</a>';   
        } else {
            $nodeList[$node['parent_id']]['children'][] = &$node;
            $url = $parent_url . $node['url'];
            $cat_num = array('?cat=','&sub1=','&sub2=');
            $url = str_replace($cat_num, '/', $url);
            echo '<li><a href="http://www.example.com/cat/' . strip_tags($url) . '" title="' . strip_tags($node['category']) . ' Link">' . strip_tags($node['category']) . '</a>';              
        }

    echo '</li>';        
    } 

    echo '</ol>';  
    unset($node);
    unset($nodeList);
}

allCategories();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

妄断弥空 2024-11-14 12:03:46

我怀疑您的查询在 MySQL 级别出错,并且您没有设置任何内容来告诉您这一点(尤其是在 php.ini 文件中关闭警告的情况下)。

尝试在以 $query 开头的行中添加类似的内容:

or die( "

SELECT query failed!

Error: " . mysqli_error ( $dbc ) . "

" );

$dbc 需要替换为保存数据库连接的任何变量。

显然,这仅用于调试。您可以在生产服务器上用一些错误处理函数替换 die

I suspect your query is erroring out at the MySQL level, and you don't have anything set up to tell you so (especially if warnings are turned off in the php.ini file).

Try adding something like this to the line that starts with $query:

or die( "<h1>SELECT query failed!</h1> <p>Error: " . mysqli_error( $dbc ) . "</p>" );

$dbc needs to be replaced with whatever variable holds your database connection.

Obviously, this is for debugging only. You would replace die with some error-handling function on a production server.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文