SELECT中的条件WHERE子句使用if语句和php变量

时间:2016-10-27 19:06:24

标签: php mysql if-statement

我正在尝试根据用户输入构建一个有条理组织的表。我需要我的WHERE和GROUP BY子句来改变基于用户输入值(从通过ajax发送到php的无线电选择 - 这些测试成功),存储在php变量中。我试图运行一系列if语句来根据ajax数据变量的值更改WHERE和GROUP BY子句,然后将其连接到我的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(); 

欢迎所有建议。谢谢!

2 个答案:

答案 0 :(得分:0)

你有太多的&#39;查询中的字符。目前,所有的WHERE语句都引用了GROUP BY语句。

改变这个:

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

对此:

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

答案 1 :(得分:0)

请注意最后一行:

// 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."";