簡體   English   中英

使用if語句和php變量的SELECT中的條件WHERE子句

[英]Conditional WHERE clause in SELECT using if statements and php variable

我正在嘗試建立一個根據用戶輸入有條件組織的表。 我需要我的WHERE和GROUP BY子句根據存儲在php變量中的用戶輸入值(通過ajax發送到php的單選信號-已成功測試)進行更改。 我試圖運行一系列if語句,以基於ajax數據變量的值更改WHERE和GROUP BY子句,然后將concat轉換為我的SELECT語句。 我嘗試使用相同的方法來構建列標題和表本身。 當我測試當前代碼時,表頭顯示(由於變量,第一列為空),但未構建這些列。 當我檢查頁面並且我的ajax變量顯示適當的值時,我沒有收到任何錯誤。

問題一定是IF語句,但我不確定如何解決。

注意:當我使用要存儲在變量中的相同值手動輸入WHERE或GROUP BY子句時,SQL語句運行完美。

我的密碼:

AJAX回應:

//Get Table Type.
if (isset($_POST['revenueTblType'])) {
  $revenueTblType = $_POST['revenueTblType'];
  print_r($revenueTblType);
};

//Get Report Type
if (isset($_POST['revenueWO'])) {
  $revenueWO = $_POST['revenueWO'];
  print_r($revenueWO);
};

//Get date include value.
if (isset($_POST['revenueWODate'])) {
  $revenueWODate = $_POST['revenueWODate'];
  print_r($revenueWODate);
};

//Get date range.
$revenuefromajax=$_POST['revenuefrom'];
$revenuetoajax=$_POST['revenueto'];

$revenuefromstring = strtotime($revenuefromajax);
$revenuetostring = strtotime($revenuetoajax);

$revenuefrom=date("Y-m-d", $revenuefromstring);
$revenueto=date("Y-m-d", $revenuetostring);

//Get selected Status Values.
if (isset($_POST['revenue_checkboxes'])) {
  $revenue_check = $_POST['revenue_checkboxes'];
};

如果聲明和表構建:

/////////////   Select Data and Display it in a table.   //////////////


//$groupbyclause = "";

    if ($revenueTblType=='Customer') {
        $groupbyclause="x.company ASC";
        $columnheader='Customer';
        $columnname='company';
    }
    else if ($revenueTblType=='Revenue Category') {
        $groupbyclause="x.revenue ASC";
        $columnheader='Revenue Category';
        $columnname='revenue';
    }


//$whereclause = "";

    if (($revenueWO=='Completed Workorders') and ($revenueWODate=='All Workorders')) {
        $whereclause="x.stagestatus = 'Complete'";
    }
    else if (($revenueWO=='Completed Workorders') and ($revenueWODate=='Workorder Date Range')) {
        $whereclause ="x.stagestatus = 'Complete' AND x.shippeddate BETWEEN '".$revenuefrom."' AND '".$revenueto."'"; 
    }
    else if ($revenueWO=='Workorder Status') {
         $whereclause ="x.stagestatus IN (". implode(',', array_map(function($item) {return '"' . $item . '"'; }, $revenue_check)) .")";
    }


    echo "<BR>";
    echo "<BR>";


    //SELECT statement pulls ALL COMPLETED history info by CUSTOMER.
    $sql="SELECT x.company, x.revenue, x.stagestatus, x.shippeddate, FORMAT(SUM(x.totprice), 2) as totalprice, FORMAT(SUM(x.sgtotquantity), 2) as totqty, FORMAT(SUM(x.sgtotalsqft), 2) as sgtotsqft, FORMAT(SUM(x.totprice)/SUM(x.sgtotalsqft), 2) as avgsqftrevenue, FORMAT(SUM(x.totprice)/SUM(x.sgtotquantity), 2) as avgunitrevenue FROM (SELECT t1.company, t1.revenue, t1.stagestatus, t1.shippeddate, t1.id, TRIM(LEADING '$' FROM t1.totalprice) AS totprice, t2.invoiceid, SUM(t2.quantity) AS sgtotquantity, SUM(t2.width * t2.height * t2.quantity ) /144 AS sgtotalsqft, (t1.totalprice/(SUM(t2.width * t2.height * t2.quantity ) /144)) as avgsqftrev, (t1.totalprice) / SUM(t2.quantity)) AS avgunitrev
      FROM invoices AS t1 INNER JOIN lineitems AS t2 ON t1.id = t2.invoiceid
      WHERE (t2.invoiceid = t1.id)
      GROUP BY t1.id) x
      WHERE '".$whereclause."'
      GROUP BY ".$groupbyclause.""; //this line edited per comment suggestions.//



