PHP函数绝对URL显示问题

时间:2011-05-14 01:42:00

标签: php function

我正在尝试让我的函数显示我的类别绝对网址,例如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();

1 个答案:

答案 0 :(得分:0)

我怀疑您的查询在MySQL级别出错,并且您没有任何设置告诉您(特别是如果php.ini文件中的警告已关闭)。

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

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

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

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