Using PHP to generate SQL statement

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I keep looking at the following code and thinking there's gotta be a better
way.  I've been in front of the computer all day tho and I'm drawing a
blank.  Any ideas?

 

Thanks,

 

-Ed

 

 

$sql = "select ID from products where ";

if ($webpage->parameter_isset("CategoryID")) {

            if ($webpage->parameter_isset("CompanyID")) {

                        if ($webpage->parameter_isset("SettingID")) {

                                    if
($webpage->parameter_isset("SystemID")) {

                                                $sql .= "CategoryID =
'{$webpage->CategoryID}' AND CompanyID = '{$webpage->CompanyID}' AND
SettingID = '{$webpage->SettingID}' AND SystemID = '{$webpage->SystemID}' ";

                                    } else {  // No SystemID

                                                $sql .= "CategoryID =
'{$webpage->CategoryID}' AND CompanyID = '{$webpage->CompanyID}' AND
SettingID = '{$webpage->SettingID}' ";

                                    }

                        } else { // No SettingID

                                    if
($webpage->parameter_isset("SystemID")) {

                                                $sql .= "CategoryID =
'{$webpage->CategoryID}' AND CompanyID = '{$webpage->CompanyID}' AND
SystemID = '{$webpage->SystemID}' ";

                                    } else {  // no SystemID

                                                $sql .= "CategoryID =
'{$webpage->CategoryID}' AND CompanyID = '{$webpage->CompanyID}' ";

                                    }

                        }

            } else { // no CompanyID

                        if ($webpage->parameter_isset("SettingID")) {

                                    if
($webpage->parameter_isset("SystemID")) {

                                                $sql .= "CategoryID =
'{$webpage->CategoryID}' AND SettingID = '{$webpage->SettingID}' AND
SystemID = '{$webpage->SystemID}' ";

                                    } else { // No SystemID

                                                $sql .= "CategoryID =
'{$webpage->CategoryID}' AND SettingID = '{$webpage->SettingID}' ";

                                    }

                        } else {

                                    if
($webpage->parameter_isset("SystemID")) {

                                                $sql .= "CategoryID =
'{$webpage->CategoryID}' AND SystemID = '{$webpage->SystemID}' ";

                                    } else { // No System ID

                                                $sql .= "CategoryID =
'{$webpage->CategoryID}' ";

                                    }

                        }

            }

} else { // no CategoryID

            if ($webpage->parameter_isset("CompanyID")) {

                        if ($webpage->parameter_isset("SettingID")) {

                                    if
($webpage->parameter_isset("SystemID")) {

                                                $sql .= "CompanyID =
'{$webpage->CompanyID}' AND SettingID = '{$webpage->SettingID}' AND SystemID
= '{$webpage->SystemID}' ";

                                    } else {  // No SystemID

                                                $sql .= "CompanyID =
'{$webpage->CompanyID}' AND SettingID = '{$webpage->SettingID}' ";

                                    }

                        } else { // No SettingID

                                    if
($webpage->parameter_isset("SystemID")) {

                                                $sql .= "CompanyID =
'{$webpage->CompanyID}' AND SystemID = '{$webpage->SystemID}' ";

                                    } else {  // no SystemID

                                                $sql .= "CompanyID =
'{$webpage->CompanyID}' ";

                                    }

                        }

            } else { // no CompanyID

                        if ($webpage->parameter_isset("SettingID")) {

                                    if
($webpage->parameter_isset("SystemID")) {

                                                $sql .= "SettingID =
'{$webpage->SettingID}' AND SystemID = '{$webpage->SystemID}' ";

                                    } else { // No SystemID

                                                $sql .= "SettingID =
'{$webpage->SettingID}' ";

                                    }

                        } else {

                                    if
($webpage->parameter_isset("SystemID")) {

                                                $sql .= "SystemID =
'{$webpage->SystemID}' ";

                                    } else { // No System ID

                                                return false;  // major
error

                                    }

                        }

            }

}

 

$sql .= "limit 10";

return $sql;


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux