Skip to content Skip to sidebar Skip to footer

Get Multiple Values From Url And Have Them In One String

I have created a simple cms for myself where i can post articles and have them have multiple categories. so i.e. Article 1 has categories: Music and Technology and Article 2 has ca

Solution 1:

I suspect you would want to do something along the lines of:

<formaction="categories.php"method="GET"><fieldset><legend>Filter Categories</legend><p><label><inputtype="checkbox"name="categories[]"value="music"/> Music</label><label><inputtype="checkbox"name="categories[]"value="technology"/> Technology</label><label><inputtype="checkbox"name="categories[]"value="film"/> Film</label></p></fieldset><buttontype="submit">Filter</button><buttontype="reset">Reset</button></form>

When submitted, this will give you a URL such as the following:

http://example.com/categories.php?categories[]=music&categories[]=technology

Then, in the script that displays the matching entries having one of these categories, you could do the following with the WHERE clause:

$get_categories = $_GET['categories'];
$cat_clauses = array();

while ($category = array_shift($get_categories)) {
    // Clean it$category = preg_replace('/[^a-z0-9-_]/i', '', $category);

    if ($category) {
        array_push($cat_clauses, "OR category LIKE '%$category%'");
    }
}

if ($cat_clauses) {
    $cat_where = "AND (" . implode(' ', $cat_clauses) . ")";
}

Which might give you something like:

SELECT*FROM blog
WHERE active =1AND (category LIKE'%technology%'OR category LIKE'%music%')

Post a Comment for "Get Multiple Values From Url And Have Them In One String"