tags:

views:

42

answers:

2

I have a script that displays categories but I want the script to display only certain categories that the user picked which can range from 1 to many categories. What do I need to do to my script to fix this problem? An example would help out a lot.

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

Here is my MySQL table.

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

Here is my list of categories I commented which categories I want to display.

Antiques
    Antiquities
    Architectural & Garden
        Asian Antiques
        Books & Manuscripts
            Decorative Arts
                Ethnographic
                    Furniture // I want to select this category
Home & Hearth
Linens & Textiles
Restoration & Care // I want to select this category
Rugs & Carpets // I want to select this category
Science & Medicine
Silver // I want to select this category
Reproduction Antiques
A: 

Change your query to something like this, you need to fetch category id's an insert them in IN clause.

SELECT * FROM categories WHERE categories IN(1,5,10,12) ORDER BY parent_id, category ASC
Webarto
This didn't work at all :(
helloWORLD
Have you typed category ID's correctly? I made up that numbers as a reference.
Webarto
A: 

I agree with the previous post about using the IN clause. Just dynamically add them to your query though. So each time a user clicks a category (however you're allowing them to do so) you can add that category's ID to an array. Then use something like PHP's implode function to make a comma separated list implode(",", $array) So you're query string would look like

$string = "SELECT * FROM categories WHERE categories IN(" . implode(",", $selectedCats) . ") ORDER BY parent_id, category ASC"
Aaron Hathaway
Now I get the this error `mysqli_error() expects exactly 1 parameter, 0 given`
helloWORLD
What's your new code look like?
Aaron Hathaway
basically the same just added what you suggested I got the same error I got form the other members answer.
helloWORLD
Hmm, getting a mysqli_error() error means that the inital query didn't work. Two things:1) Check out the docs for mysqli_error(). It requires at least one parameter.2) Something is then going wrong in your mysqli_query() call. I'm looking at the docs for that and it looks like it'd be cool with just a string parameter. Try something like$query = "SELECT * FROM categories WHERE categories IN(" . implode(",", $selectedCats) . ") ORDER BY parent_id, category ASC";$dbc = mysqli_query($query);
Aaron Hathaway
I thinks its probably my `function make_list ()` or `while ()` I just don't know what it is?
helloWORLD