Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

When I apply filter query not working and how to know the Where clause is already exist if exist so next replace where with AND?

Query 1: AND (installation.InstallationStatus='0')
Query 2: AND (installation.active='1')

When I create a filter and apply both Query1 and Query 2 so query build something like this Query: SELECT * FROM orders WHERE AND (installation.active='1') AND (installation.InstallationStatus='0')

But I Want this
Query: SELECT * FROM orders WHERE (installation.active='1') AND (installation.InstallationStatus='0');

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

and the php code is here

            ```
            //Filter By installStatus
            if (isset($_SESSION['filter']['installStatus']) && !empty($_SESSION['filter']['installStatus'])) {
                $FilterInstallStatus ="AND (installation.InstallationStatus='".$_SESSION['filter']['installStatus']."')";
            } else {
                $FilterInstallStatus = "";
            }


            //Filter By Active
            if (isset($_SESSION['filter']['active']) && !empty($_SESSION['filter']['active'])) {
                $FilterActive ="AND (installation.active='".$_SESSION['filter']['active']."')";
            } else {
                $FilterActive = "";
            }

            $allrecords = $connection->query("(SELECT orders.*,installation.* FROM orders LEFT JOIN installation ON orders.OrderId = installation.OrderId WHERE".$FilterCreationDate." ".$FilterDateFull." ".$FilterModelName." ".$FilterInstallStatus." ".$FilterActive." ".$FilterUserFilter." ".$FilterLastUpdate." GROUP BY orders.OrderId) UNION (SELECT orders.*,installation.* FROM orders RIGHT JOIN installation ON orders.OrderId = installation.OrderId WHERE".$FilterCreationDate." ".$FilterDateFull." ".$FilterModelName." ".$FilterInstallStatus." ".$FilterActive." ".$FilterUserFilter." ".$FilterLastUpdate." GROUP BY orders.OrderId) ORDER BY active DESC, CreationDate DESC, lastUpdate DESC, brandStatus DESC LIMIT $start_from, $record_per_page");

            ```

>Solution :

You should build query differently. Like this:

  $filter_query = '';

  //Filter By installStatus
        if (isset($_SESSION['filter']['installStatus']) && !empty($_SESSION['filter']['installStatus'])) {
            $filter_query = "(installation.InstallationStatus='".$_SESSION['filter']['installStatus']."')";
        }

        //Filter By Active
        if (isset($_SESSION['filter']['active']) && !empty($_SESSION['filter']['active'])) {
            if ($filter_query != '')
                $filter_query .= ' AND ';

            $filter_query  .= "(installation.active='".$_SESSION['filter']['active']."')";
        }

// here all other filters conditions with check if $filter_query is not empty
// and finally db query

$allrecords = $connection->query("(SELECT orders.*,installation.* FROM orders LEFT JOIN installation ON orders.OrderId = installation.OrderId ".($filter_query !='' ? "WHERE ".$filter_query : "")." GROUP BY orders.OrderId) ORDER BY active DESC, CreationDate DESC, lastUpdate DESC, brandStatus DESC LIMIT $start_from, $record_per_page");
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading