Jump to content

switch in mysql statement?


son
 Share

Recommended Posts

I am wrecking my brain now for some time, but I cannot see the answer to my issue. I retrieve data from a db as:

$query = "SELECT img1, imgTitle, imgCat FROM hyr_images WHERE hyrID = $HID AND (imgCat = $pid OR imgCat = 7) ORDER BY imgCat ASC";

I display the retrieved images on a several web pages and try to display the images for a specific page (assessed via imgCat). If no specific images are displayed the generic ones will be displayed (imgCat = 7). With my current setup page specific ones come first and then the generic ones, but I do only want to display the generic ones if there is not even one page specific one (and then in any case all generic ones should be displayed)..How can you achieve this? Is this possible?Son

Link to comment
Share on other sites

When you're looping through the result set have a boolean flag that says whether or not to display the generic ones, and start the flag as true. If any of the rows are in a category other than the default, set that flag to false. When you get to the generics, only display them if the flag is true. This will only work if the default category ID is either larger or smaller than all other categories, and the results should be sorted with the specific images first, then generic images. If you can't guarantee that then you'll need to loop through the results once and collect the specific and generic images in different arrays, and then figure out which array to display.

Link to comment
Share on other sites

When you're looping through the result set have a boolean flag that says whether or not to display the generic ones, and start the flag as true. If any of the rows are in a category other than the default, set that flag to false. When you get to the generics, only display them if the flag is true. This will only work if the default category ID is either larger or smaller than all other categories, and the results should be sorted with the specific images first, then generic images. If you can't guarantee that then you'll need to loop through the results once and collect the specific and generic images in different arrays, and then figure out which array to display.
Thanks. I have it working now, but have one question with regard to your statement 'When you get to the generics, only display them if the flag is true'. The generic ones are the last ones to be returned and I have done the check regarding your statement as:
if (($imgCat == 7 && $genBanner) || ($imgCat != 7 && !$genBanner)){code}

It works, but am not sure if that was what you meant. If there is a more efficient way, please let me know..Son

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...