Şimdiye kadar yeni bir alt kategori oluşturduğunuzda, url sadece yeni bir alt kategori çekiyor ama leonardo-da-vinci yeni bir alt kategori url olarak saklanır kategorisini alt bir yeni sanatçıları, örneğin üst kategori yaratmak değilse
leonardo-da-vinci
Çıktı:
http://www.example.com/leonardo-da-vinci
o kadar saklanmalıdır.
arts-entertainment/artists/leonardo-da-vinci
Çıktı:
http://www.example.com/arts-entertainment/artists/leonardo-da-vinci
Burada PHP kodudur.
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(' ', $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 veritabanı.
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)
);