Jump to content

Select from CSV MySQL value?


katlis

Recommended Posts

I have a form where, for the purposes of creating an entry, members can select multiple categories (with checkboxes) which is then stored as a comma separated value in a MySQL column called 'category'.

 

I have a separate page with a function to browse ALL entries in ANY category, but I need to filter it when a user selects certain categories to search/browse (again, with checkboxes).

 

I need to do something similar to:

SELECT * FROM table WHERE category='cats' OR 'dogs' OR 'fish' 

 

How can I accomplish this if the column "category" could have values such as:

row1: 'dogs,cats,birds'

row2: 'fish,monkeys,birds,mice'

etc.?

 

Any help is greatly appreciated.

Link to comment
https://forums.phpfreaks.com/topic/113131-select-from-csv-mysql-value/
Share on other sites

well if you have check boxes then you could have a script like this

Form

<input type="checkbox" name="categorys[]" value="cat" >
<input type="checkbox" name="categorys[]" value="dog" >
<input type="checkbox" name="categorys[]" value="fish" >
<input type="checkbox" name="categorys[]" value="bird" >

PHP

<?php
//make query
$categorys = $_POST['categorys'];
$where = array();
foreach($categorys as $value){
    $where[] = "category='%{$value}%'";
}
$where = implode(' OR ',$where);
$query = "SELECT * FROM table WHERE {$where};";
?>

 

Scott.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.