2010-08-17 123 views
0

我需要更正使用PHP顯示鏈接的方式,出於某種原因,我的鏈接子類別以下面的格式顯示。PHP鏈接顯示問題

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

當他們應該以下列格式顯示。

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

有人可以幫我糾正這個問題嗎?

這裏是PHP代碼。

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_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]); 

回答

0

貌似http://localhost/index.php後,從第四欄即將在categories表的一切。

看不到多個級別的類別的任何級聯,都似乎只是從當前類別。

在下面的循環中,它看起來像你必須獲取父id的鏈接的'url'字段,無論如何,並追加新的$ url而不是僅僅使用數據庫中的內容。

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

我該怎麼做呢?自從我編寫PHP以來,這已經有一段時間了 – HiThere 2010-08-17 02:53:01