开发者

PHP link display problem

开发者 https://www.devze.com 2023-01-11 16:40 出处:网络
I need to correct the way my links are displayed using PHP for some reason my links sub categories are displayed in the following format below.

I need to correct the way my links are displayed using PHP for some reason my links sub categories are displayed in the following format below.

http://localhost/index.php&sub=sub1
http://localhost/index.php&sub3=sub3

When they should be displayed in the following format.

http://localhost/index.php?cat=2&sub=sub1
http://localhost/index.php?cat=2&sub=sub1&sub2=sub2&sub3=sub3

Can someone help me correct this problem?

Here is the PHP code.

function make_list ($parent = 0, $parent_url = 'http://localhost/index.php') {
    global $link;
    echo '<ol>';

    foreach ($parent as $id => $cat) {
        $url = $parent_url . $cat['url'];
        // Display the item:
        echo '<li><a href="' . $url . '" title="' . $cat['category'] . ' Category Link">' . $cat['category'] . '</a>';          

        if (isset($link[$id])) {
            make_list($link[$id]);  
        }               
        echo '</li>';       
    }       
    echo '</ol>';
}

$mysqli = mysqli_connect("localhost", "root", "", "sitename");
$dbc = mysqli_开发者_JAVA技巧query($mysqli,"SELECT * FROM categories ORDER BY parent_id, category ASC");
if (!$dbc) {
    print mysqli_error();
} 

$link = array();

while (list($id, $parent_id, $category, $url) = mysqli_fetch_array($dbc, MYSQLI_NUM)) {
    $link[$parent_id][$id] =  array('category' => $category, 'url' => $url);
}
make_list($link[0]);


Looks like everything after http://localhost/index.php is coming from the fourth column in the categories table.

Can't see any concatenation of multiple levels of categories, all seems to be just from the current category.

Inside the below loop, it looks like you'll have to fetch the 'url' field of the parent id's link, whereever that is, and append the new $url to it instead of just using what's in the database.

while (list($id, $parent_id, $category, $url) = mysqli_fetch_array($dbc, MYSQLI_NUM)) {
    $link[$parent_id][$id] =  array('category' => $category, 'url' => $url);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消