Hey guys I am not sure if this can be done. I have a table with products like this:
each product has a category and some have a secondary category.
I want to be able to short in this case using the example table above the 'someother' category by Order2
and Οrder
for example:
Item2 has as a main category the 'someother' category and order number "2"
Items 1 and 5 have 'someother' category as 2ndcategory with order2 -number "3" and "1"
To display the items I am using the following query:
$query= "SELECT * FROM products WHERE category = $cat_id or 2ndcategory = $cat_id ORDER BY order ASC";
As it is right now I am displaying the items with main or 2nd category someother but the display order is item1, item2 and item5
I want to be able to display item5, item2 and item1 using in the case of items 5 and 1 order2 and in the case of item2 order.
I have tried the following but it doesn't seem to work.
$query3 = "SELECT * FROM products";
$tests= $conn->query($query3);
foreach($tests as $test){
$test_cat = $test['2ndcategory'];
$test_id = $test['order2'];
$test_id_1 = $test['order1'];
$test_code='';
if($test_cat != NULL){
$test_code = $test_id;
}else{$test_code = $test_id_1;
}
}
$query= "SELECT * FROM products WHERE category = $prod_id or 2ndcategory= $prod_id ORDER BY ".$test_code." ASC";
I have also tried
$query3 = "SELECT * FROM products";
$tests= $conn->query($query3);
foreach($tests as $test){
$test_code= $test['order2'];
}
$query= "SELECT * FROM products WHERE category = $prod_id or 2ndcategory= $prod_id ORDER BY IF($test_code != NULL){order2}else{order} ASC";
and
$query= "SELECT * FROM products WHERE category = $prod_id or 2ndcategory= $prod_id ORDER BY
CASE orderby
WHEN order2 != NULL THEN order2
WHEN order2 == NULL THEN order END
ASC";
I have also tried
ORDER BY
CASE orderby
WHEN order2 is not NULL THEN order2
ELSE order END ASC";
and
ORDER BY
CASE orderby WHEN order2 is not NULL THEN order2
CASE WHEN order2 is NULL THEN order
ELSE order END
ASC
Any ideas would be great guys. Thank you in advance.