0

In MySQL database, images in gallery can be assigned to a product range so that galleries can be filtered to only display images from a particular range.

Currently the main image gallery contains:-

$query_Cur_range_img = "SELECT * FROM `images` WHERE active = 'y' AND gallery = '1' LIMIT ".$showfrom.", ".$max;

If gallery is '1' then images are to be displayed in main gallery (ALL).

Some images are also added to product ranges from within a separate database column 'product_range_id' and the product range id's are 1, 2, or 3.

In the include file for a filtered gallery, we have:-

 $query_Cur_range_img = "SELECT * FROM `images` WHERE active = 'y' AND gallery = '1' AND product_range_id = '1' LIMIT ".$showfrom.", ".$max;

This is only displaying images that ONLY are assigned to product_range_id 1 however. Some images will be displayed to product ranges 2 and 3 for example or maybe 1 and 2. I guess we need to include an OR statement? Any help with writing the query for this would be greatly appreciated please so as above, I think we need AND product_range_id = '1' (or 2 or 3) for example?

Thanks in advance.

UPDATE

I've tried the following but all this did when filtering images by rage id 1 was display images that are ONLY assigned to range id 1 (didn't include images that assigned to range id's 1 and 2 for example).

$query_Cur_range_img = "SELECT * FROM `images` WHERE active = 'y' AND gallery = '1' AND (product_range_id = '1' OR product_range_id = '1' AND product_range_id = '2' OR product_range_id = '1' AND product_range_id = '3') LIMIT ".$showfrom.", ".$max;

UPDATE - 13/01/2012

Now Resolved - Fix Below (Thanks to Elen)

$query_Cur_range_img = "SELECT * FROM images WHERE active = 'y' AND gallery = 1 AND (FIND_IN_SET('1', product_range_id) OR product_range_id = '1') LIMIT ".$showfrom.", ".$max;

And

$query_Cur_range_img = "SELECT * FROM images WHERE active = 'y' AND gallery = 1 AND (FIND_IN_SET('2', product_range_id) OR product_range_id = '2') LIMIT ".$showfrom.", ".$max;

And

$query_Cur_range_img = "SELECT * FROM images WHERE active = 'y' AND gallery = 1 AND (FIND_IN_SET('3', product_range_id) OR product_range_id = '3') LIMIT ".$showfrom.", ".$max;

As required...

1
  • What is the table structure? The options given should have worked but it seems like the table structure may be different than expected. Commented Jan 11, 2012 at 13:55

2 Answers 2

2

is this good enough?

SELECT * FROM `images` WHERE active = 'y' AND gallery = '1' AND (product_range_id = '1' OR product_range_id = '2' OR product_range_id = '3') LIMIT ".$showfrom.", ".$max.";

edit

$query_Cur_range_img = "SELECT * FROM images WHERE active = 'y' AND gallery = 1 AND 
(FIND_IN_SET('1', product_range_id) OR product_range_id = '1') LIMIT ".$showfrom.", ".$max;
Sign up to request clarification or add additional context in comments.

10 Comments

Thanks for your comment - this doesn't work as it only displays images that displayed in EITHER product_range_id 1, 2 or 3 but doesn't include images that may be assigned to more than one range id (such as 1 and 2 for example). Thanks
ok, it's quite difficult without seeing you table structure and some examples. please see edit.
I appreciate that it is difficult without seeing exactly what we've got - your latest edit broke the page with a custom error message 'Page number does not exist' (which I appreciate is not something you will be able to help with) - no idea why that happened :/
I don't think that last quotation mark was meant to be there because now it breaks with: Parse error: syntax error, unexpected '"'
I just tried this but still doesn't show all the correct images: $query_Cur_range_img = "SELECT * FROM images WHERE active = 'y' AND gallery = '1' AND product_range_id = '1' OR product_range_id = '1,2' OR product_range_id = '1,3' LIMIT ".$showfrom.", ".$max;
|
2

try with this:

$query_Cur_range_img = "SELECT * FROM `images` 
WHERE active = 'y' 
AND gallery = '1' 
AND product_range_id IN ('1', '2', '3') 
LIMIT ".$showfrom.", ".$max;

hope it will help,,

PS if you get the product_range_id with php then plz never forget to use mysql_real_escape_string()

1 Comment

Thanks for your help, I've tried this and what this is now doing when filtering images by product_range_id 1 is including images that are only assigned to product_range_id 2 as well as others. This needs to only display images that are assigned to one other category but has to be assigned to product_range_id 1. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.