2 or 3 Categorys search checkbox php - php

Each Article can have unlimited categories.
Categories are saved in the database like this
example article_category: '1','3','8'
OR
example article_category: '2','3'
So when I search for a listing under, lets say category 3
$category_id = '1,3';
$SQL = "SELECT * FROM ARTICLES WHERE article_category = '$category_id'";
If it were just one the above would work fine.
I hate to say it but I'm completely lost.. Would I use IN ?

You can write like
$SQL = "SELECT * FROM ARTICLES WHERE article_category IN (".$category_id.")";

You can use FIND_IN_SET() like this:
SELECT *
FROM ARTICLES
WHERE FIND_IN_SET($category_id, article_category) > 0

If I understood your problem then you want to make query for different categories at a time.
If I am correct then to do so you will have to get the categories type in an array or diifferent variables then compare each one of them in the where clause using or.
Like: where article_category='$x1' or article_category='$x2' or... so on.

Use find_in_set();
$SQL= "SELECT * FROM ARTICLES WHERE FIND_IN_SET(1,article_category)>0";

Related

Mysql search for a string in an Array

Data stored in the database as below where category is column name and comma separated strings are values.
category = 'cat,cat1,cat2,cat3,cat33,cat4';
I'm trying to search with a category from the list:
$query= mysqli_query($mysqli, "SELECT * FROM table where
INSTR(category,'cat3') > 1 ");
But this doesn't work because the result includes cat3 and cat33.
Any thoughts on how I can do this search, please?
Thanks!
You can use FIND_IN_SET function :
SELECT * FROM table where FIND_IN_SET('cat3', category) > 0
But you should normalize your data by creating category table and relate to your table's primary key
"SELECT * FROM table where category LIKE '%cat3,%'";
"SELECT * FROM table where category LIKE '%cat33,%'";
if you don't want to add comma to the end of each category name, then
"SELECT * FROM table where category LIKE '%cat3,%' OR category LIKE '%cat3'";
You can "solve" this using the query below. BUT I HARDLY RECCOMEND you to normalize your database and use this only if you REALLY NEED
SELECT * FROM table WHERE fild LIKE '%,camp3,%' OR fild LIKE 'camp3' OR field LIKE '%,camp3'

I need a Query with 2 dynamic values

i have an array where i can get id's from a database table, now i need to extract those values as i run a domain link array such as: domain.com/page?id=1
Now this page will bring a category, lets say A category with ID 1 i need to list items under this category on the same database table example products, i can use it without a filter now i need a filter using arrays, here is my code, thank you in advance.
$query ="SELECT * FROM tablename WHERE id = '".$id."' DESC";
before this i have on my php code:
$id = $_GET["id"];
So when i call from this table all comes out, but i need to filter where category 1 (example) have items, not all items with category 2, 3, 4 etc.
With my code i can get the item id as the link with array works fine for example: domain.com/page.php?id=1, but here is the catch, i get item id 1 and i need let's say category id 2, if i run the same link with id=2 i get item id 2 and that's not what i want.
I need to retrieve those values as arrays, any idea? Thank you in advance!
EDITED
Table structure example:
Items
ID, Name, Category
I need Category from Items as a filter like this: domain.com/?id=2&category=2
So i get all items under row Category only
Update your URI to something like this domain.com/page.php?id=1&category=2
$category = $_GET["category"];
$query ="SELECT * FROM tablename WHERE id = '".$id."' AND `Category` = '".$category."'";
Pass the query string parameter optionally
domain.com/page.php?id=1&category=2
Add the where clause dynamically like this
$query ="";
$conact =" where 1=1 ";
if(isset($_GET['id']))
{
$conact.=" and id=".$_GET['id'];
}
if(isset($_GET['category']))
{
$conact.=" and category=".$_GET['category'];
}
$query ="SELECT * FROM tablename $conact DESC";

php/mySQL: querying records by id from array?

i want to query several records by id like:
$ids = array(10,12,14,16,....);
the query would be something like:
select * from users where id=10 or id=12 or id=14 or id=16 ..
is it possible to query it in a more comfortable way like (compared to php):
select * from user where in_array(id, array(10,12,14,16))
thanks
You can use IN instead of OR clauses
select * from users where id IN (put_your_array_here)
Example:
select * from users where id IN (10,12,14,16);
Note:
According to the manual for MySQL if the values are constant IN
sorts the list and then uses a binary search. I would imagine that
OR evaluates them one by one in no particular order. So IN is
faster in some circumstances.
Related post
Try this.
$id = array(10,12,14,16,....);
$ids = join("','",$id);
$sql = "select * from user where id IN ('$ids')";
OR
$ids = implode(',', $id);
$sql = "select * from user where id IN ($ids)";
You can do it like that using implode in PHP:
"select * from users where id in '".implode("','", $ids)."'"
Please be sure that your ids are safe though

Get products by multiple category id

I'm trying to do this kind of query,
When i make the query by get, i want to display the results from the id, for example if i have this id in my GET =11;
I want to display all the products with the category_id = 11, even if they have another id in the concat format, I don't know how i can do this kind of query,
$id = $_GET['id'];
$sql = "SELECT * FROM `product` WHERE `product_category_id`='$id'";
My product_category_id contains the id's of the categories in CONCAT format:
product_category_id (type text)
11,30,29
The FIND_IN_SET MySQL function might be what you're looking for.
$sql = "SELECT * FROM `product` WHERE FIND_IN_SET('$id', `product_category_id`)";
With a field like that, you will have to do something like this:
$sql = "SELECT *
FROM product
WHERE product_category_id LIKE '$id,%'
OR product_category_id LIKE '%,$id,%'
OR product_category_id LIKE '%,$id'";
In general, though, this will be a slow query.

Can I use two where clauses, like "SELECT * FROM table WHERE something and something"?

I have a table with my products and I'm trying to write a page that would pull bracelets with certain colors from the database. So here's what I have right now (in php):
$query = "SELECT * FROM products WHERE (products.colors LIKE '%black%')";
But I only want to select rows where the value for the column "category" equals "bracelet".
I've tried a few different things, but I keep getting warnings and errors. I appreciate any help you can give, thank you!
$query = "SELECT * FROM products WHERE products.colors LIKE '%black%' AND products.category = 'bracelet'";
There you go.
You can do:
SELECT * FROM products
WHERE colors LIKE '%black%'
AND category = 'bracelet'

Categories