PHP function Absolute URL Display Problem
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();
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.
精彩评论