tags:

views:

44

answers:

1

I have this script that loops to make a category tree. But I want it to display specific categories that a user picks like in the example below. On a side note the script loops in-order to build the url link to the category.

I guess what I'm trying to say is that I just want to know how to display specific categories and their full url?

Here is the example.

Found In Category:  Books & Authors, Html, Web design

Here is my PHP script.

function make_list ($parent = 0, $parent_url = '') {
    global $link;
    echo '<ol>';

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

        if (isset($link[$id])) {
            make_list($link[$id], $url); // $url adds url value to sub categories
        }               
        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)) {
    $link[$parent_id][$id] =  array('category' => $category, 'url' => $url);
}

make_list($link[0]);
A: 

If you don't want to display all categories but only one (if i understood correctly) change:

$dbc = mysqli_query(
    $mysqli,"SELECT * FROM categories ORDER BY parent_id, category ASC"
);

to

$cat = mysqli_real_escape_string($_POST['selectedCategory']);
$dbc = mysqli_query(
    $mysqli,
    "SELECT * FROM categories WHERE category = '$cat' ORDER BY parent_id, category ASC"
);

Edit: indents for readability

Edit2:

For multiple categories you'd use

WHERE category IN('x', 'y', 'z')

Please correct my if you did ask for something else :)

edorian
This will mess up my categories URL how can I fix the URL.
helloWORLD
I don't really see how since the url is part of every row you select but maybe thats just me... without any sample data i don't think i have any more ideas
edorian
helloWORLD