tags:

views:

35

answers:

0

So far when I create a new sub category the url only grabs the new sub category but not the parent category for example if I create a new artists sub category of leonardo-da-vinci the new sub category url will be stored as,

leonardo-da-vinci

Output:

http://www.example.com/leonardo-da-vinci

when it should be stored as.

arts-entertainment/artists/leonardo-da-vinci

Output:

http://www.example.com/arts-entertainment/artists/leonardo-da-vinci

Here is the PHP code.

echo '<p>Category: <input name="category" type="text" size="60" maxlength="255" /></p>
      <p>Parent category:';

echo '<select name="parent_id">
      <option value="0">None</option>';

function make_list ($parent, $depth) {
    global $option;     
    foreach ($parent as $id => $cat) {

        $indent = str_repeat('&nbsp; ', $depth * 2);
        echo '<option value="' . $cat['id'] . '">';
        if($cat['parent_id'] != 0){
            echo $indent;
        }               
        echo $cat['category'] . '</option>';

        if (isset($option[$id])) {              
            make_list($option[$id], $depth+1);              
        }               
    }       
}

$mysqli = mysqli_connect("localhost", "root", "", "sitename");
$dbc = mysqli_query($mysqli,"SELECT * FROM categories ORDER BY parent_id, category ASC");
if (!$dbc) {
    print mysqli_error();
} 

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

$depth = 0;
make_list($option[0], $depth+1);

echo '</select></p>
<p>Category URL: <input name="url" type="text" size="60" maxlength="255" /></p>';

MySQL database.

CREATE TABLE categories ( 
id INT UNSIGNED NOT NULL AUTO_INCREMENT, 
parent_id INT UNSIGNED NOT NULL DEFAULT 0, 
category TEXT NOT NULL, 
url TEXT NOT NULL, 
PRIMARY KEY (id), 
INDEX parent (parent_id)
);