$result = $conn->query($sql);       


    echo "<table id='revenueReportA' align='center' class='report_DT'>
    <thead>
    <tr>


    <th>".$columnheader."</th>
    <th>Total Revenue</th>
    <th>Total SQ FT</th>
    <th>AVG Revenue Per SQ FT</th>
    <th>Total Number of Units</th>
    <th>AVG Revenue Per Unit</th>
    </tr>
    </head>";


        if ($result = $conn->query($sql)) {

            // fetch associative array 
            while ($row = $result->fetch_assoc()) {

              echo "<tbody>";
              echo "<tr>";
              echo "<td>" . $row[$columnname] . "</td>";
              echo "<td>" ."$". $row['totalprice'] . "</td>";
              echo "<td>" . $row['sgtotsqft'] ."&nbsp;&nbsp;". "ft<sup>2</sup>". "</td>";
              echo "<td>" ."$". $row['avgsqftrevenue'] . "</td>";
              echo "<td>" . $row['totqty'] . "</td>";
              echo "<td>" ."$". $row['avgunitrevenue'] . "</td>";
              echo "</tr>";
              echo "</tbody>";
              }//End table while.
              echo "</table>";
              echo "<BR>";

         }//End table if.

///////////////////////////////////////////////////////////////////////////////


//Free the result variable. 
$result->free();



//Close the Database connection.
$conn->close(); 

歡迎所有建議。 謝謝!

您查詢中的字符過多。 當前,所有WHERE語句以及GROUP BY語句都被引用。

更改此:

WHERE '".$whereclause."'
GROUP BY '".$groupbyclause."'"

對此:

WHERE ".$whereclause."
GROUP BY ".$groupbyclause

請注意最后一行:

// SELECT語句通過CUSTOMER提取所有已完成的歷史記錄信息。

$sql="SELECT x.company, x.revenue, x.stagestatus, x.shippeddate, FORMAT(SUM(x.totprice), 2) as totalprice, FORMAT(SUM(x.sgtotquantity), 2) as totqty, FORMAT(SUM(x.sgtotalsqft), 2) as sgtotsqft, FORMAT(SUM(x.totprice)/SUM(x.sgtotalsqft), 2) as avgsqftrevenue, FORMAT(SUM(x.totprice)/SUM(x.sgtotquantity), 2) as avgunitrevenue FROM (SELECT t1.company, t1.revenue, t1.stagestatus, t1.shippeddate, t1.id, TRIM(LEADING '$' FROM t1.totalprice) AS totprice, t2.invoiceid, SUM(t2.quantity) AS sgtotquantity, SUM(t2.width * t2.height * t2.quantity ) /144 AS sgtotalsqft, (t1.totalprice/(SUM(t2.width * t2.height * t2.quantity ) /144)) as avgsqftrev, (t1.totalprice) / SUM(t2.quantity)) AS avgunitrev
  FROM invoices AS t1 INNER JOIN lineitems AS t2 ON t1.id = t2.invoiceid
  WHERE (t2.invoiceid = t1.id)
  GROUP BY t1.id) x
  WHERE ".$whereclause."
  GROUP BY ".$groupbyclause."";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM