I have the code below written by someone else. It is a little confusing and I would like to refactor it to look better and be more logical.
The SQL has been changed slightly to mask what it is really doing, it is just the if/elesif's that I am concerned about because the whole construct looks confusing.
From what I'm seeing, the code inside the "if($cat != "")" inside the elseif's at lines 8, 15 and 22 will never run because of the first "if($cat != "")" at line 1. i.e. if $flag == 1 and $cat == 'book' then it would always fall into the first if at line 1 and never reach the sql at line 9.
Is this a correct assumption?
How would you refactor this code?
1 if($cat != "") {
2 $sel_gig="SELECT * FROM table
3 WHERE category_id=".$cat;
4 } elseif($mny != "") {
5 $sel_gig="SELECT * FROM table
6 WHERE price=".$mny
7 } elseif($flag == "l") {
8 if($cat != "") {
9 $sel_gig="SELECT * FROM table
10 WHERE category_id=".$cat;
11 } else {
12 $sel_gig="SELECT * FROM table";
13 }
14 } elseif($flag == "p") {
15 if($cat != "") {
16 $sel_gig="SELECT * FROM `clientorder`
17 WHERE category_id=".$cat";
18 } else {
19 $sel_gig="SELECT * FROM `clientorder`:;
20 }
21 } elseif($flag == "r11") {
22 if($cat != "") {
23 $sel_gig="SELECT * FROM `feedback`
24 WHERE category_id=".$cat;
25 } else {
26 $sel_gig="SELECT * FROM `feedback`";
27 }
28 }