PHP函数绝对URL显示问题
我试图让我的函数显示我的类别绝对网址地址,例如 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('?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();
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我怀疑您的查询在 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.